How to adapt TopLevel widget size to the subWidget

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to adapt TopLevel widget size to the subWidget

Pencc
Hello, everyone!

        window is the toplevel widget while label is the subWidget, when
I change the size of label by gtk_widget_set_size_request,

if new label is larger than the old label, toplevel will be larger, but
why that not works the other way  around ?


[1]

#include <gtk/gtk.h> 
 
GtkWidget *window; 
GtkWidget *label;

void close_app(GtkWidget *window,gpointer data) 

    gtk_main_quit(); 


void button_event(GtkWidget *widget,gpointer *data) /*回调函数*/

{
    if(NULL != data) {
        gtk_widget_set_size_request(label, 200, 200);
    } else {
        gtk_widget_set_size_request(label, 50, 50);
        gtk_widget_set_size_request(window, -1, -1);
    }

}

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

    GtkWidget *button1; 
    GtkWidget *button2; 
    GtkWidget *button3; 
    GtkWidget *Hbox; 
    GtkWidget *Vbox; 
 
    gtk_init(&argc,&argv); 
 
    //new window 
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
    gtk_window_set_title(GTK_WINDOW(window),"Layout demo"); 
    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER); 
    gtk_window_set_default_size(GTK_WINDOW(window),200,200); 
   
g_signal_connect(GTK_OBJECT(window),"destroy",GTK_SIGNAL_FUNC(close_app),NULL);  

     
    //新建了3个按钮和两个组装盒 
    button1 = gtk_button_new_with_label("button1"); 
    button2 = gtk_button_new_with_label("button2"); 
    button3 = gtk_button_new_with_label("button3"); 
 
    label = gtk_label_new("status line");
 
    Hbox = gtk_hbox_new(TRUE,0); 
    Vbox = gtk_vbox_new(TRUE,0); 
     
    //将button1和button2放在Hbox上 
    gtk_box_pack_start(GTK_BOX(Hbox),button1,FALSE,FALSE,0); 
    gtk_box_pack_start(GTK_BOX(Hbox),button2,FALSE,FALSE,0); 
 
    //将button3和Hbox放在Vbox上 
    gtk_box_pack_start(GTK_BOX(Vbox),GTK_WIDGET(Hbox),FALSE,FALSE,0); 
    gtk_box_pack_start(GTK_BOX(Vbox),label,FALSE,FALSE,0); 
    gtk_box_pack_start(GTK_BOX(Vbox),button3,FALSE,FALSE,0); 

    gtk_signal_connect(GTK_OBJECT(button1), "pressed",
GTK_SIGNAL_FUNC(button_event), "1");
    gtk_signal_connect(GTK_OBJECT(button2), "pressed",
GTK_SIGNAL_FUNC(button_event), NULL);
 
    //将Vbox添加到window容器上 
    gtk_container_add(GTK_CONTAINER(window),Vbox); 
    gtk_widget_show_all(window); 
    gtk_main(); 
 
    return 0; 



_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list