Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-16-2008, 07:14 PM   #1
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
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`
Old 06-19-2008, 06:49 AM   #2
Registered: Jun 2004
Posts: 236

Original Poster
Rep: Reputation: 33


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
View As HTML / View As Text button craig467 Programming 4 01-22-2008 05:54 PM
What do after view Text Editor dalebo Linux - Newbie 23 05-21-2006 08:45 PM
Konqueror's "text view" option makes text green. newtwolinux SUSE / openSUSE 2 08-26-2005 07:45 AM
GTK and C : how to view list of gpointer children szparag Programming 1 12-10-2004 09:59 AM
GTK tree view/model/selection question elluva Programming 8 05-22-2004 01:19 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration