source: gtkfrontend/documentpageview.vala @ 694d86baafd237d38fc2df4d152aafa3d17cff88

Revision 694d86baafd237d38fc2df4d152aafa3d17cff88, 10.3 KB checked in by Matias De la Puente <mfpuente.ar@…>, 2 years ago (diff)

Move PageSetup? and PrintSettings? from Settings to Utils

Finally Core no longer depends on Gtk

  • Property mode set to 100644
Line 
1/* documentpageview.vala
2 *
3 * Copyright (C) 2010  Matias De la Puente
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 * Author:
19 *      Matias De la Puente <mfpuente.ar@gmail.com>
20 */
21using Gtk;
22
23public class I4uc.GtkFrontend.DocumentPageView : Page, I4uc.Core.DocumentPageView
24{
25        private MessageBar _error_message_bar = new MessageBar ();
26        private MessageBar _reload_message_bar = new MessageBar ();
27        private MessageBar _save_message_bar = new MessageBar ();
28       
29        //Search and Replace bar
30        private SearchBarView _search_bar_view;
31       
32        //Content
33        private SourceView _source_view;
34        private SourceBuffer _source_buffer = new SourceBuffer (null);
35       
36        public string font_name
37        {
38                set { _source_view.modify_font (Pango.FontDescription.from_string (value)); }
39                get { return ""; }
40        }
41       
42        public uint tab_width { set; get; }
43        public bool auto_indent { set; get; }
44        public bool highlight_current_line { set; get; }
45        public bool insert_spaces_instead_of_tabs { set; get; }
46        public bool show_line_numbers { set; get; }
47        public bool highlight_matching_brackets { set; get; }
48       
49        public bool wrap_mode
50        {
51                set { _source_view.wrap_mode = value ? WrapMode.WORD_CHAR : WrapMode.NONE; }
52                get { return _source_view.wrap_mode == WrapMode.WORD_CHAR; }
53        }
54       
55        public bool can_undo { get { return _source_buffer.can_undo; } }
56        public bool can_redo { get { return _source_buffer.can_redo; } }
57        public bool has_selection { get { return _source_buffer.has_selection; } }
58       
59        public string content
60        {
61                set
62                {
63                        _source_buffer.begin_not_undoable_action ();
64                       
65                        var content_size = value.size ();
66                        if (value.has_suffix ("\n"))
67                                content_size--;
68                       
69                        _source_buffer.set_text (value, (int)content_size);
70                        _source_buffer.set_modified (false);
71                        _source_buffer.end_not_undoable_action ();
72                       
73                        TextIter start;
74                        _source_buffer.get_start_iter (out start);
75                        _source_buffer.place_cursor (start);
76                }
77                owned get
78                {
79                        TextIter start, end;
80                        _source_buffer.get_bounds (out start, out end);
81                        return _source_buffer.get_text (start, end, false);
82                }
83        }
84       
85        public string language_id
86        {
87                set
88                {
89                        if (value == "none")
90                                _source_buffer.language = null;
91                        else
92                                _source_buffer.language = SourceLanguageManager.get_default ().get_language (value);
93                }
94                get
95                {
96                        if (_source_buffer.language == null)
97                                return "none";
98                        return _source_buffer.language.id;
99                }
100        }
101       
102        public I4uc.Core.MessageBar error_message_bar { get { return _error_message_bar; } }
103        public I4uc.Core.MessageBar reload_message_bar { get { return _reload_message_bar; } }
104        public I4uc.Core.MessageBar save_message_bar { get { return _save_message_bar; } }
105       
106        public I4uc.Core.SearchBarView search_bar_view { get { return _search_bar_view; } }
107       
108        public bool search_bar_view_visible { set; get; }
109        public bool content_visible { set; get; }
110       
111        public bool content_modified
112        {
113                set { _source_buffer.set_modified (value); }
114                get { return _source_buffer.get_modified (); }
115        }
116       
117        public DocumentPageView ()
118        {
119                base.with_close_button ();
120               
121                create_error_bar ();
122                create_reload_bar ();
123                create_save_bar ();
124               
125                _source_view = new SourceView.with_buffer (_source_buffer);
126                _search_bar_view = new SearchBarView (_source_view);
127               
128                var scrolled_window = new ScrolledWindow (null, null);
129                scrolled_window.add (_source_view);
130                scrolled_window.set_policy (PolicyType.AUTOMATIC, PolicyType.AUTOMATIC);
131               
132                pack_start (_error_message_bar, false, false, 2);
133                pack_start (_reload_message_bar, false, false, 2);
134                pack_start (_save_message_bar, false, false, 2);
135                pack_start (_search_bar_view, false, false, 2);
136                pack_start (scrolled_window, true, true, 0);
137               
138                //Bind properties
139                bind_property ("tab-width", _source_view, "tab-width", BindingFlags.BIDIRECTIONAL);
140                bind_property ("auto-indent", _source_view, "auto-indent", BindingFlags.BIDIRECTIONAL);
141                bind_property ("highlight-current-line", _source_view, "highlight-current-line", BindingFlags.BIDIRECTIONAL);
142                bind_property ("insert-spaces-instead-of-tabs", _source_view, "insert-spaces-instead-of-tabs", BindingFlags.BIDIRECTIONAL);
143                bind_property ("show-line-numbers", _source_view, "show-line-numbers", BindingFlags.BIDIRECTIONAL);
144                bind_property ("highlight-matching-brackets", _source_buffer, "highlight-matching-brackets", BindingFlags.BIDIRECTIONAL);
145                bind_property ("search-bar-view-visible", _search_bar_view, "visible", BindingFlags.BIDIRECTIONAL);
146                bind_property ("content-visible", _source_view, "visible", BindingFlags.BIDIRECTIONAL);
147               
148                //Connect signals
149                _error_message_bar.response.connect (() => this.error_message_bar.ok_clicked ());
150                _reload_message_bar.response.connect (on_reload_bar_response);
151                _save_message_bar.response.connect (on_save_bar_response);
152                _source_buffer.changed.connect (() => this.content_changed ());
153                _source_buffer.modified_changed.connect (() => this.modified_changed ());
154                _source_buffer.notify["has-selection"].connect (() => this.selection_changed ());
155                _source_view.focus_in_event.connect (() => {
156                        this.focused_in ();
157                        return false;
158                });
159        }
160       
161        public void undo ()
162        {
163                if (_source_buffer.can_undo)
164                        _source_buffer.undo ();
165        }
166       
167        public void redo ()
168        {
169                if (_source_buffer.can_redo)
170                        _source_buffer.redo ();
171        }
172       
173        public void cut ()
174        {
175                var clipboard = _source_view.get_clipboard (Gdk.SELECTION_CLIPBOARD);
176                _source_buffer.cut_clipboard (clipboard, true);
177        }
178       
179        public void copy ()
180        {
181                var clipboard = _source_view.get_clipboard (Gdk.SELECTION_CLIPBOARD);
182                _source_buffer.copy_clipboard (clipboard);
183        }
184       
185        public void paste ()
186        {
187                var clipboard = _source_view.get_clipboard (Gdk.SELECTION_CLIPBOARD);
188                _source_buffer.paste_clipboard (clipboard, null, true);
189        }
190       
191        public void guess_language_id (string content_type)
192        {
193                var lang_manager = SourceLanguageManager.get_default ();
194                _source_buffer.language = lang_manager.guess_language (null, content_type);
195        }
196       
197        public void show_print_dialog () throws GLib.Error
198        {
199                var settings = I4uc.Core.Settings.instance;
200               
201                var print_compositor = new SourcePrintCompositor (_source_buffer);
202                print_compositor.tab_width = settings.documents_tab_width;
203                print_compositor.print_line_numbers = settings.documents_show_line_numbers ? 1 : 0;
204                print_compositor.body_font_name = settings.documents_font;
205                print_compositor.set_header_format (true, null, this.title, null);
206                print_compositor.set_footer_format (true, null, _("Page %N of %Q"), null);
207                print_compositor.print_header = true;
208                print_compositor.print_footer = true;
209                print_compositor.wrap_mode = WrapMode.WORD_CHAR;
210               
211                var operation = new PrintOperation ();
212                operation.print_settings = Utils.print_settings;
213                operation.default_page_setup = Utils.page_setup;
214               
215                operation.paginate.connect ((operation, context) => {
216                        if (print_compositor.paginate (context))
217                        {
218                                operation.n_pages = print_compositor.n_pages;
219                                return true;
220                        }
221                        return false;
222                });
223                operation.draw_page.connect ((operation, context, page) => {
224                        print_compositor.draw_page (context, page);
225                });
226                operation.end_print.connect (() => {
227                        print_compositor = null;
228                });
229               
230                operation.run (PrintOperationAction.PRINT_DIALOG, null);
231        }
232       
233        public I4uc.Core.DialogResponse show_save_dialog (string name, ref string folder_uri, out string document_uri)
234        {
235                var dialog = new FileChooserDialog (_("Save as"), null, FileChooserAction.SAVE);
236                dialog.local_only = false;
237                dialog.set_current_name (name);
238                dialog.set_current_folder_uri (folder_uri);
239                dialog.add_button (STOCK_CANCEL, ResponseType.CANCEL);
240                dialog.add_button (STOCK_SAVE, ResponseType.OK);
241                dialog.set_default_response (ResponseType.OK);
242                dialog.do_overwrite_confirmation = true;
243                var response = dialog.run ();
244                document_uri = dialog.get_uri ();
245                folder_uri = dialog.get_current_folder_uri ();
246                dialog.destroy ();
247                return DialogResponses.get_dialog_response ((ResponseType)response);
248        }
249       
250        public I4uc.Core.DialogResponse show_close_message ()
251        {
252                var message = new MessageDialog (null, DialogFlags.MODAL, MessageType.WARNING, ButtonsType.NONE, _("Do you want to save the file <<%s>>?"), this.tab_title);
253                message.add_button (STOCK_CANCEL, ResponseType.CANCEL);
254                message.add_button (STOCK_NO, ResponseType.NO);
255                message.add_button (STOCK_YES, ResponseType.YES);
256                message.set_default_response (ResponseType.YES);
257                var response = message.run ();
258                message.destroy ();
259                return DialogResponses.get_dialog_response ((ResponseType)response);
260        }
261       
262        public new void grab_focus ()
263        {
264                _source_view.grab_focus ();
265        }
266       
267        public void add_recent (string uri)
268        {
269                RecentManager.get_default ().add_item (uri);
270        }
271       
272        public void go_to_line (int line)
273        {
274                TextIter iter;
275               
276                if (line >= _source_buffer.get_line_count ())
277                        _source_buffer.get_end_iter (out iter);
278                else
279                        _source_buffer.get_iter_at_line (out iter, line-1);
280               
281                _source_buffer.place_cursor (iter);
282                _source_view.scroll_to_iter (iter, 0.25, false, 0, 0);
283        }
284       
285        private void create_error_bar ()
286        {
287                _error_message_bar.set_message_type (MessageType.ERROR);
288                _error_message_bar.add_button (STOCK_OK, ResponseType.OK);
289        }
290       
291        private void create_reload_bar ()
292        {
293                _reload_message_bar.set_message_type (MessageType.WARNING);
294                _reload_message_bar.add_button (STOCK_REFRESH, ResponseType.OK);
295                _reload_message_bar.add_button (STOCK_CANCEL, ResponseType.CANCEL);
296        }
297       
298        private void on_reload_bar_response (int response)
299        {
300                if (response == ResponseType.OK)
301                        this.reload_message_bar.ok_clicked ();
302               
303                if (response == ResponseType.CANCEL)
304                        this.reload_message_bar.cancel_clicked ();
305        }
306
307        private void create_save_bar ()
308        {
309                _save_message_bar.set_message_type (MessageType.WARNING);
310                _save_message_bar.add_button (STOCK_SAVE, ResponseType.OK);
311                _save_message_bar.add_button (STOCK_CANCEL, ResponseType.CANCEL);
312        }
313       
314        private void on_save_bar_response (int response)
315        {
316                if (response == ResponseType.OK)
317                        this.save_message_bar.ok_clicked ();
318               
319                if (response == ResponseType.CANCEL)
320                        this.save_message_bar.cancel_clicked ();
321        }
322}
Note: See TracBrowser for help on using the repository browser.