drodri420
drodri420

Reputation: 191

How to resize a widget inside a Window

First things first, I hope you're doing great. Now, I'm doing a game with GTK in C, I'm trying to resize the Box where the board is and I'm not sure of if what I should be resizing is the box it is in or the table of the buttons itself.

I want the resize it so its user-friendly, keep in mind I'm a noob with GTK.

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

void boton_click (GtkWidget * widget, gpointer info);
void salida (Gtkdget * widget, gpointer info);
gboolean cierra_ventana (GtkWidget * widget, GdkEvent * event, gpointer info);

int
main (int argc, char *argv[])
{
    GtkWidget *ventana, *cajavertjugador1, *namejugador1, *imageplayer1,
    *framejugador1, *labeljugador1, *cajavertjugador2, *namejugador2,
    *imageplayer2, *framejugador2, *labeljugador2, *cajaH, *cajaboton,
    *separador, *separadorvjug1, *separadorvjug2, *cajatablero, *boton,
    *botoncancel, *tabla, *tablero[20][20];
    int i, j;
    gchar *arch1 = "troll.jpg";
    gchar *arch2 = "challenge.jpg";

    //***********************************1.-Initialize Enviroment
    gtk_init (&argc, &argv);

    //***************************************2.-Create Widgets and attributes
    imageplayer1 = gtk_image_new_from_file (arch1);
    imageplayer2 = gtk_image_new_from_file (arch2);
    tabla = gtk_table_new (20, 20, TRUE);
    ventana = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (ventana), "Pente");
    cajaH = gtk_hbox_new (FALSE, 25);
    cajavertjugador1 = gtk_vbox_new (FALSE, 0);
    cajatablero = gtk_hbox_new (TRUE, 500);
    cajavertjugador2 = gtk_vbox_new (TRUE, 5);
    cajaboton = gtk_hbox_new (TRUE, 5);
    boton = gtk_button_new_with_label ("Ok");
    botoncancel = gtk_button_new_with_label ("Cancel");
    separador = gtk_hseparator_new ();
    separadorvjug1 = gtk_vseparator_new ();
    separadorvjug2 = gtk_vseparator_new ();
    framejugador1 = gtk_frame_new ("Fichas Comidas");
    namejugador1 = gtk_label_new ("Variable Nombre Jugador1");
    labeljugador1 = gtk_label_new ("Aqui Imagenes de Las Fichas Comidas\n");
    gtk_label_set_justify (GTK_LABEL (labeljugador1), GTK_JUSTIFY_LEFT);
    framejugador2 = gtk_frame_new ("Fichas Comidas");
    namejugador2 = gtk_label_new ("Variable Nombre Jugador2");
    labeljugador2 = gtk_label_new ("Aqui Imagenes de Las Fichas Comidas\n");
    gtk_label_set_justify (GTK_LABEL (labeljugador1), GTK_JUSTIFY_LEFT);

    for (i = 0; i < 20; i++)
    {
        for (j = 0; j < 20; j++)
        {
            tablero[i][j] = gtk_button_new ();
        }
    }

    //**********************************3.Register Function Calls
    g_signal_connect (G_OBJECT (ventana), "delete_event",
            G_CALLBACK (cierra_ventana), NULL);
    g_signal_connect (G_OBJECT (ventana), "destroy", G_CALLBACK (salida), NULL);

    //***********************************4.-Hierarchy of Elements

    gtk_container_add (GTK_CONTAINER (ventana), cajaH);
    gtk_container_add (GTK_CONTAINER (cajaH), cajavertjugador1);
    gtk_container_add (GTK_CONTAINER (cajaH), separadorvjug1);
    gtk_container_add (GTK_CONTAINER (cajavertjugador1), namejugador1);
    gtk_container_add (GTK_CONTAINER (cajavertjugador1), imageplayer1);
    gtk_container_add (GTK_CONTAINER (framejugador1), labeljugador1);
    gtk_container_add (GTK_CONTAINER (cajavertjugador1), framejugador1);
    gtk_container_add (GTK_CONTAINER (cajatablero), tabla);
    gtk_container_set_focus_vadjustment (GTK_CONTAINER (cajatablero),
                       GTK_ADJUSTMENT (50));
    gtk_container_add (GTK_CONTAINER (cajaH), cajatablero);
    gtk_container_add (GTK_CONTAINER (cajaH), separadorvjug2);
    gtk_container_add (GTK_CONTAINER (cajavertjugador2), namejugador2);
    gtk_container_add (GTK_CONTAINER (cajavertjugador2), imageplayer2);
    gtk_container_add (GTK_CONTAINER (framejugador2), labeljugador2);
    gtk_container_add (GTK_CONTAINER (cajavertjugador2), framejugador2);
    gtk_container_add (GTK_CONTAINER (cajaH), cajavertjugador2);


    for (i = 0; i < 20; i++)
    {
        for (j = 0; j < 20; j++)
        {
            gtk_table_attach_defaults (GTK_TABLE (tabla), tablero[i][j], i,
                         i + 1, j, j + 1);
        }
    }
    //****************************5.Show the Widddgggeetss
    gtk_widget_show_all (ventana);

    //****************************6.Process Signals and Events
    gtk_main ();
    return 1;

}//main

gboolean
cierra_ventana (GtkWidget * widget, GdkEvent * event, gpointer info)
{
  return FALSE;
}

void
salida (GtkWidget * widget, gpointer info)
{
  gtk_main_quit ();
}

Upvotes: 0

Views: 313

Answers (1)

another.anon.coward
another.anon.coward

Reputation: 11395

It is not very clear what you are trying to say, if you mean that the table which you have added buttons to does not appear to be expanded to fit between the separators then it is due to the fact that when creating cajaH you have specified spacing as 25 pixel. Try changing it to lesser different value or 0 and see if you get what you wanted. As you are using default packing for table & using gtk_container_add packing will expand & fit.
Also as you are using GtkHBox & GtkVBox gtk_box_pack_start & gtk_box_pack_end will give you a greater control on layout then using gtk_container_add. See this question for information.
When using gtk_container_set_focus_vadjustment you need to create a new GtkAdjustment using gtk_adjustment_new. Typecasting 50 as GTK_ADJUSTMENT is not the way to do it!
Hope this helps!

Upvotes: 1

Related Questions