| 1 |
/* Shortcuts Window |
| 2 |
* |
| 3 |
* Gtk::ShortcutsWindow is a window that provides a help overlay |
| 4 |
* for shortcuts and gestures in an application. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <gtkmm.h> |
| 8 |
#include <iostream> // For std::cout |
| 9 |
|
| 10 |
class Example_Shortcuts : public Gtk::Window |
| 11 |
{ |
| 12 |
public: |
| 13 |
Example_Shortcuts(BaseObjectType* cobject, |
| 14 |
const Glib::RefPtr<Gtk::Builder>& builder); |
| 15 |
~Example_Shortcuts() override; |
| 16 |
|
| 17 |
protected: |
| 18 |
|
| 19 |
// Signal handler: |
| 20 |
void on_button_clicked(const Glib::ustring& id, const Glib::ustring& view); |
| 21 |
|
| 22 |
Glib::RefPtr<Gtk::Builder> m_builder; |
| 23 |
}; |
| 24 |
|
| 25 |
// Called by DemoWindow: |
| 26 |
Gtk::Window* do_shortcuts() |
| 27 |
{ |
| 28 |
// Load the XML file and instantiate its widgets: |
| 29 |
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create(); |
| 30 |
try |
| 31 |
{ |
| 32 |
builder->add_from_resource("/shortcuts/example_shortcuts.ui"); |
| 33 |
} |
| 34 |
catch (const Glib::Error& error) |
| 35 |
{ |
| 36 |
std::cout << "Error loading example_shortcuts.ui: " << error.what() << std::endl; |
| 37 |
return nullptr; |
| 38 |
} |
| 39 |
|
| 40 |
// Get the GtkBuilder-instantiated window: |
| 41 |
Example_Shortcuts* pWindow = nullptr; |
| 42 |
builder->get_widget_derived("window1", pWindow); |
| 43 |
if (!pWindow) |
| 44 |
{ |
| 45 |
std::cout << "Could not get 'window1' from the builder." << std::endl; |
| 46 |
return nullptr; |
| 47 |
} |
| 48 |
return pWindow; |
| 49 |
} |
| 50 |
|
| 51 |
Example_Shortcuts::Example_Shortcuts( |
| 52 |
BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) |
| 53 |
: Gtk::Window(cobject), |
| 54 |
m_builder(builder) |
| 55 |
{ |
| 56 |
Gtk::Button* pButton = nullptr; |
| 57 |
builder->get_widget("button_builder", pButton); |
| 58 |
if (pButton) |
| 59 |
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun( |
| 60 |
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_builder", "")); |
| 61 |
|
| 62 |
pButton = nullptr; |
| 63 |
builder->get_widget("button_gedit", pButton); |
| 64 |
if (pButton) |
| 65 |
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun( |
| 66 |
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_gedit", "")); |
| 67 |
|
| 68 |
pButton = nullptr; |
| 69 |
builder->get_widget("button_clocks", pButton); |
| 70 |
if (pButton) |
| 71 |
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun( |
| 72 |
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_clocks", "")); |
| 73 |
|
| 74 |
pButton = nullptr; |
| 75 |
builder->get_widget("button_clocks_stopwatch", pButton); |
| 76 |
if (pButton) |
| 77 |
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun( |
| 78 |
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_clocks", "stopwatch")); |
| 79 |
|
| 80 |
pButton = nullptr; |
| 81 |
builder->get_widget("button_boxes", pButton); |
| 82 |
if (pButton) |
| 83 |
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun( |
| 84 |
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_boxes", "")); |
| 85 |
|
| 86 |
pButton = nullptr; |
| 87 |
builder->get_widget("button_boxes_wizard", pButton); |
| 88 |
if (pButton) |
| 89 |
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun( |
| 90 |
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_boxes", "wizard")); |
| 91 |
|
| 92 |
pButton = nullptr; |
| 93 |
builder->get_widget("button_boxes_display", pButton); |
| 94 |
if (pButton) |
| 95 |
pButton->signal_clicked().connect(sigc::bind(sigc::mem_fun( |
| 96 |
*this, &Example_Shortcuts::on_button_clicked), "shortcuts_boxes", "display")); |
| 97 |
|
| 98 |
show_all(); |
| 99 |
} |
| 100 |
|
| 101 |
Example_Shortcuts::~Example_Shortcuts() |
| 102 |
{ |
| 103 |
} |
| 104 |
|
| 105 |
void Example_Shortcuts::on_button_clicked(const Glib::ustring& id, const Glib::ustring& view) |
| 106 |
{ |
| 107 |
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create(); |
| 108 |
try |
| 109 |
{ |
| 110 |
builder->add_from_resource("/shortcuts/example_" + id.raw() + ".ui"); |
| 111 |
} |
| 112 |
catch (const Glib::Error& error) |
| 113 |
{ |
| 114 |
std::cout << "Error loading example_" << id << ".ui: " << error.what() << std::endl; |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// Get the GtkBuilder-instantiated shortcuts window: |
| 119 |
Gtk::ShortcutsWindow* pOverlay = nullptr; |
| 120 |
builder->get_widget(id, pOverlay); |
| 121 |
if (!pOverlay) |
| 122 |
{ |
| 123 |
std::cout << "Could not get '"<< id << "' from the builder." << std::endl; |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
pOverlay->set_transient_for(*this); |
| 128 |
|
| 129 |
if (view.empty()) |
| 130 |
pOverlay->unset_view_name(); |
| 131 |
else |
| 132 |
pOverlay->property_view_name() = view; |
| 133 |
|
| 134 |
pOverlay->show(); |
| 135 |
} |