| 1 |
/* Dialog and Message Boxes |
| 2 |
* |
| 3 |
* Dialog widgets are used to pop up a transient window for user feedback. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <gtkmm.h> |
| 7 |
#include <memory> |
| 8 |
|
| 9 |
class Example_Dialog : public Gtk::Window |
| 10 |
{ |
| 11 |
public: |
| 12 |
Example_Dialog(); |
| 13 |
~Example_Dialog() override; |
| 14 |
|
| 15 |
protected: |
| 16 |
//Signal handlers: |
| 17 |
void on_button_message(); |
| 18 |
void on_button_interactive(); |
| 19 |
void on_button_non_modal(); |
| 20 |
void on_non_modal_response(int response_id); |
| 21 |
|
| 22 |
//Member widgets: |
| 23 |
Gtk::Frame m_Frame; |
| 24 |
Gtk::Box m_VBox, m_VBox2; |
| 25 |
Gtk::Box m_HBox, m_HBox2; |
| 26 |
Gtk::Button m_Button_Message; |
| 27 |
Gtk::Button m_Button_Interactive; |
| 28 |
Gtk::Button m_Button_NonModal; |
| 29 |
Gtk::Grid m_Grid; |
| 30 |
Gtk::Label m_Label1, m_Label2; |
| 31 |
Gtk::Entry m_Entry1, m_Entry2; |
| 32 |
|
| 33 |
gint m_count; |
| 34 |
std::unique_ptr<Gtk::MessageDialog> m_non_modal_dialog; |
| 35 |
}; |
| 36 |
|
| 37 |
class Dialog_Interactive : public Gtk::Dialog |
| 38 |
{ |
| 39 |
public: |
| 40 |
Dialog_Interactive(Gtk::Window& parent, const Glib::ustring& entry1, const Glib::ustring& entry2); |
| 41 |
~Dialog_Interactive() override; |
| 42 |
|
| 43 |
Glib::ustring get_entry1() const; |
| 44 |
Glib::ustring get_entry2() const; |
| 45 |
|
| 46 |
protected: |
| 47 |
//Member widgets: |
| 48 |
Gtk::Box m_HBox; |
| 49 |
Gtk::Grid m_Grid; |
| 50 |
Gtk::Label m_Label1, m_Label2; |
| 51 |
Gtk::Entry m_Entry1, m_Entry2; |
| 52 |
Gtk::Image m_Image; |
| 53 |
}; |
| 54 |
|
| 55 |
//Called by DemoWindow; |
| 56 |
Gtk::Window* do_dialog() |
| 57 |
{ |
| 58 |
return new Example_Dialog(); |
| 59 |
} |
| 60 |
|
| 61 |
Example_Dialog::Example_Dialog() |
| 62 |
: m_Frame("Dialogs"), |
| 63 |
m_VBox(Gtk::ORIENTATION_VERTICAL, 8), |
| 64 |
m_HBox(Gtk::ORIENTATION_HORIZONTAL, 8), |
| 65 |
m_HBox2(Gtk::ORIENTATION_HORIZONTAL, 8), |
| 66 |
m_Button_Message("_Message Dialog", true), |
| 67 |
m_Button_Interactive("_Interactive Dialog", true), |
| 68 |
m_Button_NonModal("_Non-modal Dialog", true), |
| 69 |
m_Label1("_Entry 1", true), |
| 70 |
m_Label2("E_ntry 2", true) |
| 71 |
{ |
| 72 |
m_count = 0; |
| 73 |
|
| 74 |
set_title("Dialogs"); |
| 75 |
set_border_width(8); |
| 76 |
|
| 77 |
add(m_Frame); |
| 78 |
|
| 79 |
m_VBox.set_border_width(8); |
| 80 |
m_Frame.add(m_VBox); |
| 81 |
|
| 82 |
/* Standard message dialog */ |
| 83 |
m_VBox.pack_start(m_HBox, Gtk::PACK_SHRINK); |
| 84 |
m_Button_Message.signal_clicked().connect(sigc::mem_fun(*this, &Example_Dialog::on_button_message)); |
| 85 |
m_HBox.pack_start(m_Button_Message, Gtk::PACK_SHRINK); |
| 86 |
m_VBox.pack_start(*Gtk::make_managed<Gtk::Separator>(Gtk::ORIENTATION_HORIZONTAL), Gtk::PACK_SHRINK); |
| 87 |
|
| 88 |
/* Interactive dialog*/ |
| 89 |
m_VBox.pack_start(m_HBox2, Gtk::PACK_SHRINK); |
| 90 |
m_Button_Interactive.signal_clicked().connect(sigc::mem_fun(*this, &Example_Dialog::on_button_interactive)); |
| 91 |
m_HBox2.pack_start(m_VBox2, Gtk::PACK_SHRINK); |
| 92 |
m_VBox2.pack_start(m_Button_Interactive, Gtk::PACK_SHRINK); |
| 93 |
m_VBox.pack_start(*Gtk::make_managed<Gtk::Separator>(Gtk::ORIENTATION_HORIZONTAL), Gtk::PACK_SHRINK); |
| 94 |
|
| 95 |
m_Grid.set_row_spacing(4); |
| 96 |
m_Grid.set_column_spacing(4); |
| 97 |
m_HBox2.pack_start(m_Grid, Gtk::PACK_SHRINK); |
| 98 |
|
| 99 |
m_Grid.attach(m_Label1, 0, 0, 1, 1); |
| 100 |
m_Grid.attach(m_Entry1, 1, 0, 1, 1); |
| 101 |
m_Label1.set_mnemonic_widget(m_Entry1); |
| 102 |
|
| 103 |
m_Grid.attach(m_Label2, 0, 1, 1, 1); |
| 104 |
m_Grid.attach(m_Entry2, 1, 1, 1, 1); |
| 105 |
m_Label2.set_mnemonic_widget(m_Entry2); |
| 106 |
|
| 107 |
// Non-modal message dialog, |
| 108 |
// i.e. a dialog that does not freeze the rest of the application. |
| 109 |
m_VBox.pack_start(m_Button_NonModal, Gtk::PACK_SHRINK); |
| 110 |
m_Button_NonModal.signal_clicked().connect(sigc::mem_fun( |
| 111 |
*this, &Example_Dialog::on_button_non_modal)); |
| 112 |
|
| 113 |
show_all(); |
| 114 |
} |
| 115 |
|
| 116 |
Example_Dialog::~Example_Dialog() |
| 117 |
{ |
| 118 |
} |
| 119 |
|
| 120 |
void Example_Dialog::on_button_message() |
| 121 |
{ |
| 122 |
Glib::ustring strMessage = "This message box has been popped up the following\n" |
| 123 |
"number of times:\n\n"; |
| 124 |
strMessage += Glib::ustring::format(m_count); |
| 125 |
|
| 126 |
Gtk::MessageDialog dialog(*this, strMessage, false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true); //true = modal |
| 127 |
/*int response =*/ dialog.run(); |
| 128 |
|
| 129 |
m_count++; |
| 130 |
} |
| 131 |
|
| 132 |
void Example_Dialog::on_button_interactive() |
| 133 |
{ |
| 134 |
Dialog_Interactive* pDialog = new Dialog_Interactive(*this, m_Entry1.get_text(), m_Entry2.get_text()); |
| 135 |
/*int response =*/ pDialog->run(); |
| 136 |
m_Entry1.set_text(pDialog->get_entry1()); |
| 137 |
m_Entry2.set_text(pDialog->get_entry2()); |
| 138 |
delete pDialog; |
| 139 |
} |
| 140 |
|
| 141 |
void Example_Dialog::on_button_non_modal() |
| 142 |
{ |
| 143 |
const Glib::ustring strMessage = "Non-modal message box."; |
| 144 |
m_non_modal_dialog.reset(new Gtk::MessageDialog(*this, strMessage, /* use_markup= */ false, |
| 145 |
Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, /* modal= */ false)); |
| 146 |
m_non_modal_dialog->signal_response().connect( |
| 147 |
sigc::mem_fun(*this, &Example_Dialog::on_non_modal_response)); |
| 148 |
m_non_modal_dialog->show_all(); |
| 149 |
} |
| 150 |
|
| 151 |
void Example_Dialog::on_non_modal_response(int /* response_id */) |
| 152 |
{ |
| 153 |
// Delete the non-modal dialog. |
| 154 |
// If the Example_Dialog is deleted without first responding to the non-modal |
| 155 |
// dialog, the non-modal dialog is deleted when m_non_modal_dialog is deleted. |
| 156 |
m_non_modal_dialog.reset(); |
| 157 |
} |
| 158 |
|
| 159 |
Dialog_Interactive::Dialog_Interactive(Gtk::Window& parent, const Glib::ustring& entry1, const Glib::ustring& entry2) |
| 160 |
: Gtk::Dialog("Interactive Dialog", parent, true), |
| 161 |
m_HBox(Gtk::ORIENTATION_HORIZONTAL, 8), |
| 162 |
m_Label1("_Entry 1", true), m_Label2("E_ntry 2", true), |
| 163 |
m_Image() |
| 164 |
{ |
| 165 |
m_Image.set_from_icon_name("dialog-question", Gtk::ICON_SIZE_DIALOG); |
| 166 |
add_button("_OK", Gtk::RESPONSE_OK); |
| 167 |
add_button("_Cancel", Gtk::RESPONSE_CANCEL); |
| 168 |
|
| 169 |
m_HBox.set_border_width(8); |
| 170 |
get_content_area()->pack_start(m_HBox, Gtk::PACK_SHRINK); |
| 171 |
m_HBox.pack_start(m_Image, Gtk::PACK_SHRINK); |
| 172 |
|
| 173 |
m_Grid.set_row_spacing(4); |
| 174 |
m_Grid.set_column_spacing(4); |
| 175 |
m_HBox.pack_start(m_Grid); |
| 176 |
|
| 177 |
m_Grid.attach(m_Label1, 0, 0, 1, 1); |
| 178 |
m_Entry1.set_text(entry1); |
| 179 |
m_Grid.attach(m_Entry1, 1, 0, 1, 1); |
| 180 |
m_Label1.set_mnemonic_widget(m_Entry1); |
| 181 |
|
| 182 |
m_Grid.attach(m_Label2, 0, 1, 1, 1); |
| 183 |
m_Entry2.set_text(entry2); |
| 184 |
m_Grid.attach(m_Entry2, 1, 1, 1, 1); |
| 185 |
m_Label2.set_mnemonic_widget(m_Entry2); |
| 186 |
|
| 187 |
show_all(); |
| 188 |
} |
| 189 |
|
| 190 |
Glib::ustring Dialog_Interactive::get_entry1() const |
| 191 |
{ |
| 192 |
return m_Entry1.get_text(); |
| 193 |
} |
| 194 |
|
| 195 |
Glib::ustring Dialog_Interactive::get_entry2() const |
| 196 |
{ |
| 197 |
return m_Entry2.get_text(); |
| 198 |
} |
| 199 |
|
| 200 |
Dialog_Interactive::~Dialog_Interactive() |
| 201 |
{ |
| 202 |
} |
| 203 |
|