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 

Pop-up menu problem

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


Joined: 25 Aug 2008
Posts: 15

PostPosted: Mon Aug 25, 2008 2:12 pm    Post subject: Pop-up menu problem Reply with quote

Hi All,

I am having a problem in creating the pop-up menu. I have writtten the following code for getting a pop-up menu when user presses a mouse button on top-level window:

#include <stdio.h>
#include <assert.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>

gboolean Show_photo(GtkWidget *widget, GdkEventButton *event)
{
if (event->type == GDK_BUTTON_PRESS && event->button == 3)
{
GdkEventButton *bevent = (GdkEventButton *) event;
gtk_menu_popup (GTK_MENU(widget), NULL, NULL, NULL, NULL,
bevent->button, bevent->time);

return TRUE;
}
return FALSE;
}

int
main(int argc, char* argv[])
{
GtkWidget* top_level_window;
GtkWidget* file_menu;
GtkWidget* open_item;
GtkWidget* save_item;
GtkWidget* quit_item;

gtk_init(&argc, &argv);

top_level_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request (top_level_window, 800,650);


file_menu = gtk_menu_new (); /* Don't need to show menus */

/* Create the menu items */
open_item = gtk_menu_item_new_with_label ("Open");
save_item = gtk_menu_item_new_with_label ("Save");
quit_item = gtk_menu_item_new_with_label ("Quit");

/* Add them to the menu */
gtk_menu_append (GTK_MENU (file_menu), open_item);
gtk_menu_append (GTK_MENU (file_menu), save_item);
gtk_menu_append (GTK_MENU (file_menu), quit_item);

gtk_widget_show (open_item);
gtk_widget_show (save_item);
gtk_widget_show (quit_item);

g_signal_connect_swapped (G_OBJECT(top_level_window),
"button-press-event",
G_CALLBACK (Show_photo),
G_OBJECT(file_menu));

gtk_widget_show_all (top_level_window);

gtk_main ();

return 0;
}


But when i do press the button, i do not see any pop-up menu. However, top level window is their.

Can anybody help me put why i am not getting the pop-up window.
This is the test application, but my objective is to right click a photo thumbnail and then i get the pop-up menu with options like rotate left, rotate right, delete, slideshow etc...

Please help me where i am wrong and provide me the solution.

Thanks in advance to all.

With Regards,
Deepak
Back to top
dreblen
Never Seen the Sunlight


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

PostPosted: Mon Aug 25, 2008 3:07 pm    Post subject: Reply with quote

you were on the right track, it's just that GtkWindow can't catch the button-press-event by itself,
you need to use something that does accept it, like a GtkEventBox
http://library.gnome.org/devel/gtk/stable/GtkEventBox.html

Here's a working example with a few comments:
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
#include <stdio.h>
#include <assert.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>

/* even if you don't use the user_data argument, it should still be there */
gboolean Show_photo(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
    if (event->type == GDK_BUTTON_PRESS && event->button == 3)
    {
        /* event is already a GdkEventButton, so this is not necessary */
        //GdkEventButton *bevent = (GdkEventButton *) event;
       
gtk_menu_popup (GTK_MENU(widget), NULL, NULL, NULL, NULL,
        event->button, event->time);

        return TRUE;
    }
    return FALSE;
}

int
main(int argc, char* argv[])
{
    GtkWidget* top_level_window;
    GtkWidget *eb;
    GtkWidget* file_menu;
    GtkWidget* open_item;
    GtkWidget* save_item;
    GtkWidget* quit_item;

    gtk_init(&argc, &argv);

    top_level_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    /* connect this so that we can quit */
   
g_signal_connect(G_OBJECT(top_level_window), "delete-event", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_set_size_request (top_level_window, 800,650);


    file_menu = gtk_menu_new (); /* Don't need to show menus */

    /* Create the menu items */
   
open_item = gtk_menu_item_new_with_label ("Open");
    save_item = gtk_menu_item_new_with_label ("Save");
    quit_item = gtk_menu_item_new_with_label ("Quit");

    /* Add them to the menu */
   
gtk_menu_append (GTK_MENU (file_menu), open_item);
    gtk_menu_append (GTK_MENU (file_menu), save_item);
    gtk_menu_append (GTK_MENU (file_menu), quit_item);

    gtk_widget_show (open_item);
    gtk_widget_show (save_item);
    gtk_widget_show (quit_item);

    /* create a GtkEventBox to catch the signal */
   
eb = gtk_event_box_new();
    gtk_container_add(GTK_CONTAINER(top_level_window), eb);
    gtk_widget_show(eb);
    g_signal_connect_swapped (    G_OBJECT(eb),
                                "button-press-event",
                                G_CALLBACK (Show_photo),
                                G_OBJECT(file_menu));

    gtk_widget_show_all (top_level_window);

    gtk_main ();

    return 0;
}
Back to top
jain1982
Familiar Face


Joined: 25 Aug 2008
Posts: 15

PostPosted: Tue Aug 26, 2008 5:51 am    Post subject: Reply with quote

Hey thanks for the reply, its working.

Also i want to know that do we can get po-up meuns on GtkImage??
Back to top
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 173
Location: INDIA

PostPosted: Tue Aug 26, 2008 6:57 am    Post subject: Reply with quote

jain1982 wrote:
Hey thanks for the reply, its working.

Also i want to know that do we can get po-up meuns on GtkImage??


window->eventbox->image(add image to event box)
then give the signal
Back to top
dreblen
Never Seen the Sunlight


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

PostPosted: Tue Aug 26, 2008 3:07 pm    Post subject: Reply with quote

while using several GtkImages and GtkEventBoxes will work, it would be better to use a GtkIconView
http://library.gnome.org/devel/gtk/stable/GtkIconView.html
if you know how to use GtkTreeViews, then using an icon view will be simple, if you don't know how to use tree views,
then this tutorial will help you greatly:
http://scentric.net/tutorial/treeview-tutorial.html
and here's an example of an icon view that reads images from a directory and puts them in an icon view (post 4):
http://gtkforums.com/viewtopic.php?t=1576
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