File: example_dialog.cc

package info (click to toggle)
gtkmm3.0 3.24.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 12,316 kB
  • sloc: xml: 121,335; cpp: 8,647; makefile: 286; sh: 202; python: 6
file content (203 lines) | stat: -rw-r--r-- 5,856 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Dialog and Message Boxes
 *
 * Dialog widgets are used to pop up a transient window for user feedback.
 */

#include <gtkmm.h>
#include <memory>

class Example_Dialog : public Gtk::Window
{
public:
  Example_Dialog();
  ~Example_Dialog() override;

protected:
  //Signal handlers:
  void on_button_message();
  void on_button_interactive();
  void on_button_non_modal();
  void on_non_modal_response(int response_id);

  //Member widgets:
  Gtk::Frame m_Frame;
  Gtk::Box m_VBox, m_VBox2;
  Gtk::Box m_HBox, m_HBox2;
  Gtk::Button m_Button_Message;
  Gtk::Button m_Button_Interactive;
  Gtk::Button m_Button_NonModal;
  Gtk::Grid m_Grid;
  Gtk::Label m_Label1, m_Label2;
  Gtk::Entry m_Entry1, m_Entry2;

  gint m_count;
  std::unique_ptr<Gtk::MessageDialog> m_non_modal_dialog;
};

class Dialog_Interactive : public Gtk::Dialog
{
public:
  Dialog_Interactive(Gtk::Window& parent, const Glib::ustring& entry1, const Glib::ustring& entry2);
  ~Dialog_Interactive() override;

  Glib::ustring get_entry1() const;
  Glib::ustring get_entry2() const;

protected:
  //Member widgets:
  Gtk::Box m_HBox;
  Gtk::Grid m_Grid;
  Gtk::Label m_Label1, m_Label2;
  Gtk::Entry m_Entry1, m_Entry2;
  Gtk::Image m_Image;
};

//Called by DemoWindow;
Gtk::Window* do_dialog()
{
  return new Example_Dialog();
}

Example_Dialog::Example_Dialog()
: m_Frame("Dialogs"),
  m_VBox(Gtk::ORIENTATION_VERTICAL, 8),
  m_HBox(Gtk::ORIENTATION_HORIZONTAL, 8),
  m_HBox2(Gtk::ORIENTATION_HORIZONTAL, 8),
  m_Button_Message("_Message Dialog", true),
  m_Button_Interactive("_Interactive Dialog", true),
  m_Button_NonModal("_Non-modal Dialog", true),
  m_Label1("_Entry 1", true),
  m_Label2("E_ntry 2", true)
{
  m_count = 0;

  set_title("Dialogs");
  set_border_width(8);

  add(m_Frame);

  m_VBox.set_border_width(8);
  m_Frame.add(m_VBox);

  /* Standard message dialog */
  m_VBox.pack_start(m_HBox, Gtk::PACK_SHRINK);
  m_Button_Message.signal_clicked().connect(sigc::mem_fun(*this, &Example_Dialog::on_button_message));
  m_HBox.pack_start(m_Button_Message, Gtk::PACK_SHRINK);
  m_VBox.pack_start(*Gtk::make_managed<Gtk::Separator>(Gtk::ORIENTATION_HORIZONTAL), Gtk::PACK_SHRINK);

  /* Interactive dialog*/
  m_VBox.pack_start(m_HBox2, Gtk::PACK_SHRINK);
  m_Button_Interactive.signal_clicked().connect(sigc::mem_fun(*this, &Example_Dialog::on_button_interactive));
  m_HBox2.pack_start(m_VBox2, Gtk::PACK_SHRINK);
  m_VBox2.pack_start(m_Button_Interactive, Gtk::PACK_SHRINK);
  m_VBox.pack_start(*Gtk::make_managed<Gtk::Separator>(Gtk::ORIENTATION_HORIZONTAL), Gtk::PACK_SHRINK);

  m_Grid.set_row_spacing(4);
  m_Grid.set_column_spacing(4);
  m_HBox2.pack_start(m_Grid, Gtk::PACK_SHRINK);

  m_Grid.attach(m_Label1, 0, 0, 1, 1);
  m_Grid.attach(m_Entry1, 1, 0, 1, 1);
  m_Label1.set_mnemonic_widget(m_Entry1);

  m_Grid.attach(m_Label2, 0, 1, 1, 1);
  m_Grid.attach(m_Entry2, 1, 1, 1, 1);
  m_Label2.set_mnemonic_widget(m_Entry2);

  // Non-modal message dialog,
  // i.e. a dialog that does not freeze the rest of the application.
  m_VBox.pack_start(m_Button_NonModal, Gtk::PACK_SHRINK);
  m_Button_NonModal.signal_clicked().connect(sigc::mem_fun(
    *this, &Example_Dialog::on_button_non_modal));

  show_all();
}

Example_Dialog::~Example_Dialog()
{
}

void Example_Dialog::on_button_message()
{
  Glib::ustring strMessage = "This message box has been popped up the following\n"
                             "number of times:\n\n";
  strMessage += Glib::ustring::format(m_count);

  Gtk::MessageDialog dialog(*this, strMessage, false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, true); //true = modal
  /*int response =*/ dialog.run();

  m_count++;
}

void Example_Dialog::on_button_interactive()
{
  Dialog_Interactive* pDialog = new Dialog_Interactive(*this, m_Entry1.get_text(), m_Entry2.get_text());
  /*int response =*/ pDialog->run();
  m_Entry1.set_text(pDialog->get_entry1());
  m_Entry2.set_text(pDialog->get_entry2());
  delete pDialog;
}

void Example_Dialog::on_button_non_modal()
{
  const Glib::ustring strMessage = "Non-modal message box.";
  m_non_modal_dialog.reset(new Gtk::MessageDialog(*this, strMessage, /* use_markup= */ false,
    Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, /* modal= */ false));
  m_non_modal_dialog->signal_response().connect(
    sigc::mem_fun(*this, &Example_Dialog::on_non_modal_response));
  m_non_modal_dialog->show_all();
}

void Example_Dialog::on_non_modal_response(int /* response_id */)
{
  // Delete the non-modal dialog.
  // If the Example_Dialog is deleted without first responding to the non-modal
  // dialog, the non-modal dialog is deleted when m_non_modal_dialog is deleted.
  m_non_modal_dialog.reset();
}

Dialog_Interactive::Dialog_Interactive(Gtk::Window& parent, const Glib::ustring& entry1, const Glib::ustring& entry2)
: Gtk::Dialog("Interactive Dialog", parent, true),
  m_HBox(Gtk::ORIENTATION_HORIZONTAL, 8),
  m_Label1("_Entry 1", true), m_Label2("E_ntry 2", true),
  m_Image()
{
  m_Image.set_from_icon_name("dialog-question", Gtk::ICON_SIZE_DIALOG);
  add_button("_OK", Gtk::RESPONSE_OK);
  add_button("_Cancel", Gtk::RESPONSE_CANCEL);

  m_HBox.set_border_width(8);
  get_content_area()->pack_start(m_HBox, Gtk::PACK_SHRINK);
  m_HBox.pack_start(m_Image, Gtk::PACK_SHRINK);

  m_Grid.set_row_spacing(4);
  m_Grid.set_column_spacing(4);
  m_HBox.pack_start(m_Grid);

  m_Grid.attach(m_Label1, 0, 0, 1, 1);
  m_Entry1.set_text(entry1);
  m_Grid.attach(m_Entry1, 1, 0, 1, 1);
  m_Label1.set_mnemonic_widget(m_Entry1);

  m_Grid.attach(m_Label2, 0, 1, 1, 1);
  m_Entry2.set_text(entry2);
  m_Grid.attach(m_Entry2, 1, 1, 1, 1);
  m_Label2.set_mnemonic_widget(m_Entry2);

  show_all();
}

Glib::ustring Dialog_Interactive::get_entry1() const
{
  return m_Entry1.get_text();
}

Glib::ustring Dialog_Interactive::get_entry2() const
{
  return m_Entry2.get_text();
}

Dialog_Interactive::~Dialog_Interactive()
{
}