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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
<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>Chapter 17. Dialogs</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="index.html" title="Programming with gtkmm 4">
<link rel="prev" href="sec-adjustment-internals.html" title="Adjustment Internals">
<link rel="next" href="sec-dialogs-filedialog.html" title="FileDialog and FileChooserDialog">
</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">Chapter 17. Dialogs</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-adjustment-internals.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center"> </th>
<td width="20%" align="right"> <a accesskey="n" href="sec-dialogs-filedialog.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="chapter">
<div class="titlepage"><div><div><h1 class="title">
<a name="chapter-dialogs"></a>Chapter 17. Dialogs</h1></div></div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<ul class="toc">
<li><span class="section"><a href="chapter-dialogs.html#sec-dialogs-alertdialog">AlertDialog and MessageDialog</a></span></li>
<li><span class="section"><a href="sec-dialogs-filedialog.html">FileDialog and FileChooserDialog</a></span></li>
<li><span class="section"><a href="sec-dialogs-colordialog.html">ColorDialog and ColorChooserDialog</a></span></li>
<li><span class="section"><a href="sec-dialogs-fontdialog.html">FontDialog and FontChooserDialog</a></span></li>
<li><span class="section"><a href="sec-about-dialog.html">Non-modal AboutDialog</a></span></li>
<li><span class="section"><a href="sec-dialogs-windowdialog.html">Custom Dialog</a></span></li>
</ul>
</div>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="icons/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top">
<p><code class="classname">Gtk::Dialog</code> and the classes derived from it,
are deprecated since <span class="application">gtkmm</span> 4.10. They can still be used in <span class="application">gtkmm</span>4 applications,
provided GTKMM_DISABLE_DEPRECATED and GDKMM_DISABLE_DEPRECATED are not defined.
Some of the dialog classes are replaced by classes that are available since <span class="application">gtkmm</span> 4.10.
</p>
<p>The examples in this chapter use classes that are available since <span class="application">gtkmm</span> 4.10.
Similar examples with the deprecated classes are available in the
<a class="ulink" href="https://gitlab.gnome.org/GNOME/gtkmm-documentation/tree/gtkmm-4-0/examples/book/dialogs/" target="_top">
gtkmm-4-0 branch</a> in the git repository.
</p>
</td></tr>
</table></div>
<p>
Dialogs are used as secondary windows, to provide specific information or to
ask questions. <code class="classname">Gtk::Dialog</code> windows contain a few pre-packed
widgets to ensure consistency, and a <code class="literal">response</code> signal which
is emitted when the user dismisses the dialog.
</p>
<p>
There are several derived <code class="classname">Dialog</code> classes which you might
find useful. <code class="classname">Gtk::MessageDialog</code> is used for most simple
notifications. But at other times you might need to derive your own dialog
class to provide more complex functionality.
</p>
<p>
To pack widgets into a custom dialog, you should pack them into the
<code class="classname">Gtk::Box</code>, available via
<code class="methodname">get_content_area()</code>. To just add a <code class="classname">Button</code>
to the bottom of the <code class="classname">Dialog</code>, you could use the
<code class="methodname">add_button()</code> method.
</p>
<p>
The <code class="literal">response</code> signal handler receives an <code class="literal">int</code>. This
may be a value from the <span class="type">Gtk::ResponseType</span> if the user
closed the dialog by clicking a standard button, or it could be the custom
response value that you specified when using <code class="methodname">add_button()</code>.
</p>
<p>
To show the dialog, call <code class="methodname">set_visible(true)</code>. If the same dialog instance
will be shown several times, you must also call <code class="methodname">set_hide_on_close()</code>,
or else the dialog will be destroyed when it's closed.
Connect to the <code class="literal">response</code> signal, if you want to know which button
was pressed. The <code class="literal">response</code> signal handler is also where you
should hide the dialog.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1Dialog.html" target="_top">Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-dialogs-alertdialog"></a>AlertDialog and MessageDialog</h2></div></div></div>
<p>
<code class="classname">MessageDialog</code> (deprecated since <span class="application">gtkmm</span> 4.10) and
<code class="classname">AlertDialog</code> (available since <span class="application">gtkmm</span> 4.10) are convenience
classes, used to create simple, standard message dialogs, with a message and buttons
for user response.
</p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1AlertDialog.html" target="_top">AlertDialog Reference</a></p>
<p><a class="ulink" href="https://gnome.pages.gitlab.gnome.org/gtkmm/classGtk_1_1MessageDialog.html" target="_top">MessageDialog Reference</a></p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="alertdialog-example"></a>Example</h3></div></div></div>
<div class="figure">
<a name="figure-dialogs-alertdialog"></a><p class="title"><b>Figure 17.1. AlertDialog</b></p>
<div class="figure-contents">
<div class="screenshot">
<div class="mediaobject"><img src="figures/dialogs_alertdialog.png" alt="AlertDialog"></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/alertdialog" 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 <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
~ExampleWindow() override;
protected:
//Signal handlers:
void on_button_info_clicked();
void on_button_question_clicked();
void on_question_dialog_finish(const Glib::RefPtr<Gio::AsyncResult>& result);
//Child widgets:
Gtk::Box m_ButtonBox;
Gtk::Button m_Button_Info;
Gtk::Button m_Button_Question;
Glib::RefPtr<Gtk::AlertDialog> m_pDialog;
};
#endif //GTKMM_EXAMPLEWINDOW_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 <gtkmm/messagedialog.h>
#include <iostream>
ExampleWindow::ExampleWindow()
: m_ButtonBox(Gtk::Orientation::VERTICAL),
m_Button_Info("Show Info AlertDialog"),
m_Button_Question("Show Question AlertDialog")
{
set_title("Gtk::AlertDialog example");
set_child(m_ButtonBox);
m_ButtonBox.append(m_Button_Info);
m_Button_Info.set_expand(true);
m_Button_Info.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_info_clicked) );
m_ButtonBox.append(m_Button_Question);
m_Button_Question.set_expand(true);
m_Button_Question.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_question_clicked) );
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_info_clicked()
{
if (!m_pDialog)
m_pDialog = Gtk::AlertDialog::create();
else
{
// Reset values that may have been set by on_button_question_clicked().
m_pDialog->set_buttons({});
m_pDialog->set_default_button(-1);
m_pDialog->set_cancel_button(-1);
}
m_pDialog->set_message("This is an INFO AlertDialog.");
m_pDialog->set_detail("And this is the secondary text that explains things.");
m_pDialog->show(*this);
}
void ExampleWindow::on_button_question_clicked()
{
if (!m_pDialog)
m_pDialog = Gtk::AlertDialog::create();
m_pDialog->set_message("This is a QUESTION AlertDialog.");
m_pDialog->set_detail("And this is the secondary text that explains things.");
m_pDialog->set_buttons({"Cancel", "Retry", "OK"});
m_pDialog->set_default_button(2); // OK button or Return key
m_pDialog->set_cancel_button(0); // Cancel button or Escape key
m_pDialog->choose(*this,
sigc::mem_fun(*this, &ExampleWindow::on_question_dialog_finish));
}
void ExampleWindow::on_question_dialog_finish(const Glib::RefPtr<Gio::AsyncResult>& result)
{
try
{
const int response_id = m_pDialog->choose_finish(result);
switch (response_id)
{
case 0:
std::cout << "Cancel clicked." << std::endl;
break;
case 1:
std::cout << "Retry clicked." << std::endl;
break;
case 2:
std::cout << "OK clicked." << std::endl;
break;
default:
std::cout << "Unexpected response: " << response_id << std::endl;
break;
}
}
catch (const Gtk::DialogError& err)
{
// Can be thrown by m_pDialog->choose_finish(result).
std::cout << "DialogError, " << err.what() << std::endl;
}
catch (const Glib::Error& err)
{
std::cout << "Unexpected exception. " << err.what() << 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 <gtkmm/application.h>
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->make_window_and_run<ExampleWindow>(argc, argv);
}
</code></pre>
</div>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-adjustment-internals.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"> </td>
<td width="40%" align="right"> <a accesskey="n" href="sec-dialogs-filedialog.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Adjustment Internals </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"> FileDialog and FileChooserDialog</td>
</tr>
</table>
</div>
</body>
</html>
|