LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-16-2008, 06:14 PM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 233

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!
Code:
#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)
{
	gtk_main_quit();
}

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);
	
	gtk_widget_show(view);
	gtk_widget_show(swin);
	gtk_widget_show(vbox);
	
	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);
	gtk_widget_show(window);
	gtk_main();
	
	return 0;
}
Cheers

p.s. compiled with: gcc main.c -o main `pkg-config --cflags --libs gtk+-2.0`
 
Old 06-19-2008, 05:49 AM   #2
rubadub
Member
 
Registered: Jun 2004
Posts: 233

Original Poster
Rep: Reputation: 33
Resolved: http://www.gtkforums.com/viewtopic.php?t=1436
 
  


Reply


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 04:54 PM
What do after view Text Editor dalebo Linux - Newbie 23 05-21-2006 07:45 PM
Konqueror's "text view" option makes text green. newtwolinux Suse/Novell 2 08-26-2005 06:45 AM
GTK and C : how to view list of gpointer children szparag Programming 1 12-10-2004 08:59 AM
GTK tree view/model/selection question elluva Programming 8 05-22-2004 12:19 PM


All times are GMT -5. The time now is 10:07 AM.

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