| 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 | */ |
|---|
| 21 | using Gtk; |
|---|
| 22 | |
|---|
| 23 | public 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 print_compositor = new SourcePrintCompositor (_source_buffer); |
|---|
| 200 | print_compositor.tab_width = _app.settings.documents_tab_width; |
|---|
| 201 | print_compositor.print_line_numbers = _app.settings.documents_show_line_numbers ? 1 : 0; |
|---|
| 202 | print_compositor.body_font_name = _app.settings.documents_font; |
|---|
| 203 | print_compositor.set_header_format (true, null, this.title, null); |
|---|
| 204 | print_compositor.set_footer_format (true, null, _("Page %N of %Q"), null); |
|---|
| 205 | print_compositor.print_header = true; |
|---|
| 206 | print_compositor.print_footer = true; |
|---|
| 207 | print_compositor.wrap_mode = WrapMode.WORD_CHAR; |
|---|
| 208 | |
|---|
| 209 | var operation = new PrintOperation (); |
|---|
| 210 | operation.print_settings = Utils.print_settings; |
|---|
| 211 | operation.default_page_setup = Utils.page_setup; |
|---|
| 212 | |
|---|
| 213 | operation.paginate.connect ((operation, context) => { |
|---|
| 214 | if (print_compositor.paginate (context)) |
|---|
| 215 | { |
|---|
| 216 | operation.n_pages = print_compositor.n_pages; |
|---|
| 217 | return true; |
|---|
| 218 | } |
|---|
| 219 | return false; |
|---|
| 220 | }); |
|---|
| 221 | operation.draw_page.connect ((operation, context, page) => { |
|---|
| 222 | print_compositor.draw_page (context, page); |
|---|
| 223 | }); |
|---|
| 224 | operation.end_print.connect (() => { |
|---|
| 225 | print_compositor = null; |
|---|
| 226 | }); |
|---|
| 227 | |
|---|
| 228 | operation.run (PrintOperationAction.PRINT_DIALOG, null); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | public I4uc.Core.DialogResponse show_close_message () |
|---|
| 232 | { |
|---|
| 233 | var message = new MessageDialog (null, DialogFlags.MODAL, MessageType.WARNING, ButtonsType.NONE, _("Do you want to save the file <<%s>>?"), this.tab_title); |
|---|
| 234 | message.add_button (STOCK_CANCEL, ResponseType.CANCEL); |
|---|
| 235 | message.add_button (STOCK_NO, ResponseType.NO); |
|---|
| 236 | message.add_button (STOCK_YES, ResponseType.YES); |
|---|
| 237 | message.set_default_response (ResponseType.YES); |
|---|
| 238 | var response = message.run (); |
|---|
| 239 | message.destroy (); |
|---|
| 240 | return DialogResponses.get_dialog_response ((ResponseType)response); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | public new void grab_focus () |
|---|
| 244 | { |
|---|
| 245 | _source_view.grab_focus (); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | public void add_recent (string uri) |
|---|
| 249 | { |
|---|
| 250 | RecentManager.get_default ().add_item (uri); |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | public void go_to_line (int line) |
|---|
| 254 | { |
|---|
| 255 | TextIter iter; |
|---|
| 256 | |
|---|
| 257 | if (line >= _source_buffer.get_line_count ()) |
|---|
| 258 | _source_buffer.get_end_iter (out iter); |
|---|
| 259 | else |
|---|
| 260 | _source_buffer.get_iter_at_line (out iter, line-1); |
|---|
| 261 | |
|---|
| 262 | _source_buffer.place_cursor (iter); |
|---|
| 263 | _source_view.scroll_to_iter (iter, 0.25, false, 0, 0); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | private void create_error_bar () |
|---|
| 267 | { |
|---|
| 268 | _error_message_bar.set_message_type (MessageType.ERROR); |
|---|
| 269 | _error_message_bar.add_button (STOCK_OK, ResponseType.OK); |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | private void create_reload_bar () |
|---|
| 273 | { |
|---|
| 274 | _reload_message_bar.set_message_type (MessageType.WARNING); |
|---|
| 275 | _reload_message_bar.add_button (STOCK_REFRESH, ResponseType.OK); |
|---|
| 276 | _reload_message_bar.add_button (STOCK_CANCEL, ResponseType.CANCEL); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | private void on_reload_bar_response (int response) |
|---|
| 280 | { |
|---|
| 281 | if (response == ResponseType.OK) |
|---|
| 282 | this.reload_message_bar.ok_clicked (); |
|---|
| 283 | |
|---|
| 284 | if (response == ResponseType.CANCEL) |
|---|
| 285 | this.reload_message_bar.cancel_clicked (); |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | private void create_save_bar () |
|---|
| 289 | { |
|---|
| 290 | _save_message_bar.set_message_type (MessageType.WARNING); |
|---|
| 291 | _save_message_bar.add_button (STOCK_SAVE, ResponseType.OK); |
|---|
| 292 | _save_message_bar.add_button (STOCK_CANCEL, ResponseType.CANCEL); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | private void on_save_bar_response (int response) |
|---|
| 296 | { |
|---|
| 297 | if (response == ResponseType.OK) |
|---|
| 298 | this.save_message_bar.ok_clicked (); |
|---|
| 299 | |
|---|
| 300 | if (response == ResponseType.CANCEL) |
|---|
| 301 | this.save_message_bar.cancel_clicked (); |
|---|
| 302 | } |
|---|
| 303 | } |
|---|