Re: Drawing a line



Venkat,

I modified a program I wrote a while ago for experimentation purposes.
This program draws a resizable smiley face.  I have added a gdk_draw_line()
call to draw a crude, flat nose.  Note that all drawing is done in the 
"expose_event" callback.

Hope that helps.


Brad


/* drawing area.c */
#include <gtk/gtk.h>
#include <gdk/gdk.h>

#define MIN_DRAWING_XSIZE   300
#define MIN_DRAWING_YSIZE   200

#define EYEHEIGHT           20
#define EYEWIDTH            20

void destroy( GtkWidget *widget, gpointer   data )
{
   gtk_main_quit();
}

gboolean expose_event_callback( GtkWidget      *widget, 
                                GdkEventExpose *event, 
                                gpointer       data
                              )
{
   GdkColor      mycolor         = { 0, 0xaa, 0xaa, 0xaa };
   GdkColormap   *mycolormap;
   GdkGCValues   mygcvalues;
   GdkColor      originalcolor;
   GdkWindow     *window;

   window = widget->window;

   /*  Draw the circle with the standard widget properties 
    *  (i.e., color, etc.).
    */
   gdk_draw_arc( window,
                 widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                 TRUE,
                 0, 
                 0, 
                 widget->allocation.width,
                 widget->allocation.height,
                 0, 
                 64 * 360
               );

   /*  Get the widget's graphic context properties and store the original
    *  foreground color.
    */
   gdk_gc_get_values( widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                      &mygcvalues
                    );   
   originalcolor = mygcvalues.foreground;

   
   /*  Get the widget's graphic context properties.  */
   mycolormap = gtk_widget_get_colormap( widget );

   /*  Allocate the color we wish to use.  */
   gdk_colormap_alloc_color( mycolormap, &mycolor, TRUE, TRUE );

   /*  Set the foreground color to the new color.  */
/*  Why doesn't this work?
   gtk_widget_modify_fg( widget, GTK_STATE_NORMAL, &mycolor );
 */
   gdk_gc_set_foreground( widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                          &mycolor
                        );

   /*  Draw left eye.  */
   gdk_draw_arc( window,
                 widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                 TRUE,
                 widget->allocation.width * 1 / 3, 
                 widget->allocation.height / 4, 
                 EYEWIDTH,
                 EYEHEIGHT,
                 0, 
                 64 * 360
               );

   /*  Draw right eye.  */
   gdk_draw_arc( window,
                 widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                 TRUE,
                 widget->allocation.width * 2 / 3,
                 widget->allocation.height / 4,
                 EYEWIDTH,
                 EYEHEIGHT,
                 0,
                 64 * 360
               );
  
   /*  Draw a straight line for Venkat.  */
   gdk_draw_line( window,
                  widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                  widget->allocation.width / 2 - EYEWIDTH / 2,
                  widget->allocation.height / 2,
                  widget->allocation.width / 2 + EYEWIDTH / 2,
                  widget->allocation.height / 2
                );

   /*  Draw the mouth.  */
   gdk_draw_arc( window,
                 widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                 FALSE,
                 ( widget->allocation.width / 3 ) + ( EYEWIDTH / 2 ),
                 widget->allocation.height * 3 / 4,
                 ( widget->allocation.width / 3 ) - ( EYEWIDTH / 2 ),
                 widget->allocation.height / 10,
                 225 * 64,
                 90  * 64
               );
  
   /*  Reset the foreground color to the new color.  */
   gdk_gc_set_foreground( widget->style->fg_gc[GTK_WIDGET_STATE( widget )],
                          &originalcolor
                        );

   return FALSE;
}

int main( int   argc, char *argv[] )
{
   GtkWidget  *drawingarea;
   GtkWidget  *qbutton;
   GtkWidget  *qbuttonbox;
   GtkWidget  *separator;
   GtkWidget  *vbox;
   GtkWidget  *window;
   
   gtk_init (&argc, &argv);
   
   /* Create a window */
   window = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
   gtk_window_set_title( GTK_WINDOW( window ), "Drawing Demo");
   gtk_container_set_border_width( GTK_CONTAINER( window ), 10 );
   g_signal_connect( G_OBJECT( window ), "delete_event",
                     G_CALLBACK ( gtk_main_quit ), NULL
                   );

   /*  Create a vbox.  */
   vbox = gtk_vbox_new( FALSE, 0 );
   gtk_container_add( GTK_CONTAINER( window ), vbox );
   gtk_widget_show( vbox );
   
   /*  Create a drawing area.  */
   drawingarea = gtk_drawing_area_new();
   gtk_widget_set_size_request( GTK_WIDGET( drawingarea ),
                                MIN_DRAWING_XSIZE,
                                MIN_DRAWING_XSIZE
                              );
   g_signal_connect( G_OBJECT( drawingarea ), "expose_event",
                     G_CALLBACK ( expose_event_callback ), drawingarea
                   );

   /*  Put the drawing area into the vbox.  */
   gtk_box_pack_start( GTK_BOX( vbox ), drawingarea, TRUE, TRUE, 0 );

   /*  Show the drawing area & the scrollable window.  */
   gtk_widget_show( drawingarea );

   /*  Create a separator.  */
   separator = gtk_hseparator_new();
   gtk_box_pack_start( GTK_BOX( vbox ), separator, FALSE, FALSE, 10 );
   gtk_widget_show( separator );

   /*  Create a button box.  */
   qbuttonbox = gtk_hbutton_box_new();
   
   /*  Create the quit button.  */
   qbutton = gtk_button_new_from_stock( GTK_STOCK_QUIT );
   g_signal_connect_swapped( G_OBJECT( qbutton ), "clicked",
                             G_CALLBACK( gtk_main_quit ),
                             NULL
                           );

   /*  Put the button into the button box.  */
   gtk_container_add( GTK_CONTAINER( qbuttonbox ), qbutton );

   /*  Add the button box to the vbox.  */
   gtk_box_pack_start( GTK_BOX( vbox ), qbuttonbox, FALSE, FALSE, 0 );

   /*  Show the button.  */
   gtk_widget_show( qbutton );

   /*  Show the buttonbox.  */
   gtk_widget_show( qbuttonbox );

   /*  Show the window.  */
   gtk_widget_show( window );

   /*  Handle all events.  */
   gtk_main();

   return( 0 );
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]