RE: how to draw something on GtkDrawingArea when I click a button?





As a matter of fact. I would suggest using Cairo. Which will allow you to then draw in any context (IE 
Printer, PDF, etc...)


    /* Local Variables */
    cairo_t *cr;        /* Rendering context derived from widget */

    /* Get the context to which we need to draw */
    cr = gdk_cairo_create(widget->window);
    /* Printer context would be: 
      cr = gtk_print_context_get_cairo_context(context) 
       where context is a GtkPrinterContext* */

    /* If we have a valid context draw valid object */
    if (cr != NULL){

       /* Boarder rectangle */
       cairo_rectangle(cr,  0.00, 0.00, 20.0 , 40.0 ); 

       cairo_stroke(cr);

       cairo_set_line_width(cr,line_width / 60);

       /* Virtical Seporation lines */
       cairo_move_to(cr, 20.0 , 40.0 ); 
       cairo_line_to(cr, 40.0, 40.0); 

       cairo_stroke(cr);

      /* Distroy the context we created */ 
      cairo_destroy(cr);

   }

 







 EMAILING FOR THE GREATER GOOD
Join me

Subject: Re: how to draw something on GtkDrawingArea when I click a button?
From: csaavedra gnome org
To: gtk-app-devel-list gnome org
Date: Fri, 19 Mar 2010 08:46:33 +0200

El vie, 19-03-2010 a las 13:34 +0800, Tolic Ma escribió:
Hi,everyone!

I want to draw something on GtkDrawingArea when I click a GtkButton,but I
don't know how to do this...

this is my code(it doesn't work...):

*void click_button(void){


gdk_draw_line(drawing_area,drawing_area->style->white_gc,x,y,width+x,height+y);

}

You shouldn't draw directly in your callback for the button. Instead,
you should call gtk_widget_queue_draw() to tell GTK+ to schedule a paint
of the widget.

How will the widget be painted? GTK+ will tell your code that the widget
needs to be painted by emitting the GtkWidget::expose-event signal.
Then, you should connect to that signal and paint in your callback.

Last but no least, don't forget to paint to the GdkWindow of the drawing
area, since that's the drawable.

More or less (untested code, just for clarity):

*void
click_button(GtkButton *button, gpointer data)
{
   button_clicked = TRUE;
   gtk_widget_queue_draw (drawing_area);
   ...
}

gboolean
expose_drawing_area (GtkWidget *area, 
                     GdkExposureEvent *event,
                     gpointer data)
{
   ...
   if (button_clicked) {
      gdk_draw_line(drawing_area->window,
                    drawing_area->style->white_gc,x,y,width+x,height+y);
   }
   ...
}

int main(int argc,char *argv[]){
   ...
   g_signal_connect(button,"clicked",G_CALLBACK(click_button),NULL);
   g_signal_connect(drawing_area,"expose-event",G_CALLBACK(expose_drawing_area),NULL);
   ...
}


Claudio

-- 
Claudio Saavedra <csaavedra gnome org>

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
                                          


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