<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Chapter 14. Dialogs</title> <link rel="stylesheet" href="style.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.73.2"> <link rel="start" href="index.html" title="Programming with gtkmm"> <link rel="up" href="index.html" title="Programming with gtkmm"> <link rel="prev" href="ch13.html" title="Chapter 13. Widgets Without X-Windows"> <link rel="next" href="ch14s02.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 14. Dialogs</th></tr> <tr> <td width="20%" align="left"> <a accesskey="p" href="ch13.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="ch14s02.html"><img src="../icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="chapter" lang="en"> <div class="titlepage"><div><div><h2 class="title"> <a name="sec-Dialogs"></a>Chapter 14. Dialogs</h2></div></div></div> <div class="toc"> <p><b>Table of Contents</b></p> <ul> <li><span class="sect1"><a href="ch14.html#sec-dialogs_messagedialog">MessageDialog</a></span></li> <li><span class="sect1"><a href="ch14s02.html">FileChooserDialog</a></span></li> <li><span class="sect1"><a href="ch14s03.html">ColorSelectionDialog</a></span></li> <li><span class="sect1"><a href="ch14s04.html">FontSelectionDialog</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="function">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::VBox</code>, available via <code class="function">get_vbox()</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="function">add_button()</code> method. </p> <p> The <code class="function">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 button by clicking a standard button, or it could be the custom response value that you specified when using <code class="function">add_button()</code>. </p> <p><a class="ulink" href="../../reference/html/classGtk_1_1Dialog.html" target="_top">Reference</a></p> <div class="sect1" lang="en"> <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="../../reference/html/classGtk_1_1MessageDialog.html" target="_top">Reference</a></p> <div class="sect2" lang="en"> <div class="titlepage"><div><div><h3 class="title"> <a name="id2583909"></a>Example</h3></div></div></div> <div class="figure"> <a name="figure-dialogs-messagedialog"></a><p class="title"><b>Figure 14.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="../../../examples/book/dialogs/messagedialog" target="_top">Source Code</a></p> <p>File: <code class="filename">examplewindow.h</code> </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: virtual void on_button_info_clicked(); virtual void on_button_question_clicked(); //Child widgets: Gtk::VButtonBox m_ButtonBox; Gtk::Button m_Button_Info, m_Button_Question; }; #endif //GTKMM_EXAMPLEWINDOW_H </pre> <p>File: <code class="filename">main.cc</code> </p> <pre class="programlisting"> #include <gtkmm/main.h> #include "examplewindow.h" int main(int argc, char *argv[]) { Gtk::Main kit(argc, argv); ExampleWindow window; //Shows the window and returns when it is closed. Gtk::Main::run(window); return 0; } </pre> <p>File: <code class="filename">examplewindow.cc</code> </p> <pre class="programlisting"> #include "examplewindow.h" #include <gtkmm/dialog.h> #include <iostream> ExampleWindow::ExampleWindow() : 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="ch13.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="ch14s02.html"><img src="../icons/next.png" alt="Next"></a> </td> </tr> <tr> <td width="40%" align="left" valign="top">Chapter 13. 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>