From f3791ef8d2aa8c8dba485f2f778e41784e5e01b3 Mon Sep 17 00:00:00 2001 From: JosephMcc Date: Fri, 30 Dec 2016 03:17:05 -0800 Subject: [PATCH] Add view menu entry for word wrap Adds an entry to the "View" menu that allows temporarily overriding the word wrap settings in the preferences. --- xed/xed-commands-view.c | 22 ++++++++++++++ xed/xed-commands.h | 1 + xed/xed-ui.h | 19 +++++++----- xed/xed-ui.xml | 1 + xed/xed-window-private.h | 1 + xed/xed-window.c | 63 +++++++++++++++++++++++++++++++++------- 6 files changed, 89 insertions(+), 18 deletions(-) diff --git a/xed/xed-commands-view.c b/xed/xed-commands-view.c index 78ee31c..9df6fe2 100644 --- a/xed/xed-commands-view.c +++ b/xed/xed-commands-view.c @@ -134,6 +134,28 @@ _xed_cmd_view_toggle_fullscreen_mode (GtkAction *action, _xed_window_fullscreen (window); } +void +_xed_cmd_view_toggle_word_wrap (GtkAction *action, + XedWindow *window) +{ + XedView *view; + gboolean do_word_wrap; + + xed_debug (DEBUG_COMMANDS); + + do_word_wrap = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action)); + view = xed_window_get_active_view (window); + + if (do_word_wrap) + { + gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_WORD); + } + else + { + gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (view), GTK_WRAP_NONE); + } +} + void _xed_cmd_view_leave_fullscreen_mode (GtkAction *action, XedWindow *window) diff --git a/xed/xed-commands.h b/xed/xed-commands.h index c053ad2..9a4a693 100644 --- a/xed/xed-commands.h +++ b/xed/xed-commands.h @@ -47,6 +47,7 @@ void _xed_cmd_view_show_statusbar (GtkAction *action, XedWindow *window); void _xed_cmd_view_show_side_pane (GtkAction *action, XedWindow *window); void _xed_cmd_view_show_bottom_pane (GtkAction *action, XedWindow *window); void _xed_cmd_view_toggle_fullscreen_mode (GtkAction *action, XedWindow *window); +void _xed_cmd_view_toggle_word_wrap (GtkAction *action, XedWindow *window); void _xed_cmd_view_leave_fullscreen_mode (GtkAction *action, XedWindow *window); void _xed_cmd_search_find (GtkAction *action, XedWindow *window); diff --git a/xed/xed-ui.h b/xed/xed-ui.h index 321597a..acefb5e 100644 --- a/xed/xed-ui.h +++ b/xed/xed-ui.h @@ -2,7 +2,7 @@ * xed-ui.h * This file is part of xed * - * Copyright (C) 2005 - Paolo Maggi + * Copyright (C) 2005 - Paolo Maggi * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,14 +16,14 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, + * Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301, USA. */ - + /* - * Modified by the xed Team, 2005. See the AUTHORS file for a - * list of people on the xed Team. - * See the ChangeLog files for a list of changes. + * Modified by the xed Team, 2005. See the AUTHORS file for a + * list of people on the xed Team. + * See the ChangeLog files for a list of changes. * * $Id$ */ @@ -67,7 +67,7 @@ static const GtkActionEntry xed_always_sensitive_menu_entries[] = N_("Open the xed manual"), G_CALLBACK (_xed_cmd_help_contents) }, { "HelpAbout", "help-about-symbolic", N_("_About"), NULL, N_("About this application"), G_CALLBACK (_xed_cmd_help_about) }, - + /* Fullscreen toolbar */ { "LeaveFullscreen", "view-restore-symbolic", NULL, NULL, N_("Leave fullscreen mode"), @@ -156,7 +156,10 @@ static const GtkToggleActionEntry xed_always_sensitive_toggle_menu_entries[] = G_CALLBACK (_xed_cmd_view_show_statusbar), TRUE }, { "ViewFullscreen", GTK_STOCK_FULLSCREEN, NULL, "F11", N_("Edit text in fullscreen"), - G_CALLBACK (_xed_cmd_view_toggle_fullscreen_mode), FALSE } + G_CALLBACK (_xed_cmd_view_toggle_fullscreen_mode), FALSE }, + { "ViewWordWrap", NULL, N_("_Word wrap"), NULL, + N_("Set word wrap for the current document"), + G_CALLBACK (_xed_cmd_view_toggle_word_wrap), FALSE } }; /* separate group, should be always sensitive except when there are no panes */ diff --git a/xed/xed-ui.xml b/xed/xed-ui.xml index d859216..6b5fe27 100644 --- a/xed/xed-ui.xml +++ b/xed/xed-ui.xml @@ -55,6 +55,7 @@ + diff --git a/xed/xed-window-private.h b/xed/xed-window-private.h index 4cc95ce..20ecea2 100644 --- a/xed/xed-window-private.h +++ b/xed/xed-window-private.h @@ -74,6 +74,7 @@ struct _XedWindowPrivate guint tab_width_id; guint spaces_instead_of_tabs_id; guint language_changed_id; + guint use_word_wrap_id; /* Menus & Toolbars */ GtkUIManager *manager; diff --git a/xed/xed-window.c b/xed/xed-window.c index 44bbc5e..bd755f9 100644 --- a/xed/xed-window.c +++ b/xed/xed-window.c @@ -2050,6 +2050,34 @@ language_changed (GObject *object, g_list_free (items); } +static void +word_wrap_changed (GObject *object, + GParamSpec *pspec, + XedWindow *window) +{ + XedView *view; + GtkWrapMode wrap_mode; + GtkAction *action; + gboolean wrap_enabled; + + view = XED_VIEW (object); + wrap_mode = gtk_text_view_get_wrap_mode (GTK_TEXT_VIEW (view)); + action = gtk_action_group_get_action (window->priv->always_sensitive_action_group, "ViewWordWrap"); + + if (wrap_mode == GTK_WRAP_NONE) + { + wrap_enabled = FALSE; + } + else + { + wrap_enabled = TRUE; + } + + g_signal_handlers_block_by_func (action, G_CALLBACK (_xed_cmd_view_toggle_word_wrap), window); + gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), wrap_enabled); + g_signal_handlers_unblock_by_func (action, G_CALLBACK (_xed_cmd_view_toggle_word_wrap), window); +} + static void notebook_switch_page (GtkNotebook *book, GtkWidget *pg, @@ -2080,10 +2108,15 @@ notebook_switch_page (GtkNotebook *book, if (window->priv->spaces_instead_of_tabs_id) { - g_signal_handler_disconnect (xed_tab_get_view (window->priv->active_tab), - window->priv->spaces_instead_of_tabs_id); + g_signal_handler_disconnect (xed_tab_get_view (window->priv->active_tab), window->priv->spaces_instead_of_tabs_id); window->priv->spaces_instead_of_tabs_id = 0; } + + if (window->priv->use_word_wrap_id) + { + g_signal_handler_disconnect (xed_tab_get_view (window->priv->active_tab), window->priv->use_word_wrap_id); + window->priv->use_word_wrap_id = 0; + } } /* set the active tab */ @@ -2102,7 +2135,7 @@ notebook_switch_page (GtkNotebook *book, */ if (action != NULL) { - gtk_toggle_action_set_active (GTK_TOGGLE_ACTION(action), TRUE); + gtk_toggle_action_set_active (GTK_TOGGLE_ACTION (action), TRUE); } g_free (action_name); @@ -2113,8 +2146,8 @@ notebook_switch_page (GtkNotebook *book, view = xed_tab_get_view (tab); /* sync the statusbar */ - update_cursor_position_statusbar (GTK_TEXT_BUFFER(xed_tab_get_document (tab)), window); - xed_statusbar_set_overwrite (XED_STATUSBAR(window->priv->statusbar), + update_cursor_position_statusbar (GTK_TEXT_BUFFER (xed_tab_get_document (tab)), window); + xed_statusbar_set_overwrite (XED_STATUSBAR (window->priv->statusbar), gtk_text_view_get_overwrite (GTK_TEXT_VIEW(view))); gtk_widget_show (window->priv->tab_width_combo); @@ -2128,12 +2161,16 @@ notebook_switch_page (GtkNotebook *book, window->priv->language_changed_id = g_signal_connect(xed_tab_get_document (tab), "notify::language", G_CALLBACK (language_changed), window); - /* call it for the first time */ - tab_width_changed (G_OBJECT(view), NULL, window); - spaces_instead_of_tabs_changed (G_OBJECT(view), NULL, window); - language_changed (G_OBJECT(xed_tab_get_document (tab)), NULL, window); + window->priv->use_word_wrap_id = g_signal_connect (view, "notify::wrap-mode", + G_CALLBACK (word_wrap_changed), window); - g_signal_emit (G_OBJECT(window), signals[ACTIVE_TAB_CHANGED], 0, window->priv->active_tab); + /* call it for the first time */ + tab_width_changed (G_OBJECT (view), NULL, window); + spaces_instead_of_tabs_changed (G_OBJECT (view), NULL, window); + language_changed (G_OBJECT (xed_tab_get_document (tab)), NULL, window); + word_wrap_changed (G_OBJECT (view), NULL, window); + + g_signal_emit (G_OBJECT (window), signals[ACTIVE_TAB_CHANGED], 0, window->priv->active_tab); } static void @@ -2887,6 +2924,12 @@ notebook_tab_removed (XedNotebook *notebook, window->priv->language_changed_id = 0; } + if (window->priv->use_word_wrap_id && tab == xed_window_get_active_tab (window)) + { + g_signal_handler_disconnect (view, window->priv->use_word_wrap_id); + window->priv->use_word_wrap_id = 0; + } + g_return_if_fail(window->priv->num_tabs >= 0); if (window->priv->num_tabs == 0) {