Horizon
Loading...
Searching...
No Matches
gtk_util.hpp
1#pragma once
2#include <gtkmm.h>
3#include <stdint.h>
4namespace horizon {
5void bind_widget(class SpinButtonDim *sp, int64_t &v);
6void bind_widget(class SpinButtonDim *sp, uint64_t &v);
7
8void bind_widget(Gtk::Switch *sw, bool &v);
9void bind_widget(Gtk::CheckButton *sw, bool &v);
10void bind_widget(Gtk::SpinButton *sp, int &v);
11void bind_widget(Gtk::Scale *sc, float &v);
12void bind_widget(Gtk::Entry *en, std::string &v, std::function<void(std::string &v)> extra_cb = nullptr);
13void bind_widget(Gtk::ColorButton *color_button, class Color &color,
14 std::function<void(const Color &c)> extra_cb = nullptr);
15
16template <typename T>
17void bind_widget(std::map<T, Gtk::RadioButton *> &widgets, T &v, std::function<void(T)> extra_cb = nullptr)
18{
19 widgets[v]->set_active(true);
20 for (auto &it : widgets) {
21 T key = it.first;
22 Gtk::RadioButton *w = it.second;
23 it.second->signal_toggled().connect([key, w, &v, extra_cb] {
24 if (w->get_active()) {
25 v = key;
26 if (extra_cb)
27 extra_cb(key);
28 }
29 });
30 }
31}
32
33template <typename T>
34void bind_widget(Gtk::ComboBoxText *combo, const std::map<T, std::string> &lut, T &v,
35 std::function<void(T)> extra_cb = nullptr)
36{
37 for (const auto &it : lut) {
38 combo->append(std::to_string(static_cast<int>(it.first)), it.second);
39 }
40 combo->set_active_id(std::to_string(static_cast<int>(v)));
41 combo->signal_changed().connect([combo, &v, extra_cb] {
42 v = static_cast<T>(std::stoi(combo->get_active_id()));
43 if (extra_cb)
44 extra_cb(v);
45 });
46}
47
48Gtk::Label *grid_attach_label_and_widget(Gtk::Grid *gr, const std::string &label, Gtk::Widget *w, int &top);
49void label_set_tnum(Gtk::Label *la);
50void entry_set_tnum(Gtk::Entry &en);
51
52void tree_view_scroll_to_selection(Gtk::TreeView *view);
53Gtk::TreeViewColumn *tree_view_append_column_ellipsis(Gtk::TreeView *view, const std::string &name,
54 const Gtk::TreeModelColumnBase &column,
55 Pango::EllipsizeMode ellipsize);
56void tree_view_set_search_contains(Gtk::TreeView *view);
57
58void entry_set_warning(Gtk::Entry *e, const std::string &text);
59
60void header_func_separator(Gtk::ListBoxRow *row, Gtk::ListBoxRow *before);
61
62void entry_add_sanitizer(Gtk::Entry *entry);
63
64void info_bar_show(Gtk::InfoBar *bar);
65void info_bar_hide(Gtk::InfoBar *bar);
66std::string make_link_markup(const std::string &href, const std::string &label);
67
68Gtk::Box *make_boolean_ganged_switch(bool &v, const std::string &label_false, const std::string &label_true,
69 std::function<void(bool v)> extra_cb = nullptr);
70
71void make_label_small(Gtk::Label *label);
72void install_esc_to_close(Gtk::Window &win);
73void widget_set_insensitive_tooltip(Gtk::Widget &w, const std::string &txt);
74
75void widget_remove_scroll_events(Gtk::Widget &widget);
76void spinbutton_connect_activate(Gtk::SpinButton *sp, std::function<void()> cb);
77
78void open_directory(Gtk::Window &win, const std::string &filename);
79
80bool run_native_filechooser_with_retry(Glib::RefPtr<Gtk::FileChooser> chooser, const std::string &msg,
81 std::function<void()> fun);
82
83void listbox_ensure_row_visible(Gtk::ListBox *box, Gtk::ListBoxRow *row);
84void listbox_ensure_row_visible_new(Gtk::ListBox *box, Gtk::ListBoxRow *row);
85
86#define GET_WIDGET(name) \
87 do { \
88 x->get_widget(#name, name); \
89 } while (0)
90
91#define GET_OBJECT(name) \
92 do { \
93 name = name.cast_dynamic(x->get_object(#name)); \
94 } while (0)
95
96} // namespace horizon
Definition common.hpp:278