| 1 |
/* Application main window |
| 2 |
* |
| 3 |
* Demonstrates a typical application window, with menubar, toolbar, statusbar. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <gtkmm.h> |
| 7 |
|
| 8 |
class Example_AppWindow : public Gtk::Window |
| 9 |
{ |
| 10 |
public: |
| 11 |
Example_AppWindow(); |
| 12 |
~Example_AppWindow() override; |
| 13 |
|
| 14 |
protected: |
| 15 |
//Signal handlers: |
| 16 |
virtual void on_menu_item(); |
| 17 |
virtual void on_text_changed(); |
| 18 |
virtual void on_text_mark_set(const Gtk::TextBuffer::iterator& new_location, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark); |
| 19 |
|
| 20 |
//Member widgets: |
| 21 |
Gtk::Grid m_Grid; |
| 22 |
Gtk::Menu m_Menubar; |
| 23 |
Gtk::Toolbar m_Toolbar; |
| 24 |
Gtk::ScrolledWindow m_ScrolledWindow; |
| 25 |
Gtk::Statusbar m_Statusbar; |
| 26 |
Gtk::TextView m_TextView; |
| 27 |
}; |
| 28 |
|
| 29 |
|
| 30 |
//Called by DemoWindow; |
| 31 |
Gtk::Window* do_appwindow() |
| 32 |
{ |
| 33 |
return new Example_AppWindow(); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
Example_AppWindow::Example_AppWindow() |
| 38 |
{ |
| 39 |
set_title("Application Window"); |
| 40 |
|
| 41 |
add(m_Grid); |
| 42 |
|
| 43 |
/* |
| 44 |
//Menu: |
| 45 |
{ |
| 46 |
using namespace Gtk::Menu_Helpers; |
| 47 |
|
| 48 |
//File menu: |
| 49 |
Gtk::Menu* pMenuFile = Gtk::make_managed<Gtk::Menu>(); |
| 50 |
MenuList& list_file = pMenuFile->items(); |
| 51 |
list_file.push_back( MenuElem("_New", "<control>N", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 52 |
list_file.push_back( MenuElem("_Open", "<control>O", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 53 |
list_file.push_back( MenuElem("_Save", "<control>S", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 54 |
list_file.push_back( MenuElem("Save _As", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 55 |
list_file.push_back(SeparatorElem()); |
| 56 |
list_file.push_back( MenuElem("_Quit", "<control>Q", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 57 |
|
| 58 |
//Preferences menu: |
| 59 |
Gtk::Menu* pMenuPreferences = Gtk::make_managed<Gtk::Menu>(); |
| 60 |
MenuList& list_preferences = pMenuPreferences->items(); |
| 61 |
|
| 62 |
// Create a submenu |
| 63 |
Gtk::Menu* pMenuSub_Color = Gtk::make_managed<Gtk::Menu>(); |
| 64 |
MenuList& list_sub = pMenuSub_Color->items(); |
| 65 |
list_sub.push_back( MenuElem("_Red", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 66 |
list_sub.push_back( MenuElem("_Green", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 67 |
list_sub.push_back( MenuElem("_Blue", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 68 |
|
| 69 |
list_preferences.push_back( MenuElem("_Color", *pMenuSub_Color) ); |
| 70 |
|
| 71 |
// Create a submenu |
| 72 |
Gtk::Menu* pMenuSub_Shape = Gtk::make_managed<Gtk::Menu>(); |
| 73 |
list_sub = pMenuSub_Shape->items(); |
| 74 |
list_sub.push_back( MenuElem("_Square", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 75 |
list_sub.push_back( MenuElem("_Rectangle", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 76 |
list_sub.push_back( MenuElem("_Oval", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 77 |
|
| 78 |
list_preferences.push_back( MenuElem("_Shape", *pMenuSub_Shape) ); |
| 79 |
|
| 80 |
//Help menu: |
| 81 |
Gtk::Menu* pMenuHelp = Gtk::make_managed<Gtk::Menu>(); |
| 82 |
MenuList& list_help = pMenuHelp->items(); |
| 83 |
list_help.push_back( MenuElem("_About", sigc::mem_fun(*this, &Example_AppWindow::on_menu_item)) ); |
| 84 |
|
| 85 |
|
| 86 |
//Create the menu bar |
| 87 |
MenuList& list_bar = m_Menubar.items(); |
| 88 |
list_bar.push_front(MenuElem("_Help", *pMenuHelp)); |
| 89 |
list_bar.front()->set_right_justified(); |
| 90 |
list_bar.push_front(MenuElem("_Preferences", *pMenuPreferences)); |
| 91 |
list_bar.push_front(MenuElem("_File", *pMenuFile)); |
| 92 |
|
| 93 |
//Add the menu bar to the Grid: |
| 94 |
// left top width height |
| 95 |
m_Grid.attach(m_Menubar, 0, 0, 1, 1); |
| 96 |
} //menu |
| 97 |
|
| 98 |
*/ |
| 99 |
//Toolbar: |
| 100 |
{ |
| 101 |
m_Toolbar.set_hexpand(); |
| 102 |
// left top width height |
| 103 |
m_Grid.attach(m_Toolbar, 0, 1, 1, 1); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
| 108 |
m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_IN); |
| 109 |
// left top width height |
| 110 |
m_Grid.attach(m_ScrolledWindow, 0, 2, 1, 1); |
| 111 |
|
| 112 |
set_default_size(200, 200); |
| 113 |
|
| 114 |
m_ScrolledWindow.add(m_TextView); |
| 115 |
|
| 116 |
|
| 117 |
/* Create statusbar */ |
| 118 |
m_Statusbar.set_hexpand(); |
| 119 |
// left top width height |
| 120 |
m_Grid.attach(m_Statusbar, 0, 3, 1, 1); |
| 121 |
|
| 122 |
/* Show text widget info in the statusbar */ |
| 123 |
Glib::RefPtr<Gtk::TextBuffer> refTextBuffer = m_TextView.get_buffer(); |
| 124 |
refTextBuffer->signal_changed().connect(sigc::mem_fun(*this, &Example_AppWindow::on_text_changed)); |
| 125 |
refTextBuffer->signal_mark_set().connect(sigc::mem_fun(*this, &Example_AppWindow::on_text_mark_set)); |
| 126 |
on_text_changed(); |
| 127 |
|
| 128 |
show_all(); |
| 129 |
} |
| 130 |
|
| 131 |
Example_AppWindow::~Example_AppWindow() |
| 132 |
{ |
| 133 |
} |
| 134 |
|
| 135 |
void Example_AppWindow::on_menu_item() |
| 136 |
{ |
| 137 |
Gtk::MessageDialog dialog(*this, "You selected or toggled the menu item", false, |
| 138 |
Gtk::MESSAGE_INFO, Gtk::BUTTONS_CLOSE); |
| 139 |
dialog.run(); |
| 140 |
} |
| 141 |
|
| 142 |
void Example_AppWindow::on_text_changed() |
| 143 |
{ |
| 144 |
m_Statusbar.pop(); |
| 145 |
|
| 146 |
Glib::RefPtr<Gtk::TextBuffer> refBuffer = m_TextView.get_buffer(); |
| 147 |
gint count = refBuffer->get_char_count(); |
| 148 |
|
| 149 |
Gtk::TextBuffer::iterator iter = refBuffer->get_iter_at_mark(refBuffer->get_insert()); |
| 150 |
|
| 151 |
gint row = iter.get_line(); |
| 152 |
gint col = iter.get_line_offset(); |
| 153 |
|
| 154 |
gchar* msg = g_strdup_printf ("Cursor at row %d column %d - %d chars in document", |
| 155 |
row, col, count); |
| 156 |
m_Statusbar.push(msg); |
| 157 |
g_free (msg); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
void Example_AppWindow::on_text_mark_set(const Gtk::TextBuffer::iterator&, const Glib::RefPtr<Gtk::TextBuffer::Mark>&) |
| 162 |
{ |
| 163 |
on_text_changed(); |
| 164 |
} |
| 165 |
|