GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Nov 26, 2014 1:25 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: textview and popup menu
PostPosted: Tue Jun 17, 2008 9:55 am 
Offline
Familiar Face

Joined: Tue Jun 17, 2008 9:45 am
Posts: 8
Hi, pretty new to GTK+, all was going ok but for some reason when I connect the signal for my rc popup menu, the text buffer wont connect to my text view. I'm basically at a loss as to why, I know it'll be obvious and don't worry i'll kick myself but i'd like to know why...

Look for the *** comment...

Code:
#include <gtk/gtk.h>

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");
}

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();
   //buffer = gtk_text_buffer_new(NULL);
   //view = gtk_text_view_new_with_buffer(buffer);
   
   GtkWidget *menu = get_popup_menu();
   g_signal_connect(G_OBJECT(view), "event", G_CALLBACK(popup_cb), (gpointer) menu);   //   ***
   
   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_widget_set_size_request (GTK_WIDGET (window), 640, 480);
   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...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 3:55 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
for the buffer, try doing this after you create your GtkTextView:
Code:
buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));

as for the signal, try connecting "button-press-event" instead of "event"
http://library.gnome.org/devel/gtk/stab ... ress-event

also, for future reference, when you put code tags on your text on this forum, there's a drop down menu to the left of the CodeBB button,
this will allow you to select the language which makes it easier to read
welcome to Gtk+ forums!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 10:15 am 
Offline
Familiar Face

Joined: Tue Jun 17, 2008 9:45 am
Posts: 8
Thanks for the reply an' welcome dreblen, yep the signal change did the trick. To start with I had the buffer the other way round, it now looks like this:
Code:
   ...
   view = gtk_text_view_new();
   buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW (view));
   gtk_text_buffer_set_text(buffer, "Hello, this is some text", -1);
   
   //buffer = gtk_text_buffer_new(NULL);
   //view = gtk_text_view_new_with_buffer(buffer);
   
   //get_popup_menu(view);
   GtkWidget *menu = get_popup_menu();
   //g_signal_connect(G_OBJECT(view), "event", G_CALLBACK(popup_cb), (gpointer) menu);
   g_signal_connect(G_OBJECT(view), "button-press-event", G_CALLBACK(popup_cb), (gpointer) menu);
   ...


I have a habit of blocking js with noscript and tend to write the bb myself, is this feature also used in your search (by language)?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 6:22 pm 
Offline
Never Seen the Sunlight

Joined: Thu Jun 14, 2007 11:02 pm
Posts: 923
Location: Falun, WI USA
If by "your" search, you mean the search function on this forum, then no, there doesn't appear to be a "Search by language", but you could search for:
"Code: (language)"
Also regarding your use of "your search", this isn't my forum, I was just welcoming :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group