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 Oct 22, 2014 9:42 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: UI widget trouble. error using 2 widgets similtaneously
PostPosted: Mon Apr 23, 2012 5:17 am 
Offline
Familiar Face

Joined: Mon Mar 05, 2012 11:55 pm
Posts: 12
So i am coming along in my UI development for my chat server.

I am having some trouble though!

---> HERE IS THE ERROR I RECEIVE: <---
Code:
(gtk_test:8236): Gtk-WARNING **: Attempting to add a widget with type GtkLabel to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one widget at a time; it already contains a widget of type GtkVBox


if i create a table
Code:
  table = gtk_table_new(0, 10, TRUE);
  gtk_container_add(GTK_CONTAINER(window), table);
  gtk_widget_show(table);


and place it before the vertical box widget
Code:
  vbox = gtk_vbox_new(TRUE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  gtk_widget_show(vbox);


and menu widget
Code:
  menubar = gtk_menu_bar_new();
  gtk_widget_show(menubar);
  filemenu = gtk_menu_new();
  gtk_widget_show(filemenu);


it will not display the file menu and will display the rest of the widgets.

but if i place the vertical box and file menu widgets before creating the table, it will display the file menu and the frame. Oddly, the file menu, when it works, will only appear if i maximize the UI.

Am i supposed to close out one widget before the use of another? Or am i packing wrong?

Thanks.

This is the code so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

//To compile append `pkg-config --cflags --libs gtk+-2.0`
static void destroy_event(GtkWidget *widget, gpointer data)
  {
    gtk_main_quit();
  }

  static gboolean delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
  {
    gtk_main_quit();
    return FALSE;
  }

int main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *entry_ip;
  GtkWidget *entry_port;
  GtkWidget *table;
  GtkWidget *frame;
  GtkWidget *label;
  GtkWidget *scrollbar;
  GtkWidget *button_send;
  GtkWidget *button_connect;

  GtkWidget *vbox;
  GtkWidget *menubar;
  GtkWidget *filemenu;
  GtkWidget *quit;
  GtkWidget *file;
  //GtkWidget *sep;

  GtkAccelGroup *accel_group = NULL;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 700, 700);
  //gtk_window_set_title(GTK_WINDOW(window), "-Anthony's Mini Chat-");
  gtk_widget_show(window);

  vbox = gtk_vbox_new(TRUE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);
  gtk_widget_show(vbox);

  menubar = gtk_menu_bar_new();
  gtk_widget_show(menubar);
  filemenu = gtk_menu_new();
  gtk_widget_show(filemenu);

  accel_group = gtk_accel_group_new();
  gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);

  file = gtk_menu_item_new_with_mnemonic("_File");
  //sep = gtk_separator_menu_item_new();
  quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel_group);
  gtk_widget_show(quit);
  gtk_widget_show(file);

  gtk_widget_add_accelerator(quit, "activate", accel_group,
      GDK_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

  gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
  //gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), sep);
  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
  gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);

  table = gtk_table_new(0, 10, TRUE);
  gtk_container_add(GTK_CONTAINER(window), table);
  gtk_widget_show(table);

  scrollbar = gtk_vscrollbar_new(NULL);
  gtk_container_add(GTK_CONTAINER(window), scrollbar);
  gtk_table_attach_defaults(GTK_TABLE(table), scrollbar, 7, 8, 0, 10);
  gtk_widget_show(scrollbar);

  /*pane = gtk_vpaned_new();
  gtk_container_add(GTK_CONTAINER(window), pane);
  gtk_table_attach_defaults(GTK_TABLE(table), pane, 0, 10, 8, 10);
  gtk_widget_show(pane);*/
 
  frame = gtk_frame_new(NULL);
  gtk_container_add(GTK_CONTAINER(window), frame);
  gtk_table_attach_defaults(GTK_TABLE(table), frame, 7, 10, 1, 9);
  gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_OUT);
  gtk_widget_show(frame);
 
  button_connect = gtk_button_new_with_label("Connect");
  gtk_container_add(GTK_CONTAINER(window), button_connect);
  gtk_table_attach_defaults(GTK_TABLE(table), button_connect, 6, 7, 0, 1);
  gtk_widget_show(button_connect);

  button_send = gtk_button_new_with_label("Send");
  gtk_container_add(GTK_CONTAINER(window), button_send);
  gtk_table_attach_defaults(GTK_TABLE(table), button_send, 6, 7, 9, 10);
  gtk_widget_show(button_send);

  entry_ip = gtk_entry_new();
  gtk_container_add(GTK_CONTAINER(window), entry_ip);
  //gtk_box_pack_start(GTK_BOX(box), entry, TRUE, TRUE, 5);
  gtk_table_attach_defaults(GTK_TABLE(table), entry_ip, 2, 4, 0, 1);
  gtk_widget_show(entry_ip);

  const gchar *entry_text;
  entry_text = gtk_entry_get_text(GTK_ENTRY(entry_ip));

  label = gtk_label_new ("[I.P. ADDRESS]");
  gtk_container_add(GTK_CONTAINER(window), label);
  gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
  gtk_widget_show(label);

  entry_port = gtk_entry_new();
  gtk_container_add(GTK_CONTAINER(window), entry_port);
  //gtk_box_pack_start(GTK_BOX(box), entry, TRUE, TRUE, 5);
  gtk_table_attach_defaults(GTK_TABLE(table), entry_port, 2, 4, 1, 2);
  gtk_widget_show(entry_port);

  label = gtk_label_new ("[PORT NUMBER]");
  gtk_container_add(GTK_CONTAINER(window), label);
  gtk_table_attach_defaults(GTK_TABLE (table), label, 1, 2, 1, 2);
  gtk_widget_show(label);

  g_signal_connect_swapped(G_OBJECT(window), "destroy-event",
      G_CALLBACK(destroy_event), NULL);

  g_signal_connect_swapped(G_OBJECT(window), "delete-event",
      G_CALLBACK(delete_event), NULL);

  g_signal_connect(G_OBJECT(quit), "activate",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;

}


Top
 Profile  
 
 Post subject: Re: UI widget trouble. error using 2 widgets similtaneously
PostPosted: Tue Apr 24, 2012 6:16 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
Hi,

Anything that is derived from a GtkContainer can only contain one Widget. What you are trying to do is add more than one Widget to a GtkContainer, so in your case that is the GtkWindow.

Layout containers can have more than one Widget see http://developer.gnome.org/gtk/stable/LayoutContainers.html. These can be added to a GtkContainer to allow you more that just the one Widget.

_________________
E.


Top
 Profile  
 
 Post subject: Re: UI widget trouble. error using 2 widgets similtaneously
PostPosted: Wed Apr 25, 2012 12:44 am 
Offline
Familiar Face

Joined: Mon Mar 05, 2012 11:55 pm
Posts: 12
errol wrote:
Hi,

Anything that is derived from a GtkContainer can only contain one Widget.

I did not know this. I was under the assumption that multiple widget could occupy one container.

errol wrote:
What you are trying to do is add more than one Widget to a GtkContainer, so in your case that is the GtkWindow.

Yes, the error has showed me that.


errol wrote:
Layout containers can have more than one Widget see http://developer.gnome.org/gtk/stable/LayoutContainers.html. These can be added to a GtkContainer to allow you more that just the one Widget.


I just removed the pointless adds to the same containers. For example; I was unnecessarily adding a label the window container. I removed that, among others and now I no longer have the issue/errors.

Thanks.


Top
 Profile  
 
 Post subject: Re: UI widget trouble. error using 2 widgets similtaneously
PostPosted: Wed Apr 25, 2012 2:43 am 
Offline
Familiar Face

Joined: Mon Mar 05, 2012 11:55 pm
Posts: 12
@errol, I have decided to go with a smaller project for my first. Also, because i cannot find a partner for the chat anyway. So i have decided to make a bdm screen locker. I have a centered frame laid inside the window, a table, a label for the entry widget, and a button outside of the frame to the right labeled unlock. I also added the gtk_entry_set_visibility function to make the password entry not visible to third parties(shoulder peekers).

i am using
Code:
const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry_passwd));
   if(strcmp(text, "password") == 0) {
   //fprintf(stdout, "Passwd matched.\n");
   exit(EXIT_SUCCESS);
   }


What i would like to do is hook that into the unlock button. To do this I assume i would set up a signal handler via g_signal_connect_swapped. If this is the correct method, how would i do such? pass text as an arg in the signal handler?

Thanks again.


Top
 Profile  
 
 Post subject: Re: UI widget trouble. error using 2 widgets similtaneously
PostPosted: Thu Apr 26, 2012 6:58 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 764
Location: UK
You can use g_signal_connect_swapped() or g_signal_connec(), but it may be easier to use the latter as then your call-back functions will match what is in the documentation.

You can not pass the string via the signal handler, but you could pass a pointer to the GtkEntry widget and then get the string that way.
Note have you looked at the documentation and tutorials.

_________________
E.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 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