Trouble setting the value of a GtkAdjustment

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble setting the value of a GtkAdjustment

Richard Shann
Compiling with Gtk 3.14.5
I can't seem to set the "value" of a GtkAdjustment once I have added a widget to it with gtk_container_add(), it seems to be locked at 0.0

The code looks like this fragment:

8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><

 GtkAdjustment *ADJ = gtk_adjustment_new (1.5, 0.0, 2.0, 0.01, 0.01, 0.01);
   
 GtkWidget *sw = gtk_scrolled_window_new (ADJ , gtk_adjustment_new (1.5, 0.0, 2.0, 0.01, 0.01, 0.01));
 gtk_container_add (GTK_CONTAINER(keyboard_window), sw);
 gtk_container_add (GTK_CONTAINER(sw), keyboard);
 
 gtk_adjustment_set_value (ADJ, 1.22222);

 g_print ("value %p is %f\n", ADJ, gtk_adjustment_get_value (ADJ));

8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><8><

where keyboard_window is a toplevel GtkWindow, and keyboard is a GtkGrid.

The value printed out is always 0.0 after the widget is added to the scrolled window.

Can anyone help? I'm trying to center the scroll-bars on the window.

Richard Shann


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Trouble setting the value of a GtkAdjustment

Gtk+ - Apps Dev mailing list

Hi Richard,

It looks like how you add containers makes a difference in this case. Also the window placement might have to be adjusted after the window is shown. Here is a try at it.

Eric


//gcc -Wall adj1.c -o adj1 `pkg-config --cflags --libs gtk+-3.0`
//Tested on Ubuntu16.04 and GTK3.18

#include<gtk/gtk.h>

int main(int argc, char **argv)
 {
   gtk_init(&argc, &argv);

   GtkWidget *keyboard_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title(GTK_WINDOW(keyboard_window), "Keyboard Window");
   gtk_window_set_default_size(GTK_WINDOW(keyboard_window), 200, 200);
   gtk_window_set_position(GTK_WINDOW(keyboard_window), GTK_WIN_POS_CENTER);
   g_signal_connect(keyboard_window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

   GtkWidget *label=gtk_label_new("Label");
   gtk_widget_set_size_request(label, 400, 400);

   //GtkAdjustment *ADJ = gtk_adjustment_new (1.5, 0.0, 2.0, 0.01, 0.01, 0.01);
   //GtkWidget *sw = gtk_scrolled_window_new (ADJ , gtk_adjustment_new (1.5, 0.0, 2.0, 0.01, 0.01, 0.01));
   GtkAdjustment *ADJ = gtk_adjustment_new (100.0, 0.0, 400.0, 0.0, 0.0, 0.0);
   GtkWidget *sw = gtk_scrolled_window_new (ADJ , ADJ);

   //gtk_adjustment_set_value (ADJ, 1.22222);
   g_print ("value %p is %f\n", ADJ, gtk_adjustment_get_value (ADJ));

   GtkWidget *keyboard=gtk_grid_new();
   gtk_grid_attach(GTK_GRID(keyboard), label, 0, 0, 1, 1);
   gtk_container_add(GTK_CONTAINER(sw), keyboard);  
   gtk_container_add(GTK_CONTAINER(keyboard_window), sw);

   //gtk_container_add(GTK_CONTAINER(keyboard_window), sw);
   //GtkWidget *keyboard=gtk_grid_new();
   //gtk_grid_attach(GTK_GRID(keyboard), label, 0, 0, 1, 1);  
   //gtk_container_add(GTK_CONTAINER(sw), keyboard);

   gtk_widget_show_all(keyboard_window);

   gtk_adjustment_set_value (ADJ, 100.0);

   gtk_main();

   return 0;  
 }


_______________________________________________
gtk-app-devel-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Reply | Threaded
Open this post in threaded view
|

Re: Trouble setting the value of a GtkAdjustment

Richard Shann
On Mon, 2017-04-24 at 14:48 -0400, [hidden email] wrote:
>
> Hi Richard,
>
> It looks like how you add containers makes a difference in this case.
> Also the window placement might have to be adjusted after the window
> is shown. Here is a try at it.

Thank you very much - it seems you are right, by moving the
gtk_container_add() calls until just before the gtk_show_all() the
GtkAdjustment widget becomes controllable. The values it has are no
longer what were set when it was created however, so I set it with this
code after the gtk_show_all() call:

    gdouble lower, upper, page;
    lower = gtk_adjustment_get_lower (hadj);
    upper = gtk_adjustment_get_upper (hadj);
    page = gtk_adjustment_get_page_size (hadj);
    //g_print ("Lower ... %f %f %f\n", lower, upper, page);
    gtk_adjustment_set_value (hadj, (upper-page - lower)/2);

which gets the horizontal slider in the middle.

The values when printed out are:

Lower ... 0.000000 1901.000000 800.000000

where those values come from is a mystery. And, I suppose the way it
changes depending on the order of placing the widgets inside each other
will have to remain a mystery too.

Thanks once again,

Richard


>
> Eric
>
>
> //gcc -Wall adj1.c -o adj1 `pkg-config --cflags --libs gtk+-3.0`
> //Tested on Ubuntu16.04 and GTK3.18
>
> #include<gtk/gtk.h>
>
> int main(int argc, char **argv)
>  {
>    gtk_init(&argc, &argv);
>
>    GtkWidget *keyboard_window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
>    gtk_window_set_title(GTK_WINDOW(keyboard_window), "Keyboard
> Window");
>    gtk_window_set_default_size(GTK_WINDOW(keyboard_window), 200, 200);
>    gtk_window_set_position(GTK_WINDOW(keyboard_window),
> GTK_WIN_POS_CENTER);
>    g_signal_connect(keyboard_window, "destroy",
> G_CALLBACK(gtk_main_quit), NULL);
>
>    GtkWidget *label=gtk_label_new("Label");
>    gtk_widget_set_size_request(label, 400, 400);
>
>    //GtkAdjustment *ADJ = gtk_adjustment_new (1.5, 0.0, 2.0, 0.01,
> 0.01, 0.01);
>    //GtkWidget *sw = gtk_scrolled_window_new (ADJ , gtk_adjustment_new
> (1.5, 0.0, 2.0, 0.01, 0.01, 0.01));
>    GtkAdjustment *ADJ = gtk_adjustment_new (100.0, 0.0, 400.0, 0.0,
> 0.0, 0.0);
>    GtkWidget *sw = gtk_scrolled_window_new (ADJ , ADJ);
>
>    //gtk_adjustment_set_value (ADJ, 1.22222);
>    g_print ("value %p is %f\n", ADJ, gtk_adjustment_get_value (ADJ));
>
>    GtkWidget *keyboard=gtk_grid_new();
>    gtk_grid_attach(GTK_GRID(keyboard), label, 0, 0, 1, 1);
>    gtk_container_add(GTK_CONTAINER(sw), keyboard);  
>    gtk_container_add(GTK_CONTAINER(keyboard_window), sw);
>
>    //gtk_container_add(GTK_CONTAINER(keyboard_window), sw);
>    //GtkWidget *keyboard=gtk_grid_new();
>    //gtk_grid_attach(GTK_GRID(keyboard), label, 0, 0, 1, 1);  
>    //gtk_container_add(GTK_CONTAINER(sw), keyboard);
>
>    gtk_widget_show_all(keyboard_window);
>
>    gtk_adjustment_set_value (ADJ, 100.0);
>
>    gtk_main();
>
>    return 0;  
>  }
>
>


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