GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

gtk_update

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Fri Jan 09, 2009 6:52 am    Post subject: gtk_update Reply with quote

Hey guys and girls,

I'm a new user of gtk and c. I am trying to write a program wherein the window updates its self based on the current data input in real time. Currently, I am using
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
static gboolean motion_notify_event (GtkWidget *widget,
                     GdkEventMotion *motion_event)
{
int x, y;
GdkModifierType state;
//GdkEventFunc state
gtk_widget_draw(GTK_WIDGET(widget), NULL);

if  (motion_event -> is_hint)
    gdk_event_request_motions (motion_event);

    else {
        x = motion_event->x;
        y = motion_event->y;
        state = (GdkModifierType) motion_event->state;
         }

if (state &&pixmap !=NULL)
    draw_brush (widget, x, y);
return TRUE;
}
and gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
            // | GDK_LEAVE_NOTIFY_MASK
|GDK_ENTER_NOTIFY_MASK
             | GDK_ALL_EVENTS_MASK
             | GDK_BUTTON_PRESS_MASK);

(in main). I have also tried using
Code: (C)
1
gtk_window_begin_move_drag(widget);
and
Code: (C)
1
gtk_widget_draw(GTK_WIDGET(widget), NULL);
added codebb -dreblen
Non of them seem to work the way i want them to work. In the best instance, the object in the window is only updated once i move the move. Is there a way of notifying the motion based on data it is obtaining based on time without having to use a mouse?
cheers, Moses
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Sun Jan 11, 2009 7:40 pm    Post subject: Reply with quote

Hi and welcome to the Gtk+ forums.

I think that the best way of updating you GUI without blocking normal operation is to use timeout or idle function.

You can read more about them at http://library.gnome.org/devel/glib/unstable/glib-The-Main-Event-Loop.html#g-timeout-add and http://library.gnome.org/devel/glib/unstable/glib-The-Main-Event-Loop.html#g-idle-add

If you post more specific data regarding what are you trying to achieve, we may be able to provide more specific help.
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Sun Jan 11, 2009 11:06 pm    Post subject: Thx Reply with quote

Dear Tad,
Thanks for your quick response. With the code, I am trying to display on a widget the position of a vehicle as it moves. The function moslaserout obtains its x and y coordinates. The problem is, I can't see the exact position or new position of the vehicle without moving my mouse. Moving my mouse faster leads to segmentation fault. I would need help with the automatic display of the position of the vehicle without having to move the mouse. Here is a section of the code again.
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Backing pixmap for drawing area */
static GdkPixmap *pixmap = NULL;

/* 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);

  return TRUE;
}


/* This is my version of redrawing the screen from a backed pixmap. */
static gboolean expose_event(GtkWidget *widget, GdkEventExpose *motion_event)

{//moslaserout(int argc, char *argv[]);   
 
gdk_draw_drawable(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE (widget)], pixmap,
          motion_event->area.x, motion_event->area.y,
          motion_event->area.x, motion_event->area.y,
          motion_event->area.width, motion_event->area.height);
return FALSE;
}

/* Draw a rectangle on the screen (since we need event update) */

static void draw_brush (GtkWidget *widget,
                        gdouble x,
            gdouble y)

{
unsigned int num[scnsz];
   float  xu[scnsz], yu[scnsz];

//while (! done) {
moslaserout(0, 0, xu, yu);

//

int counter2 = 0;

/*while (counter2 <scnsz)
{
printf(" hope %f %f",xu[counter2],yu[counter2]);
counter2 ++; }
//}*/
 


GdkRectangle coords;

coords.x  = 6; //xu[counter2];
coords.y = 7; //yu[counter2];
coords.width = 5;
coords.height = 5;

gdk_draw_rectangle(pixmap,
           widget->style->black_gc,
           TRUE,
           coords.x, coords.y,
           coords.width, coords.height);

gtk_widget_queue_draw_area (widget,
                coords.x, coords.y,
                coords.width, coords.height);
//printf(" hope %d %d",coords.x, coords.y);
}   

/* Notifying the motion (this is useful inorder that the update is done */
/*static gboolean expose_event_callback7 (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{

gdk_draw_arc (widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
TRUE,
xu, yu,5,5
,0, 360*64);


return TRUE;
} */
static gboolean motion_notify_event (GtkWidget *widget,
                     GdkEventMotion *motion_event)
{
int x, y;
GdkModifierType state;
//GdkEventFunc state


if  (motion_event -> is_hint)
    gdk_event_request_motions (motion_event);

    else {
        x = motion_event->x;
        y = motion_event->y;
        state = (GdkModifierType) motion_event->state;
         }

if (state &&pixmap !=NULL)
    draw_brush (widget, x, y);
return TRUE;
}



void quit ()
{
  exit (0);
}

int main( int   argc, char **argv )
{
  GtkWidget *window, *drawing_area, *drawing_area2, *fixed, *vbox;
 

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_name (window, "PDA Current Position"); //pls note i havent set window size

 
vbox = gtk_vbox_new (FALSE, 1);
  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), 750, 480);
  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);

  /* Event signals */

 
g_signal_connect (G_OBJECT (drawing_area), "motion_notify_event",
            G_CALLBACK (motion_notify_event), NULL);
 

  gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
             | GDK_LEAVE_NOTIFY_MASK
             | GDK_BUTTON_PRESS_MASK
             | GDK_POINTER_MOTION_MASK
             | GDK_POINTER_MOTION_HINT_MASK);

/* Displaying the vehicle position */

g_signal_connect (G_OBJECT (drawing_area), "expose_event",
G_CALLBACK (expose_event_callback), NULL);

/*g_signal_connect (G_OBJECT (drawing_area), "expose_event",
G_CALLBACK (expose_event_callback7), NULL); */


gtk_widget_show_all (GTK_WIDGET(window));

gtk_widget_show(vbox);

  gtk_main ();

  return 0;
}
added codebb -dreblen

Thanks
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Mon Jan 12, 2009 4:04 pm    Post subject: Reply with quote

Hi.

I think that by using cairo you can greatly simplify your code that is responsible for drawing vehicle's position. I created a simple example that demonstrates some useful techniques, among other timeout functions and some cairo drawing.

Official documentation can be found at http://library.gnome.org/devel/cairo/1.8/index.html and http://library.gnome.org/devel/glib/stable/glib-The-Main-Event-Loop.html#g-timeout-add

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <gtk/gtk.h>

static gboolean
timeout( GtkWidget *area )
{
    gtk_widget_queue_draw( area );

    return( TRUE );
}

static void
get_cur_loc( gint *x,
             gint *y )
{
    static gint reference = 0;

    if( reference == 3000 )
        reference = 0;

    reference += 10;

    *y = reference / 10;
    *x = reference % 350;
}

static gboolean
cb_expose_event( GtkWidget      *draw,
                 GdkEventExpose *event,
                 gpointer        data )
{
    cairo_t          *cr;
    static GdkPixbuf *pixbuf = NULL;
    gint              x, y;

    if( ! pixbuf )
    {
        pixbuf = gdk_pixbuf_new_from_file_at_size( "test.png", 400, 400, NULL );
    }

    /* Paint background map */
   
cr = gdk_cairo_create( draw->window );
    gdk_cairo_set_source_pixbuf( cr, pixbuf, 0, 0 );
    cairo_paint( cr );

    /* Draw our current location */
   
get_cur_loc( &x, &y );
    cairo_set_line_width( cr, 20 );
    cairo_set_source_rgb( cr, 1, 0, 0 );
    cairo_set_line_cap( cr, CAIRO_LINE_CAP_ROUND );
    cairo_move_to( cr, x, y );
    cairo_close_path( cr );
    cairo_stroke( cr );

    cairo_destroy( cr );

    return( FALSE );
}

int
main( int    argc,
      char **argv )
{
    GtkWidget *window;
    GtkWidget *draw;

    gtk_init( &argc, &argv );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size( GTK_WINDOW( window ), 400, 400 );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    draw = gtk_drawing_area_new();
    g_signal_connect( G_OBJECT( draw ), "expose-event",
                      G_CALLBACK( cb_expose_event ), NULL );
    gtk_container_add( GTK_CONTAINER( window ), draw );

    gtk_widget_show_all( window );

    g_timeout_add( 100, (GSourceFunc)timeout, draw );

    gtk_main();

    return( 0 );
}
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Tue Jan 13, 2009 1:21 am    Post subject: Reply with quote

Thanks Tad,

My code now works with the timeout function. There is only one problem though, i.e. if the streaming/ update time changes, i get segmentation fault. I think an if loop should be able to resolve this problem.

Another question is, is it possible to shift the origin of the drawing area so that i can plot negative coords. I have tried putting it into vbox then move it but the origin doesnt change.

Cheers,
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Tue Jan 13, 2009 9:25 am    Post subject: Reply with quote

Hi.

You don't need to do anything with drawing area. You just need to transform cairo's current transformation matrix. The function needed to move the origin would be cairo_translate (you may want to look at other transformation functions too, since they are most useful for eg. drawing lines at an angle without the need to use heavy geometric calculations to obtain x and y coordinates).

I modified my example to demonstrate the translation transformation.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <gtk/gtk.h>

static gboolean
timeout( GtkWidget *area )
{
    gtk_widget_queue_draw( area );

    return( TRUE );
}

static void
get_cur_loc( gint *x,
             gint *y )
{
    static gint reference = 150;
    static gint increment = 1;

    if( reference == 150 || reference == -150 )
        increment *= -1;

    reference += increment * 5;

    *x = reference;
    if( reference >= 0 )
        *y = increment * ( 150 - reference );
    else
       
*y = increment * ( 150 + reference );
}

static gboolean
cb_expose_event( GtkWidget      *draw,
                 GdkEventExpose *event,
                 gpointer        data )
{
    cairo_t          *cr;
    static GdkPixbuf *pixbuf = NULL;
    gint              x, y;

    if( ! pixbuf )
    {
        pixbuf = gdk_pixbuf_new_from_file_at_size( "test.png", 400, 400, NULL );
    }

    /* Paint background map */
   
cr = gdk_cairo_create( draw->window );
    gdk_cairo_set_source_pixbuf( cr, pixbuf, 0, 0 );
    cairo_paint( cr );

    /* Draw our current location */
   
get_cur_loc( &x, &y );
    g_print( "Coords: (%d, %d)\n", x, y );
    cairo_translate( cr, 200, 200 );
    cairo_set_line_width( cr, 20 );
    cairo_set_source_rgb( cr, 1, 0, 0 );
    cairo_set_line_cap( cr, CAIRO_LINE_CAP_ROUND );
    cairo_move_to( cr, x, y );
    cairo_close_path( cr );
    cairo_stroke( cr );

    cairo_destroy( cr );

    return( FALSE );
}

int
main( int    argc,
      char **argv )
{
    GtkWidget *window;
    GtkWidget *draw;

    gtk_init( &argc, &argv );

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    gtk_window_set_default_size( GTK_WINDOW( window ), 400, 400 );
    g_signal_connect( G_OBJECT( window ), "destroy",
                      G_CALLBACK( gtk_main_quit ), NULL );

    draw = gtk_drawing_area_new();
    g_signal_connect( G_OBJECT( draw ), "expose-event",
                      G_CALLBACK( cb_expose_event ), NULL );
    gtk_container_add( GTK_CONTAINER( window ), draw );

    gtk_widget_show_all( window );

    g_timeout_add( 100, (GSourceFunc)timeout, draw );

    gtk_main();

    return( 0 );
}
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Tue Jan 13, 2009 12:22 pm    Post subject: Reply with quote

Thanks Tad,
I've got it right without using cairo library. The problem now is how to change the color of each image in the widget say for e.g. if I display a rectangle, circle and points and do want to use the colors red, black and yellow. I have tried using GdkColor color but changes the color of everything in the widget.

Cheers.
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Tue Jan 13, 2009 12:44 pm    Post subject: Reply with quote

Hi.

I cannot give you the exact answer to this question, since you haven't revealed your method of drawing. But if you use GDK's functions to draw, you can set colors by modifying foreground color property of GdkGC object.
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Wed Jan 14, 2009 5:10 am    Post subject: Reply with quote

Dear Tad,

Thanks once again for your quick response. I am using purely gtk to draw my lines. My project does not require the use of cairo library. The function that I use to draw each of my lines, points and shapes is similar to this
Code: (C)
1
2
3
4
5
6
         gdk_draw_rectangle (pixmap,
              widget->style->white_gc,
              TRUE,
              0, 0,
              widget->allocation.width,
              widget->allocation.height);
added codebb -dreblen
To change the colors, i have used GdkColor but this changes the color of the container (widhet/window). So I wonder if there is a better way of doing it .i.e. changing the color of individual geometries.
Cheers.
Back to top
tadeboro
Never Seen the Sunlight


Joined: 23 Jul 2008
Posts: 2114
Location: Slovenia

PostPosted: Wed Jan 14, 2009 11:46 am    Post subject: Reply with quote

Hello.

I think that you need to do something like this.

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
GdkGC    *gc;
GdkColor  color;

gc = gdk_gc_new( widget->window );

/* White rectange */
color.red   = 65535;
color.green = 65535;
color.blue  = 65535;
gdk_gc_set_rgb_fg_color( gc, &color );
gdk_draw_rectangle (pixmap, gc, TRUE, 0, 0, widget->allocation.width,
                    widget->allocation.height);

/* Black rectange */
color.red   = 0;
color.green = 0;
color.blue  = 0;
gdk_gc_set_rgb_fg_color( gc, &color );
gdk_draw_rectangle (pixmap, gc, TRUE, 0, 0, widget->allocation.width,
                    widget->allocation.height);

g_object_unref( G_OBJECT( gc ) );
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Thu Jan 15, 2009 6:02 am    Post subject: Reply with quote

I tried using
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
static gboolean expose_event_callback7 (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
GdkGC *gc;
GdkColor color;
gc = gdk_gc_new( widget->window );


GdkRectangle rect;

rect.x = 50;
rect.y = 100;
rect.width = 100;
rect.height = 60;



color.red   = 65535;
color.green =65535 ;
color.blue  = 65535;

gdk_gc_set_rgb_fg_color( gc, &color );
gdk_draw_rectangle (pixmap, gc, TRUE, rect.x, rect.y, rect.width, rect.height);

g_object_unref( G_OBJECT( gc ) );

return TRUE;
}
added codebb -dreblen

It doesn't work and i wont worry about it since its not that important for my project. I dont get how you set the colors color.red = 65535; above.
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Fri Jan 16, 2009 7:25 am    Post subject: Reply with quote

Thanks very much.
Here is the most part of my code.
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <stdio.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <gdk/gdkkeysyms.h>
#define size 4

static gboolean expose_event_callback7 (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
gdk_draw_arc (widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
TRUE,
100, 100,5,5
,0, 360*64);

return TRUE;
}

static gboolean expose_event_callback2 (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{

GdkPoint points[size];

unsigned int xmap[size] = {50,400,200,80};
unsigned int ymap[size] = {60,110,400,300};

gint i = 0;
while (i < size)
{
points[i].x = xmap[i];
points[i].y = ymap[i];
i++;
}




gdk_draw_polygon (widget->window,
            widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
            FALSE,points, 4);

//gtk_widget_draw();//(widget->window, &points);
 
gdk_threads_leave();

/*
gdk_draw_polygon (pixmap, gc, TRUE, points, 4); */
return FALSE;
}

/* Backing pixmap for drawing area */
static GdkPixmap *pixmap = NULL;

/* 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);

  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,
                        gdouble    x,
                        gdouble    y)
{
  GdkRectangle update_rect;

  update_rect.x = x - 5;
  update_rect.y = y - 5;
  update_rect.width = 5;
  update_rect.height = 5;
  gdk_draw_rectangle (pixmap,
              widget->style->black_gc,
              TRUE,
              update_rect.x, update_rect.y,
              update_rect.width, update_rect.height);
  gtk_widget_queue_draw_area (widget,
                      update_rect.x, update_rect.y,
                      update_rect.width, update_rect.height);
printf("\n %d is %d",update_rect.x, update_rect.y);
}

static gboolean button_press_event( GtkWidget      *widget,
                                    GdkEventButton *event )
{
  if (event->button == 1 && pixmap != NULL)
    draw_brush (widget, event->x, event->y);
 g_print("clicked\n");

  return TRUE;
}

static gboolean button_press_event2( GtkWidget      *widget,
                                    GdkEventButton *event )
{g_print("hello");
//snprintf( should be able to display stuffs on the screen
}

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, x, y);
 
  return TRUE;
}
/* This crap to show vehicle status */
void vehicle_status(GtkWindow *window,
     GdkEvent *event, gpointer data)
 {
   int x, y;
   char buf[100];
   x = event->configure.x;
   y = event->configure.y;
   sprintf(buf, "%d, %d", x, y);
   gtk_window_set_title(window, buf);
 }



void quit ()
{
  exit (0);
}

int main( int   argc,
          char *argv[] )
{
  GtkWidget *window, *drawing_area, *fixed, *vbox, *filemenu, *file, *quit, *sep, *menubar, *position, *cruc;
    GtkAccelGroup *accel_group = NULL;


  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 // gtk_widget_set_name (window, "Test Draw 9");

 
vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_widget_show (vbox);

menubar = gtk_menu_bar_new();
  filemenu = gtk_menu_new();

accel_group = gtk_accel_group_new();
  gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);


file = gtk_menu_item_new_with_label("TASKS");
position = gtk_menu_item_new_with_label("HMC Position");
cruc = gtk_menu_item_new_with_label("Pick Crucible");


  sep = gtk_separator_menu_item_new();
  quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel_group);

  gtk_widget_add_accelerator(quit, "activate", accel_group,
      GDK_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);


gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), position);
gtk_menu_shell_append(GTK_MENU_SHELL(filemenu),cruc);
 

  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), sep);
  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
   
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);
/*
g_signal_connect_swapped(G_OBJECT(window), "destroy",
      G_CALLBACK(expose_event_callback2), NULL); */

 
g_signal_connect(G_OBJECT(quit), "activate",
      G_CALLBACK(gtk_main_quit), NULL);

g_signal_connect(G_OBJECT(position), "button_press_event",
      G_CALLBACK(expose_event_callback2), NULL);//NULL);

/*g_signal_connect(G_OBJECT(position), "clicked",
      G_CALLBACK(expose_event_callback2), G_OBJECT(window)); */
 
gtk_widget_show_all(window);




  g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (quit), NULL);

g_signal_connect (G_OBJECT (window), "destroy",
                    G_CALLBACK (position), NULL);

  /* Create the drawing area */

 
drawing_area = gtk_drawing_area_new ();


  gtk_widget_set_size_request (GTK_WIDGET (drawing_area), 750, 480);


  gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);


  gtk_widget_show (drawing_area);
//gtk_widget_show (drawing_area2);
  /* 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);

  /* Event signals */

 
g_signal_connect (G_OBJECT (drawing_area), "motion_notify_event",
            G_CALLBACK (motion_notify_event), NULL);

  g_signal_connect (G_OBJECT (drawing_area), "button_press_event",
            G_CALLBACK (button_press_event), NULL);

gtk_widget_add_events(GTK_WIDGET(window), GDK_CONFIGURE);

  gtk_widget_add_events (drawing_area, GDK_EXPOSURE_MASK
             | GDK_LEAVE_NOTIFY_MASK
             | GDK_BUTTON_PRESS_MASK
             | GDK_POINTER_MOTION_MASK
             | GDK_POINTER_MOTION_HINT_MASK);

/* Displaying the vehicle position */
/*
g_signal_connect(G_OBJECT(window), "configure-event",
        G_CALLBACK(vehicle_status), NULL); */



gtk_widget_show_all (GTK_WIDGET(window));
gtk_widget_show(vbox);

  gtk_main ();

  return 0;
}
added codebb -dreblen

There is a problem though hitting hmcposition calls expose_event_callback2 but only lasts for some seconds and then dissappears. The purpose of the code is to open a different page when a menu is hit. I have also tried using "activate" instead of "expose_even" but it just doesnt seem to work. Is there an alternative way of doing it or my approach is totally wrong?

Cheers.
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 936
Location: Falun, WI USA

PostPosted: Fri Jan 16, 2009 7:55 pm    Post subject: Reply with quote

I won't lock either topic, since they both can serve a purpose, but please pick either the current topic, or this one:
http://gtkforums.com/viewtopic.php?t=2596
to discuss the above piece of code.
Thank you...
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP