Sophie

Sophie

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

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RecentChooser</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-recent-documents.html" title="Chapter 19. Recently Used Documents">
<link rel="prev" href="chapter-recent-documents.html" title="Chapter 19. Recently Used Documents">
<link rel="next" href="chapter-plugs-sockets.html" title="Chapter 20. Plugs and Sockets">
</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">RecentChooser</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="chapter-recent-documents.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 19. Recently Used Documents</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-plugs-sockets.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="RecentChooser">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-recentchooser"></a>RecentChooser</h2></div></div></div>
<div class="note" title="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>Recent Files support is available in <span class="application">gtkmm</span> version 2.10 and
        later</p></td></tr>
</table></div>
<p>
      <code class="classname">RecentChooser</code> is an interface that can be
      implemented by widgets displaying the list of recently used files.
      <span class="application">gtkmm</span> provides three built-in implementations for choosing recent files:
      <code class="classname">RecentChooserWidget</code>,
      <code class="classname">RecentChooserDialog</code>, and
      <code class="classname">RecentChooserMenu</code>.
    </p>
<p>
      <code class="classname">RecentChooserWidget</code> is a simple widget for
      displaying a list of recently used files.
      <code class="classname">RecentChooserWidget</code> is the basic building block for
      <code class="classname">RecentChooserDialog</code>, but you can embed it into your
      user interface if you want to.
    </p>
<p>
      The last class that implements the <code class="classname">RecentChooser</code>
      interface is <code class="classname">RecentChooserMenu</code>.  This class allows
      you to list recently used files as a menu.
    </p>
<div class="sect2" title="Simple RecentChooserWidget example">
<div class="titlepage"><div><div><h3 class="title">
<a name="recenchooserwidget-example"></a>Simple RecentChooserWidget example</h3></div></div></div>
<p>
        Shown below is a simple example of how to use the
        <code class="classname">RecentChooserDialog</code> class in a program.  This
        simple program has a menubar with a "Recent Files Dialog" menu item.
        When you select this menu item, a dialog pops up showing the list of
        recently used files.
      </p>
<div class="note" title="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>
          If this is the first time you're using a program that uses the Recent
          Files framework, the dialog may be empty at first.  Otherwise it
          should show the list of recently used documents registered by other
          applications.
        </p></td></tr>
</table></div>
<p>
        After selecting the <span class="guimenuitem">Recent Files Dialog</span> menu
        item, you should see something similar to the following window.
      </p>
<div class="screenshot"><div><img src="figures/recentchooserdialog.png"></div></div>
<p><a class="ulink" href="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/recent_files" 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_recent_files_dialog();
  void on_menu_file_quit();
  void on_menu_file_new();

  //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::RecentManager&gt; m_refRecentManager;
};

#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_refRecentManager(Gtk::RecentManager::get_default())
{
  set_title("recent files example");
  set_default_size(200, 200);

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

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

  //File menu:
  m_refActionGroup-&gt;add( Gtk::Action::create("FileMenu", "_File") );
  m_refActionGroup-&gt;add( Gtk::Action::create("FileNew", Gtk::Stock::NEW),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_new));

  /* A recent-files sub-menu: */
  //TODO: Shouldn't this have a default constructor?: 
  //See bug #450032.
  //m_refActionGroup-&gt;add( Gtk::RecentAction::create() );
  m_refActionGroup-&gt;add( Gtk::RecentAction::create("FileRecentFiles",
              "_Recent Files"));

  /* A menu item to open the recent-files dialog: */
  m_refActionGroup-&gt;add( Gtk::Action::create("FileRecentDialog",
              "Recent Files _Dialog"), sigc::mem_fun(*this,
                  &amp;ExampleWindow::on_menu_file_recent_files_dialog) );

  m_refActionGroup-&gt;add( Gtk::Action::create("FileQuit", Gtk::Stock::QUIT),
          sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_quit) );


  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;menuitem action='FileNew'/&gt;"
        "      &lt;menuitem action='FileRecentFiles'/&gt;"
        "      &lt;menuitem action='FileRecentDialog'/&gt;"
        "      &lt;separator/&gt;"
        "      &lt;menuitem action='FileQuit'/&gt;"
        "    &lt;/menu&gt;"
        "  &lt;/menubar&gt;"
        "  &lt;toolbar  name='ToolBar'&gt;"
        "    &lt;toolitem action='FileNew'/&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_new()
{
    std::cout &lt;&lt; " New File" &lt;&lt; std::endl;
}

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

void ExampleWindow::on_menu_file_recent_files_dialog()
{
  Gtk::RecentChooserDialog dialog(*this, "Recent Files", m_refRecentManager);
  dialog.add_button("Select File", Gtk::RESPONSE_OK);
  dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);

  const int response = dialog.run();
  dialog.hide();
  if(response == Gtk::RESPONSE_OK)
  {
     std::cout &lt;&lt; "URI selected = " &lt;&lt; dialog.get_current_uri() &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>
<p>
        The constructor for <code class="classname">ExampleWindow</code> creates the
        menu using <code class="classname">UIManager</code> (see <a class="xref" href="chapter-menus-and-toolbars.html" title="Chapter 11. Menus and Toolbars">Chapter 11, <i>Menus and Toolbars</i></a> for more information).  It then adds
        the menu and the toolbar to the window.
      </p>
</div>
<div class="sect2" title="Filtering Recent Files">
<div class="titlepage"><div><div><h3 class="title">
<a name="recent-files-filtering"></a>Filtering Recent Files</h3></div></div></div>
<p>
        For any of the <code class="classname">RecentChooser</code> classes, if you
        don't wish to display all of the items in the list of recent files, you
        can filter the list to show only those that you want.  You can filter
        the list with the help of the <code class="classname">RecentFilter</code> class.
        This class allows you to filter recent files by their name
        (<code class="methodname">add_pattern()</code>), their mime type
        (<code class="methodname">add_mime_type()</code>), the application that registered
        them (<code class="methodname">add_application()</code>), or by a custom filter
        function (<code class="methodname">add_custom()</code>).  It also provides the
        ability to filter based on how long ago the file was modified and which
        groups it belongs to.
      </p>
<p>
        After you've created and set up the filter to match only the items you
        want, you can apply a filter to a chooser widget with the
        <code class="methodname">RecentChooser::add_filter()</code> function.
      </p>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="chapter-recent-documents.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-recent-documents.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-plugs-sockets.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 19. Recently Used Documents </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 20. Plugs and Sockets</td>
</tr>
</table>
</div>
</body>
</html>