Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > 345aa895e80053137c21f8693106c3a0 > files > 18

gtkmm2.4-documentation-2.17.4-1mdv2010.0.noarch.rpm

<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.75.1">
<link rel="home" href="index.html" title="Programming with gtkmm">
<link rel="up" href="index.html" title="Programming with gtkmm">
<link rel="prev" href="chapter-widgets-without-xwindows.html" title="Chapter 13. 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 14. 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" title="Chapter 14. Dialogs">
<div class="titlepage"><div><div><h2 class="title">
<a name="chapter-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="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">ColorSelectionDialog</a></span></li>
<li><span class="sect1"><a href="sec-font-selection-dialog.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="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::VBox</code>, available via
<code class="methodname">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="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 button 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://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1Dialog.html" target="_top">Reference</a></p>
<div class="sect1" title="MessageDialog">
<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://library.gnome.org/devel/gtkmm/unstable/classGtk_1_1MessageDialog.html" target="_top">Reference</a></p>
<div class="sect2" title="Example">
<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 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="http://git.gnome.org/cgit/gtkmm-documentation/tree/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 &lt;gtkmm.h&gt;

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::VButtonBox m_ButtonBox;
  Gtk::Button m_Button_Info, m_Button_Question;
};

#endif //GTKMM_EXAMPLEWINDOW_H
</pre>
<p>File: <code class="filename">examplewindow.cc</code>
</p>
<pre class="programlisting">
#include "examplewindow.h"
#include &lt;gtkmm/messagedialog.h&gt;
#include &lt;iostream&gt;


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,
              &amp;ExampleWindow::on_button_info_clicked) );

  m_ButtonBox.pack_start(m_Button_Question);
  m_Button_Question.signal_clicked().connect(sigc::mem_fun(*this,
              &amp;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 &lt;&lt; "OK clicked." &lt;&lt; std::endl;
      break;
    }
    case(Gtk::RESPONSE_CANCEL):
    {
      std::cout &lt;&lt; "Cancel clicked." &lt;&lt; std::endl;
      break;
    }
    default:
    {
      std::cout &lt;&lt; "Unexpected button clicked." &lt;&lt; std::endl;
      break;
    }
  }
}
</pre>
<p>File: <code class="filename">main.cc</code>
</p>
<pre class="programlisting">
#include &lt;gtkmm/main.h&gt;
#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>
</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 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>