File: sec-dialogs-windowdialog.html

package info (click to toggle)
gtkmm-documentation 4.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 25,772 kB
  • sloc: cpp: 15,541; javascript: 1,208; makefile: 1,080; python: 401; xml: 106; perl: 67; sh: 8
file content (256 lines) | stat: -rw-r--r-- 7,844 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="highlight.min.css">
<script src="highlight.min.js"></script><script>
      hljs.configure({languages: ['cpp']});
      hljs.highlightAll();
    </script><title>Custom Dialog</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
<link rel="home" href="index.html" title="Programming with gtkmm 4">
<link rel="up" href="chapter-dialogs.html" title="Chapter 17. Dialogs">
<link rel="prev" href="sec-about-dialog.html" title="Non-modal AboutDialog">
<link rel="next" href="chapter-drawingarea.html" title="Chapter 18. The DrawingArea Widget">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Custom Dialog</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-about-dialog.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 17. Dialogs</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-drawingarea.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-dialogs-windowdialog"></a>Custom Dialog</h2></div></div></div>

<p>
When none of the predefined dialog classes suit your needs, you can make your own
dialog by deriving a class from <code class="classname">Window</code> and fill it with
the widgets you need.
</p>

<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1Window.html" target="_top">Window Reference</a></p>

<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="windowdialog-example"></a>Example</h3></div></div></div>


<div class="figure">
<a name="figure-dialogs-windowdialog"></a><p class="title"><b>Figure 17.6. Window Dialog</b></p>
<div class="figure-contents">
  
  <div class="screenshot">
    <div class="mediaobject"><img src="figures/dialogs_windowdialog.png" alt="Window Dialog"></div>
  </div>
</div>
</div>
<br class="figure-break">

<p><a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/master/examples/book/dialogs/windowdialog" target="_top">Source Code</a></p>

<p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include &lt;gtkmm.h&gt;
#include "namedialog.h"

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

protected:
  //Signal handlers:
  void on_button_clicked();
  void on_dialog_response(const Glib::ustring&amp; response);
  void on_dialog_hidden();

  //Child widgets:
  Gtk::Button m_Button;

  NameDialog m_Dialog;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</code></pre>
<p>File: <code class="filename">namedialog.h</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#ifndef GTKMM_NAME_DIALOG_H_
#define GTKMM_NAME_DIALOG_H_

#include &lt;gtkmm.h&gt;

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

  void buttons_clicked_connect(const sigc::slot&lt;void(const Glib::ustring&amp;)&gt;&amp; slot);
  Glib::ustring get_entry1() const;
  Glib::ustring get_entry2() const;

protected:
  //Member widgets:
  Gtk::Grid m_Grid;
  Gtk::Image m_Image;
  Gtk::Label m_Label1;
  Gtk::Label m_Label2;
  Gtk::Entry m_Entry1;
  Gtk::Entry m_Entry2;
  Gtk::Box m_ButtonBox;
  Gtk::Button m_Button_OK;
  Gtk::Button m_Button_Cancel;
};

#endif /* GTKMM_NAME_DIALOG_H_ */
</code></pre>
<p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;iostream&gt;

ExampleWindow::ExampleWindow()
: m_Button("Show Dialog"),
  m_Dialog()
{
  set_title("Custom Dialog example");

  set_child(m_Button);
  m_Button.signal_clicked().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_button_clicked));

  m_Dialog.set_default_size(250, 100);
  m_Dialog.set_transient_for(*this);
  m_Dialog.set_modal();
  m_Dialog.set_hide_on_close();
  m_Dialog.buttons_clicked_connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_dialog_response));
  m_Dialog.signal_hide().connect(
    sigc::mem_fun(*this, &amp;ExampleWindow::on_dialog_hidden));
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_button_clicked()
{
  m_Dialog.set_visible(true);
}

void ExampleWindow::on_dialog_response(const Glib::ustring&amp; response)
{
  m_Dialog.set_visible(false);

  if (response == "OK")
    std::cout &lt;&lt; "Name: " &lt;&lt; m_Dialog.get_entry1() &lt;&lt; " "
      &lt;&lt; m_Dialog.get_entry2() &lt;&lt; std::endl; 
  else
    std::cout &lt;&lt; response &lt;&lt; " button clicked" &lt;&lt; std::endl;
}

void ExampleWindow::on_dialog_hidden()
{
  std::cout &lt;&lt; "Dialog hidden" &lt;&lt; std::endl;
}
</code></pre>
<p>File: <code class="filename">main.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">#include "examplewindow.h"
#include &lt;gtkmm/application.h&gt;

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create("org.gtkmm.example");

  //Shows the window and returns when it is closed.
  return app-&gt;make_window_and_run&lt;ExampleWindow&gt;(argc, argv);
}
</code></pre>
<p>File: <code class="filename">namedialog.cc</code> (For use with gtkmm 4)</p>
<pre class="programlisting"><code class="code">NameDialog::NameDialog()
: m_Label1("_First name", true),
  m_Label2("_Second name", true),
  m_ButtonBox(Gtk::Orientation::HORIZONTAL, 5),
  m_Button_OK("_OK", true),
  m_Button_Cancel("_Cancel", true)
{
  set_destroy_with_parent(true);

  set_title("Name Dialog");
  set_child(m_Grid);

  m_Grid.set_row_spacing(4);
  m_Grid.set_column_spacing(4);
  m_Grid.set_expand(true);

  m_Image.set_from_icon_name("dialog-question");
  m_Image.set_icon_size(Gtk::IconSize::LARGE);
  m_Grid.attach(m_Image, 0, 0, 1, 2);

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

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

  m_Grid.attach(m_ButtonBox, 0, 2, 3, 1);
  m_ButtonBox.set_halign(Gtk::Align::END);
  m_ButtonBox.append(m_Button_OK);
  m_ButtonBox.append(m_Button_Cancel);
}

void NameDialog::buttons_clicked_connect(
  const sigc::slot&lt;void(const Glib::ustring&amp;)&gt;&amp; slot)
{
  m_Button_OK.signal_clicked().connect(sigc::bind(slot, "OK"));
  m_Button_Cancel.signal_clicked().connect(sigc::bind(slot, "Cancel"));
}

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

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

NameDialog::~NameDialog()
{
}
</code></pre>

</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-about-dialog.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-dialogs.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-drawingarea.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Non-modal AboutDialog </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 18. The DrawingArea Widget</td>
</tr>
</table>
</div>
</body>
</html>