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_menus

 
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 16, 2009 12:19 pm    Post subject: gtk_menus Reply with quote

Dear all;

I am trying to use gtk in designing a project that has atleast 3 different applications depending on what the cnsumer wants. To do this, I have created a file menu with some of the applications such as "Position", "New Project". Each of these is meant to be independent of the other. I use "activate" to connect the Position but never got connected so decided to use "expose_event" and it now shows the image but only for few second so wonder if anyone has a much better technique or can help solve this problem. Heres is the 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
#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
Back to top
bruce89
GTK+ Geek


Joined: 19 Dec 2007
Posts: 61
Location: Glasgow, Scotland

PostPosted: Fri Jan 16, 2009 11:30 pm    Post subject: Reply with quote

I have absolutely no idea what you're trying to do, and the formatting of the code doesn't help.
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Fri Jan 16, 2009 11:40 pm    Post subject: Reply with quote

I am trying to create a widget with the FILE menus: hmcposition, mission1, mission2 and quit. So that when the use clicks FILE then scrolls to any of the missions, the widget should be activated to show a new drawing or shape (e.g. a rectangle) while erasing what was originally there. If you run the code and click quit, the widget quits but if you click hmcPosition, the widget shows the figure its meant to show but only lasts for seconds. This is because I am using "expose_even" instead of "activate". I wonder if there is a way of achieving this. Thanks and looks forward to your response.
Back to top
bruce89
GTK+ Geek


Joined: 19 Dec 2007
Posts: 61
Location: Glasgow, Scotland

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

dnovichman wrote:
I am trying to create a widget with the FILE menus: hmcposition, mission1, mission2 and quit. So that when the use clicks FILE then scrolls to any of the missions, the widget should be activated to show a new drawing or shape (e.g. a rectangle) while erasing what was originally there. If you run the code and click quit, the widget quits but if you click hmcPosition, the widget shows the figure its meant to show but only lasts for seconds. This is because I am using "expose_even" instead of "activate". I wonder if there is a way of achieving this. Thanks and looks forward to your response.


It doesn't make much sense that you are connecting a menu's button-press-event (which should be a activate signal) to a expose callback.

If you want the MenuItem to clear the DrawingArea when activated, connect to activate, and use this callback:

Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void
position_activated (GtkMenuItem *position, GtkDrawingArea *da)
{   
    GdkPoint points[size];

    guint xmap[size] = {50,400,200,80};
    guint 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);
}


You might find that using cairo to draw on the canvas is easier. It's a case of getting a cairo_t with gdk_cairo_create (da->window). Then you use the cairo API to do all the drawing (cairo_rectangle for instance).
Back to top
dnovichman
Familiar Face


Joined: 09 Jan 2009
Posts: 16

PostPosted: Mon Jan 19, 2009 7:28 am    Post subject: Reply with quote

I have done exactly that but it doesnt work. By that I mean, I have used
static void position_activated (GtkWidget *widget, GtkMenuItem *position, GtkDrawingArea *da)

And in main, I've changed expose_event to activate and now its stopped showing the polygon. With the expose_event as I was saying, it shows the polygon but only in the menus and lasts as long as the click is on.

Is there a way to activate a menu just as the 'QUIT' menu that will allow the window to change and allow me to see my polygon ?
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