Skip to content

Commit

Permalink
convert bauhaus and iop graphs to gtkgestures
Browse files Browse the repository at this point in the history
prep for gtk4
  • Loading branch information
dterrahe committed Oct 3, 2024
1 parent ea3a80e commit dc9fb84
Show file tree
Hide file tree
Showing 22 changed files with 913 additions and 953 deletions.
121 changes: 64 additions & 57 deletions src/bauhaus/bauhaus.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ static gboolean _popup_key_press(GtkWidget *widget, GdkEventKey *event, gpointer
static gboolean _widget_draw(GtkWidget *widget, cairo_t *crf);
static gboolean _widget_scroll(GtkWidget *widget, GdkEventScroll *event);
static gboolean _widget_key_press(GtkWidget *widget, GdkEventKey *event);
static gboolean _widget_button_press(GtkWidget *widget, GdkEventButton *event);
static gboolean _widget_button_release(GtkWidget *widget, GdkEventButton *event);
static gboolean _widget_motion_notify(GtkWidget *widget, GdkEventMotion *event);
static void _widget_button_press(GtkGestureSingle*, int, double, double, GtkWidget*);
static void _widget_button_release(GtkGestureSingle*, int, double, double, GtkWidget*);
static void _widget_motion(GtkEventControllerMotion*, double, double, GtkWidget*);
static void _widget_get_preferred_width(GtkWidget *widget,
gint *minimum_size,
gint *natural_size);
Expand Down Expand Up @@ -642,10 +642,30 @@ static void _window_show(GtkWidget *w, gpointer user_data)
gtk_grab_add(GTK_WIDGET(user_data));
}

static void _widget_leave(GtkEventControllerMotion *controller,
GtkWidget *widget)
{
if(controller == NULL)
// gtk_widget_set_state_flags triggers resize&draw avalanche
// instead add GTK_STATE_FLAG_PRELIGHT in _widget_draw
darktable.bauhaus->hovered = widget;
else
darktable.bauhaus->hovered = NULL;

gtk_widget_queue_draw(widget);
}

static void _widget_enter(GtkEventControllerMotion *controller,
double x,
double y,
GtkWidget *widget)
{
_widget_leave(NULL, widget);
}

static void dt_bh_init(DtBauhausWidget *w)
{
w->field = NULL;

w->section = NULL;

// no quad icon and no toggle button:
Expand All @@ -655,30 +675,18 @@ static void dt_bh_init(DtBauhausWidget *w)
w->show_quad = TRUE;
w->show_label = TRUE;

gtk_widget_add_events(GTK_WIDGET(w), GDK_POINTER_MOTION_MASK
| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
| GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK
| GDK_FOCUS_CHANGE_MASK
if(darktable.control->accel_initialising) return;

gtk_widget_add_events(GTK_WIDGET(w), GDK_FOCUS_CHANGE_MASK
| darktable.gui->scroll_mask);

dt_gui_connect_click_all(w, _widget_button_press, _widget_button_release, w);
dt_gui_connect_motion(w, _widget_motion, _widget_enter, _widget_leave, w);

gtk_widget_set_can_focus(GTK_WIDGET(w), TRUE);
dt_gui_add_class(GTK_WIDGET(w), "dt_bauhaus");
}

static gboolean _widget_enter_leave(GtkWidget *widget, GdkEventCrossing *event)
{
if(event->type == GDK_ENTER_NOTIFY)
// gtk_widget_set_state_flags triggers resize&draw avalanche
// instead add GTK_STATE_FLAG_PRELIGHT in _widget_draw
darktable.bauhaus->hovered = widget;
else
darktable.bauhaus->hovered = NULL;

gtk_widget_queue_draw(widget);

return FALSE;
}

static void _widget_finalize(GObject *widget)
{
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
Expand Down Expand Up @@ -716,13 +724,8 @@ static void dt_bh_class_init(DtBauhausWidgetClass *class)
widget_class->draw = _widget_draw;
widget_class->scroll_event = _widget_scroll;
widget_class->key_press_event = _widget_key_press;
widget_class->button_press_event = _widget_button_press;
widget_class->button_release_event = _widget_button_release;
widget_class->motion_notify_event = _widget_motion_notify;
widget_class->get_preferred_width = _widget_get_preferred_width;
widget_class->get_preferred_height = _widget_get_preferred_height;
widget_class->enter_notify_event = _widget_enter_leave;
widget_class->leave_notify_event = _widget_enter_leave;
G_OBJECT_CLASS(class)->finalize = _widget_finalize;
}

Expand Down Expand Up @@ -3378,8 +3381,11 @@ static gboolean _popup_key_press(GtkWidget *widget,
return TRUE;
}

static gboolean _widget_button_press(GtkWidget *widget,
GdkEventButton *event)
static void _widget_button_press(GtkGestureSingle *gesture,
int n_press,
double x,
double y,
GtkWidget *widget)
{
dt_bauhaus_widget_t *w = (dt_bauhaus_widget_t *)widget;
_request_focus(w);
Expand All @@ -3389,33 +3395,34 @@ static gboolean _widget_button_press(GtkWidget *widget,
gtk_widget_get_allocation(widget, &allocation);
const int w3 = allocation.width - w->margin.left - w->padding.left
- w->margin.right - w->padding.right;
const double ex = event->x - w->margin.left - w->padding.left;
const double ey = event->y - w->margin.top - w->padding.top;
const double ex = x - w->margin.left - w->padding.left;
const double ey = y - w->margin.top - w->padding.top;
guint button = gtk_gesture_single_get_current_button(gesture);

if(w->quad_paint
&& event->x > allocation.width - _widget_get_quad_width(w)
&& x > allocation.width - _widget_get_quad_width(w)
- w->margin.right - w->padding.right)
{
dt_bauhaus_widget_press_quad(widget);
}
else if(event->button == 1
&& event->type == GDK_2BUTTON_PRESS)
else if(button == 1 && n_press == 2)
{
if(!(dt_modifier_is(event->state, GDK_CONTROL_MASK) && w->field
if(!(dt_modifier_eq(gesture, GDK_CONTROL_MASK) && w->field
&& dt_gui_presets_autoapply_for_module((dt_iop_module_t *)w->module, widget)))
dt_bauhaus_widget_reset(widget); // reset to default.
// never called for combo, as we popup the other window under your cursor before.
// (except in weird corner cases where the popup is under the -1st entry
_popup_hide();
}
else if(event->button == 3 || w->type == DT_BAUHAUS_COMBOBOX)
else if(button == 3 || w->type == DT_BAUHAUS_COMBOBOX)
{
GdkEventMotion *event = (GdkEventMotion *)gtk_get_current_event(); // TODO cleanup
darktable.bauhaus->opentime = event->time;
darktable.bauhaus->mouse_x = event->x;
darktable.bauhaus->mouse_y = event->y;
darktable.bauhaus->mouse_x = x;
darktable.bauhaus->mouse_y = y;
_popup_show(widget);
}
else if(event->button == 2)
else if(button == 2)
{
_slider_zoom_range(w, 0); // reset zoom range to soft min/max
_slider_zoom_toast(w);
Expand All @@ -3424,7 +3431,7 @@ static gboolean _widget_button_press(GtkWidget *widget,
{
dt_bauhaus_slider_data_t *d = &w->data.slider;
d->is_dragging = -1;
if(!dt_modifier_is(event->state, 0))
if(!dt_modifier_eq(gesture, 0))
darktable.bauhaus->mouse_x = ex;
else if(ey > darktable.bauhaus->line_height / 2.0f)
{
Expand All @@ -3433,36 +3440,36 @@ static gboolean _widget_button_press(GtkWidget *widget,

darktable.bauhaus->mouse_x = NAN;
}
else
return FALSE;
}

return TRUE;
dt_gui_claim(gesture);
}

static gboolean _widget_button_release(GtkWidget *widget,
GdkEventButton *event)
static void _widget_button_release(GtkGestureSingle *gesture,
int n_press,
double x,
double y,
GtkWidget *widget)
{
dt_bauhaus_widget_t *w = (dt_bauhaus_widget_t *)widget;
dt_bauhaus_widget_release_quad(widget);

if(w->type != DT_BAUHAUS_SLIDER) return FALSE;
if(w->type != DT_BAUHAUS_SLIDER) return;

dt_bauhaus_slider_data_t *d = &w->data.slider;
if(event->button == 1 && d->is_dragging)
if(gtk_gesture_single_get_current_button(gesture) == 1 && d->is_dragging)
{
d->is_dragging = 0;
if(d->timeout_handle) g_source_remove(d->timeout_handle);
d->timeout_handle = 0;
_slider_set_normalized(w, d->pos);

return TRUE;
}
return FALSE;
}

static gboolean _widget_motion_notify(GtkWidget *widget,
GdkEventMotion *event)
static void _widget_motion(GtkEventControllerMotion *controller,
double x,
double y,
GtkWidget *widget)
{
dt_bauhaus_widget_t *w = (dt_bauhaus_widget_t *)widget;
dt_bauhaus_slider_data_t *d = &w->data.slider;
Expand All @@ -3471,21 +3478,21 @@ static gboolean _widget_motion_notify(GtkWidget *widget,
gtk_widget_get_allocation(widget, &allocation);
const int w3 = allocation.width - w->margin.left - w->padding.left
- w->margin.right - w->padding.right;
const double ex = event->x - w->margin.left - w->padding.left;
const double ex = x - w->margin.left - w->padding.left;

if(w->type == DT_BAUHAUS_COMBOBOX)
{
darktable.control->element =
event->x <= allocation.width - _widget_get_quad_width(w) || !w->quad_paint
x <= allocation.width - _widget_get_quad_width(w) || !w->quad_paint
? DT_ACTION_ELEMENT_SELECTION : DT_ACTION_ELEMENT_BUTTON;
}
else if(d->is_dragging && event->state & GDK_BUTTON1_MASK)
else if(d->is_dragging)
{
const float r = _slider_right_pos((float)w3, w);

if(dt_isnan(darktable.bauhaus->mouse_x))
{
if(dt_modifier_is(event->state, 0))
if(dt_modifier_eq(controller, 0))
_slider_set_normalized(w, (ex / w3) / r);
else
darktable.bauhaus->mouse_x = ex;
Expand All @@ -3495,6 +3502,7 @@ static gboolean _widget_motion_notify(GtkWidget *widget,
const float scaled_step =
w3 * r * dt_bauhaus_slider_get_step(widget) / (d->max - d->min);
const float steps = floorf((ex - darktable.bauhaus->mouse_x) / scaled_step);
GdkEventMotion *event = (GdkEventMotion *)gtk_get_current_event(); // TODO cleanup
_slider_add_step(widget, copysignf(1, d->factor) * steps, event->state, FALSE);

darktable.bauhaus->mouse_x += steps * scaled_step;
Expand All @@ -3513,7 +3521,6 @@ static gboolean _widget_motion_notify(GtkWidget *widget,
w->quad_paint ? DT_ACTION_ELEMENT_BUTTON : DT_ACTION_ELEMENT_VALUE;

gtk_widget_queue_draw(widget);
return TRUE;
}

void dt_bauhaus_vimkey_exec(const char *input)
Expand Down
60 changes: 32 additions & 28 deletions src/develop/imageop.c
Original file line number Diff line number Diff line change
Expand Up @@ -2489,47 +2489,52 @@ void dt_iop_gui_update_expanded(dt_iop_module_t *module)
dtgtk_expander_set_expanded(DTGTK_EXPANDER(module->expander), expanded);
}

static gboolean _iop_plugin_body_button_press(GtkWidget *w,
GdkEventButton *e,
gpointer user_data)
static void _iop_plugin_body_button_press(GtkGestureSingle *gesture,
int n_press,
double x,
double y,
dt_iop_module_t *module)
{
dt_iop_module_t *module = (dt_iop_module_t *)user_data;
if(e->button == 1)
guint button = gtk_gesture_single_get_current_button(gesture);
if(button == 1)
{
dt_iop_request_focus(module);
return TRUE;
dt_gui_claim(gesture);
return;
}
else if(e->button == 3)
else if(button == 3)
{
_presets_popup_callback(NULL, NULL, module);

return TRUE;
dt_gui_claim(gesture);
return;
}
return FALSE;
}

static gboolean _iop_plugin_header_button_press(GtkWidget *w,
GdkEventButton *e,
gpointer user_data)
static void _iop_plugin_header_button_press(GtkGestureSingle *gesture,
int n_press,
double x,
double y,
dt_iop_module_t *module)
{
if(e->type == GDK_2BUTTON_PRESS || e->type == GDK_3BUTTON_PRESS) return TRUE;
if(n_press > 1) return;

dt_iop_module_t *module = (dt_iop_module_t *)user_data;

if(e->button == 1)
guint button = gtk_gesture_single_get_current_button(gesture);
if(button == 1)
{
if(dt_modifier_is(e->state, GDK_SHIFT_MASK | GDK_CONTROL_MASK))
if(dt_modifier_eq(gesture, GDK_SHIFT_MASK | GDK_CONTROL_MASK))
; // do nothing (for easier dragging)
else if(dt_modifier_is(e->state, GDK_CONTROL_MASK))
else if(dt_modifier_eq(gesture, GDK_CONTROL_MASK))
{
dt_iop_gui_rename_module(module);
return TRUE;
dt_gui_claim(gesture);
return;
}
else
{
const gboolean collapse_others =
!dt_conf_get_bool("darkroom/ui/single_module")
!= (!dt_modifier_is(e->state, GDK_SHIFT_MASK));
!= (!dt_modifier_eq(gesture, GDK_SHIFT_MASK));

dt_iop_gui_set_expanded(module, !module->expanded, collapse_others);

Expand All @@ -2539,16 +2544,17 @@ static gboolean _iop_plugin_header_button_press(GtkWidget *w,
//used to take focus away from module search text input box when module selected
gtk_widget_grab_focus(dt_ui_center(darktable.gui->ui));

return TRUE;
dt_gui_claim(gesture);
return;
}
}
else if(e->button == 3)
else if(button == 3)
{
_presets_popup_callback(NULL, NULL, module);

return TRUE;
dt_gui_claim(gesture);
return;
}
return FALSE;
}

static void _header_size_callback(GtkWidget *widget,
Expand Down Expand Up @@ -3071,17 +3077,15 @@ void dt_iop_gui_set_expander(dt_iop_module_t *module)
module->header = header;

/* setup the header box */
g_signal_connect(G_OBJECT(header_evb), "button-press-event",
G_CALLBACK(_iop_plugin_header_button_press), module);
dt_gui_connect_click_all(header_evb, _iop_plugin_header_button_press, NULL, module);
gtk_widget_add_events(header_evb, GDK_POINTER_MOTION_MASK);
g_signal_connect(G_OBJECT(header_evb), "enter-notify-event",
G_CALLBACK(_header_motion_notify_show_callback), module);
g_signal_connect(G_OBJECT(header_evb), "leave-notify-event",
G_CALLBACK(_header_motion_notify_hide_callback), module);

/* connect mouse button callbacks for focus and presets */
g_signal_connect(G_OBJECT(body_evb), "button-press-event",
G_CALLBACK(_iop_plugin_body_button_press), module);
dt_gui_connect_click_all(body_evb, _iop_plugin_body_button_press, NULL, module);
gtk_widget_add_events(body_evb, GDK_POINTER_MOTION_MASK);
g_signal_connect(G_OBJECT(body_evb), "enter-notify-event",
G_CALLBACK(_header_motion_notify_show_callback), module);
Expand Down
Loading

0 comments on commit dc9fb84

Please sign in to comment.