From: Emmanuele Bassi Date: Tue, 16 May 2023 17:51:07 +0000 (+0100) Subject: Implement GtkSelectionModel for GtkNotebookPages X-Git-Tag: archive/raspbian/4.12.3+ds-1+rpi1~1^2^2^2~22^2~1^2~250^2~1 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=0bbe68db0be9228ba996906e9d746488231cb1d2;p=gtk4.git Implement GtkSelectionModel for GtkNotebookPages The documentation says that the model returned by gtk_notebook_get_pages() implements the GtkSelectionModel interface, but checking the history confirms this is a lie. Instead of fixing the documentation, we can easily make it true, and reduce the differences between GtkNotebook and GtkStack. Fixes: #5837 --- diff --git a/gtk/gtknotebook.c b/gtk/gtknotebook.c index f0c84d8ed1..932701206d 100644 --- a/gtk/gtknotebook.c +++ b/gtk/gtknotebook.c @@ -46,6 +46,7 @@ #include "gtkorientable.h" #include "gtksizerequest.h" #include "gtkprivate.h" +#include "gtkselectionmodel.h" #include "gtkstack.h" #include "gtktypebuiltins.h" #include "gtkwidgetprivate.h" @@ -1172,7 +1173,7 @@ gtk_notebook_class_init (GtkNotebookClass *class) * * A selection model with the pages. */ - properties[PROP_PAGES] = + properties[PROP_PAGES] = g_param_spec_object ("pages", NULL, NULL, G_TYPE_LIST_MODEL, GTK_PARAM_READABLE); @@ -1358,7 +1359,7 @@ gtk_notebook_class_init (GtkNotebookClass *class) /** * GtkNotebook|menu.popup: * - * Opens the context menu. + * Opens the context menu. */ gtk_widget_class_install_action (widget_class, "menu.popup", NULL, gtk_notebook_popup_menu); @@ -1827,7 +1828,7 @@ gtk_notebook_reorder_tab (GtkNotebook *notebook, page_num = reorder_tab (notebook, child->next, notebook->focus_tab); else page_num = reorder_tab (notebook, child, notebook->focus_tab); - + gtk_notebook_child_reordered (notebook, notebook->focus_tab->data); for (element = notebook->children, i = 0; element; element = element->next, i++) { @@ -7131,7 +7132,7 @@ gtk_notebook_get_page (GtkNotebook *notebook, list = gtk_notebook_find_child (notebook, child); if (list != NULL) page = list->data; - + return page; } @@ -7197,8 +7198,51 @@ gtk_notebook_pages_list_model_init (GListModelInterface *iface) iface->get_n_items = gtk_notebook_pages_get_n_items; iface->get_item = gtk_notebook_pages_get_item; } + +static gboolean +gtk_notebook_pages_is_selected (GtkSelectionModel *model, + guint position) +{ + GtkNotebookPages *pages = GTK_NOTEBOOK_PAGES (model); + GtkNotebookPage *page; + + page = g_list_nth_data (pages->notebook->children, position); + if (page == NULL) + return FALSE; + + return page == pages->notebook->cur_page; +} + +static gboolean +gtk_notebook_pages_select_item (GtkSelectionModel *model, + guint position, + gboolean exclusive) +{ + GtkNotebookPages *pages = GTK_NOTEBOOK_PAGES (model); + GtkNotebookPage *page; + + page = g_list_nth_data (pages->notebook->children, position); + if (page == NULL) + return FALSE; + + if (page == pages->notebook->cur_page) + return FALSE; + + gtk_notebook_switch_page (pages->notebook, page); + + return TRUE; +} + +static void +gtk_notebook_pages_selection_model_init (GtkSelectionModelInterface *iface) +{ + iface->is_selected = gtk_notebook_pages_is_selected; + iface->select_item = gtk_notebook_pages_select_item; +} + G_DEFINE_TYPE_WITH_CODE (GtkNotebookPages, gtk_notebook_pages, G_TYPE_OBJECT, - G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_notebook_pages_list_model_init)) + G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, gtk_notebook_pages_list_model_init) + G_IMPLEMENT_INTERFACE (GTK_TYPE_SELECTION_MODEL, gtk_notebook_pages_selection_model_init)) static void gtk_notebook_pages_init (GtkNotebookPages *pages)