<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 16. Dialogs</title> <link rel="stylesheet" type="text/css" href="style.css"> <meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> <link rel="home" href="index.html" title="Programming with gtkmm 3"> <link rel="up" href="index.html" title="Programming with gtkmm 3"> <link rel="prev" href="chapter-widgets-without-xwindows.html" title="Chapter 15. Widgets Without X-Windows"> <link rel="next" href="sec-dialogs-filechooserdialog.html" title="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 16. Dialogs</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="chapter-widgets-without-xwindows.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-filechooserdialog.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 16. Dialogs</h1></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <ul class="toc"> <li><span class="sect1"><a href="chapter-dialogs.html#sec-dialogs-messagedialog">MessageDialog</a></span></li> <li><span class="sect1"><a href="sec-dialogs-filechooserdialog.html">FileChooserDialog</a></span></li> <li><span class="sect1"><a href="sec-color-selection-dialog.html">ColorChooserDialog</a></span></li> <li><span class="sect1"><a href="sec-font-chooser-dialog.html">FontChooserDialog</a></span></li> <li><span class="sect1"><a href="sec-about-dialog.html">Non-modal AboutDialog</a></span></li> </ul> </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="methodname">run()</code> method which blocks until 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="methodname">run()</code> method returns an <code class="literal">int</code>. This may be a value from the <code class="literal">Gtk::ResponseType</code> 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><a class="ulink" href="http://developer.gnome.org/gtkmm/3.24/classGtk_1_1Dialog.html" target="_top">Reference</a></p> <div class="sect1"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="sec-dialogs-messagedialog"></a>MessageDialog</h2></div></div></div> <p> <code class="classname">MessageDialog</code> is a convenience class, used to create simple, standard message dialogs, with a message, an icon, and buttons for user response. You can specify the type of message and the text in the constructor, as well as specifying standard buttons via the <code class="literal">Gtk::ButtonsType</code> enum. </p> <p><a class="ulink" href="http://developer.gnome.org/gtkmm/3.24/classGtk_1_1MessageDialog.html" target="_top">Reference</a></p> <div class="sect2"> <div class="titlepage"><div><div><h3 class="title"> <a name="messagedialog-example"></a>Example</h3></div></div></div> <div class="figure"> <a name="figure-dialogs-messagedialog"></a><p class="title"><b>Figure 16.1. MessageDialog</b></p> <div class="figure-contents"><div class="screenshot"><div><img src="figures/dialogs_messagedialog.png" alt="MessageDialog"></div></div></div> </div> <br class="figure-break"><p><a class="ulink" href="http://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/dialogs/messagedialog?h=gtkmm-3-24" target="_top">Source Code</a></p> <p>File: <code class="filename">examplewindow.h</code> (For use with gtkmm 3, not gtkmm 2) </p> <pre class="programlisting"> #ifndef GTKMM_EXAMPLEWINDOW_H #define GTKMM_EXAMPLEWINDOW_H #include <gtkmm.h> class ExampleWindow : public Gtk::Window { public: ExampleWindow(); virtual ~ExampleWindow(); protected: //Signal handlers: void on_button_info_clicked(); void on_button_question_clicked(); //Child widgets: Gtk::ButtonBox m_ButtonBox; Gtk::Button m_Button_Info, m_Button_Question; }; #endif //GTKMM_EXAMPLEWINDOW_H </pre> <p>File: <code class="filename">main.cc</code> (For use with gtkmm 3, not gtkmm 2) </p> <pre class="programlisting"> #include "examplewindow.h" #include <gtkmm/application.h> int main(int argc, char *argv[]) { auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example"); ExampleWindow window; //Shows the window and returns when it is closed. return app->run(window); } </pre> <p>File: <code class="filename">examplewindow.cc</code> (For use with gtkmm 3, not gtkmm 2) </p> <pre class="programlisting"> #include "examplewindow.h" #include <gtkmm/messagedialog.h> #include <iostream> ExampleWindow::ExampleWindow() : m_ButtonBox(Gtk::ORIENTATION_VERTICAL), m_Button_Info("Show Info MessageDialog"), m_Button_Question("Show Question MessageDialog") { set_title("Gtk::MessageDialog example"); add(m_ButtonBox); m_ButtonBox.pack_start(m_Button_Info); m_Button_Info.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_button_info_clicked) ); m_ButtonBox.pack_start(m_Button_Question); m_Button_Question.signal_clicked().connect(sigc::mem_fun(*this, &ExampleWindow::on_button_question_clicked) ); show_all_children(); } ExampleWindow::~ExampleWindow() { } void ExampleWindow::on_button_info_clicked() { Gtk::MessageDialog dialog(*this, "This is an INFO MessageDialog"); dialog.set_secondary_text( "And this is the secondary text that explains things."); dialog.run(); } void ExampleWindow::on_button_question_clicked() { Gtk::MessageDialog dialog(*this, "This is a QUESTION MessageDialog", false /* use_markup */, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_OK_CANCEL); dialog.set_secondary_text( "And this is the secondary text that explains things."); int result = dialog.run(); //Handle the response: switch(result) { case(Gtk::RESPONSE_OK): { std::cout << "OK clicked." << std::endl; break; } case(Gtk::RESPONSE_CANCEL): { std::cout << "Cancel clicked." << std::endl; break; } default: { std::cout << "Unexpected button clicked." << std::endl; break; } } } </pre> </div> </div> </div> <div class="navfooter"> <hr> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"> <a accesskey="p" href="chapter-widgets-without-xwindows.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-filechooserdialog.html"><img src="icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Chapter 15. Widgets Without X-Windows </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"> FileChooserDialog</td> </tr> </table> </div> </body> </html>