Encoding menu in the Document menu

master
Yevgen Muntyan 2007-11-29 19:53:33 -06:00
parent f6387fc5c3
commit e004005558
7 changed files with 267 additions and 144 deletions

View File

@ -55,7 +55,7 @@
<args>--app-name foobar</args> <args>--app-name foobar</args>
<exe>medit/medit</exe> <exe>medit/medit</exe>
<vars> <vars>
<var name="LANGUAGE">ru</var> <var name="LANGUAGEE">ru</var>
<var name="MOO_DEBUG">misc,input</var> <var name="MOO_DEBUG">misc,input</var>
</vars> </vars>
</run> </run>

View File

@ -85,7 +85,7 @@ void _moo_edit_set_filename (MooEdit *edit,
const char *encoding); const char *encoding);
void _moo_edit_set_encoding (MooEdit *edit, void _moo_edit_set_encoding (MooEdit *edit,
const char *encoding); const char *encoding);
char *_moo_edit_get_default_encoding (void); const char *_moo_edit_get_default_encoding (void);
void _moo_edit_stop_file_watch (MooEdit *edit); void _moo_edit_stop_file_watch (MooEdit *edit);

View File

@ -102,7 +102,8 @@ enum {
PROP_0, PROP_0,
PROP_EDITOR, PROP_EDITOR,
PROP_ENABLE_BOOKMARKS, PROP_ENABLE_BOOKMARKS,
PROP_HAS_COMMENTS PROP_HAS_COMMENTS,
PROP_ENCODING
}; };
/* MOO_TYPE_EDIT */ /* MOO_TYPE_EDIT */
@ -160,6 +161,14 @@ moo_edit_class_init (MooEditClass *klass)
FALSE, FALSE,
G_PARAM_READABLE)); G_PARAM_READABLE));
g_object_class_install_property (gobject_class,
PROP_ENCODING,
g_param_spec_string ("encoding",
"encoding",
"encoding",
NULL,
G_PARAM_READWRITE));
signals[CONFIG_NOTIFY] = signals[CONFIG_NOTIFY] =
g_signal_new ("config-notify", g_signal_new ("config-notify",
G_OBJECT_CLASS_TYPE (klass), G_OBJECT_CLASS_TYPE (klass),
@ -540,6 +549,10 @@ moo_edit_set_property (GObject *object,
moo_edit_set_enable_bookmarks (edit, g_value_get_boolean (value)); moo_edit_set_enable_bookmarks (edit, g_value_get_boolean (value));
break; break;
case PROP_ENCODING:
_moo_edit_set_encoding (edit, g_value_get_string (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -569,6 +582,10 @@ moo_edit_get_property (GObject *object,
g_value_set_boolean (value, _moo_edit_has_comments (edit, NULL, NULL)); g_value_set_boolean (value, _moo_edit_has_comments (edit, NULL, NULL));
break; break;
case PROP_ENCODING:
g_value_set_string (value, edit->priv->encoding);
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -674,8 +691,14 @@ _moo_edit_set_encoding (MooEdit *edit,
{ {
g_return_if_fail (MOO_IS_EDIT (edit)); g_return_if_fail (MOO_IS_EDIT (edit));
g_return_if_fail (encoding != NULL); g_return_if_fail (encoding != NULL);
g_free (edit->priv->encoding);
edit->priv->encoding = g_strdup (encoding); if (!_moo_str_equal (encoding, edit->priv->encoding))
{
char *tmp = edit->priv->encoding;
edit->priv->encoding = g_strdup (encoding);
g_free (tmp);
g_object_notify (G_OBJECT (edit), "encoding");
}
} }

View File

@ -1192,10 +1192,10 @@ add_untitled (MooEdit *edit)
} }
char * const char *
_moo_edit_get_default_encoding (void) _moo_edit_get_default_encoding (void)
{ {
return g_strdup (moo_prefs_get_string (moo_edit_setting (MOO_EDIT_PREFS_ENCODING_SAVE))); return moo_prefs_get_string (moo_edit_setting (MOO_EDIT_PREFS_ENCODING_SAVE));
} }
@ -1204,12 +1204,11 @@ _moo_edit_set_filename (MooEdit *edit,
const char *file, const char *file,
const char *encoding) const char *encoding)
{ {
char *tmp1, *tmp3, *tmp4, *tmp5; char *tmp1, *tmp3, *tmp4;
tmp1 = edit->priv->filename; tmp1 = edit->priv->filename;
tmp3 = edit->priv->display_filename; tmp3 = edit->priv->display_filename;
tmp4 = edit->priv->display_basename; tmp4 = edit->priv->display_basename;
tmp5 = edit->priv->encoding;
if (!UNTITLED_NO) if (!UNTITLED_NO)
UNTITLED_NO = g_hash_table_new (g_direct_hash, g_direct_equal); UNTITLED_NO = g_hash_table_new (g_direct_hash, g_direct_equal);
@ -1243,9 +1242,9 @@ _moo_edit_set_filename (MooEdit *edit,
} }
if (!encoding) if (!encoding)
edit->priv->encoding = _moo_edit_get_default_encoding (); _moo_edit_set_encoding (edit, _moo_edit_get_default_encoding ());
else else
edit->priv->encoding = g_strdup (encoding); _moo_edit_set_encoding (edit, encoding);
g_signal_emit_by_name (edit, "filename-changed", edit->priv->filename, NULL); g_signal_emit_by_name (edit, "filename-changed", edit->priv->filename, NULL);
moo_edit_status_changed (edit); moo_edit_status_changed (edit);
@ -1253,7 +1252,6 @@ _moo_edit_set_filename (MooEdit *edit,
g_free (tmp1); g_free (tmp1);
g_free (tmp3); g_free (tmp3);
g_free (tmp4); g_free (tmp4);
g_free (tmp5);
} }

View File

@ -49,7 +49,6 @@
#define ENABLE_BOOKMARKS #define ENABLE_BOOKMARKS
#define ACTIVE_DOC moo_edit_window_get_active_doc #define ACTIVE_DOC moo_edit_window_get_active_doc
#define ACTIVE_PAGE(window) (moo_notebook_get_current_page (window->priv->notebook))
#define LANG_ACTION_ID "LanguageMenu" #define LANG_ACTION_ID "LanguageMenu"
#define STOP_ACTION_ID "StopJob" #define STOP_ACTION_ID "StopJob"
@ -153,6 +152,9 @@ static void edit_changed (MooEditWindow *window,
static void edit_filename_changed (MooEditWindow *window, static void edit_filename_changed (MooEditWindow *window,
const char *filename, const char *filename,
MooEdit *doc); MooEdit *doc);
static void edit_encoding_changed (MooEditWindow *window,
GParamSpec *pspec,
MooEdit *doc);
static void edit_lang_changed (MooEditWindow *window, static void edit_lang_changed (MooEditWindow *window,
guint var_id, guint var_id,
GParamSpec *pspec, GParamSpec *pspec,
@ -226,6 +228,7 @@ static void action_new_doc (MooEditWindow *window);
static void action_open (MooEditWindow *window); static void action_open (MooEditWindow *window);
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 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);
@ -411,6 +414,10 @@ moo_edit_window_class_init (MooEditWindowClass *klass)
(MooWindowActionFunc) create_reopen_with_encoding_action, (MooWindowActionFunc) create_reopen_with_encoding_action,
NULL, NULL); NULL, NULL);
moo_window_class_new_action_custom (window_class, "EncodingMenu", NULL,
(MooWindowActionFunc) create_doc_encoding_action,
NULL, NULL);
moo_window_class_new_action (window_class, "Save", NULL, moo_window_class_new_action (window_class, "Save", NULL,
"display-name", GTK_STOCK_SAVE, "display-name", GTK_STOCK_SAVE,
"label", GTK_STOCK_SAVE, "label", GTK_STOCK_SAVE,
@ -617,12 +624,6 @@ moo_edit_window_class_init (MooEditWindowClass *klass)
"condition::sensitive", "has-open-document", "condition::sensitive", "has-open-document",
NULL); NULL);
moo_window_class_new_action (window_class, "DocumentSubmenu", NULL,
"label", _("_Document"),
"no-accel", TRUE,
"condition::sensitive", "has-open-document",
NULL);
moo_window_class_new_action (window_class, "WrapText", NULL, moo_window_class_new_action (window_class, "WrapText", NULL,
"action-type::", MOO_TYPE_TOGGLE_ACTION, "action-type::", MOO_TYPE_TOGGLE_ACTION,
"display-name", _("Toggle Text Wrapping"), "display-name", _("Toggle Text Wrapping"),
@ -973,7 +974,7 @@ static void moo_edit_window_get_property(GObject *object,
break; break;
case PROP_ACTIVE_DOC: case PROP_ACTIVE_DOC:
g_value_set_object (value, moo_edit_window_get_active_doc (window)); g_value_set_object (value, ACTIVE_DOC (window));
break; break;
case PROP_CAN_RELOAD: case PROP_CAN_RELOAD:
@ -1318,20 +1319,20 @@ action_open (MooEditWindow *window)
static void static void
action_reload (MooEditWindow *window) action_reload (MooEditWindow *window)
{ {
MooEdit *edit = moo_edit_window_get_active_doc (window); MooEdit *edit = ACTIVE_DOC (window);
g_return_if_fail (edit != NULL); g_return_if_fail (edit != NULL);
_moo_editor_reload (window->priv->editor, edit, NULL, NULL); _moo_editor_reload (window->priv->editor, edit, NULL, NULL);
} }
static void static void
encoding_item_activated (const char *encoding, reopen_encoding_item_activated (const char *encoding,
gpointer data) gpointer data)
{ {
MooEditWindow *window = data; MooEditWindow *window = data;
MooEdit *doc; MooEdit *doc;
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
_moo_editor_reload (window->priv->editor, doc, encoding, NULL); _moo_editor_reload (window->priv->editor, doc, encoding, NULL);
@ -1344,7 +1345,7 @@ create_reopen_with_encoding_action (MooEditWindow *window)
action = _moo_encodings_menu_action_new ("ReopenWithEncoding", action = _moo_encodings_menu_action_new ("ReopenWithEncoding",
_("Reopen Using Encoding"), _("Reopen Using Encoding"),
encoding_item_activated, reopen_encoding_item_activated,
window); window);
moo_bind_bool_property (action, "sensitive", moo_bind_bool_property (action, "sensitive",
window, "can-reload", FALSE); window, "can-reload", FALSE);
@ -1353,10 +1354,60 @@ create_reopen_with_encoding_action (MooEditWindow *window)
} }
static void
update_doc_encoding_item (MooEditWindow *window)
{
MooEdit *doc;
GtkAction *action;
const char *enc;
if (!(doc = ACTIVE_DOC (window)))
return;
action = moo_window_get_action (MOO_WINDOW (window), "EncodingMenu");
g_return_if_fail (action != NULL);
enc = moo_edit_get_encoding (doc);
if (!enc)
enc = _moo_edit_get_default_encoding ();
_moo_encodings_menu_action_set_current (action, enc);
}
static void
doc_encoding_item_activated (const char *encoding,
gpointer data)
{
MooEditWindow *window = data;
MooEdit *doc;
doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL);
_moo_edit_set_encoding (doc, encoding);
}
static GtkAction *
create_doc_encoding_action (MooEditWindow *window)
{
GtkAction *action;
action = _moo_encodings_menu_action_new ("EncodingMenu",
_("_Encoding"),
doc_encoding_item_activated,
window);
moo_bind_bool_property (action, "sensitive",
window, "has-open-document", FALSE);
return action;
}
static void static void
action_save (MooEditWindow *window) action_save (MooEditWindow *window)
{ {
MooEdit *edit = moo_edit_window_get_active_doc (window); MooEdit *edit = ACTIVE_DOC (window);
g_return_if_fail (edit != NULL); g_return_if_fail (edit != NULL);
_moo_editor_save (window->priv->editor, edit, NULL); _moo_editor_save (window->priv->editor, edit, NULL);
} }
@ -1365,7 +1416,7 @@ action_save (MooEditWindow *window)
static void static void
action_save_as (MooEditWindow *window) action_save_as (MooEditWindow *window)
{ {
MooEdit *edit = moo_edit_window_get_active_doc (window); MooEdit *edit = ACTIVE_DOC (window);
g_return_if_fail (edit != NULL); g_return_if_fail (edit != NULL);
_moo_editor_save_as (window->priv->editor, edit, NULL, NULL, NULL); _moo_editor_save_as (window->priv->editor, edit, NULL, NULL, NULL);
} }
@ -1374,7 +1425,7 @@ action_save_as (MooEditWindow *window)
static void static void
action_close_tab (MooEditWindow *window) action_close_tab (MooEditWindow *window)
{ {
MooEdit *edit = moo_edit_window_get_active_doc (window); MooEdit *edit = ACTIVE_DOC (window);
g_return_if_fail (edit != NULL); g_return_if_fail (edit != NULL);
moo_editor_close_doc (window->priv->editor, edit, TRUE); moo_editor_close_doc (window->priv->editor, edit, TRUE);
} }
@ -1401,7 +1452,7 @@ switch_to_tab (MooEditWindow *window,
moo_notebook_set_current_page (window->priv->notebook, n); moo_notebook_set_current_page (window->priv->notebook, n);
if ((doc = moo_edit_window_get_active_doc (window))) if ((doc = ACTIVE_DOC (window)))
gtk_widget_grab_focus (GTK_WIDGET (doc)); gtk_widget_grab_focus (GTK_WIDGET (doc));
} }
@ -1447,7 +1498,7 @@ moo_edit_window_find_now (MooEditWindow *window,
{ {
MooEdit *doc; MooEdit *doc;
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
g_signal_emit_by_name (doc, "find-word-at-cursor", forward); g_signal_emit_by_name (doc, "find-word-at-cursor", forward);
@ -1478,7 +1529,7 @@ action_abort_jobs (MooEditWindow *window)
static void static void
action_toggle_bookmark (MooEditWindow *window) action_toggle_bookmark (MooEditWindow *window)
{ {
MooEdit *doc = moo_edit_window_get_active_doc (window); MooEdit *doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
moo_edit_toggle_bookmark (doc, moo_text_view_get_cursor_line (MOO_TEXT_VIEW (doc))); moo_edit_toggle_bookmark (doc, moo_text_view_get_cursor_line (MOO_TEXT_VIEW (doc)));
} }
@ -1489,7 +1540,7 @@ action_next_bookmark (MooEditWindow *window)
{ {
int cursor; int cursor;
GSList *bookmarks; GSList *bookmarks;
MooEdit *doc = moo_edit_window_get_active_doc (window); MooEdit *doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
@ -1509,7 +1560,7 @@ action_prev_bookmark (MooEditWindow *window)
{ {
int cursor; int cursor;
GSList *bookmarks = NULL; GSList *bookmarks = NULL;
MooEdit *doc = moo_edit_window_get_active_doc (window); MooEdit *doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
@ -1539,7 +1590,7 @@ goto_bookmark_activated (GtkAction *action,
window = _moo_action_get_window (action); window = _moo_action_get_window (action);
g_return_if_fail (window != NULL); g_return_if_fail (window != NULL);
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
if ((bk = moo_edit_get_bookmark (doc, n))) if ((bk = moo_edit_get_bookmark (doc, n)))
@ -1633,7 +1684,7 @@ populate_bookmarks (MooEditWindow *window,
GtkWidget *item; GtkWidget *item;
const GSList *bookmarks; const GSList *bookmarks;
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
bookmarks = moo_edit_list_bookmarks (doc); bookmarks = moo_edit_list_bookmarks (doc);
@ -1761,7 +1812,7 @@ create_bookmarks_menu_action (MooWindow *window,
static void static void
action_next_ph (MooEditWindow *window) action_next_ph (MooEditWindow *window)
{ {
MooEdit *doc = moo_edit_window_get_active_doc (window); MooEdit *doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
moo_text_view_next_placeholder (MOO_TEXT_VIEW (doc)); moo_text_view_next_placeholder (MOO_TEXT_VIEW (doc));
} }
@ -1770,7 +1821,7 @@ action_next_ph (MooEditWindow *window)
static void static void
action_prev_ph (MooEditWindow *window) action_prev_ph (MooEditWindow *window)
{ {
MooEdit *doc = moo_edit_window_get_active_doc (window); MooEdit *doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
moo_text_view_prev_placeholder (MOO_TEXT_VIEW (doc)); moo_text_view_prev_placeholder (MOO_TEXT_VIEW (doc));
} }
@ -1788,7 +1839,7 @@ action_page_setup (MooEditWindow *window)
static void static void
action_print (MooEditWindow *window) action_print (MooEditWindow *window)
{ {
gpointer doc = moo_edit_window_get_active_doc (window); gpointer doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
_moo_edit_print (doc, GTK_WIDGET (window)); _moo_edit_print (doc, GTK_WIDGET (window));
} }
@ -1797,7 +1848,7 @@ action_print (MooEditWindow *window)
static void static void
action_print_preview (MooEditWindow *window) action_print_preview (MooEditWindow *window)
{ {
gpointer doc = moo_edit_window_get_active_doc (window); gpointer doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
_moo_edit_print_preview (doc, GTK_WIDGET (window)); _moo_edit_print_preview (doc, GTK_WIDGET (window));
} }
@ -1809,7 +1860,7 @@ action_print_pdf (MooEditWindow *window)
char *start_name; char *start_name;
const char *doc_name, *dot; const char *doc_name, *dot;
const char *filename; const char *filename;
gpointer doc = moo_edit_window_get_active_doc (window); gpointer doc = ACTIVE_DOC (window);
doc_name = doc ? moo_edit_get_display_basename (doc) : "output"; doc_name = doc ? moo_edit_get_display_basename (doc) : "output";
dot = strrchr (doc_name, '.'); dot = strrchr (doc_name, '.');
@ -1825,7 +1876,7 @@ action_print_pdf (MooEditWindow *window)
start_name = g_strdup_printf ("%s.pdf", doc_name); start_name = g_strdup_printf ("%s.pdf", doc_name);
} }
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
filename = moo_file_dialogp (GTK_WIDGET (window), filename = moo_file_dialogp (GTK_WIDGET (window),
@ -1850,7 +1901,7 @@ wrap_text_toggled (MooEditWindow *window,
MooEdit *doc; MooEdit *doc;
GtkWrapMode mode; GtkWrapMode mode;
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
g_object_get (doc, "wrap-mode", &mode, NULL); g_object_get (doc, "wrap-mode", &mode, NULL);
@ -1881,7 +1932,7 @@ line_numbers_toggled (MooEditWindow *window,
MooEdit *doc; MooEdit *doc;
gboolean show; gboolean show;
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
g_return_if_fail (doc != NULL); g_return_if_fail (doc != NULL);
g_object_get (doc, "show-line-numbers", &show, NULL); g_object_get (doc, "show-line-numbers", &show, NULL);
@ -1959,49 +2010,6 @@ notebook_switch_page (G_GNUC_UNUSED MooNotebook *notebook,
} }
static void
doc_encoding_menu_item_activated (const char *encoding,
gpointer data)
{
_moo_edit_set_encoding (data, encoding);
}
static GtkWidget *
create_doc_encoding_menu_item (MooEdit *doc)
{
GtkWidget *item, *menu, *enc_item;
const char *enc, *display_enc;
char *freeme = NULL;
enc = moo_edit_get_encoding (doc);
if (!enc)
{
freeme = _moo_edit_get_default_encoding ();
enc = freeme;
}
display_enc = _moo_encoding_get_display_name (enc);
/* Translators: do not translate the part before | */
item = gtk_menu_item_new_with_label (Q_("Item in the notebook popup menu|Encoding"));
gtk_widget_show (item);
menu = _moo_encodings_menu_new (doc_encoding_menu_item_activated, doc,
enc, FALSE);
gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
gtk_menu_shell_prepend (GTK_MENU_SHELL (menu),
g_object_new (GTK_TYPE_SEPARATOR_MENU_ITEM,
"visible", TRUE, NULL));
enc_item = gtk_radio_menu_item_new_with_label (NULL, display_enc);
gtk_widget_show (enc_item);
gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), enc_item);
g_free (freeme);
return item;
}
static gboolean static gboolean
notebook_populate_popup (MooNotebook *notebook, notebook_populate_popup (MooNotebook *notebook,
GtkWidget *child, GtkWidget *child,
@ -2052,12 +2060,6 @@ notebook_populate_popup (MooNotebook *notebook,
window); window);
} }
gtk_menu_shell_append (GTK_MENU_SHELL (menu),
g_object_new (GTK_TYPE_SEPARATOR_MENU_ITEM,
"visible", TRUE, NULL));
item = create_doc_encoding_menu_item (edit);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
return FALSE; return FALSE;
} }
@ -2166,12 +2168,21 @@ edit_changed (MooEditWindow *window,
update_statusbar (window); update_statusbar (window);
update_lang_menu (window); update_lang_menu (window);
update_doc_view_actions (window); update_doc_view_actions (window);
update_doc_encoding_item (window);
} }
if (doc) if (doc)
update_tab_label (window, doc); update_tab_label (window, doc);
} }
static void
edit_encoding_changed (MooEditWindow *window,
G_GNUC_UNUSED GParamSpec *pspec,
MooEdit *doc)
{
if (doc == ACTIVE_DOC (window))
update_doc_encoding_item (window);
}
static void static void
edit_overwrite_changed (MooEditWindow *window, edit_overwrite_changed (MooEditWindow *window,
@ -2226,7 +2237,7 @@ update_doc_view_actions (MooEditWindow *window)
{ {
MooEdit *doc; MooEdit *doc;
doc = moo_edit_window_get_active_doc (window); doc = ACTIVE_DOC (window);
if (!doc) if (!doc)
return; return;
@ -2397,10 +2408,14 @@ _moo_edit_window_insert_doc (MooEditWindow *window,
gtk_container_add (GTK_CONTAINER (scrolledwindow), GTK_WIDGET (edit)); gtk_container_add (GTK_CONTAINER (scrolledwindow), GTK_WIDGET (edit));
gtk_widget_show_all (scrolledwindow); gtk_widget_show_all (scrolledwindow);
if (position < 0)
position = moo_notebook_get_current_page (window->priv->notebook) + 1;
moo_notebook_insert_page (window->priv->notebook, scrolledwindow, label, position); moo_notebook_insert_page (window->priv->notebook, scrolledwindow, label, position);
g_signal_connect_swapped (edit, "doc_status_changed", g_signal_connect_swapped (edit, "doc_status_changed",
G_CALLBACK (edit_changed), window); G_CALLBACK (edit_changed), window);
g_signal_connect_swapped (edit, "notify::encoding",
G_CALLBACK (edit_encoding_changed), window);
g_signal_connect_swapped (edit, "notify::overwrite", g_signal_connect_swapped (edit, "notify::overwrite",
G_CALLBACK (edit_overwrite_changed), window); G_CALLBACK (edit_overwrite_changed), window);
g_signal_connect_swapped (edit, "notify::wrap-mode", g_signal_connect_swapped (edit, "notify::wrap-mode",
@ -3247,21 +3262,22 @@ set_statusbar_numbers (MooEditWindow *window,
int column, int column,
int chars) int chars)
{ {
char line_buf[10] = {0};
char column_buf[10] = {0};
char chars_buf[10] = {0};
char *text, *text2; char *text, *text2;
if (line > 0 && column > 0) if (line > 0 && column > 0)
/* Label in the statusbar - line and column numbers */ {
text = g_strdup_printf (_("Line: %d Col: %d"), line, column); g_snprintf (line_buf, sizeof line_buf, "%d", line);
else g_snprintf (column_buf, sizeof column_buf, "%d", column);
/* Disabled label in the statusbar when no document is open */ }
text = g_strdup (_("Line: Col: "));
if (chars >= 0) if (chars >= 0)
/* Label in the statusbar - number of characters in the document */ g_snprintf (chars_buf, sizeof chars_buf, "%d", chars);
text2 = g_strdup_printf (_("Chars: %d"), chars);
else text = g_strdup_printf (_("Line: %s Col: %s"), line_buf, column_buf);
/* Disabled label in the statusbar when no document is open */ text2 = g_strdup_printf (_("Chars: %s"), chars_buf);
text2 = g_strdup (_("Chars: "));
gtk_label_set_text (window->priv->cursor_label, text); gtk_label_set_text (window->priv->cursor_label, text);
gtk_label_set_text (window->priv->chars_label, text2); gtk_label_set_text (window->priv->chars_label, text2);
@ -4156,7 +4172,7 @@ moo_edit_window_update_doc_list (MooEditWindow *window)
window, NULL); window, NULL);
if (!window->priv->history_blocked && if (!window->priv->history_blocked &&
(doc = moo_edit_window_get_active_doc (window))) (doc = ACTIVE_DOC (window)))
{ {
GList *link = g_list_find (window->priv->history, doc); GList *link = g_list_find (window->priv->history, doc);

View File

@ -74,7 +74,6 @@ static void enc_mgr_load (EncodingsManager *enc_mgr);
static void enc_mgr_add_used (EncodingsManager *enc_mgr, static void enc_mgr_add_used (EncodingsManager *enc_mgr,
Encoding *enc); Encoding *enc);
static void sync_recent_menu (MenuData *menu, static void sync_recent_menu (MenuData *menu,
const char *exclude,
gboolean need_separator); gboolean need_separator);
@ -512,7 +511,7 @@ update_menu_idle (EncodingsManager *mgr)
for (l = mgr->menus; l != NULL; l = l->next) for (l = mgr->menus; l != NULL; l = l->next)
{ {
MenuData *menu = l->data; MenuData *menu = l->data;
sync_recent_menu (menu, NULL, FALSE); sync_recent_menu (menu, FALSE);
} }
enc_mgr_save (mgr); enc_mgr_save (mgr);
@ -989,14 +988,14 @@ create_menu_item (Encoding *enc,
} }
static void static void
sync_recent_menu (MenuData *menu, sync_recent_menu (MenuData *menu,
const char *exclude, gboolean first_time)
gboolean need_separator)
{ {
EncodingsManager *mgr = get_enc_mgr (); EncodingsManager *mgr = get_enc_mgr ();
GList *children, *l; GList *children, *l;
GSList *recent_items; GSList *recent_items;
gboolean have_separator = FALSE; gboolean have_separator = FALSE;
int pos = first_time ? 0 : 2;
children = gtk_container_get_children (GTK_CONTAINER (menu->menu)); children = gtk_container_get_children (GTK_CONTAINER (menu->menu));
for (l = children; l != NULL; l = l->next) for (l = children; l != NULL; l = l->next)
@ -1009,31 +1008,54 @@ sync_recent_menu (MenuData *menu,
while (recent_items) while (recent_items)
{ {
Encoding *enc = recent_items->data; Encoding *enc = recent_items->data;
GtkWidget *item;
if (!exclude || strcmp (enc->name, exclude) != 0) if (first_time && !have_separator)
{ {
GtkWidget *item; item = gtk_separator_menu_item_new ();
gtk_menu_shell_insert (GTK_MENU_SHELL (menu->menu), item, pos);
if (need_separator && !have_separator) have_separator = TRUE;
{
item = gtk_separator_menu_item_new ();
gtk_menu_shell_prepend (GTK_MENU_SHELL (menu->menu), item);
have_separator = TRUE;
}
item = create_menu_item (enc, menu, TRUE);
gtk_menu_shell_prepend (GTK_MENU_SHELL (menu->menu), item);
} }
item = create_menu_item (enc, menu, TRUE);
gtk_menu_shell_insert (GTK_MENU_SHELL (menu->menu), item, pos);
recent_items = g_slist_delete_link (recent_items, recent_items); recent_items = g_slist_delete_link (recent_items, recent_items);
} }
} }
GtkWidget * static void
exclude_item (MenuData *menu_data,
Encoding *exclude_enc)
{
GList *children, *l;
children = gtk_container_get_children (GTK_CONTAINER (menu_data->menu));
for (l = children; l != NULL; l = l->next)
{
GtkWidget *item = l->data;
if (g_object_get_data (G_OBJECT (item), "moo-recent-encoding"))
{
gboolean visible = TRUE;
if (exclude_enc)
{
Encoding *enc = g_object_get_data (G_OBJECT (item), "moo-encoding");
visible = exclude_enc != enc;
}
g_object_set (item, "visible", visible, NULL);
}
}
g_list_free (children);
}
static MenuData *
_moo_encodings_menu_new (MooEncodingsMenuFunc func, _moo_encodings_menu_new (MooEncodingsMenuFunc func,
gpointer data, gpointer data)
const char *exclude,
gboolean sync)
{ {
MenuData *menu_data = NULL; MenuData *menu_data = NULL;
GtkWidget *widget; GtkWidget *widget;
@ -1051,16 +1073,8 @@ _moo_encodings_menu_new (MooEncodingsMenuFunc func,
widget = gtk_menu_new (); widget = gtk_menu_new ();
menu_data->menu = widget; menu_data->menu = widget;
if (sync) mgr->menus = g_slist_prepend (mgr->menus, menu_data);
{ g_signal_connect (widget, "destroy", G_CALLBACK (menu_destroyed), menu_data);
mgr->menus = g_slist_prepend (mgr->menus, menu_data);
g_signal_connect (widget, "destroy", G_CALLBACK (menu_destroyed), menu_data);
}
else
{
g_object_set_data_full (G_OBJECT (widget), "moo-menu-data", menu_data,
(GDestroyNotify) menu_data_free);
}
for (cgr = 0; cgr < mgr->n_groups; ++cgr) for (cgr = 0; cgr < mgr->n_groups; ++cgr)
{ {
@ -1081,16 +1095,21 @@ _moo_encodings_menu_new (MooEncodingsMenuFunc func,
} }
} }
sync_recent_menu (menu_data, exclude, TRUE); sync_recent_menu (menu_data, TRUE);
gtk_widget_show_all (widget); gtk_widget_show_all (widget);
return widget; return menu_data;
} }
typedef struct { typedef struct {
MooAction base; MooAction base;
MooEncodingsMenuFunc func; MooEncodingsMenuFunc func;
gpointer func_data; gpointer func_data;
Encoding *cur_enc;
GtkWidget *cur_item;
GtkWidget *cur_separator;
MenuData *menu_data;
guint update_idle;
} MooEncodingsMenuAction; } MooEncodingsMenuAction;
typedef MooActionClass MooEncodingsMenuActionClass; typedef MooActionClass MooEncodingsMenuActionClass;
MOO_DEFINE_TYPE_STATIC (MooEncodingsMenuAction, moo_encodings_menu_action, MOO_TYPE_ACTION) MOO_DEFINE_TYPE_STATIC (MooEncodingsMenuAction, moo_encodings_menu_action, MOO_TYPE_ACTION)
@ -1106,6 +1125,10 @@ moo_encodings_menu_action_init (MooEncodingsMenuAction *action)
{ {
action->func = NULL; action->func = NULL;
action->func_data = NULL; action->func_data = NULL;
action->cur_enc = NULL;
action->cur_item = NULL;
action->cur_separator = NULL;
action->menu_data = NULL;
} }
static void static void
@ -1113,7 +1136,9 @@ action_item_activated (const char *encoding,
gpointer data) gpointer data)
{ {
MooEncodingsMenuAction *action = data; MooEncodingsMenuAction *action = data;
g_return_if_fail (action->func != NULL); g_return_if_fail (action->func != NULL);
action->func (encoding, action->func_data); action->func (encoding, action->func_data);
} }
@ -1121,11 +1146,23 @@ static GtkWidget *
moo_encodings_menu_action_create_menu_item (GtkAction *gtkaction) moo_encodings_menu_action_create_menu_item (GtkAction *gtkaction)
{ {
MooEncodingsMenuAction *action = MOO_ENCODINGS_MENU_ACTION (gtkaction); MooEncodingsMenuAction *action = MOO_ENCODINGS_MENU_ACTION (gtkaction);
GtkWidget *menu_item, *menu; GtkWidget *menu_item;
menu_item = GTK_ACTION_CLASS (moo_encodings_menu_action_parent_class)->create_menu_item (gtkaction); menu_item = GTK_ACTION_CLASS (moo_encodings_menu_action_parent_class)->create_menu_item (gtkaction);
menu = _moo_encodings_menu_new (action_item_activated, action, NULL, TRUE); action->menu_data = _moo_encodings_menu_new (action_item_activated, action);
gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu); gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), action->menu_data->menu);
action->cur_separator = gtk_separator_menu_item_new ();
gtk_menu_shell_prepend (GTK_MENU_SHELL (action->menu_data->menu), action->cur_separator);
action->cur_item = gtk_radio_menu_item_new_with_label (NULL, "");
gtk_menu_shell_prepend (GTK_MENU_SHELL (action->menu_data->menu), action->cur_item);
if (action->cur_enc)
{
Encoding *enc = action->cur_enc;
action->cur_enc = NULL;
_moo_encodings_menu_action_set_current (gtkaction, enc->name);
}
return menu_item; return menu_item;
} }
@ -1155,3 +1192,54 @@ _moo_encodings_menu_action_new (const char *id,
return GTK_ACTION (action); return GTK_ACTION (action);
} }
static gboolean
update_recent_list_visibility (MooEncodingsMenuAction *action)
{
action->update_idle = 0;
if (action->cur_enc)
{
GtkWidget *child;
gtk_widget_show (action->cur_item);
gtk_widget_show (action->cur_separator);
child = GTK_BIN (action->cur_item)->child;
gtk_label_set_text (GTK_LABEL (child), action->cur_enc->display_name);
exclude_item (action->menu_data, action->cur_enc);
}
else
{
gtk_widget_hide (action->cur_item);
gtk_widget_hide (action->cur_separator);
}
return FALSE;
}
void
_moo_encodings_menu_action_set_current (GtkAction *gtkaction,
const char *enc_name)
{
MooEncodingsMenuAction *action;
Encoding *enc;
EncodingsManager *mgr;
g_return_if_fail (MOO_IS_ENCODINGS_MENU_ACTION (gtkaction));
action = MOO_ENCODINGS_MENU_ACTION (gtkaction);
mgr = get_enc_mgr ();
enc = enc_name ? get_encoding (mgr, enc_name) : NULL;
if (enc != action->cur_enc)
{
action->cur_enc = enc;
if (action->cur_item && !action->update_idle)
action->update_idle = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE + 1,
(GSourceFunc) update_recent_list_visibility,
action, NULL);
}
}

View File

@ -48,10 +48,8 @@ GtkAction *_moo_encodings_menu_action_new (const char *id,
const char *label, const char *label,
MooEncodingsMenuFunc func, MooEncodingsMenuFunc func,
gpointer data); gpointer data);
GtkWidget *_moo_encodings_menu_new (MooEncodingsMenuFunc func, void _moo_encodings_menu_action_set_current (GtkAction *action,
gpointer data, const char *enc);
const char *exclude,
gboolean sync);
const char *_moo_encoding_locale (void); const char *_moo_encoding_locale (void);
gboolean _moo_encodings_equal (const char *enc1, gboolean _moo_encodings_equal (const char *enc1,