-   Programming (
-   -   GTK Text View and RC Menu (

rubadub 06-16-2008 06:14 PM

GTK Text View and RC Menu
Hi, i'm having a mess with GTK+, I implemented a text view with a buffer, all was ok, then I added a rc menu, now the buffer doesn't work, can't figure why? If you (un)comment out the line with the *** comment then you'll see what I mean!

#include <gtk/gtk.h>

GtkTextBuffer *buffer;

static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
        return FALSE;        //TRUE;

static void destroy(GtkWidget *widget, gpointer data)

static void print_hello( GtkWidget *w, gpointer  data )
        g_message ("Hello, World!\n");

/* Our menu, an array of GtkItemFactoryEntry structures that defines each menu item */
static GtkItemFactoryEntry menu_items[] = {
  { "/_File",        NULL,        NULL,          0, "<Branch>" },
  { "/File/_New",    "<control>N", print_hello,    0, "<StockItem>", GTK_STOCK_NEW },
  { "/File/_Open",    "<control>O", print_hello,    0, "<StockItem>", GTK_STOCK_OPEN },
  { "/File/_Save",    "<control>S", print_hello,    0, "<StockItem>", GTK_STOCK_SAVE },
static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);

static gboolean popup_cb( GtkWidget *widget, GdkEventButton *event, GtkWidget *menu )
        GdkEventButton *bevent = (GdkEventButton *)event;
        if (event->type == GDK_BUTTON_PRESS && event->button == 3)
                gtk_menu_popup (GTK_MENU(menu), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
        return TRUE;

GtkWidget *get_popup_menu()
        GtkItemFactory *item_factory;
        GtkWidget *menu;
        item_factory = gtk_item_factory_new (GTK_TYPE_MENU, "<main>", NULL);
        gtk_item_factory_create_items (item_factory, nmenu_items, menu_items, NULL);
        menu = gtk_item_factory_get_widget (item_factory, "<main>");
        return menu;

GtkWidget *init_editor(GtkWidget *window)
        GtkWidget *vbox;
        GtkWidget *swin;
        GtkWidget *view;
        GtkTextBuffer *buffer;
        vbox = gtk_vbox_new(FALSE, 2);
        swin = gtk_scrolled_window_new (NULL, NULL);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin), GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
        gtk_box_pack_start (GTK_BOX (vbox), swin, TRUE, TRUE, 0);
        view = gtk_text_view_new();
        GtkWidget *menu = get_popup_menu();
        g_signal_connect(G_OBJECT(view), "event", G_CALLBACK(popup_cb), (gpointer) menu);        //        ***
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (view));
        gtk_text_buffer_set_text(buffer, "Hello, this is some text", -1);
        gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW (swin), view);
        return vbox;

int main(int argc, char **argv)
        GtkWidget *window;
        gtk_init(&argc, &argv);
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title (GTK_WINDOW (window), "GTK DEMO");
        g_signal_connect(G_OBJECT (window), "delete_event", G_CALLBACK (delete_event), NULL);
        g_signal_connect(G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);
        gtk_container_set_border_width(GTK_CONTAINER (window), 0);
        GtkWidget *box = init_editor(window);
        gtk_container_add(GTK_CONTAINER (window), box);
        return 0;


p.s. compiled with: gcc main.c -o main `pkg-config --cflags --libs gtk+-2.0`

rubadub 06-19-2008 05:49 AM


All times are GMT -5. The time now is 09:41 AM.