~writer-devs/writer/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/***
The MIT License (MIT)

Copyright (c) 2014 Tuur Dutoit

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***/


using Gtk;
using Gdk;
using Pango;
using Writer.Utils;

namespace Writer {
    public class TextEditor : TextBuffer {

        public TextView text_view;
        public bool style_bold;
        public bool style_italic;
        public bool style_underline;
        public bool style_strikethrough;

        public signal void cursor_moved ();
        public signal void text_inserted ();

        public TextEditor () {
            setup_tagtable (this);
            text_view = new TextView.with_buffer (this);

            style_bold = false;
            style_italic = false;
            style_underline = false;
            style_strikethrough = false;

            text_view.move_cursor.connect (cursor_moved_callback);
            text_view.button_release_event.connect (editor_clicked_callback);
            this.insert_text.connect_after (text_inserted_callback);

            text_view.key_press_event.connect (key_press_callback);
            text_view.pixels_below_lines = 20;
        }

        // Get a TextTagTable with all the default tags
        private void setup_tagtable (TextBuffer buffer) {

            buffer.create_tag ("bold", "weight", 700);
            buffer.create_tag ("italic", "style", Pango.Style.ITALIC);
            buffer.create_tag ("underline", "underline", Pango.Underline.SINGLE);
            buffer.create_tag ("strikethrough", "strikethrough", true);

            buffer.create_tag ("align-left", "justification", Gtk.Justification.LEFT);
            buffer.create_tag ("align-center", "justification", Gtk.Justification.CENTER);
            buffer.create_tag ("align-right", "justification", Gtk.Justification.RIGHT);
            buffer.create_tag ("align-fill", "justification", Gtk.Justification.FILL);

        }


        /*
         * Styles (apply, remove, check)
         */

        public void apply_style (string name) {
            if (has_selection)
                get_selection_range ().apply_style (name);

            if (name == "bold")
                style_bold = true;
            else if (name == "italic")
                style_italic = true;
            else if (name == "underline")
                style_underline = true;
            else if (name == "strikethrough")
                style_strikethrough = true;
        }

        public void remove_style (string name) {
            if (has_selection)
                get_selection_range ().remove_style (name);

            if (name == "bold")
                style_bold = false;
            else if (name == "italic")
                style_italic = false;
            else if (name == "underline")
                style_underline = false;
            else if (name == "strikethrough")
                style_strikethrough = false;
        }

        public void toggle_style (string name) {
            if (has_selection)
                get_selection_range ().toggle_style (name);

            if (name == "bold")
                style_bold = !style_bold;
            else if (name == "italic")
                style_italic = !style_italic;
            else if (name == "underline")
                style_underline = !style_underline;
            else if (name == "strikethrough")
                style_strikethrough = !style_strikethrough;
        }

        public bool has_style (string name) {
            if (has_selection)
                return get_selection_range ().has_style (name);
            else
                return iter_has_style (get_cursor (), name);
        }


        public Gtk.Justification get_justification () {
            if (has_selection)
                return get_selection_range ().get_justification ();
            else
                return get_justification_at_iter (get_cursor ());
        }

        public Gtk.Justification get_justification_at_iter (TextIter iter) {
            if (iter_has_style (iter, "align-center"))
                return Gtk.Justification.CENTER;
            else if (iter_has_style (iter, "align-right"))
                return Gtk.Justification.RIGHT;
            else if (iter_has_style (iter, "align-fill"))
                return Gtk.Justification.FILL;
            else
                return Gtk.Justification.LEFT;
        }

        public int get_justification_as_int () {
            if (has_selection)
                return get_selection_range ().get_justification_as_int ();
            else
                return get_justification_as_int_at_iter (get_cursor ());
        }

        public int get_justification_as_int_at_iter (TextIter iter) {
            if (iter_has_style (iter, "align-center"))
                return 1;
            else if (iter_has_style (iter, "align-right"))
                return 2;
            else if (iter_has_style (iter, "align-fill"))
                return 3;
            else
                return 0;
        }

        public void set_justification (string align) {
            get_paragraph (get_cursor ()).set_justification (align);
        }


        public void set_font (FontDescription font) {
            get_selection_range ().set_font (font);
        }

        public void set_font_from_string (string font) {
            get_selection_range ().set_font_from_string (font);
        }


        public void set_font_color (Gdk.Color color) {
            get_selection_range ().set_font_color (color);
        }

        public void set_font_color_from_string (string color) {
            get_selection_range ().set_font_color_from_string (color);
        }



        /*
         * Inserts
         */

        public void insert_comment () {
            print ("Insert Comment\n");
        }

        public void insert_image () {
            print ("Insert Image\n");
        }

        public void insert_link () {
            print ("Insert Link\n");
        }

        public void insert_table (int cols, int rows) {
            print ("Insert Table of %d columns by %d rows\n", cols, rows);
        }


        public void insert_line () {
            TextIter insert_cursor = get_cursor ();
            insert_line_at_iter (insert_cursor);
        }

        public void insert_paragraph () {
            TextIter cursor = get_cursor ();
            insert_paragraph_at_iter (cursor);
        }

        public void insert_line_at_iter (TextIter iter) {
            //TODO
            // Cursor does not move to next line
            // Only happens when inserting at the end of the buffer
            // Bug reported in GTK+

            insert (ref iter, "\u2028", -1);
        }

        public void insert_paragraph_at_iter (TextIter iter) {
            insert (ref iter, "\n", -1);
        }




        /*
         * paragraphs
         */

        // Moves iter to the start of the paragraph it is located in
        public TextIter get_paragraph_start (TextIter iter) {
            iter.backward_find_char ((char) => {
                if (char.to_string () == "\n")
                    return true;
                else
                    return false;
            }, null);
            return iter;
        }

        // Moves iter to the end of the paragraph it is located in
        public TextIter get_paragraph_end (TextIter iter) {
            iter.forward_find_char ((char) => {
                if (char.to_string () == "\n")
                    return true;
                else
                    return false;
            }, null);
            return iter;
        }

        public TextRange get_paragraph (TextIter iter) {
            TextIter start = get_paragraph_start (iter);
            TextIter end = get_paragraph_end (iter);

            return new TextRange (this, start, end);
        }




        /*
         * Search
         */

        public void search (string text) {
            if (text.length > 0)
                search_string (text);
            else
                clear_search ();
        }

        public void search_string (string text) {
            print ("Searching for: %s\n", text);
        }

        public void clear_search () {
            print ("Clearing search");
        }



        /*
         * Utilities
         */

        public TextIter get_cursor () {
            TextIter iter;
            get_iter_at_mark (out iter, get_insert ());
            return iter;
        }

        public TextRange get_cursor_as_range () {
            var cursor = get_cursor ();
            return new TextRange (this, cursor, cursor);
        }

        public TextRange get_selection_range () {
            TextIter start; TextIter end;
            get_selection_bounds (out start, out end);
            return new TextRange (this, start, end);
        }

        public bool iter_has_tag (TextIter iter, TextTag tag) {
            return iter.has_tag (tag) || iter.begins_tag (tag) || iter.ends_tag (tag);
        }

        public bool iter_has_style (TextIter iter, string name) {
            var tag = tag_table.lookup (name);
            return iter_has_tag (iter, tag);
        }

        public TextIter copy_iter (TextIter iter) {
            TextIter copy;
            get_iter_at_offset (out copy, iter.get_offset ());
            return copy;
        }



        private void update_styles () {
            var iter = get_cursor();

            style_bold = iter_has_style (iter, "bold");
            style_italic = iter_has_style (iter, "italic");
            style_underline = iter_has_style (iter, "underline");
            style_strikethrough = iter_has_style (iter, "strikethrough");
        }

        /*
         * Signal callbacks
         */

        private void cursor_moved_callback () {
            // Emit 'cursor_moved' signal
            update_styles ();
            cursor_moved ();
        }

        private void text_inserted_callback (TextIter cursor, string new_text, int length) {
            TextIter previous;
            get_iter_at_offset (out previous, cursor.get_offset () - length);

            if (style_bold)
                apply_tag_by_name ("bold", previous, cursor);
            if (style_italic)
                apply_tag_by_name ("italic", previous, cursor);
            if (style_underline)
                apply_tag_by_name ("underline", previous, cursor);
            if (style_strikethrough)
                apply_tag_by_name ("strikethrough", previous, cursor);


            // Emit signals
            text_inserted ();
            cursor_moved ();
        }

        private bool key_press_callback (EventKey event) {
            if (Gdk.keyval_name (event.keyval) == "Return") {
                ModifierType modifiers = Gtk.accelerator_get_default_mod_mask ();

                if ((event.state & modifiers) == Gdk.ModifierType.SHIFT_MASK)
                    insert_line ();
                else
                    insert_paragraph ();

                return true;
            }
            else {
                return false;
            }
        }

        private bool editor_clicked_callback (EventButton event) {
            update_styles ();
            cursor_moved ();

            return false;
        }

    }
}