Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
resize_handle.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
21
22#include "system/macros.h"
23
24#include <math.h>
25
38
39static gboolean _resize_handle_cursor(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
40{
41 dtgtk_resize_handle_t *handle = (dtgtk_resize_handle_t *)user_data;
42 if(event->type == GDK_ENTER_NOTIFY)
43 {
48 }
49 else if(!handle->dragging)
50 {
53 }
54
56 return TRUE;
57}
58
59static gboolean _resize_handle_button(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
60{
61 dtgtk_resize_handle_t *handle = (dtgtk_resize_handle_t *)user_data;
62 if(event->button != 1) return TRUE;
63
64 if(event->type == GDK_BUTTON_PRESS)
65 {
66 handle->dragging = TRUE;
67 handle->start_root = (handle->orientation == GTK_ORIENTATION_VERTICAL) ? event->y_root : event->x_root;
68 handle->start_size = handle->get_size(handle->user_data);
69 handle->current_size = handle->start_size;
70 gtk_grab_add(widget);
74 }
75 else if(event->type == GDK_BUTTON_RELEASE)
76 {
77 handle->dragging = FALSE;
78 gtk_grab_remove(widget);
79 handle->current_size = handle->resize(handle->current_size, TRUE, handle->user_data);
80
81 GtkAllocation allocation;
82 gtk_widget_get_allocation(widget, &allocation);
83 const gboolean pointer_on_handle = event->x >= 0. && event->x <= allocation.width
84 && event->y >= 0. && event->y <= allocation.height;
87 else
89
94 : GDK_LEFT_PTR);
96 }
97
98 return TRUE;
99}
100
101static gboolean _resize_handle_motion(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
102{
103 dtgtk_resize_handle_t *handle = (dtgtk_resize_handle_t *)user_data;
104 if(!handle->dragging) return TRUE;
105
106 /* Each motion event is one sample in the GTK pointer stream. Convert only the
107 * axis matching the resize direction and leave clamping/application to the
108 * owner callback, which is the code owning the resized widget. */
109 const double root = (handle->orientation == GTK_ORIENTATION_VERTICAL) ? event->y_root : event->x_root;
110 double delta = root - handle->start_root;
111 if(handle->invert) delta = -delta;
112 const int requested_size = handle->start_size + (int)round(delta);
113 handle->current_size = handle->resize(requested_size, FALSE, handle->user_data);
114 return TRUE;
115}
116
117GtkWidget *dtgtk_resize_handle_new(GtkOrientation orientation, gboolean invert, const char *tooltip,
119 dtgtk_resize_handle_resize_f resize, gpointer user_data)
120{
121 // A GtkEventBox is enough: it owns a GdkWindow for pointer events and renders its CSS
122 // background/border, so the whole hover affordance lives in the stylesheet (.resize-handle)
123 // with no custom drawing. It is meant to be added as an overlay child on the resized widget.
125 dtgtk_resize_handle_t *handle = g_malloc0(sizeof(*handle));
126 handle->orientation = orientation;
127 handle->invert = invert;
128 handle->get_size = get_size;
129 handle->resize = resize;
130 handle->user_data = user_data;
131
132 // Pin the grip to the edge that grows the target when dragged outward, filling the other axis.
133 // `invert` means the target grows in the negative direction, so the grip sits on the start edge.
134 // The grab thickness is set here (a CSS min-height/width isn't honoured for an empty overlay
135 // child -- GTK allocates its ~0px natural size); an edge class is added for hover styling.
137 gtk_style_context_add_class(ctx, "resize-handle");
138 if(orientation == GTK_ORIENTATION_VERTICAL)
139 {
143 gtk_style_context_add_class(ctx, invert ? "resize-handle-top" : "resize-handle-bottom");
144 }
145 else
146 {
150 gtk_style_context_add_class(ctx, invert ? "resize-handle-left" : "resize-handle-right");
151 }
152
156 if(!IS_NULL_PTR(tooltip))
158
159 g_object_set_data_full(G_OBJECT(handle_widget), "dtgtk-resize-handle", handle, g_free);
161 g_signal_connect(G_OBJECT(handle_widget), "button-release-event", G_CALLBACK(_resize_handle_button), handle);
165
166 return handle_widget;
167}
168
169// clang-format off
170// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
171// vim: shiftwidth=2 expandtab tabstop=2 cindent
172// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
173// 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
const float delta
const char * tooltip
Definition image.h:284
#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:65
static dt_mipmap_size_t get_size(const uint32_t key)
GtkWidget * dtgtk_resize_handle_new(GtkOrientation orientation, gboolean invert, const char *tooltip, dtgtk_resize_handle_get_size_f get_size, dtgtk_resize_handle_resize_f resize, gpointer user_data)
Create a themed handle widget driving one-dimensional resize gestures.
static gboolean _resize_handle_button(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
static gboolean _resize_handle_cursor(GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
static gboolean _resize_handle_motion(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
int(* dtgtk_resize_handle_resize_f)(int requested_size, gboolean finished, gpointer user_data)
G_BEGIN_DECLS typedef int(* dtgtk_resize_handle_get_size_f)(gpointer user_data)
dtgtk_resize_handle_get_size_f get_size
GtkOrientation orientation
dtgtk_resize_handle_resize_f resize
void dt_widget_set_cursor(GdkCursorType cursor)
#define DT_PIXEL_APPLY_DPI(value)