2
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

/*Devo fare un pulsante che, una volta premuto, legga
 * i due numeri e ne calcoli l'MCD*/

int main(int argc, char *argv[]){

    GtkWidget *window;
    GtkWidget *table;

    GtkWidget *label1;
    GtkWidget *label2;
    GtkWidget *label3;

    GtkWidget *num1;
    GtkWidget *num2;
    GtkWidget *mcdt;

    GtkWidget *button;

    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_title(GTK_WINDOW(window), "MCD Calculator");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);

    table = gtk_table_new(4, 4, FALSE);

    gtk_container_add(GTK_CONTAINER(window), table);

    label1 = gtk_label_new("Num1");
    label2 = gtk_label_new("Num2");
    label3 = gtk_label_new("MCD");

    gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,5, 5);
    gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,5, 5);
    gtk_table_attach(GTK_TABLE(table), label3, 0, 1, 3, 4, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,5, 5);

    num1 = gtk_entry_new();
    num2 = gtk_entry_new();
    mcdt = gtk_entry_new(); 

    gtk_table_attach(GTK_TABLE(table), num1, 1, 2, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
    gtk_table_attach(GTK_TABLE(table), num2, 1, 2, 1, 2, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
    gtk_table_attach(GTK_TABLE(table), mcdt, 1, 2, 3, 4, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);

    button = gtk_button_new_with_label ("Calcola MCD");
    gtk_container_add(GTK_CONTAINER(window), button);
    gtk_table_attach(GTK_TABLE(table), button, 1, 5, 2, 3, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
    //g_signal_connect (button, "clicked", G_CALLBACK (callback), (gpointer) "Calcola");

    gtk_widget_show_all(window);

    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    gtk_main();

    return EXIT_SUCCESS;
}

This code give me this warning:

*Gtk-WARNING **: Attempting to add a widget with type GtkButton 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 GtkTable*

I've read that I have to use vbox or hbox, but I didn't understand how to use them.

Youssef Bouhjira
  • 1,599
  • 2
  • 22
  • 38
polslinux
  • 1,739
  • 9
  • 34
  • 73

1 Answers1

3

A box widget allows you to add multiple sub-widgets, arranged either horizontally or vertically. The table widget is similar, but it allows for a 2-dimensional grid layout. In other words, hbox and vbox are just simplified versions of the table that you are already using.

To use a box, just create an hbox or a vbox and add it to the window.

GtkWidget * box = gtk_hbox_new(0,0);
gtk_container_add(GTK_CONTAINER(window),box);

Now add the table and the button to the box instead of to the window.

gtk_container_add(GTK_CONTAINER(box), table);
gtk_container_add(GTK_CONTAINER(box), button);

In the code that you posted you are adding the button to two different containers. You should disable one of the following lines. If you disable the line with gtk_container_add(), then you don't actually need to use a box, since you are adding the button to the existing table.

gtk_container_add(GTK_CONTAINER(box), button);
gtk_table_attach(GTK_TABLE(table), button, 1, 5, 2, 3, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);

I thought it would be interesting (and perhaps helpful) to write the callback too.

void callback()
   {
   const gchar * s1 = gtk_entry_get_text(GTK_ENTRY(num1));
   const gchar * s2 = gtk_entry_get_text(GTK_ENTRY(num2));
   int n1 = atoi(s1), n2=atoi(s2);
   n1=abs(n1), n2=abs(n2); // GCD defined to return non-negative value
   int i; for ( i=(n1>n2?n1:n2); i>=1; i-- ) if ( n1/i*i==n1 && n2/i*i==n2 ) break;
   char s3[32]; snprintf(s3,32,"%d",i);
   gtk_entry_set_text(GTK_ENTRY(mcdt),s3);
   }

Here's what it looks like with the hbox. Notice that the button is placed next to the table horizontally.

enter image description here

Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • The current documentation for VBox/HBox says: "Use Box instead, which is a very quick and easy change. But we recommend switching to Grid, since Box will go away eventually." See here: [How do you replace HBox/VBox with Box/Grid](http://stackoverflow.com/questions/19532619/how-do-you-replace-hbox-vbox-with-box-grid) – Brent Bradburn Oct 23 '13 at 04:15