Posted by PEACEY Thu 1st Mar 2007 22:17 - Syntax is C - 36 views
Download | New Post | Modify | Hide line numbers
Description:
This is what i did s far...

  1. #include
  2. #include
  3. #include "scribble.h"
  4.  
  5. #define min(x, y) (x > y) ? y : x
  6.  
  7. /* Backing pixmap for drawing area */
  8. static GdkPixmap *pixmap = NULL;
  9. GSList *data = NULL;
  10.  
  11. typedef struct {
  12.     gdouble x1;
  13.     gdouble y1;
  14.     gdouble x2;
  15.     gdouble y2;
  16. } points_info;
  17.  
  18. /* Create a new backing pixmap of the appropriate size */
  19. static gboolean configure_event( GtkWidget         *widget,
  20.                                  GdkEventConfigure *event )
  21. {
  22.     if (pixmap)
  23.         g_object_unref(pixmap);
  24.  
  25.   pixmap = gdk_pixmap_new (widget->window,
  26.                widget->allocation.width,
  27.                widget->allocation.height,
  28.                -1);
  29.   gdk_draw_rectangle (pixmap,
  30.               widget->style->white_gc,
  31.               TRUE,
  32.               0, 0,
  33.               widget->allocation.width,
  34.               widget->allocation.height);
  35.  
  36.   draw_brush(widget, data);
  37.   return TRUE;
  38. }
  39.  
  40. /* Redraw the screen from the backing pixmap */
  41. static gboolean expose_event( GtkWidget      *widget,
  42.                               GdkEventExpose *event )
  43. {
  44.   gdk_draw_drawable (widget->window,
  45.              widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
  46.              pixmap,
  47.              event->area.x, event->area.y,
  48.              event->area.x, event->area.y,
  49.              event->area.width, event->area.height);
  50.  
  51.   return FALSE;
  52. }
  53.  
  54. /* Draw a rectangle on the screen */
  55. static void draw_brush( GtkWidget *widget,
  56.                         GSList *list)
  57. {
  58.   gdouble cx, cy, mindim, scale;
  59.   gint spacer = 10;
  60.   gint scaledmax = 120;
  61.   gdouble w = widget->allocation.width;
  62.   gdouble h = widget->allocation.height;
  63.  
  64.   /* Finding the center point */
  65.   h = h + 2*spacer;
  66.   cx = w/2;
  67.   cy = h/2;
  68.  
  69.   /* Calculating the scale factor */
  70.   mindim = min(cx, h/2);
  71.   scale = mindim/scaledmax;
  72.  
  73.   gdk_draw_line(pixmap, widget->style->black_gc, cx-110*scale, cy, cx+110*scale, cy);
  74.   gdk_draw_line(pixmap, widget->style->black_gc, cx, cy-110*scale, cx, cy+110*scale);
  75.  
  76.   g_print("%f %f %f\n", cx, cy, scale);
  77.  
  78.   GSList *tmp = data;
  79.   points_info *points;
  80.   while (tmp != NULL) {
  81.     points = tmp->data;
  82.     gdk_draw_line(pixmap, widget->style->black_gc, ((points->x1)*scale)+cx, ((points->y1)*scale)+cy, ((points->x2)*scale)+cx, ((points->y2)*scale)+cy);
  83.     tmp = g_slist_next(tmp);
  84.   }
  85.  
  86.   gtk_widget_queue_draw(widget);
  87.  
  88. }
  89.  
  90. static gboolean motion_notify_event( GtkWidget *widget,
  91.                                      GdkEventMotion *event )
  92. {
  93.   int x, y;
  94.   GdkModifierType state;
  95.  
  96.   if (event->is_hint)
  97.     gdk_window_get_pointer (event->window, &x, &y, &state);
  98.   else
  99.     {
  100.       x = event->x;
  101.       y = event->y;
  102.       state = event->state;
  103.     }
  104.    
  105.   if (state & GDK_BUTTON1_MASK && pixmap != NULL)
  106.     draw_brush (widget, data);
  107.  
  108.   return TRUE;
  109. }
  110.  
  111. static void btn_graph_clicked( GtkWidget *widget, gpointer *radio, GdkEvent *event ) {
  112.     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio)) == TRUE) printf("lala\n");
  113.    
  114. }
  115.  
  116. void quit ()
  117. {
  118.   exit (0);
  119. }
  120.  
  121. int main( int   argc, char *argv[] ) {
  122.     GtkWidget *window;
  123.     GtkWidget *drawing_area;
  124.     GtkWidget *vbox;
  125.    
  126.     GtkWidget *btn_graph;
  127.     GtkWidget *btn_clear;
  128.     GtkWidget *entry_straight;
  129.     GtkWidget *entry_function;
  130.     GtkWidget *radio_straight;
  131.     GtkWidget *radio_function;
  132.     GtkWidget *hbox_straight;
  133.     GtkWidget *hbox_function;
  134.     GtkWidget *hbox_btn;
  135.    
  136.     GtkTextBuffer *text_buffer;
  137.    
  138.     gtk_init(&argc, &argv);
  139.    
  140.     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  141.     gtk_widget_set_name(window, "Ze Grapher");
  142.    
  143.     /* Create a new vertical container */
  144.     vbox = gtk_vbox_new (FALSE, 0);
  145.     gtk_container_add(GTK_CONTAINER(window), vbox);
  146.     gtk_widget_show(vbox);
  147.    
  148.     g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(quit), NULL);
  149.    
  150.     /* Create the drawing area */
  151.    
  152.     drawing_area = gtk_drawing_area_new ();
  153.     gtk_widget_set_size_request(GTK_WIDGET(drawing_area), 500, 500);
  154.     gtk_box_pack_start(GTK_BOX(vbox), drawing_area, TRUE, TRUE, 0);
  155.    
  156.     gtk_widget_show(drawing_area);
  157.    
  158.     /* Signals used to handle backing pixmap */
  159.    
  160.     g_signal_connect(G_OBJECT(drawing_area), "expose_event", G_CALLBACK(expose_event), NULL);
  161.     g_signal_connect(G_OBJECT(drawing_area), "configure_event", G_CALLBACK(configure_event), NULL);
  162.    
  163.     /* Create new horizontal containers */
  164.     hbox_straight = gtk_hbox_new(FALSE, 0);
  165.     hbox_function = gtk_hbox_new(FALSE, 0);
  166.     hbox_btn = gtk_hbox_new(FALSE, 0);
  167.     gtk_box_pack_start(GTK_BOX(vbox), hbox_straight, FALSE, FALSE, 0);
  168.     gtk_box_pack_start(GTK_BOX(vbox), hbox_function, FALSE, FALSE, 0);
  169.     gtk_box_pack_start(GTK_BOX(vbox), hbox_btn, FALSE, FALSE, 0);
  170.     gtk_widget_show(hbox_straight);
  171.     gtk_widget_show(hbox_function);
  172.     gtk_widget_show(hbox_btn);
  173.    
  174.     /* Add the two radio buttons for mx+b or function */
  175.     radio_straight = gtk_radio_button_new_with_label(NULL, "fx = mx + b");
  176.     radio_function = gtk_radio_button_new_with_label(gtk_radio_button_get_group(GTK_RADIO_BUTTON(radio_straight)), "fx = ");
  177.     gtk_box_pack_start(GTK_BOX(hbox_straight), radio_straight, FALSE, FALSE, 0);
  178.     gtk_box_pack_start(GTK_BOX(hbox_function), radio_function, FALSE, FALSE, 0);
  179.  
  180.     /* Add the text entrie boxes */
  181.     entry_straight = gtk_entry_new();
  182.     entry_function = gtk_text_view_new();
  183.     text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(entry_function));
  184.     gtk_text_buffer_set_text(text_buffer, "y = 2*x + 4", -1);
  185.     gtk_box_pack_start(GTK_BOX(hbox_straight), entry_straight, FALSE, FALSE, 0);
  186.     gtk_box_pack_start(GTK_BOX(hbox_function), entry_function, FALSE, FALSE, 0);
  187.  
  188.     /* Add some buttons */
  189.     btn_graph = gtk_button_new_with_label("Graph");
  190.     btn_clear = gtk_button_new_with_label("Clear");
  191.     g_signal_connect(G_OBJECT(btn_graph), "clicked", G_CALLBACK(btn_graph_clicked), (gpointer) radio_straight);
  192.    
  193.     gtk_box_set_spacing(GTK_BOX(hbox_btn), 20);
  194.     gtk_box_pack_start(GTK_BOX(hbox_btn), btn_graph, TRUE, TRUE, 10);
  195.     gtk_box_pack_start(GTK_BOX(hbox_btn), btn_clear, TRUE, TRUE, 10);
  196.  
  197.     /* Show everythign that is left to show... */
  198.     gtk_widget_show(radio_straight);
  199.     gtk_widget_show(radio_function);
  200.     gtk_widget_show(entry_straight);
  201.     gtk_widget_show(entry_function);
  202.     gtk_widget_show(btn_graph);
  203.     gtk_widget_show(btn_clear);
  204.     gtk_widget_show(window);
  205.    
  206.     gtk_main();
  207.    
  208.     return 0;
  209. }

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

 

Comments: 0