Gtk 3.24, Greybird theme: Scrollbar is rendered in the wrong position
Disclaimer I'm aware this might look like multi-posting stuff, but while finding workarounds in other places, this here is only for filing a bug.
This might be an edge case due to a not-allowed UI structuring (although I wouldn't know what that was).
Description
In Greybird
theme (standard of XUbuntu), the scrollbar is not positioned properly after showing it over an "notify::page-size"
event.
Details
The example code creates a program that adds buttons on a button click. If the buttons exceed the visible vertical space a GtkScrollbar
will be shown:
However, when the first button exceeds the vertical height by a certain amount (this "amount" also seems to depend on the theme in use), the scrollbar is rendered behind the buttons:
And here a video for how it looks on different themes.
This is weird, because the scrollbar is actually inside a horizontal GtkBox in a cell next to those buttons.
UI Architecture of sample code
Whether the scrollbar is shown or not, is decided when the "page-size" property the GtkAdjustment is changed.
Code
#include <gtk/gtk.h>
gboolean
adj_page_sizechanged (GtkAdjustment *adjustment,
GdkEvent *unused,
GtkScrollbar *scrollbar)
{
gdouble maxValue = gtk_adjustment_get_upper (adjustment);
gdouble maxSize = gtk_adjustment_get_page_size (adjustment);
gboolean show = (maxValue - maxSize >= 0.000001);
//g_object_set (scrollbar, "visible", show, NULL);
if (show) gtk_widget_show (GTK_WIDGET (scrollbar));
else gtk_widget_hide (GTK_WIDGET (scrollbar));
return FALSE;
}
void
add_button (GtkButton *button,
GtkBox *box)
{
g_print("add button\n");
gtk_container_add (GTK_CONTAINER (box), gtk_button_new_with_label ("b 2"));
gtk_widget_show_all (GTK_WIDGET (box));
}
GtkWidget *
create_ui()
{
GtkWidget * action_button = NULL;
GtkWidget * box_outer = NULL;
GtkWidget * box_inner = NULL;
// create the inner GtkBox with the buttons
box_inner = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
action_button = gtk_button_new_with_label ("click me");
gtk_container_add (GTK_CONTAINER (box_inner), action_button);
g_signal_connect (action_button, "clicked", G_CALLBACK (add_button), box_inner);
// create the outer GtkBox (with ScrolledWindow and Scrollbar)
{
box_outer = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
// setup adjustment
GtkAdjustment * adj_v = gtk_adjustment_new (0,0,100,1,10,10);
// setup scrollbar
{
GtkWidget * scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_VERTICAL, adj_v);
g_signal_connect(adj_v, "notify::page-size", G_CALLBACK (adj_page_sizechanged), scrollbar);
gtk_box_pack_end (GTK_BOX (box_outer), scrollbar, FALSE, FALSE, 0);
}
// setup scrollable area
{
GtkWidget * scrolled_window = gtk_scrolled_window_new (NULL, adj_v);
gtk_container_add (GTK_CONTAINER (scrolled_window), box_inner);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
GTK_POLICY_NEVER, GTK_POLICY_EXTERNAL);
gtk_box_pack_start (GTK_BOX (box_outer), scrolled_window, TRUE, TRUE, 0);
}
}
return box_outer;
}
int main(int argc, char *argv[])
{
gtk_init (&argc, &argv);
GtkWidget *window;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size (GTK_WINDOW (window), -1, 60);
GtkWidget * ui = create_ui ();
gtk_container_add (GTK_CONTAINER (window), ui);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (gtk_main_quit), G_OBJECT (window));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
Notes
- The code above first inserts the scrollbar and then the scrollable are. If the order is reversed: first the scrollable are and then the scrollbar, the outcome looks a little different:
Related issues
This issue might be related to
- Gtk+ >=3.20 Whole window goes black
- attempt to allocate widget with width -1164904012 and height 173
System:
- XUbuntu 21.04
- GTK Theme: Greybird ( possibly crutial for this bug to reproduce )
- Window Manager Theme: Greybird (provided screenshots use a different theme.