Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > e40b0b2b839eccdb3c85993a7b738115 > files > 216

gtkmm-documentation-3.24.0-1.mga7.noarch.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>RecentChooser</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="chapter-recent-documents.html" title="Chapter 21. Recently Used Documents">
<link rel="prev" href="chapter-recent-documents.html" title="Chapter 21. Recently Used Documents">
<link rel="next" href="chapter-plugs-sockets.html" title="Chapter 22. 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 21. 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">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-recentchooser"></a>RecentChooser</h2></div></div></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 four built-in implementations for choosing recent files:
      <code class="classname">RecentChooserWidget</code>,
      <code class="classname">RecentChooserDialog</code>,
      <code class="classname">RecentChooserMenu</code>, and the deprecated
      <code class="classname">RecentAction</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>
      <code class="classname">RecentChooserMenu</code> allows you to list recently used
      files as a menu.
    </p>
<div class="sect2">
<div class="titlepage"><div><div><h3 class="title">
<a name="recentchooserdialog-example"></a>Simple RecentChooserDialog 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
        <span class="guimenuitem">Recent Files Dialog</span> menu item.
        When you select this menu item, a dialog pops up showing the list of
        recently used files.
      </p>
<div class="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/browse/gtkmm-documentation/tree/examples/book/recent_files?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 &lt;gtkmm.h&gt;

class ExampleWindow : public Gtk::Window
{
public:
  ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; app);
  virtual ~ExampleWindow();

protected:
  //Signal handlers:
  void on_menu_file_recent_files_item();
  void on_menu_file_recent_files_dialog();
  void on_menu_file_quit();
  void on_menu_file_new();

  //Child widgets:
  Gtk::Box m_Box;

  Glib::RefPtr&lt;Gtk::Builder&gt; m_refBuilder;
  Glib::RefPtr&lt;Gio::SimpleActionGroup&gt; m_refActionGroup;

  Glib::RefPtr&lt;Gtk::RecentManager&gt; m_refRecentManager;
};

#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 &lt;gtkmm/application.h&gt;

int main(int argc, char *argv[])
{
  auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  ExampleWindow window(app);

  //Shows the window and returns when it is closed.
  return app-&gt;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 &lt;iostream&gt;

ExampleWindow::ExampleWindow(const Glib::RefPtr&lt;Gtk::Application&gt;&amp; app)
: m_Box(Gtk::ORIENTATION_VERTICAL),
  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 = Gio::SimpleActionGroup::create();

  //File menu:
  m_refActionGroup-&gt;add_action("new",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_new));

  //A menu item to open the recent-files dialog:
  m_refActionGroup-&gt;add_action("recent-files-dialog",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_recent_files_dialog) );

  m_refActionGroup-&gt;add_action("quit",
    sigc::mem_fun(*this, &amp;ExampleWindow::on_menu_file_quit) );

  insert_action_group("example", m_refActionGroup);


  m_refBuilder = Gtk::Builder::create();

  // When the menubar is a child of a Gtk::Window, keyboard accelerators are not
  // automatically fetched from the Gio::Menu.
  // See the examples/book/menus/main_menu example for an alternative way of
  // adding the menubar when using Gtk::ApplicationWindow.
  app-&gt;set_accel_for_action("example.new", "&lt;Primary&gt;n");
  app-&gt;set_accel_for_action("example.recent-files-dialog", "&lt;Primary&gt;o");
  app-&gt;set_accel_for_action("example.quit", "&lt;Primary&gt;q");

  //Layout the actions in a menubar and a toolbar:
  const char* ui_info =
    "&lt;interface&gt;"
    "  &lt;menu id='menubar'&gt;"
    "    &lt;submenu&gt;"
    "      &lt;attribute name='label' translatable='yes'&gt;_File&lt;/attribute&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;_New&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.new&lt;/attribute&gt;"
    "        &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;n&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;Recent Files _Dialog&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.recent-files-dialog&lt;/attribute&gt;"
    "        &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;o&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "      &lt;item&gt;"
    "        &lt;attribute name='label' translatable='yes'&gt;_Quit&lt;/attribute&gt;"
    "        &lt;attribute name='action'&gt;example.quit&lt;/attribute&gt;"
    "        &lt;attribute name='accel'&gt;&amp;lt;Primary&amp;gt;q&lt;/attribute&gt;"
    "      &lt;/item&gt;"
    "    &lt;/submenu&gt;"
    "  &lt;/menu&gt;"
    "  &lt;object class='GtkToolbar' id='toolbar'&gt;"
    "    &lt;property name='visible'&gt;True&lt;/property&gt;"
    "    &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "    &lt;child&gt;"
    "      &lt;object class='GtkToolButton' id='toolbutton_new'&gt;"
    "        &lt;property name='visible'&gt;True&lt;/property&gt;"
    "        &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "        &lt;property name='tooltip_text' translatable='yes'&gt;New&lt;/property&gt;"
    "        &lt;property name='action_name'&gt;example.new&lt;/property&gt;"
    "        &lt;property name='icon_name'&gt;document-new&lt;/property&gt;"
    "      &lt;/object&gt;"
    "      &lt;packing&gt;"
    "        &lt;property name='expand'&gt;False&lt;/property&gt;"
    "        &lt;property name='homogeneous'&gt;True&lt;/property&gt;"
    "      &lt;/packing&gt;"
    "    &lt;/child&gt;"
    "    &lt;child&gt;"
    "      &lt;object class='GtkToolButton' id='toolbutton_quit'&gt;"
    "        &lt;property name='visible'&gt;True&lt;/property&gt;"
    "        &lt;property name='can_focus'&gt;False&lt;/property&gt;"
    "        &lt;property name='tooltip_text' translatable='yes'&gt;Quit&lt;/property&gt;"
    "        &lt;property name='action_name'&gt;example.quit&lt;/property&gt;"
    "        &lt;property name='icon_name'&gt;application-exit&lt;/property&gt;"
    "      &lt;/object&gt;"
    "      &lt;packing&gt;"
    "        &lt;property name='expand'&gt;False&lt;/property&gt;"
    "        &lt;property name='homogeneous'&gt;True&lt;/property&gt;"
    "      &lt;/packing&gt;"
    "    &lt;/child&gt;"
    "  &lt;/object&gt;"
    "&lt;/interface&gt;";

  try
  {
    m_refBuilder-&gt;add_from_string(ui_info);
  }
  catch(const Glib::Error&amp; ex)
  {
    std::cerr &lt;&lt; "building menubar and toolbar failed: " &lt;&lt;  ex.what();
  }

  //Get the menubar and toolbar widgets, and add them to a container widget:
  auto object = m_refBuilder-&gt;get_object("menubar");
  auto gmenu = Glib::RefPtr&lt;Gio::Menu&gt;::cast_dynamic(object);
  if (gmenu)
  {
    //Menubar:
    auto pMenubar = Gtk::make_managed&lt;Gtk::MenuBar&gt;(gmenu);
    m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK);
  }
  else
    g_warning("GMenu not found");

  Gtk::Toolbar* pToolbar = nullptr;
  m_refBuilder-&gt;get_widget("toolbar", pToolbar);
  if (pToolbar)
    //Toolbar:
    m_Box.pack_start(*pToolbar, Gtk::PACK_SHRINK);
  else
    g_warning("GtkToolbar not found");

  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 app-&gt;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("_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>
        The constructor for <code class="classname">ExampleWindow</code> creates the
        menu and the toolbar using <code class="classname">Builder</code> (see <a class="xref" href="chapter-menus-and-toolbars.html" title="Chapter 12. Menus and Toolbars">Chapter 12, <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">
<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 21. 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 22. Plugs and Sockets</td>
</tr>
</table>
</div>
</body>
</html>