| Note | |
|---|---|
最近使ったファイル機能はgtkmmバージョン2.10以降でサポートされています。 |
RecentChooserはインターフェイスであり、この上に最近使ったファイルのリストを表示するウィジットを実装できます。gtkmmには最近使ったファイルを選ぶための組込みの実装が3つ提供されています:
RecentChooserWidget,
RecentChooserDialog,
RecentChooserMenu です。
RecentChooserWidgetは最近使ったファイルのリストを表示するシンプルなウィジットです。
RecentChooserWidgetは
RecentChooserDialogの基本的な部品ではありますが、アプリケーションのUIに組み込むこともできます。
最後のクラスはRecentChooserインターフェイスを実装したRecentChooserMenuです。これは最近使ったファイルのリストをメニューにします。
以下の例ではどのようにRecentChooserDialogクラスを使うかという単純な例です。このプログラムはメニューバーに「最近使ったファイルのダイアログ」というメニューアイテムを持ち、このアイテムを選ぶと、ダイアログがポップアップして最近使ったファイルのリストを表示します。
| Note | |
|---|---|
このプログラムが最近使ったファイル機能の使う最初のものであったなら、ダイアログは空っぽで表示されるかもしれません。そうでないなら、ダイアログは他のアプリケーションから登録された、最近使ったドキュメントのリストを表示します。 |
メニューアイテムを選択すると、次のものと似たウィンドウが表示されるはずです

File: examplewindow.h
#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
File: main.cc
#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;
}
File: examplewindow.cc
#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;
}
}
ExampleWindowはコンストラクタでメニューを生成し、そこでUIManagerを使っています(詳しい情報はChapter 11, メニューとツールバーを見てください)。それから、ウィンドウにメニューとツールバーを加えています。
RecentChooserのクラスにおいて最近使ったファイルのリストを全て表示したくない場合、望ましいものだけを表示できるようにリストをフィルタリングできます。これはRecentFilterクラスを使うことで行えます。このクラスで出来るのは、ファイルの名前でフィルタ(add_pattern())、MIMEタイプでフィルタ(add_mime_type())、登録されたアプリケーションでフィルタ(add_application())、またはカスタムフィルタ関数でのフィルタ(add_custom()です。さらに、このクラスはファイルの変更から経過した時間や、そのファイルが所属するグループでフィルタする機能を提供しています。
望ましいアイテムにマッチするフィルタを生成、設定したら、これをRecentChooser::add_filter()でウィジットに適用してください。