Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
notebook.c
Go to the documentation of this file.
1/*
2 * This file is part of Ansel,
3 * Copyright (C) 2026 Aurélien PIERRE.
4 *
5 * Ansel is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * Ansel is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Ansel. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "widgets/notebook.h"
20
21#include "system/macros.h" // IS_NULL_PTR
22#include "widgets/widget_style.h" // dt_capitalize_label
23#include "widgets/widget_settings.h" // DT_PIXEL_APPLY_DPI, dt_widget_notebook_page_changed
24#include "widgets/widget_style.h" // dt_gui_add_class
25#include <glib/gi18n.h>
26#include "system/mem_alloc.h"
27
28static void _notebook_size_callback(GtkNotebook *notebook, GdkRectangle *allocation, gpointer *data)
29{
30 const int n = gtk_notebook_get_n_pages(notebook);
31 g_return_if_fail(n > 0);
32
33 GtkRequestedSize *sizes = g_malloc_n(n, sizeof(GtkRequestedSize));
34
35 for(int i = 0; i < n; i++)
36 {
37 sizes[i].data = gtk_notebook_get_tab_label(notebook, gtk_notebook_get_nth_page(notebook, i));
38 sizes[i].minimum_size = 0;
39 GtkRequisition natural_size;
40 gtk_widget_get_preferred_size(sizes[i].data, NULL, &natural_size);
41 sizes[i].natural_size = natural_size.width;
42 }
43
44 GtkAllocation first, last;
45 gtk_widget_get_allocation(sizes[0].data, &first);
46 gtk_widget_get_allocation(sizes[n - 1].data, &last);
47
48 const gint total_space = last.x + last.width - first.x; // ignore tab padding; CSS sets padding for label
49
50 if(total_space > 0)
51 {
52 gtk_distribute_natural_allocation(total_space, n, sizes);
53
54 for(int i = 0; i < n; i++)
55 gtk_widget_set_size_request(sizes[i].data, sizes[i].minimum_size, -1);
56
57 gtk_widget_size_allocate(GTK_WIDGET(notebook), allocation);
58
59 for(int i = 0; i < n; i++)
60 gtk_widget_set_size_request(sizes[i].data, -1, -1);
61 }
62
63 dt_free(sizes);
64}
65
66// GTK_STATE_FLAG_PRELIGHT does not seem to get set on the label on hover so
67// state-flags-changed cannot update dt_control_get_global()->element for shortcut mapping
68static gboolean _notebook_motion_notify_callback(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
69{
70 GtkAllocation notebook_alloc, label_alloc;
71 gtk_widget_get_allocation(widget, &notebook_alloc);
72
73 GtkNotebook *notebook = GTK_NOTEBOOK(widget);
74 const int n = gtk_notebook_get_n_pages(notebook);
75 for(int i = 0; i < n; i++)
76 {
77 gtk_widget_get_allocation(gtk_notebook_get_tab_label(notebook, gtk_notebook_get_nth_page(notebook, i)), &label_alloc);
78 }
79
80 return FALSE;
81}
82
83GtkNotebook *dt_ui_notebook_new()
84{
85 return GTK_NOTEBOOK(gtk_notebook_new());
86}
87
88GtkWidget *dt_ui_notebook_page(GtkNotebook *notebook, const char *text, const char *tooltip)
89{
90 gchar *text_cpy = g_strdup(_(text));
91 dt_capitalize_label(text_cpy);
92 GtkWidget *label = gtk_label_new(text_cpy);
93 dt_free(text_cpy);
94 GtkWidget *page = gtk_box_new(GTK_ORIENTATION_VERTICAL, DT_GUI_BOX_SPACING);
95 if(strlen(text) > 2)
96 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
97 gtk_widget_set_tooltip_text(label, tooltip ? tooltip : _(text));
98 gtk_widget_set_has_tooltip(GTK_WIDGET(notebook), FALSE);
99
100 gint page_num = gtk_notebook_append_page(notebook, page, label);
101 gtk_container_child_set(GTK_CONTAINER(notebook), page, "tab-expand", TRUE, "tab-fill", TRUE, NULL);
102 if(page_num == 1 &&
103 !g_signal_handler_find(G_OBJECT(notebook), G_SIGNAL_MATCH_FUNC, 0, 0, NULL, _notebook_size_callback, NULL))
104 {
105 g_signal_connect(G_OBJECT(notebook), "size-allocate", G_CALLBACK(_notebook_size_callback), NULL);
106 g_signal_connect(G_OBJECT(notebook), "motion-notify-event", G_CALLBACK(_notebook_motion_notify_callback), NULL);
107 }
108
109 return page;
110}
111
112static void _notebook_switch_page_signal_relay(GtkNotebook *notebook, GtkWidget *page, guint page_num,
113 gpointer user_data)
114{
116}
117
118void dt_ui_notebook_set_picker_owner(GtkNotebook *notebook, gpointer owner)
119{
120 if(IS_NULL_PTR(notebook)) return;
121
122 // Guard against connecting twice on the same notebook (e.g. a caller re-entering
123 // gui_init after a GUI rebuild); user_data is set per-connection, so re-registering
124 // a different owner on an already-relayed notebook would otherwise leak the old
125 // connection instead of updating it.
126 const gulong existing = g_signal_handler_find(G_OBJECT(notebook), G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
127 G_CALLBACK(_notebook_switch_page_signal_relay), NULL);
128 if(existing) g_signal_handler_disconnect(G_OBJECT(notebook), existing);
129
130 g_signal_connect(G_OBJECT(notebook), "switch_page", G_CALLBACK(_notebook_switch_page_signal_relay), owner);
131}
132
133// clang-format off
134// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
135// vim: shiftwidth=2 expandtab tabstop=2 cindent
136// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
137// clang-format on
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
struct _GtkWidget GtkWidget
GtkWidget, opaque, spelled exactly as GTK spells it.
Definition colorspaces.h:98
void * data
Definition geometry.h:111
const char * tooltip
Definition image.h:319
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
static gboolean _notebook_motion_notify_callback(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
Definition notebook.c:68
GtkWidget * dt_ui_notebook_page(GtkNotebook *notebook, const char *text, const char *tooltip)
Definition notebook.c:88
GtkNotebook * dt_ui_notebook_new()
Definition notebook.c:83
static void _notebook_size_callback(GtkNotebook *notebook, GdkRectangle *allocation, gpointer *data)
Definition notebook.c:28
static void _notebook_switch_page_signal_relay(GtkNotebook *notebook, GtkWidget *page, guint page_num, gpointer user_data)
Definition notebook.c:112
void dt_ui_notebook_set_picker_owner(GtkNotebook *notebook, gpointer owner)
Register an opaque owner for a GtkNotebook's page switches, and report every "switch_page" to the hos...
Definition notebook.c:118
GtkWidget * notebook
void dt_widget_notebook_page_changed(gpointer owner)
#define DT_GUI_BOX_SPACING
void dt_capitalize_label(gchar *text)