RecentChooser

Recent Files support is available in gtkmm version 2.10 and later

RecentChooser is an interface that can be implemented by widgets displaying the list of recently used files. gtkmm provides three built-in implementations for choosing recent files: RecentChooserWidget, RecentChooserDialog, and RecentChooserMenu.

RecentChooserWidget is a simple widget for displaying a list of recently used files. RecentChooserWidget is the basic building block for RecentChooserDialog, but you can embed it into your user interface if you want to.

The last class that implements the RecentChooser interface is RecentChooserMenu. This class allows you to list recently used files as a menu.

20.2.1. Simple RecentChooserWidget example

Shown below is a simple example of how to use the RecentChooserDialog 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.

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.

After selecting the Recent Files Dialog menu item, you should see something similar to the following window.

Source Code

File: examplewindow.h (For use with gtkmm 2, not gtkmm 3)

#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H

#include <gtkmm.h>

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<Gtk::UIManager> m_refUIManager;
  Glib::RefPtr<Gtk::ActionGroup> m_refActionGroup;

  Glib::RefPtr<Gtk::RecentManager> m_refRecentManager;
};

#endif //GTKMM_EXAMPLEWINDOW_H

File: examplewindow.cc (For use with gtkmm 2, not gtkmm 3)

#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>";

  try
  {
    m_refUIManager->add_ui_from_string(ui_info);
  }
  catch(const Glib::Error& ex)
  {
    std::cerr << "building menus failed: " <<  ex.what();
  }

  //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;
  }
}

File: main.cc (For use with gtkmm 2, not gtkmm 3)

#include "examplewindow.h"
#include <gtkmm/main.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;
}

The constructor for ExampleWindow creates the menu using UIManager (see Chapter 11 ― Menus and Toolbars for more information). It then adds the menu and the toolbar to the window.

20.2.2. Filtering Recent Files

For any of the RecentChooser 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 RecentFilter class. This class allows you to filter recent files by their name (add_pattern()), their mime type (add_mime_type()), the application that registered them (add_application()), or by a custom filter function (add_custom()). It also provides the ability to filter based on how long ago the file was modified and which groups it belongs to.

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 RecentChooser::add_filter() function.