Use GtkRecentStuff for recent files menu

master
Yevgen Muntyan 2007-11-30 02:19:12 -06:00
parent 9eb42ece50
commit 00b5abde0f
4 changed files with 184 additions and 27 deletions

View File

@ -69,7 +69,12 @@ static WindowInfo *window_list_find_file (MooEditor *editor,
static void set_single_window (MooEditor *editor, static void set_single_window (MooEditor *editor,
gboolean single); gboolean single);
#if !GTK_CHECK_VERSION(2,12,0)
static GtkAction *create_recent_action (MooEditWindow *window); static GtkAction *create_recent_action (MooEditWindow *window);
static void activate_history_item (MooEditor *editor,
MooHistoryItem *item,
MooEditWindow *window);
#endif
static MooEditWindow *create_window (MooEditor *editor); static MooEditWindow *create_window (MooEditor *editor);
static void moo_editor_add_doc (MooEditor *editor, static void moo_editor_add_doc (MooEditor *editor,
@ -87,10 +92,6 @@ static gboolean close_docs_real (MooEditor *editor,
gboolean ask_confirm); gboolean ask_confirm);
static GSList *find_modified (GSList *docs); static GSList *find_modified (GSList *docs);
static void activate_history_item (MooEditor *editor,
MooHistoryItem *item,
MooEditWindow *window);
static void add_new_window_action (void); static void add_new_window_action (void);
static void remove_new_window_action (void); static void remove_new_window_action (void);
@ -111,7 +112,9 @@ struct _MooEditorPrivate {
MooUIXML *doc_ui_xml; MooUIXML *doc_ui_xml;
MooUIXML *ui_xml; MooUIXML *ui_xml;
MooFilterMgr *filter_mgr; MooFilterMgr *filter_mgr;
#if !GTK_CHECK_VERSION(2,12,0)
MooHistoryList *history; MooHistoryList *history;
#endif
MooLangMgr *lang_mgr; MooLangMgr *lang_mgr;
MooFileWatch *file_watch; MooFileWatch *file_watch;
gboolean open_single; gboolean open_single;
@ -174,7 +177,7 @@ static void
moo_editor_class_init (MooEditorClass *klass) moo_editor_class_init (MooEditorClass *klass)
{ {
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
MooWindowClass *edit_window_class; G_GNUC_UNUSED MooWindowClass *edit_window_class;
gobject_class->finalize = moo_editor_finalize; gobject_class->finalize = moo_editor_finalize;
gobject_class->set_property = moo_editor_set_property; gobject_class->set_property = moo_editor_set_property;
@ -271,11 +274,13 @@ moo_editor_class_init (MooEditorClass *klass)
MOO_TYPE_EDIT_WINDOW, MOO_TYPE_EDIT_WINDOW,
G_TYPE_BOOLEAN); G_TYPE_BOOLEAN);
#if !GTK_CHECK_VERSION(2,12,0)
edit_window_class = g_type_class_ref (MOO_TYPE_EDIT_WINDOW); edit_window_class = g_type_class_ref (MOO_TYPE_EDIT_WINDOW);
moo_window_class_new_action_custom (edit_window_class, RECENT_ACTION_ID, NULL, moo_window_class_new_action_custom (edit_window_class, RECENT_ACTION_ID, NULL,
(MooWindowActionFunc) create_recent_action, (MooWindowActionFunc) create_recent_action,
NULL, NULL); NULL, NULL);
g_type_class_unref (edit_window_class); g_type_class_unref (edit_window_class);
#endif
add_new_window_action (); add_new_window_action ();
} }
@ -299,6 +304,7 @@ moo_editor_init (MooEditor *editor)
editor->priv->filter_mgr = moo_filter_mgr_new (); editor->priv->filter_mgr = moo_filter_mgr_new ();
#if !GTK_CHECK_VERSION(2,12,0)
editor->priv->history = moo_history_list_get ("Editor"); editor->priv->history = moo_history_list_get ("Editor");
g_object_ref (editor->priv->history); g_object_ref (editor->priv->history);
moo_history_list_set_display_func (editor->priv->history, moo_history_list_set_display_func (editor->priv->history,
@ -309,6 +315,7 @@ moo_editor_init (MooEditor *editor)
NULL); NULL);
g_signal_connect_swapped (editor->priv->history, "activate-item", g_signal_connect_swapped (editor->priv->history, "activate-item",
G_CALLBACK (activate_history_item), editor); G_CALLBACK (activate_history_item), editor);
#endif
editor->priv->windows = NULL; editor->priv->windows = NULL;
editor->priv->windowless = window_info_new (NULL); editor->priv->windowless = window_info_new (NULL);
@ -444,8 +451,10 @@ moo_editor_finalize (GObject *object)
g_object_unref (editor->priv->ui_xml); g_object_unref (editor->priv->ui_xml);
if (editor->priv->filter_mgr) if (editor->priv->filter_mgr)
g_object_unref (editor->priv->filter_mgr); g_object_unref (editor->priv->filter_mgr);
#if !GTK_CHECK_VERSION(2,12,0)
if (editor->priv->history) if (editor->priv->history)
g_object_unref (editor->priv->history); g_object_unref (editor->priv->history);
#endif
g_object_unref (editor->priv->lang_mgr); g_object_unref (editor->priv->lang_mgr);
g_object_unref (editor->priv->doc_ui_xml); g_object_unref (editor->priv->doc_ui_xml);
@ -721,14 +730,6 @@ moo_editor_get_filter_mgr (MooEditor *editor)
} }
MooHistoryList *
moo_editor_get_history (MooEditor *editor)
{
g_return_val_if_fail (MOO_IS_EDITOR (editor), NULL);
return editor->priv->history;
}
MooUIXML * MooUIXML *
moo_editor_get_ui_xml (MooEditor *editor) moo_editor_get_ui_xml (MooEditor *editor)
{ {
@ -963,16 +964,43 @@ window_list_find_file (MooEditor *editor,
} }
#if GTK_CHECK_VERSION(2,12,0)
static void
add_recent_file (G_GNUC_UNUSED MooEditor *editor,
const char *filename)
{
char *uri;
uri = g_filename_to_uri (filename, NULL, NULL);
g_return_if_fail (uri != NULL);
gtk_recent_manager_add_item (gtk_recent_manager_get_default (), uri);
g_free (uri);
}
#else
static void
add_recent_file (MooEditor *editor,
const char *filename)
{
moo_history_list_add_filename (editor->priv->history, filename);
}
#endif
#if !GTK_CHECK_VERSION(2,12,0)
static GtkAction* static GtkAction*
create_recent_action (MooEditWindow *window) create_recent_action (MooEditWindow *window)
{ {
MooEditor *editor = moo_edit_window_get_editor (window); MooEditor *editor = moo_edit_window_get_editor (window);
GtkAction *action; GtkAction *action;
MooMenuMgr *mgr; G_GNUC_UNUSED MooMenuMgr *mgr;
g_return_val_if_fail (editor != NULL, NULL); g_return_val_if_fail (editor != NULL, NULL);
action = moo_menu_action_new (RECENT_ACTION_ID, _("Open Recent")); action = moo_menu_action_new (RECENT_ACTION_ID, _("Open Recent"));
mgr = moo_history_list_get_menu_mgr (editor->priv->history); mgr = moo_history_list_get_menu_mgr (editor->priv->history);
moo_menu_mgr_set_show_tooltips (mgr, TRUE); moo_menu_mgr_set_show_tooltips (mgr, TRUE);
moo_menu_action_set_mgr (MOO_MENU_ACTION (action), mgr); moo_menu_action_set_mgr (MOO_MENU_ACTION (action), mgr);
@ -984,7 +1012,6 @@ create_recent_action (MooEditWindow *window)
return action; return action;
} }
static void static void
activate_history_item (MooEditor *editor, activate_history_item (MooEditor *editor,
MooHistoryItem *item, MooHistoryItem *item,
@ -1003,6 +1030,8 @@ activate_history_item (MooEditor *editor,
moo_history_list_remove (editor->priv->history, item->data); moo_history_list_remove (editor->priv->history, item->data);
} }
#endif /* !GTK_CHECK_VERSION(2,12,0) */
/*****************************************************************************/ /*****************************************************************************/
@ -1203,7 +1232,7 @@ moo_editor_load_file (MooEditor *editor,
if (window_list_find_file (editor, filename, docp)) if (window_list_find_file (editor, filename, docp))
{ {
if (add_history) if (add_history)
moo_history_list_add_filename (editor->priv->history, filename); add_recent_file (editor, filename);
g_free (filename); g_free (filename);
return FALSE; return FALSE;
} }
@ -1253,7 +1282,7 @@ moo_editor_load_file (MooEditor *editor,
} }
if (add_history) if (add_history)
moo_history_list_add_filename (editor->priv->history, filename); add_recent_file (editor, filename);
} }
if (result) if (result)
@ -2330,7 +2359,7 @@ _moo_editor_save (MooEditor *editor,
goto out; goto out;
} }
moo_history_list_add_filename (editor->priv->history, filename); add_recent_file (editor, filename);
result = TRUE; result = TRUE;
/* fall through */ /* fall through */
@ -2399,7 +2428,7 @@ _moo_editor_save_as (MooEditor *editor,
goto out; goto out;
} }
moo_history_list_add_filename (editor->priv->history, file_info->filename); add_recent_file (editor, file_info->filename);
result = TRUE; result = TRUE;
/* fall through */ /* fall through */

View File

@ -123,7 +123,6 @@ void moo_editor_set_app_name (MooEditor *editor,
const char *name); const char *name);
const char *moo_editor_get_app_name (MooEditor *editor); const char *moo_editor_get_app_name (MooEditor *editor);
MooHistoryList *moo_editor_get_history (MooEditor *editor);
MooFilterMgr *moo_editor_get_filter_mgr (MooEditor *editor); MooFilterMgr *moo_editor_get_filter_mgr (MooEditor *editor);
MooUIXML *moo_editor_get_ui_xml (MooEditor *editor); MooUIXML *moo_editor_get_ui_xml (MooEditor *editor);

View File

@ -226,9 +226,13 @@ static gboolean notebook_drag_motion (GtkWidget *widget,
/* actions */ /* actions */
static void action_new_doc (MooEditWindow *window); static void action_new_doc (MooEditWindow *window);
static void action_open (MooEditWindow *window); static void action_open (MooEditWindow *window);
#if GTK_CHECK_VERSION(2,12,0)
static void action_open_recent_dialog (MooEditWindow *window);
static GtkAction *create_open_recent_action (MooEditWindow *window);
#endif
static void action_reload (MooEditWindow *window); static void action_reload (MooEditWindow *window);
static GtkAction *create_reopen_with_encoding_action (MooEditWindow *window); static GtkAction *create_reopen_with_encoding_action (MooEditWindow *window);
static GtkAction *create_doc_encoding_action (MooEditWindow *window); static GtkAction *create_doc_encoding_action (MooEditWindow *window);
static void action_save (MooEditWindow *window); static void action_save (MooEditWindow *window);
static void action_save_as (MooEditWindow *window); static void action_save_as (MooEditWindow *window);
static void action_close_tab (MooEditWindow *window); static void action_close_tab (MooEditWindow *window);
@ -400,6 +404,23 @@ moo_edit_window_class_init (MooEditWindowClass *klass)
"closure-callback", action_open, "closure-callback", action_open,
NULL); NULL);
#if GTK_CHECK_VERSION(2,12,0)
moo_window_class_new_action_custom (window_class, "OpenRecent", NULL,
(MooWindowActionFunc) create_open_recent_action,
NULL, NULL);
moo_window_class_new_action (window_class, "OpenRecentDialog", NULL,
/* name of the More... menu item from the Open Recent menu,
* as shown in the Configure Shortcuts dialog */
"display-name", _("Open Recent Files Chooser"),
/* label of the More... menu item in the Open Recent menu,
* do not translate the part before | */
"label", Q_("Open Recent|_More..."),
"accel", "<shift><ctrl>O",
"closure-callback", action_open_recent_dialog,
NULL);
#endif
moo_window_class_new_action (window_class, "Reload", NULL, moo_window_class_new_action (window_class, "Reload", NULL,
"display-name", _("Reload"), "display-name", _("Reload"),
"label", _("_Reload"), "label", _("_Reload"),
@ -1298,6 +1319,120 @@ moo_edit_window_close (MooWindow *window)
} }
/****************************************************************************/
/* Recent files
*/
#if GTK_CHECK_VERSION(2,12,0)
static void
recent_item_activated (GtkRecentChooser *chooser,
MooEditWindow *window)
{
char *uri;
uri = gtk_recent_chooser_get_current_uri (chooser);
g_return_if_fail (uri != NULL);
moo_editor_open_uri (window->priv->editor, window, NULL, uri, NULL);
g_free (uri);
}
static void
action_open_recent_dialog (MooEditWindow *window)
{
GtkWidget *dialog;
int response;
GtkRecentFilter *filter;
dialog = gtk_recent_chooser_dialog_new ("Choose File",
GTK_WINDOW (window),
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
gtk_recent_chooser_set_show_not_found (GTK_RECENT_CHOOSER (dialog), FALSE);
gtk_recent_chooser_set_show_tips (GTK_RECENT_CHOOSER (dialog), TRUE);
gtk_recent_chooser_set_local_only (GTK_RECENT_CHOOSER (dialog), TRUE);
gtk_recent_chooser_set_limit (GTK_RECENT_CHOOSER (dialog), -1);
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (dialog), GTK_RECENT_SORT_MRU);
filter = gtk_recent_filter_new ();
gtk_recent_filter_add_pattern (filter, "*");
gtk_recent_filter_set_name (filter, "All Files");
gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
filter = gtk_recent_filter_new ();
gtk_recent_filter_add_mime_type (filter, "text/*");
gtk_recent_filter_set_name (filter, "Text Files");
gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (dialog), filter);
gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (dialog), filter);
response = gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_hide (dialog);
if (response == GTK_RESPONSE_OK)
recent_item_activated (GTK_RECENT_CHOOSER (dialog), window);
gtk_widget_destroy (dialog);
}
static GtkWidget *
create_recent_menu (GtkAction *action)
{
GtkWidget *menu, *item;
GtkRecentManager *manager;
GtkRecentFilter *filter;
GtkAction *action_more;
MooWindow *window;
window = _moo_action_get_window (action);
g_return_val_if_fail (MOO_IS_EDIT_WINDOW (window), NULL);
manager = gtk_recent_manager_get_default ();
menu = gtk_recent_chooser_menu_new_for_manager (manager);
g_signal_connect (menu, "item-activated",
G_CALLBACK (recent_item_activated),
window);
filter = gtk_recent_filter_new ();
gtk_recent_filter_add_mime_type (filter, "text/*");
gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (menu), filter);
gtk_recent_chooser_set_local_only (GTK_RECENT_CHOOSER (menu), TRUE);
gtk_recent_chooser_set_show_not_found (GTK_RECENT_CHOOSER (menu), FALSE);
gtk_recent_chooser_set_show_tips (GTK_RECENT_CHOOSER (menu), TRUE);
gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (menu), GTK_RECENT_SORT_MRU);
item = gtk_separator_menu_item_new ();
gtk_widget_show (item);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
action_more = moo_window_get_action (window, "OpenRecentDialog");
item = gtk_action_create_menu_item (action_more);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
item = gtk_menu_item_new ();
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
return item;
}
static GtkAction*
create_open_recent_action (G_GNUC_UNUSED MooEditWindow *window)
{
GtkAction *action;
action = moo_menu_action_new ("OpenRecent", _("Open Recent"));
moo_menu_action_set_func (MOO_MENU_ACTION (action), create_recent_menu);
return action;
}
#endif /* GTK_CHECK_VERSION(2,12,0) */
/****************************************************************************/ /****************************************************************************/
/* Actions /* Actions
*/ */

View File

@ -221,12 +221,6 @@
) )
) )
(define-method get_history
(of-object "MooEditor")
(c-name "moo_editor_get_history")
(return-type "MooHistoryList*")
)
(define-method get_filter_mgr (define-method get_filter_mgr
(of-object "MooEditor") (of-object "MooEditor")
(c-name "moo_editor_get_filter_mgr") (c-name "moo_editor_get_filter_mgr")