Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > 345aa895e80053137c21f8693106c3a0 > files > 162

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Examples</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="chapter-menus-and-toolbars.html" title="Chapter 11. Menus and Toolbars">
<link rel="prev" href="sec-menus-popup.html" title="Popup Menus">
<link rel="next" href="chapter-adjustment.html" title="Chapter 12. Adjustments">
</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">Examples</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="sec-menus-popup.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 11. Menus and Toolbars</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-adjustment.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Examples">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-menus-examples"></a>Examples</h2></div></div></div>
<div class="sect2" title="Main Menu example">
<div class="titlepage"><div><div><h3 class="title">
<a name="menu-example-main"></a>Main Menu example</h3></div></div></div>
<div class="figure">
<a name="figure-menus-mainmenu"></a><p class="title"><b>Figure 11.1. Main Menu</b></p>
<div class="figure-contents"><div class="screenshot"><div><img src="figures/main_menu.png" alt="Main Menu"></div></div></div>
</div>
<br class="figure-break"><p><a class="ulink" href="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/menus/main_menu/" 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_menu_file_new_generic();
  void on_menu_file_quit();
  void on_menu_others();

  void on_menu_choices_one();
  void on_menu_choices_two();

  //Child widgets:
  Gtk::VBox m_Box;

  Glib::RefPtr&lt;Gtk::UIManager&gt; m_refUIManager;
  Glib::RefPtr&lt;Gtk::ActionGroup&gt; m_refActionGroup;
  Glib::RefPtr&lt;Gtk::RadioAction&gt; m_refChoiceOne, m_refChoiceTwo;
};

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

ExampleWindow::ExampleWindow()
{
  set_title("main menu example");
  set_default_size(200, 200);

  add(m_Box); // put a MenuBar at the top of the box and other stuff below it.

  //Create actions for menus and toolbars:
  m_refActionGroup = Gtk::ActionGroup::create();

  //File|New sub menu:
  m_refActionGroup-&gt;add(Gtk::Action::create("FileNewStandard",
              Gtk::Stock::NEW, "_New", "Create a new file"),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_new_generic));

  m_refActionGroup-&gt;add(Gtk::Action::create("FileNewFoo",
              Gtk::Stock::NEW, "New Foo", "Create a new foo"),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_new_generic));

  m_refActionGroup-&gt;add(Gtk::Action::create("FileNewGoo",
              Gtk::Stock::NEW, "_New Goo", "Create a new goo"),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_new_generic));

  //File menu:
  m_refActionGroup-&gt;add(Gtk::Action::create("FileMenu", "File"));
  //Sub-menu.
  m_refActionGroup-&gt;add(Gtk::Action::create("FileNew", Gtk::Stock::NEW));
  m_refActionGroup-&gt;add(Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_quit));

  //Edit menu:
  m_refActionGroup-&gt;add(Gtk::Action::create("EditMenu", "Edit"));
  m_refActionGroup-&gt;add(Gtk::Action::create("EditCopy", Gtk::Stock::COPY),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others));
  m_refActionGroup-&gt;add(Gtk::Action::create("EditPaste", Gtk::Stock::PASTE),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others));
  m_refActionGroup-&gt;add(Gtk::Action::create("EditSomething", "Something"),
          Gtk::AccelKey("&lt;control&gt;&lt;alt&gt;S"),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others));


  //Choices menu, to demonstrate Radio items
  m_refActionGroup-&gt;add( Gtk::Action::create("ChoicesMenu", "Choices") );
  Gtk::RadioAction::Group group_userlevel;
  m_refChoiceOne = Gtk::RadioAction::create(group_userlevel, "ChoiceOne", "One");
  m_refActionGroup-&gt;add(m_refChoiceOne,
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_choices_one) );
  m_refChoiceTwo = Gtk::RadioAction::create(group_userlevel, "ChoiceTwo", "Two");
  m_refActionGroup-&gt;add(m_refChoiceTwo,
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_choices_two) );

  //Help menu:
  m_refActionGroup-&gt;add( Gtk::Action::create("HelpMenu", "Help") );
  m_refActionGroup-&gt;add( Gtk::Action::create("HelpAbout", Gtk::Stock::HELP),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_others) );

  m_refUIManager = Gtk::UIManager::create();
  m_refUIManager-&gt;insert_action_group(m_refActionGroup);

  add_accel_group(m_refUIManager-&gt;get_accel_group());

  //Layout the actions in a menubar and toolbar:
  Glib::ustring ui_info = 
        "&lt;ui&gt;"
        "  &lt;menubar name='MenuBar'&gt;"
        "    &lt;menu action='FileMenu'&gt;"
        "      &lt;menu action='FileNew'&gt;"
        "        &lt;menuitem action='FileNewStandard'/&gt;"
        "        &lt;menuitem action='FileNewFoo'/&gt;"
        "        &lt;menuitem action='FileNewGoo'/&gt;"
        "      &lt;/menu&gt;"
        "      &lt;separator/&gt;"
        "      &lt;menuitem action='FileQuit'/&gt;"
        "    &lt;/menu&gt;"
        "    &lt;menu action='EditMenu'&gt;"
        "      &lt;menuitem action='EditCopy'/&gt;"
        "      &lt;menuitem action='EditPaste'/&gt;"
        "      &lt;menuitem action='EditSomething'/&gt;"
        "    &lt;/menu&gt;"
        "    &lt;menu action='ChoicesMenu'&gt;"
        "      &lt;menuitem action='ChoiceOne'/&gt;"
        "      &lt;menuitem action='ChoiceTwo'/&gt;"
        "    &lt;/menu&gt;"
        "    &lt;menu action='HelpMenu'&gt;"
        "      &lt;menuitem action='HelpAbout'/&gt;"
        "    &lt;/menu&gt;"
        "  &lt;/menubar&gt;"
        "  &lt;toolbar  name='ToolBar'&gt;"
        "    &lt;toolitem action='FileNewStandard'/&gt;"
        "    &lt;toolitem action='FileQuit'/&gt;"
        "  &lt;/toolbar&gt;"
        "&lt;/ui&gt;";

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
    m_refUIManager-&gt;add_ui_from_string(ui_info);
  }
  catch(const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "building menus failed: " &lt;&lt;  ex.what();
  }
  #else
  std::auto_ptr&lt;Glib::Error&gt; ex;
  m_refUIManager-&gt;add_ui_from_string(ui_info, ex);
  if(ex.get())
  {
    std::cerr &lt;&lt; "building menus failed: " &lt;&lt;  ex-&gt;what();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  //Get the menubar and toolbar widgets, and add them to a container widget:
  Gtk::Widget* pMenubar = m_refUIManager-&gt;get_widget("/MenuBar");
  if(pMenubar)
    m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);

  Gtk::Widget* pToolbar = m_refUIManager-&gt;get_widget("/ToolBar") ;
  if(pToolbar)
    m_Box.pack_start(*pToolbar, Gtk::PACK_SHRINK);

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_menu_file_quit()
{
  hide(); //Closes the main window to stop the Gtk::Main::run().
}

void ExampleWindow::on_menu_file_new_generic()
{
   std::cout &lt;&lt; "A File|New menu item was selected." &lt;&lt; std::endl;
}

void ExampleWindow::on_menu_others()
{
  std::cout &lt;&lt; "A menu item was selected." &lt;&lt; std::endl;
}

void ExampleWindow::on_menu_choices_one()
{
  Glib::ustring message;
  if(m_refChoiceOne-&gt;get_active())
    message = "Choice 1 was selected.";
  else
    message = "Choice 1 was deselected";

  std::cout &lt;&lt; message &lt;&lt; std::endl;
}

void ExampleWindow::on_menu_choices_two()
{
  Glib::ustring message;
  if(m_refChoiceTwo-&gt;get_active())
    message = "Choice 2 was selected.";
  else
    message = "Choice 2 was deselected";

  std::cout &lt;&lt; message &lt;&lt; std::endl;
}

</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 class="sect2" title="Popup Menu example">
<div class="titlepage"><div><div><h3 class="title">
<a name="menu-example-popup"></a>Popup Menu example</h3></div></div></div>
<div class="figure">
<a name="figure-menus-popup"></a><p class="title"><b>Figure 11.2. Popup Menu</b></p>
<div class="figure-contents"><div class="screenshot"><div><img src="figures/menu_popup.png" alt="Popup Menu"></div></div></div>
</div>
<br class="figure-break"><p><a class="ulink" href="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/menus/popup/" 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:
  virtual bool on_button_press_event(GdkEventButton* event);
  void on_menu_file_popup_generic();

  //Child widgets:
  Gtk::VBox m_Box;
  Gtk::EventBox m_EventBox;
  Gtk::Label m_Label;

  Glib::RefPtr&lt;Gtk::UIManager&gt; m_refUIManager;
  Glib::RefPtr&lt;Gtk::ActionGroup&gt; m_refActionGroup;

  Gtk::Menu* m_pMenuPopup;
};

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

ExampleWindow::ExampleWindow()
: m_Label("Right-click to see the popup menu."),
  m_pMenuPopup(0)
  /* m_Image(Gtk::Stock::DIALOG_QUESTION, Gtk::ICON_SIZE_MENU) */
{
  set_title("popup example");
  set_default_size(200, 200);

  add(m_Box);

  //Add an event box that can catch button_press events:
  m_Box.pack_start(m_EventBox);
  m_EventBox.signal_button_press_event().connect(sigc::mem_fun(*this,
              &amp;ExampleWindow::on_button_press_event) );

  m_EventBox.add(m_Label);

  //Create actions:

  //Fill menu:

  m_refActionGroup = Gtk::ActionGroup::create();

  //File|New sub menu:
  //These menu actions would normally already exist for a main menu, because a
  //context menu should not normally contain menu items that are only available
  //via a context menu.
  m_refActionGroup-&gt;add(Gtk::Action::create("ContextMenu", "Context Menu"));

  m_refActionGroup-&gt;add(Gtk::Action::create("ContextEdit", "Edit"),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_popup_generic));

  m_refActionGroup-&gt;add(Gtk::Action::create("ContextProcess", "Process"),
          Gtk::AccelKey("&lt;control&gt;P"),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_popup_generic));

  m_refActionGroup-&gt;add(Gtk::Action::create("ContextRemove", "Remove"),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_popup_generic));

  //TODO:
  /*
    //Add a ImageMenuElem:
    menulist.push_back( Gtk::Menu_Helpers::ImageMenuElem("_Something", m_Image,
      sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_popup_generic) ) ) ;
  */

  m_refUIManager = Gtk::UIManager::create();
  m_refUIManager-&gt;insert_action_group(m_refActionGroup);

  add_accel_group(m_refUIManager-&gt;get_accel_group());

  //Layout the actions in a menubar and toolbar:
  Glib::ustring ui_info =
        "&lt;ui&gt;"
        "  &lt;popup name='PopupMenu'&gt;"
        "    &lt;menuitem action='ContextEdit'/&gt;"
        "    &lt;menuitem action='ContextProcess'/&gt;"
        "    &lt;menuitem action='ContextRemove'/&gt;"
        "  &lt;/popup&gt;"
        "&lt;/ui&gt;";

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
    m_refUIManager-&gt;add_ui_from_string(ui_info);
  }
  catch(const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "building menus failed: " &lt;&lt;  ex.what();
  }
  #else
  std::auto_ptr&lt;Glib::Error&gt; ex;
  m_refUIManager-&gt;add_ui_from_string(ui_info, ex);
  if(ex.get())
  {
    std::cerr &lt;&lt; "building menus failed: " &lt;&lt;  ex-&gt;what();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  //Get the menu:
  m_pMenuPopup = dynamic_cast&lt;Gtk::Menu*&gt;(
          m_refUIManager-&gt;get_widget("/PopupMenu")); 
  if(!m_pMenuPopup)
    g_warning("menu not found");

  show_all_children();
}

ExampleWindow::~ExampleWindow()
{
}

void ExampleWindow::on_menu_file_popup_generic()
{
   std::cout &lt;&lt; "A popup menu item was selected." &lt;&lt; std::endl;
}

bool ExampleWindow::on_button_press_event(GdkEventButton* event)
{
  if( (event-&gt;type == GDK_BUTTON_PRESS) &amp;&amp; (event-&gt;button == 3) )
  {
    if(m_pMenuPopup)
      m_pMenuPopup-&gt;popup(event-&gt;button, event-&gt;time);

    return true; //It has been handled.
  }
  else
    return false;
}

</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 class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="sec-menus-popup.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-menus-and-toolbars.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-adjustment.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Popup Menus </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 12. Adjustments </td>
</tr>
</table>
</div>
</body>
</html>