1 /* ide-editor-page-settings.c
2  *
3  * Copyright 2017-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "ide-editor-page-settings"
22 
23 #include "config.h"
24 
25 #include "ide-editor-private.h"
26 
27 #include <gtksourceview/gtksource.h>
28 
29 static gboolean
30 get_smart_home_end (GValue   *value,
31                     GVariant *variant,
32                     gpointer  user_data)
33 {
34   if (g_variant_get_boolean (variant))
35     g_value_set_enum (value, GTK_SOURCE_SMART_HOME_END_BEFORE);
36   else
37     g_value_set_enum (value, GTK_SOURCE_SMART_HOME_END_DISABLED);
38   return TRUE;
39 }
40 
41 static gboolean
42 get_wrap_mode (GValue   *value,
43                GVariant *variant,
44                gpointer  user_data)
45 {
46   if (g_variant_get_boolean (variant))
47     g_value_set_enum (value, GTK_WRAP_WORD);
48   else
49     g_value_set_enum (value, GTK_WRAP_NONE);
50   return TRUE;
51 }
52 
53 static void
54 on_keybindings_changed (IdeEditorPage *self,
55                         const gchar   *key,
56                         GSettings     *settings)
57 {
58   IdeSourceView *source_view;
59 
60   g_assert (IDE_IS_EDITOR_PAGE (self));
61   g_assert (g_strcmp0 (key, "keybindings") == 0);
62   g_assert (G_IS_SETTINGS (settings));
63 
64   source_view = ide_editor_page_get_view (self);
65 
66   g_signal_emit_by_name (source_view,
67                          "set-mode",
68                          NULL,
69                          IDE_SOURCE_VIEW_MODE_TYPE_PERMANENT);
70 }
71 
72 static void
73 on_draw_spaces_changed (IdeEditorPage *self,
74                         const gchar   *key,
75                         GSettings     *settings)
76 {
77   GtkSourceView *source_view;
78   GtkSourceSpaceDrawer *drawer;
79   guint flags;
80   GtkSourceSpaceLocationFlags location_flags = GTK_SOURCE_SPACE_LOCATION_NONE;
81   GtkSourceSpaceTypeFlags type_flags = GTK_SOURCE_SPACE_TYPE_NONE;
82 
83   g_assert (IDE_IS_EDITOR_PAGE (self));
84   g_assert (g_strcmp0 (key, "draw-spaces") == 0);
85   g_assert (G_IS_SETTINGS (settings));
86 
87   source_view = GTK_SOURCE_VIEW (ide_editor_page_get_view (self));
88   drawer = gtk_source_view_get_space_drawer (source_view);
89   flags = g_settings_get_flags (settings, "draw-spaces");
90 
91   if (flags == 0)
92     {
93       gtk_source_space_drawer_set_enable_matrix (drawer, FALSE);
94       return;
95     }
96 
97   /* Reset the matrix before setting it */
98   gtk_source_space_drawer_set_types_for_locations (drawer, GTK_SOURCE_SPACE_LOCATION_ALL, GTK_SOURCE_SPACE_TYPE_NONE);
99 
100   if (flags & 1)
101     type_flags |= GTK_SOURCE_SPACE_TYPE_SPACE;
102 
103   if (flags & 2)
104     type_flags |= GTK_SOURCE_SPACE_TYPE_TAB;
105 
106   if (flags & 4)
107     {
108       gtk_source_space_drawer_set_types_for_locations (drawer, GTK_SOURCE_SPACE_LOCATION_ALL, GTK_SOURCE_SPACE_TYPE_NEWLINE);
109       type_flags |= GTK_SOURCE_SPACE_TYPE_NEWLINE;
110     }
111 
112   if (flags & 8)
113     type_flags |= GTK_SOURCE_SPACE_TYPE_NBSP;
114 
115   if (flags & 16)
116     location_flags |= GTK_SOURCE_SPACE_LOCATION_LEADING;
117 
118   if (flags & 32)
119     location_flags |= GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT;
120 
121   if (flags & 64)
122     location_flags |= GTK_SOURCE_SPACE_LOCATION_TRAILING;
123 
124   if (type_flags > 0 && location_flags == 0)
125     location_flags |= GTK_SOURCE_SPACE_LOCATION_ALL;
126 
127   gtk_source_space_drawer_set_enable_matrix (drawer, TRUE);
128   gtk_source_space_drawer_set_types_for_locations (drawer, location_flags, type_flags);
129 }
130 
131 void
132 _ide_editor_page_init_settings (IdeEditorPage *self)
133 {
134   IdeSourceView *source_view;
135   IdeBuffer *buffer;
136 
137   g_assert (IDE_IS_EDITOR_PAGE (self));
138   g_assert (self->editor_settings == NULL);
139   g_assert (self->insight_settings == NULL);
140 
141   source_view = ide_editor_page_get_view (self);
142   buffer = ide_editor_page_get_buffer (self);
143 
144   self->editor_settings = g_settings_new ("org.gnome.builder.editor");
145 
146   g_settings_bind (self->editor_settings, "highlight-current-line",
147                    source_view, "highlight-current-line",
148                    G_SETTINGS_BIND_GET);
149 
150   g_settings_bind (self->editor_settings, "highlight-matching-brackets",
151                    buffer, "highlight-matching-brackets",
152                    G_SETTINGS_BIND_GET);
153 
154   g_settings_bind (self->editor_settings, "show-line-changes",
155                    source_view, "show-line-changes",
156                    G_SETTINGS_BIND_GET);
157 
158   g_settings_bind (self->editor_settings, "show-line-diagnostics",
159                    source_view, "show-line-diagnostics",
160                    G_SETTINGS_BIND_GET);
161 
162   g_settings_bind (self->editor_settings, "show-line-numbers",
163                    source_view, "show-line-numbers",
164                    G_SETTINGS_BIND_GET);
165 
166   g_settings_bind (self->editor_settings, "show-relative-line-numbers",
167                    source_view, "show-relative-line-numbers",
168                    G_SETTINGS_BIND_GET);
169 
170   g_settings_bind (self->editor_settings, "smart-backspace",
171                    source_view, "smart-backspace",
172                    G_SETTINGS_BIND_GET);
173 
174   g_settings_bind_with_mapping (self->editor_settings, "smart-home-end",
175                                 source_view, "smart-home-end",
176                                 G_SETTINGS_BIND_GET,
177                                 get_smart_home_end, NULL, NULL, NULL);
178 
179   g_settings_bind (self->editor_settings, "style-scheme-name",
180                    buffer, "style-scheme-name",
181                    G_SETTINGS_BIND_GET);
182 
183   g_settings_bind (self->editor_settings, "font-name",
184                    source_view, "font-name",
185                    G_SETTINGS_BIND_GET);
186 
187   g_settings_bind (self->editor_settings, "overscroll",
188                    source_view, "overscroll",
189                    G_SETTINGS_BIND_GET);
190 
191   g_settings_bind (self->editor_settings, "scroll-offset",
192                    source_view, "scroll-offset",
193                    G_SETTINGS_BIND_GET);
194 
195   g_settings_bind (self->editor_settings, "show-grid-lines",
196                    source_view, "show-grid-lines",
197                    G_SETTINGS_BIND_GET);
198 
199   g_settings_bind_with_mapping (self->editor_settings, "wrap-text",
200                                 source_view, "wrap-mode",
201                                 G_SETTINGS_BIND_GET,
202                                 get_wrap_mode, NULL, NULL, NULL);
203 
204   g_settings_bind (self->editor_settings, "completion-n-rows",
205                    source_view, "completion-n-rows",
206                    G_SETTINGS_BIND_GET);
207 
208   g_settings_bind (self->editor_settings, "interactive-completion",
209                    source_view, "interactive-completion",
210                    G_SETTINGS_BIND_GET);
211 
212   g_settings_bind (self->editor_settings, "show-map",
213                    self, "show-map",
214                    G_SETTINGS_BIND_GET);
215 
216   g_settings_bind (self->editor_settings, "auto-hide-map",
217                    self, "auto-hide-map",
218                    G_SETTINGS_BIND_GET);
219 
220   g_signal_connect_object (self->editor_settings,
221                            "changed::keybindings",
222                            G_CALLBACK (on_keybindings_changed),
223                            self,
224                            G_CONNECT_SWAPPED);
225 
226   on_keybindings_changed (self, "keybindings", self->editor_settings);
227 
228   g_signal_connect_object (self->editor_settings,
229                            "changed::draw-spaces",
230                            G_CALLBACK (on_draw_spaces_changed),
231                            self,
232                            G_CONNECT_SWAPPED);
233 
234   on_draw_spaces_changed (self, "draw-spaces", self->editor_settings);
235 
236   self->insight_settings = g_settings_new ("org.gnome.builder.code-insight");
237 }
238