Re: GtkText widget and right click menus



On Sun, Dec 10, 2000 at 03:08:57AM -0800, learfox furry ao net wrote:
How can I get a right click menu into a GtkText widget?

I've tried using the same procedure with GtkCList and other widgets by
creating a menu and setting the widget's signal to recieve a
button_press_event and when the event is recieved I simple map the menu.

But with the GtkText widget it dosen't seem the work right, when I map the
menu the menu does not respond untill I release the button and for some
reason while I move the pointer about the text on the GtkText gets marked
under the pointer while the menu is not recieving any events.

I also tried gdk_pointer_ungrab() and gdk_pointer_grab() but it didn't
help.
[snip]

I got it to work by using gtk_signal_connect_after()
I don't know if it's the right way to do it, but it seems to work.

#include <gtk/gtk.h>

gint text_button_press_cb (GtkWidget      *text,
                           GdkEventButton *event,
                           gpointer        data)
{
        GtkWidget *popup_menu = GTK_WIDGET (data);

        if (event->button == 3) {
                gtk_menu_popup (GTK_MENU (popup_menu),
                                NULL, 
                                NULL, 
                                NULL, 
                                NULL,
                                event->button, 
                                event->time);
                return TRUE;
        }

        return FALSE;
}

gint menu_item_activate_cb (GtkWidget *menuitem, gpointer data)
{
        GtkWidget *label = GTK_BIN (menuitem)->child;
        gchar *label_string;

        gtk_label_get (GTK_LABEL (label), &label_string);
        g_print ("%s\n", label_string);

        return FALSE;
}

void add_menu_items_to_menu (GtkMenu *menu)
{
        GtkWidget *menuitem, *label;
        gchar *label_buffer;
        guint i;

        for (i = 0;  i < 4;  i++) {
                menuitem = gtk_menu_item_new ();
                gtk_signal_connect (GTK_OBJECT (menuitem),
                                    "activate",
                                    GTK_SIGNAL_FUNC (menu_item_activate_cb),
                                    NULL);      
                gtk_menu_append (GTK_MENU (menu), menuitem);

                label_buffer = g_strdup_printf ("menuitem - %d", i);
                label = gtk_label_new (label_buffer);
                g_free (label_buffer);
                gtk_container_add (GTK_CONTAINER (menuitem), label);

                gtk_widget_show_all (menuitem);
        }
}

int main (int argc, char *argv[])
{
        GtkWidget *window, *popup_menu, *text;

        gtk_init (&argc, &argv);
        
        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_widget_set_usize (window, 300, 300);

        popup_menu = gtk_menu_new ();
        add_menu_items_to_menu (GTK_MENU (popup_menu));

        add_menu_items_to_menu (GTK_MENU (popup_menu));

        text = gtk_text_new (NULL, NULL);
        gtk_text_insert (GTK_TEXT (text),
                         NULL,
                         NULL,
                         NULL,
                         "sldkfjsldkjfsdljf\nsdlfksjdfk\n\nsdlfkjsdlf\n",
                         -1);
        /* make sure it's connect_after, otherwise the menu behaves 
         * strangely 
         */
        gtk_signal_connect_after (GTK_OBJECT (text),
                                  "button_press_event",
                                  GTK_SIGNAL_FUNC (text_button_press_cb),
                                  popup_menu);
        gtk_container_add (GTK_CONTAINER (window), text);

        gtk_widget_show_all (window);
        gtk_main ();
        return 0;
}





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