Search code examples
cgtkgtk3

GTK not rendering all user interfaces


I am using gtk+ with c . I have created menu bar and now want to attach a table with some buttons. I have written all essential API routines but, problem I am facing is, if I first create menu bar then table is not rendered to window and it I render table first menu bar is not seen at its place.. .I don't know why. here is the code

    /* Button Boxes
 *
 * The Button Box widgets are used to arrange buttons with padding.
 */
#include <gtk/gtk.h>


 GtkWidget *window,*button,*container,*menubar,*fileM,*menu,*vbox,*aboutM,*quitM,*bdrM;
 GtkWidget *image,*table,*Fileframe,*framecnt;
bool status=false;

void aboutDialog()
{
    GtkWidget *box;
    box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"EXAMPLE");;
    gtk_dialog_run(GTK_DIALOG(box));
    gtk_widget_destroy(box);

}

void Warning_quit()
{
    GtkWidget *box;
    box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_WARNING,GTK_BUTTONS_YES_NO,"Sure want to quit..");
    gtk_dialog_run(GTK_DIALOG(box));
    gtk_widget_destroy(box);
    gtk_main_quit();

}

void toggle_Border()
{
    if(status==false)
    {
        gtk_window_set_decorated(GTK_WINDOW(window),true);
        gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"fix");
        status=true;
    }
    else if(status==true)
    {
        gtk_window_set_decorated(GTK_WINDOW(window),false);
        gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"move");
        status=false;

    }

}
void initDecoration()
{
    vbox=gtk_vbox_new(false,0);
    menubar=gtk_menu_bar_new();
    fileM=gtk_menu_item_new_with_label("file");
    aboutM=gtk_menu_item_new_with_label("about");
    quitM=gtk_menu_item_new_with_label("quit");
    bdrM=gtk_menu_item_new_with_label("move");
    menu=gtk_menu_new();
    ////////////////////////
    gtk_container_add(GTK_CONTAINER(window),vbox);//attach vbox with window..
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar),fileM);//attach menu into menubar
    gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileM),menu);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),aboutM);
    gtk_menu_shell_append(GTK_MENU_SHELL(menubar),bdrM);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),quitM);//attach quit menu with menu..
    gtk_box_pack_start(GTK_BOX(vbox),menubar,0,0,1);//attach menubar with vbox//

    //attach signal intrrupt for menus..
    g_signal_connect(G_OBJECT(aboutM),"activate",G_CALLBACK(aboutDialog),0);//signal for about menu..
    g_signal_connect(G_OBJECT(quitM),"activate",G_CALLBACK(Warning_quit),0);//signal for quit menu..
    g_signal_connect(G_OBJECT(bdrM),"activate",G_CALLBACK(toggle_Border),0);
}

void initTable()
{
    GtkWidget *button;
    gtk_container_add(GTK_CONTAINER(window),table); //table attached to main window..
    button=gtk_button_new_with_label("button");
    table=gtk_table_new(4,4,true); //table created..
    gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1); //attaching frame container to table cell..

}
int main(int argc, char *argv[]) 
{
  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), 250, 200);
  //setup menubar;
  initDecoration();
  initTable();
  g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),0);
  gtk_widget_show_all(window);
  gtk_main();

  return 0;
}

Solution

  • The problem is that you are trying to add more than one child to the same container, GtkWindow, which only can hold one child.

    You did create a vbox but after you did not add the GtkTable, which by the way its deprecated since Gtk+ 3.4, to the GtkBox.

    Also note that you are adding the GtkTable to the window before creation and GtkWindow will complain about that.

    So, your initTable function should be:

    void initTable()
    {
        GtkWidget *button;
        button=gtk_button_new_with_label("button");
        table=gtk_table_new(4,4,true); //table created..
        gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1); //attaching frame container to table cell..
    
        gtk_box_pack_start(GTK_BOX(vbox),table,0,0,1);//attach menubar with vbox//
    
    }
    

    In order to compile we had to define true, false and bool (due to the includes, not sure of which framework you are using).

    Full source code is:

     /* Button Boxes
     *
     * The Button Box widgets are used to arrange buttons with padding.
     */
    #include <gtk/gtk.h>
    
    #define true TRUE
    #define false FALSE
    #define bool gboolean
    
     GtkWidget *window,*button,*container,*menubar,*fileM,*menu,*vbox,*aboutM,*quitM,*bdrM;
     GtkWidget *image,*table,*Fileframe,*framecnt;
    bool status=false;
    
    void aboutDialog()
    {
        GtkWidget *box;
        box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"EXAMPLE");;
        gtk_dialog_run(GTK_DIALOG(box));
        gtk_widget_destroy(box);
    
    }
    
    void Warning_quit()
    {
        GtkWidget *box;
        box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_WARNING,GTK_BUTTONS_YES_NO,"Sure want to quit..");
        gtk_dialog_run(GTK_DIALOG(box));
        gtk_widget_destroy(box);
        gtk_main_quit();
    
    }
    
    void toggle_Border()
    {
        if(status==false)
        {
            gtk_window_set_decorated(GTK_WINDOW(window),true);
            gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"fix");
            status=true;
        }
        else if(status==true)
        {
            gtk_window_set_decorated(GTK_WINDOW(window),false);
            gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"move");
            status=false;
    
        }
    
    }
    void initDecoration()
    {
        vbox=gtk_vbox_new(false,0);
        menubar=gtk_menu_bar_new();
        fileM=gtk_menu_item_new_with_label("file");
        aboutM=gtk_menu_item_new_with_label("about");
        quitM=gtk_menu_item_new_with_label("quit");
        bdrM=gtk_menu_item_new_with_label("move");
        menu=gtk_menu_new();
        ////////////////////////
        gtk_container_add(GTK_CONTAINER(window),vbox);//attach vbox with window..
        gtk_menu_shell_append(GTK_MENU_SHELL(menubar),fileM);//attach menu into menubar
        gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileM),menu);
        gtk_menu_shell_append(GTK_MENU_SHELL(menu),aboutM);
        gtk_menu_shell_append(GTK_MENU_SHELL(menubar),bdrM);
        gtk_menu_shell_append(GTK_MENU_SHELL(menu),quitM);//attach quit menu with menu..
        gtk_box_pack_start(GTK_BOX(vbox),menubar,0,0,1);//attach menubar with vbox//
    
        //attach signal intrrupt for menus..
        g_signal_connect(G_OBJECT(aboutM),"activate",G_CALLBACK(aboutDialog),0);//signal for about menu..
        g_signal_connect(G_OBJECT(quitM),"activate",G_CALLBACK(Warning_quit),0);//signal for quit menu..
        g_signal_connect(G_OBJECT(bdrM),"activate",G_CALLBACK(toggle_Border),0);
    }
    
    void initTable()
    {
        GtkWidget *button;
        button=gtk_button_new_with_label("button");
        table=gtk_table_new(4,4,true); //table created..
        gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1); //attaching frame container to table cell..
    
        gtk_box_pack_start(GTK_BOX(vbox),table,0,0,1);//attach menubar with vbox//
    
    }
    int main(int argc, char *argv[]) 
    {
      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), 250, 200);
      //setup menubar;
      initDecoration();
      initTable();
      g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),0);
      gtk_widget_show_all(window);
      gtk_main();
    
      return 0;
    }
    

    Compiled with:

    gcc -o main main.c `pkg-config --cflags --libs gtk+-3.0`