GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon May 20, 2013 2:56 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: gtk_menus
PostPosted: Fri Jan 16, 2009 12:19 pm 
Offline
Familiar Face

Joined: Fri Jan 09, 2009 6:42 am
Posts: 16
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:
#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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 11:30 pm 
Offline
GTK+ Geek

Joined: Wed Dec 19, 2007 9:15 pm
Posts: 61
Location: Glasgow, Scotland
I have absolutely no idea what you're trying to do, and the formatting of the code doesn't help.

_________________
Jabber – bcowan [at] fastmail.co.uk.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 11:40 pm 
Offline
Familiar Face

Joined: Fri Jan 09, 2009 6:42 am
Posts: 16
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 11:55 pm 
Offline
GTK+ Geek

Joined: Wed Dec 19, 2007 9:15 pm
Posts: 61
Location: Glasgow, Scotland
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:
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).

_________________
Jabber – bcowan [at] fastmail.co.uk.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 7:28 am 
Offline
Familiar Face

Joined: Fri Jan 09, 2009 6:42 am
Posts: 16
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 ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group