Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
label.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/label.h"
20
21#include "common/glib_utils.h" // dt_string_replace
22#include "system/macros.h" // IS_NULL_PTR
23#include "system/mem_alloc.h" // dt_free
24#include "widgets/widget_style.h" // dt_gui_add_class, dt_capitalize_label
25
26#include "system/macros.h" // IS_NULL_PTR
27#include "widgets/widget_settings.h" // DT_PIXEL_APPLY_DPI
28
29GdkPixbuf *dt_gui_symbolic_icon_pixbuf(const char *icon_name, GtkIconSize size, const GdkRGBA *color,
30 GtkStyleContext *context)
31{
32 gint width = 16, height = 16;
33 gtk_icon_size_lookup(size, &width, &height);
34 // Icon themes only look up square icons: request the larger dimension,
35 // then scale the result down to the exact width/height below if the two differ.
36 GtkIconInfo *info = gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), icon_name, MAX(width, height),
37 GTK_ICON_LOOKUP_FORCE_SYMBOLIC);
38 if(IS_NULL_PTR(info)) return NULL;
39
40 GdkPixbuf *pixbuf = IS_NULL_PTR(color)
41 ? gtk_icon_info_load_symbolic_for_context(info, context, NULL, NULL)
42 : gtk_icon_info_load_symbolic(info, color, color, color, color, NULL, NULL);
43 g_object_unref(info);
44 if(IS_NULL_PTR(pixbuf)) return NULL;
45
46 if(gdk_pixbuf_get_width(pixbuf) != width || gdk_pixbuf_get_height(pixbuf) != height)
47 {
48 GdkPixbuf *scaled = gdk_pixbuf_scale_simple(pixbuf, width, height, GDK_INTERP_BILINEAR);
49 g_object_unref(pixbuf);
50 pixbuf = scaled;
51 }
52
53 return pixbuf;
54}
55
56void dt_gui_set_symbolic_icon(GtkWidget *image, const char *icon_name, GtkIconSize size, const GdkRGBA *color)
57{
58 GdkPixbuf *pixbuf
59 = dt_gui_symbolic_icon_pixbuf(icon_name, size, color, gtk_widget_get_style_context(image));
60
61 // Naming the icon and letting GTK draw it is the fallback for a theme that has no symbolic
62 // variant of it -- and now also for one whose variant failed to load, which used to leave the
63 // image showing nothing at all.
64 if(IS_NULL_PTR(pixbuf))
65 {
66 gtk_image_set_from_icon_name(GTK_IMAGE(image), icon_name, size);
67 return;
68 }
69
70 gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
71 g_object_unref(pixbuf);
72}
73
75gchar *delete_underscore(const char *s)
76{
77 return dt_string_replace(s, "_");
78}
79
87gchar *strip_markup(const char *s)
88{
89 if(IS_NULL_PTR(s)) return g_strdup("");
90
91 PangoAttrList *attrs = NULL;
92 gchar *plain = NULL;
93
94 const gchar *underscore = "_";
95 gunichar mnemonic = underscore[0];
96 if(!pango_parse_markup(s, -1, mnemonic, &attrs, &plain, NULL, NULL))
97 plain = delete_underscore(s);
98
99 pango_attr_list_unref(attrs);
100 return plain;
101}
102
105{
106 gtk_widget_set_halign(label, GTK_ALIGN_FILL); // make it span the whole available width
107 gtk_label_set_xalign (GTK_LABEL(label), 0.5f);
108 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END); // ellipsize labels
109 dt_gui_add_class(label, "dt_section_label"); // make sure that we can style these easily
110}
111
115{
116 gchar *str_cpy = g_strdup(str);
117 dt_capitalize_label(str_cpy);
118 GtkWidget *label = gtk_label_new(str_cpy);
119 dt_free(str_cpy);
121 return label;
122}
123
125GtkWidget *dt_ui_label_new(const gchar *str)
126{
127 gchar *str_cpy = g_strdup(str);
128 dt_capitalize_label(str_cpy);
129 GtkWidget *label = gtk_label_new(str_cpy);
130 dt_free(str_cpy);
131 gtk_widget_set_halign(label, GTK_ALIGN_START);
132 gtk_label_set_xalign (GTK_LABEL(label), 0.0f);
133 gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
134 return label;
135}
136
137void dt_ellipsize_combo(GtkComboBox *cbox)
138{
139 GList *renderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(cbox));
140 for(const GList *it = renderers; it; it = g_list_next(it))
141 {
142 GtkCellRendererText *tr = GTK_CELL_RENDERER_TEXT(it->data);
143 g_object_set(G_OBJECT(tr), "ellipsize", PANGO_ELLIPSIZE_MIDDLE, (gchar *)0);
144 }
145 g_list_free(renderers);
146 renderers = NULL;
147}
148
149void dt_gui_textview_set_padding(GtkTextView *textview)
150{
151 if(!GTK_IS_TEXT_VIEW(textview)) return;
152
153 gtk_text_view_set_left_margin(textview, DT_PIXEL_APPLY_DPI(4));
154 gtk_text_view_set_right_margin(textview, DT_PIXEL_APPLY_DPI(4));
155 gtk_text_view_set_top_margin(textview, DT_PIXEL_APPLY_DPI(2));
156 gtk_text_view_set_bottom_margin(textview, DT_PIXEL_APPLY_DPI(2));
157}
158
159// clang-format off
160// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
161// vim: shiftwidth=2 expandtab tabstop=2 cindent
162// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
163// clang-format on
struct _GtkWidget GtkWidget
GtkWidget, opaque, spelled exactly as GTK spells it.
Definition colorspaces.h:98
GdkRGBA color[]
Definition geotagging.c:541
static gchar * dt_string_replace(const char *string, const char *to_replace)
Definition glib_utils.h:64
GtkWidget * dt_ui_section_label_new(const gchar *str)
Definition label.c:114
gchar * delete_underscore(const char *s)
Definition label.c:75
GtkWidget * dt_ui_label_new(const gchar *str)
Definition label.c:125
void dt_gui_textview_set_padding(GtkTextView *textview)
Apply the standard recessed-input text padding to a GtkTextView.
Definition label.c:149
void dt_ellipsize_combo(GtkComboBox *cbox)
Definition label.c:137
GdkPixbuf * dt_gui_symbolic_icon_pixbuf(const char *icon_name, GtkIconSize size, const GdkRGBA *color, GtkStyleContext *context)
Load a symbolic icon as a pre-tinted pixbuf, at the pixel size size names.
Definition label.c:29
void dt_gui_set_symbolic_icon(GtkWidget *image, const char *icon_name, GtkIconSize size, const GdkRGBA *color)
Set a symbolic icon on an image widget, optionally forcing a specific color.
Definition label.c:56
void dt_ui_section_label_set(GtkWidget *label)
Definition label.c:104
gchar * strip_markup(const char *s)
Remove Pango/Gtk markup and accel mnemonics from a text label. If markup parsing fails,...
Definition label.c:87
#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
uint32_t width
Definition mipmap_cache.c:0
uint32_t height
Definition mipmap_cache.c:1
size_t size
Definition mipmap_cache.c:3
#define MAX(a, b)
Definition thinplate.c:29
#define DT_PIXEL_APPLY_DPI(value)
void dt_capitalize_label(gchar *text)
void dt_gui_add_class(GtkWidget *widget, const gchar *class_name)