Re: How to draw points on the drawing area?



Hello,

First ignoring the PDU part. You draw to the drawing are with the gc_draw_*()
functions in the exposure event callback. Here is the critical code 
that you need to set up:


    gboolean event_handler (GtkWidget *drawing_area,
			    GdkEvent *event,
			    gpointer client_data)
    {
      if (event->type == GDK_EXPOSE)
	  draw_graphics((MyApp*)client_data);

      return 1;
    }

    :

    app.drawing_area = gtk_drawing_area_new ();

    gtk_widget_set_events (app.drawing_area,
                           GDK_EXPOSURE_MASK);

    gtk_signal_connect (GTK_OBJECT(app.drawing_area),
                        "event",
                        GTK_SIGNAL_FUNC(event_handler),
                        &app);

You may then do all you drawing in the draw_graphics callback.
I'm including a complete program draw_star.c below that shows
you a complete example.

Note that you may want to optimize the draw_graphics() function
to only update the rectangle that was exposed.

About the PDU that you mentioned (though I have no idea what it is).
Whenever you have data to read on that channel you should get a
callback in which you read the data. You should then call draw_graphics()
manually to update the display.

The way you set up to have gtk call you when you have data on the
PDU channel is by the command gdk_input_add() in Gtk-1.2.* or
gtk_input_add_full() in Gtk-2.0.*.

I hope this helps.

Regards,
Dov

--

                                                        ___   ___
                                                      /  o  \   o \
Dov Grobgeld                                         ( o  o  ) o   |
The Weizmann Institute of Science, Israel             \  o  /o  o /
"Where the tree of wisdom carries oranges"              | |   | |
                                                       _| |_ _| |_
 
    
On Sat, Sep 14, 2002 at 10:47:49PM -0000, viewworld wrote:
> Hi,
> 
> I am a new glade user. I am using glad to generate a porject in c language. 
> 
> In this project,I will get positions of a moving object from a PDU continuously, and will draw these points on the drawing area and update some labels accordingly. 
> 
> Any suggestion or hints would be greatly appreciated.
> 
> Rose

/*======================================================================
//  Makefile:
//
//     CFLAGS = `pkg-config --cflags gtk+-2.0`           
//     LIBS = `pkg-config --libs gtk+-2.0`            
//                                                    
//     draw-star: draw-star.o                         
//             $(CC) -o $@ $  o $(LIBS)               
//
//----------------------------------------------------------------------
//
//  An example that draws line widths in a star. This file is in the
//  public domain.
//
//  Dov Grobgeld <dov imagic weizmann ac il>
//----------------------------------------------------------------------*/
#include <glib.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <math.h>

typedef struct {
    GtkWidget *window;
    GtkWidget *drawing_area;
    GdkGC *drawing_area_gc;
} MyApp;

void draw_star(GdkWindow *window,
               GdkGC *gc,
               int star_center_x,
               int star_center_y,
               double line_width,
               double r1,
               double r2,
               int delta_angle
               )
{
    int angle;
    gdk_gc_set_line_attributes(gc,
                               line_width,
                               GDK_LINE_SOLID,
                               GDK_CAP_ROUND,
			       GDK_JOIN_ROUND);
    
    for (angle=0; angle<360; angle+=delta_angle) {
        double angle_rad = 1.0*angle/180 * M_PI;
        int x1 = (int)(star_center_x + r1 * cos(angle_rad)+0.5);
        int y1 = (int)(star_center_y + r1 * sin(angle_rad)+0.5);
        int x2 = (int)(star_center_x + r2 * cos(angle_rad)+0.5);
        int y2 = (int)(star_center_y + r2 * sin(angle_rad)+0.5);

        gdk_draw_line(window,
                      gc,
                      x1,y1,
                      x2,y2);
        
    }
}

void draw_graphics(MyApp *app)
{
    int lw_idx;
    
    for (lw_idx=0; lw_idx<9; lw_idx++) {
        double col_idx = lw_idx % 3;
        double row_idx = lw_idx / 3;

        draw_star(app->drawing_area->window,
                  app->drawing_area_gc,
                  64+128*col_idx,64+128*row_idx,
                  lw_idx,10,60, 15 );
    }
}

gboolean event_handler (GtkWidget *drawing_area,
                        GdkEvent *event,
                        gpointer client_data)
{
  if (event->type == GDK_EXPOSE)
      draw_graphics((MyApp*)client_data);

  return 1;
}

gint main (gint argc,gchar *argv[])
{
    MyApp app;

    gtk_init (&argc, &argv);

    app.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_signal_connect (GTK_OBJECT (app.window), "destroy",
                        GTK_SIGNAL_FUNC (gtk_main_quit), NULL);

    app.drawing_area = gtk_drawing_area_new ();
    gtk_drawing_area_size (GTK_DRAWING_AREA(app.drawing_area),
                           128*3, 128*3);

    gtk_widget_set_events (app.drawing_area,
                           GDK_EXPOSURE_MASK);

    gtk_signal_connect (GTK_OBJECT(app.drawing_area),
                        "event",
                        GTK_SIGNAL_FUNC(event_handler),
                        &app);

    gtk_container_add (GTK_CONTAINER(app.window),
                       app.drawing_area);

    gtk_widget_realize (app.window);
    app.drawing_area_gc = gdk_gc_new (app.window->window);

    gtk_widget_show_all (app.window);

    gtk_main ();

    return 0;
}


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