Posted by PEACEY Thu 1st Mar 2007 22:17 - Syntax is C - 36 views
Download | New Post | Modify | Hide line numbers
Download | New Post | Modify | Hide line numbers
Description:
This is what i did s far...
This is what i did s far...
-
#include
-
#include
-
#include "scribble.h"
-
-
#define min(x, y) (x > y) ? y : x
-
-
/* Backing pixmap for drawing area */
-
static GdkPixmap *pixmap = NULL;
-
GSList *data = NULL;
-
-
typedef struct {
-
gdouble x1;
-
gdouble y1;
-
gdouble x2;
-
gdouble y2;
-
} points_info;
-
-
/* Create a new backing pixmap of the appropriate size */
-
static gboolean configure_event( GtkWidget *widget,
-
GdkEventConfigure *event )
-
{
-
if (pixmap)
-
g_object_unref(pixmap);
-
-
pixmap = gdk_pixmap_new (widget->window,
-
widget->allocation.width,
-
widget->allocation.height,
-
-1);
-
gdk_draw_rectangle (pixmap,
-
widget->style->white_gc,
-
TRUE,
-
0, 0,
-
widget->allocation.width,
-
widget->allocation.height);
-
-
draw_brush(widget, data);
-
return TRUE;
-
}
-
-
/* Redraw the screen from the backing pixmap */
-
static gboolean expose_event( GtkWidget *widget,
-
GdkEventExpose *event )
-
{
-
gdk_draw_drawable (widget->window,
-
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
-
pixmap,
-
event->area.x, event->area.y,
-
event->area.x, event->area.y,
-
event->area.width, event->area.height);
-
-
return FALSE;
-
}
-
-
/* Draw a rectangle on the screen */
-
static void draw_brush( GtkWidget *widget,
-
GSList *list)
-
{
-
gdouble cx, cy, mindim, scale;
-
gint spacer = 10;
-
gint scaledmax = 120;
-
gdouble w = widget->allocation.width;
-
gdouble h = widget->allocation.height;
-
-
/* Finding the center point */
-
h = h + 2*spacer;
-
cx = w/2;
-
cy = h/2;
-
-
/* Calculating the scale factor */
-
mindim = min(cx, h/2);
-
scale = mindim/scaledmax;
-
-
gdk_draw_line(pixmap, widget->style->black_gc, cx-110*scale, cy, cx+110*scale, cy);
-
gdk_draw_line(pixmap, widget->style->black_gc, cx, cy-110*scale, cx, cy+110*scale);
-
-
g_print("%f %f %f\n", cx, cy, scale);
-
-
GSList *tmp = data;
-
points_info *points;
-
while (tmp != NULL) {
-
points = tmp->data;
-
gdk_draw_line(pixmap, widget->style->black_gc, ((points->x1)*scale)+cx, ((points->y1)*scale)+cy, ((points->x2)*scale)+cx, ((points->y2)*scale)+cy);
-
tmp = g_slist_next(tmp);
-
}
-
-
gtk_widget_queue_draw(widget);
-
-
}
-
-
static gboolean motion_notify_event( GtkWidget *widget,
-
GdkEventMotion *event )
-
{
-
int x, y;
-
GdkModifierType state;
-
-
if (event->is_hint)
-
gdk_window_get_pointer (event->window, &x, &y, &state);
-
else
-
{
-
x = event->x;
-
y = event->y;
-
state = event->state;
-
}
-
-
if (state & GDK_BUTTON1_MASK && pixmap != NULL)
-
draw_brush (widget, data);
-
-
return TRUE;
-
}
-
-
static void btn_graph_clicked( GtkWidget *widget, gpointer *radio, GdkEvent *event ) {
-
-
}
-
-
void quit ()
-
{
-
exit (0);
-
}
-
-
int main( int argc, char *argv[] ) {
-
GtkWidget *window;
-
GtkWidget *drawing_area;
-
GtkWidget *vbox;
-
-
GtkWidget *btn_graph;
-
GtkWidget *btn_clear;
-
GtkWidget *entry_straight;
-
GtkWidget *entry_function;
-
GtkWidget *radio_straight;
-
GtkWidget *radio_function;
-
GtkWidget *hbox_straight;
-
GtkWidget *hbox_function;
-
GtkWidget *hbox_btn;
-
-
GtkTextBuffer *text_buffer;
-
-
gtk_init(&argc, &argv);
-
-
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
-
gtk_widget_set_name(window, "Ze Grapher");
-
-
/* Create a new vertical container */
-
vbox = gtk_vbox_new (FALSE, 0);
-
gtk_container_add(GTK_CONTAINER(window), vbox);
-
gtk_widget_show(vbox);
-
-
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(quit), NULL);
-
-
/* Create the drawing area */
-
-
drawing_area = gtk_drawing_area_new ();
-
gtk_widget_set_size_request(GTK_WIDGET(drawing_area), 500, 500);
-
gtk_box_pack_start(GTK_BOX(vbox), drawing_area, TRUE, TRUE, 0);
-
-
gtk_widget_show(drawing_area);
-
-
/* Signals used to handle backing pixmap */
-
-
g_signal_connect(G_OBJECT(drawing_area), "expose_event", G_CALLBACK(expose_event), NULL);
-
g_signal_connect(G_OBJECT(drawing_area), "configure_event", G_CALLBACK(configure_event), NULL);
-
-
/* Create new horizontal containers */
-
hbox_straight = gtk_hbox_new(FALSE, 0);
-
hbox_function = gtk_hbox_new(FALSE, 0);
-
hbox_btn = gtk_hbox_new(FALSE, 0);
-
gtk_box_pack_start(GTK_BOX(vbox), hbox_straight, FALSE, FALSE, 0);
-
gtk_box_pack_start(GTK_BOX(vbox), hbox_function, FALSE, FALSE, 0);
-
gtk_box_pack_start(GTK_BOX(vbox), hbox_btn, FALSE, FALSE, 0);
-
gtk_widget_show(hbox_straight);
-
gtk_widget_show(hbox_function);
-
gtk_widget_show(hbox_btn);
-
-
/* Add the two radio buttons for mx+b or function */
-
radio_straight = gtk_radio_button_new_with_label(NULL, "fx = mx + b");
-
radio_function = gtk_radio_button_new_with_label(gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_straight)), "fx = ");
-
gtk_box_pack_start(GTK_BOX(hbox_straight), radio_straight, FALSE, FALSE, 0);
-
gtk_box_pack_start(GTK_BOX(hbox_function), radio_function, FALSE, FALSE, 0);
-
-
/* Add the text entrie boxes */
-
entry_straight = gtk_entry_new();
-
entry_function = gtk_text_view_new();
-
text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry_function));
-
gtk_text_buffer_set_text(text_buffer, "y = 2*x + 4", -1);
-
gtk_box_pack_start(GTK_BOX(hbox_straight), entry_straight, FALSE, FALSE, 0);
-
gtk_box_pack_start(GTK_BOX(hbox_function), entry_function, FALSE, FALSE, 0);
-
-
/* Add some buttons */
-
btn_graph = gtk_button_new_with_label("Graph");
-
btn_clear = gtk_button_new_with_label("Clear");
-
g_signal_connect(G_OBJECT(btn_graph), "clicked", G_CALLBACK(btn_graph_clicked), (gpointer) radio_straight);
-
-
gtk_box_set_spacing(GTK_BOX(hbox_btn), 20);
-
gtk_box_pack_start(GTK_BOX(hbox_btn), btn_graph, TRUE, TRUE, 10);
-
gtk_box_pack_start(GTK_BOX(hbox_btn), btn_clear, TRUE, TRUE, 10);
-
-
/* Show everythign that is left to show... */
-
gtk_widget_show(radio_straight);
-
gtk_widget_show(radio_function);
-
gtk_widget_show(entry_straight);
-
gtk_widget_show(entry_function);
-
gtk_widget_show(btn_graph);
-
gtk_widget_show(btn_clear);
-
gtk_widget_show(window);
-
-
gtk_main();
-
-
return 0;
-
}
PermaLink to this entry https://pastebin.co.uk/11263
Posted by PEACEY Thu 1st Mar 2007 22:17 - Syntax is C - 36 views
Download | New Post | Modify | Hide line numbers
Download | New Post | Modify | Hide line numbers
Comments: 0