vmap/libs/gtkutil/nonmodal.cpp

113 lines
2.9 KiB
C++
Raw Normal View History

2020-11-17 11:16:16 +00:00
#include "nonmodal.h"
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
gboolean escape_clear_focus_widget(ui::Widget widget, GdkEventKey *event, gpointer data)
{
2021-08-04 20:23:18 +00:00
if (event->keyval == GDK_KEY_Escape) {
gtk_window_set_focus(widget.window(), NULL);
return TRUE;
}
return FALSE;
2020-11-17 11:16:16 +00:00
}
void widget_connect_escape_clear_focus_widget(ui::Widget widget)
{
2021-08-04 20:23:18 +00:00
widget.connect("key_press_event", G_CALLBACK(escape_clear_focus_widget), 0);
2020-11-17 11:16:16 +00:00
}
gboolean NonModalEntry::focus_in(ui::Entry entry, GdkEventFocus *event, NonModalEntry *self)
{
2021-08-04 20:23:18 +00:00
self->m_editing = false;
return FALSE;
2020-11-17 11:16:16 +00:00
}
gboolean NonModalEntry::focus_out(ui::Entry entry, GdkEventFocus *event, NonModalEntry *self)
{
2021-08-04 20:23:18 +00:00
if (self->m_editing && gtk_widget_get_visible(entry)) {
self->m_apply();
}
self->m_editing = false;
return FALSE;
2020-11-17 11:16:16 +00:00
}
gboolean NonModalEntry::changed(ui::Entry entry, NonModalEntry *self)
{
2021-08-04 20:23:18 +00:00
self->m_editing = true;
return FALSE;
2020-11-17 11:16:16 +00:00
}
gboolean NonModalEntry::enter(ui::Entry entry, GdkEventKey *event, NonModalEntry *self)
{
2021-08-04 20:23:18 +00:00
if (event->keyval == GDK_KEY_Return) {
self->m_apply();
self->m_editing = false;
gtk_window_set_focus(entry.window(), NULL);
return TRUE;
}
return FALSE;
2020-11-17 11:16:16 +00:00
}
gboolean NonModalEntry::escape(ui::Entry entry, GdkEventKey *event, NonModalEntry *self)
{
2021-08-04 20:23:18 +00:00
if (event->keyval == GDK_KEY_Escape) {
self->m_cancel();
self->m_editing = false;
gtk_window_set_focus(entry.window(), NULL);
return TRUE;
}
return FALSE;
2020-11-17 11:16:16 +00:00
}
void NonModalEntry::connect(ui::Entry entry)
{
2021-08-04 20:23:18 +00:00
entry.connect("focus_in_event", G_CALLBACK(focus_in), this);
entry.connect("focus_out_event", G_CALLBACK(focus_out), this);
entry.connect("key_press_event", G_CALLBACK(enter), this);
entry.connect("key_press_event", G_CALLBACK(escape), this);
entry.connect("changed", G_CALLBACK(changed), this);
2020-11-17 11:16:16 +00:00
}
gboolean NonModalSpinner::changed(ui::SpinButton spin, NonModalSpinner *self)
{
2021-08-04 20:23:18 +00:00
self->m_apply();
return FALSE;
2020-11-17 11:16:16 +00:00
}
gboolean NonModalSpinner::enter(ui::SpinButton spin, GdkEventKey *event, NonModalSpinner *self)
{
2021-08-04 20:23:18 +00:00
if (event->keyval == GDK_KEY_Return) {
gtk_window_set_focus(spin.window(), NULL);
return TRUE;
}
return FALSE;
2020-11-17 11:16:16 +00:00
}
gboolean NonModalSpinner::escape(ui::SpinButton spin, GdkEventKey *event, NonModalSpinner *self)
{
2021-08-04 20:23:18 +00:00
if (event->keyval == GDK_KEY_Escape) {
self->m_cancel();
gtk_window_set_focus(spin.window(), NULL);
return TRUE;
}
return FALSE;
2020-11-17 11:16:16 +00:00
}
void NonModalSpinner::connect(ui::SpinButton spin)
{
2021-08-04 20:23:18 +00:00
auto adj = ui::Adjustment::from(gtk_spin_button_get_adjustment(spin));
guint handler = adj.connect("value_changed", G_CALLBACK(changed), this);
g_object_set_data(G_OBJECT(spin), "handler", gint_to_pointer(handler));
spin.connect("key_press_event", G_CALLBACK(enter), this);
spin.connect("key_press_event", G_CALLBACK(escape), this);
2020-11-17 11:16:16 +00:00
}
void NonModalRadio::connect(ui::RadioButton radio)
{
2021-08-04 20:23:18 +00:00
GSList *group = gtk_radio_button_get_group(radio);
for (; group != 0; group = g_slist_next(group)) {
toggle_button_connect_callback(ui::ToggleButton::from(group->data), m_changed);
}
2020-11-17 11:16:16 +00:00
}