<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.73.2"> <link rel="start" href="index.html" title="Programming with gtkmm"> <link rel="up" href="ch19.html" title="Chapter 19. Recently Used Documents"> <link rel="prev" href="ch19.html" title="Chapter 19. Recently Used Documents"> <link rel="next" href="ch20.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="ch19.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="ch20.html"><img src="../icons/next.png" alt="Next"></a> </td> </tr> </table> <hr> </div> <div class="sect1" lang="en"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="id2589808"></a>RecentChooser</h2></div></div></div> <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>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" lang="en"> <div class="titlepage"><div><div><h3 class="title"> <a name="id2589886"></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" 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="../../../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 <gtkmm.h> class ExampleWindow : public Gtk::Window { public: ExampleWindow(); virtual ~ExampleWindow(); protected: //Signal handlers: virtual void on_menu_file_recent_files_dialog(); virtual void on_menu_file_quit(); virtual void on_menu_file_new(); //Child widgets: Gtk::VBox m_Box; Glib::RefPtr<Gtk::UIManager> m_refUIManager; Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup; Glib::RefPtr<Gtk::RecentManager> m_refRecentManager; }; #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/stock.h> #include <iostream> 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->add( Gtk::Action::create("FileMenu", "_File") ); m_refActionGroup->add( Gtk::Action::create("FileNew", Gtk::Stock::NEW), sigc::mem_fun(*this, &ExampleWindow::on_menu_file_new)); /* A recent-files sub-menu: */ //TODO: Shouldn't this have a default constructor?: //See bug #450032. //m_refActionGroup->add( Gtk::RecentAction::create() ); m_refActionGroup->add( Gtk::RecentAction::create("FileRecentFiles", "_Recent Files")); /* A menu item to open the recent-files dialog: */ m_refActionGroup->add( Gtk::Action::create("FileRecentDialog", "Recent Files _Dialog"), sigc::mem_fun(*this, &ExampleWindow::on_menu_file_recent_files_dialog) ); m_refActionGroup->add( Gtk::Action::create("FileQuit", Gtk::Stock::QUIT), sigc::mem_fun(*this, &ExampleWindow::on_menu_file_quit) ); m_refUIManager = Gtk::UIManager::create(); m_refUIManager->insert_action_group(m_refActionGroup); add_accel_group(m_refUIManager->get_accel_group()); //Layout the actions in a menubar and toolbar: Glib::ustring ui_info = "<ui>" " <menubar name='MenuBar'>" " <menu action='FileMenu'>" " <menuitem action='FileNew'/>" " <menuitem action='FileRecentFiles'/>" " <menuitem action='FileRecentDialog'/>" " <separator/>" " <menuitem action='FileQuit'/>" " </menu>" " </menubar>" " <toolbar name='ToolBar'>" " <toolitem action='FileNew'/>" " <toolitem action='FileQuit'/>" " </toolbar>" "</ui>"; #ifdef GLIBMM_EXCEPTIONS_ENABLED try { m_refUIManager->add_ui_from_string(ui_info); } catch(const Glib::Error& ex) { std::cerr << "building menus failed: " << ex.what(); } #else std::auto_ptr<Glib::Error> ex; m_refUIManager->add_ui_from_string(ui_info, ex); if(ex.get()) std::cerr << "building menus failed: " << ex->what(); #endif //GLIBMM_EXCEPTIONS_ENABLED //Get the menubar and toolbar widgets, and add them to a container widget: Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar"); if(pMenubar) m_Box.pack_start(*pMenubar, Gtk::PACK_SHRINK); Gtk::Widget* pToolbar = m_refUIManager->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 << " New File" << 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 << "URI selected = " << dialog.get_current_uri() << std::endl; } } </pre> <p> The constructor for <code class="classname">ExampleWindow</code> creates the menu using <code class="classname">UIManager</code> (see <a class="xref" href="ch11.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" lang="en"> <div class="titlepage"><div><div><h3 class="title"> <a name="id2590132"></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="function">add_pattern()</code>), their mime type (<code class="function">add_mime_type()</code>), the application that registered them (<code class="function">add_application()</code>), or by a custom filter function (<code class="function">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="function">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="ch19.html"><img src="../icons/prev.png" alt="Prev"></a> </td> <td width="20%" align="center"><a accesskey="u" href="ch19.html"><img src="../icons/up.png" alt="Up"></a></td> <td width="40%" align="right"> <a accesskey="n" href="ch20.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>