~writer-devs/writer/trunk

« back to all changes in this revision

Viewing changes to src/Widgets/EditorToolBar.vala

  • Committer: Tuur Dutoit
  • Date: 2014-10-26 10:41:33 UTC
  • Revision ID: me@tuurdutoit.be-20141026104133-gzvzgh1m6ntrrc9o
Renamed Editor to TextEditor and EditorToolBar to TextToolBar

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***
2
 
The MIT License (MIT)
3
 
 
4
 
Copyright (c) 2014 Tuur Dutoit
5
 
 
6
 
Permission is hereby granted, free of charge, to any person obtaining a copy
7
 
of this software and associated documentation files (the "Software"), to deal
8
 
in the Software without restriction, including without limitation the rights
9
 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 
copies of the Software, and to permit persons to whom the Software is
11
 
furnished to do so, subject to the following conditions:
12
 
 
13
 
The above copyright notice and this permission notice shall be included in all
14
 
copies or substantial portions of the Software.
15
 
 
16
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 
SOFTWARE.
23
 
***/
24
 
 
25
 
 
26
 
using Gtk;
27
 
using Gdk;
28
 
using Granite.Widgets;
29
 
 
30
 
namespace Writer.Widgets {
31
 
    public class EditorToolBar : Gtk.Toolbar {
32
 
    
33
 
        private Editor editor;
34
 
        public FontButton font_button;
35
 
        public ColorButton font_color_button;
36
 
        public ToggleButton bold_button;
37
 
        public ToggleButton italic_button;
38
 
        public ToggleButton underline_button;
39
 
        public ToggleButton strikethrough_button;
40
 
        public Button indent_more_button;
41
 
        public Button indent_less_button;
42
 
        public ModeButton align_button;
43
 
        public Popover insert_popover;
44
 
    
45
 
        public EditorToolBar (Editor editor) {
46
 
            this.get_style_context ().add_class ("writer-toolbar");
47
 
            
48
 
            this.editor = editor;
49
 
            editor.cursor_moved.connect (cursor_moved);
50
 
    
51
 
            setup_ui ();
52
 
        }
53
 
        
54
 
        public void setup_ui () {
55
 
        
56
 
            // Make Widgets
57
 
            
58
 
            var paragraph_combobox = new Gtk.ComboBoxText ();
59
 
                paragraph_combobox.append ("Paragraph", ("Paragraph"));
60
 
                paragraph_combobox.append ("Title", ("Title"));
61
 
                paragraph_combobox.append ("Subtitle", ("Subtitle"));
62
 
                paragraph_combobox.append ("Bullet List", ("Bullet List"));
63
 
                paragraph_combobox.append ("Dashed List", ("Dashed List"));
64
 
                paragraph_combobox.append ("Numbered List", ("Numbered List"));
65
 
                paragraph_combobox.append ("Two-Column", ("Two-Column"));
66
 
                paragraph_combobox.set_active_id ("Paragraph");
67
 
            var paragraph_item = new ToolItem ();
68
 
                paragraph_item.add (paragraph_combobox);
69
 
                
70
 
            var font_item = new ToolItem ();
71
 
                font_button = new Gtk.FontButton ();
72
 
                font_button.use_font = true;
73
 
                font_button.use_size = true;
74
 
                font_item.add (font_button);
75
 
 
76
 
            font_color_button = new Gtk.ColorButton ();
77
 
                font_color_button.use_alpha = false;
78
 
                font_color_button.set_title ("Choose a Font Color");
79
 
 
80
 
            var font_color_item = new Gtk.ToolItem ();
81
 
                font_color_item.add (font_color_button);
82
 
                
83
 
            var styles_item = new ToolItem ();
84
 
                var styles_buttons = new ButtonGroup ();
85
 
                    bold_button = new Gtk.ToggleButton ();
86
 
                        bold_button.add (new Image.from_icon_name ("format-text-bold-symbolic", Gtk.IconSize.MENU));
87
 
                        bold_button.focus_on_click = false;
88
 
                        styles_buttons.pack_start (bold_button);
89
 
                    italic_button = new Gtk.ToggleButton ();
90
 
                        italic_button.add (new Image.from_icon_name ("format-text-italic-symbolic", Gtk.IconSize.BUTTON));
91
 
                        styles_buttons.pack_start (italic_button);
92
 
                    underline_button = new Gtk.ToggleButton ();
93
 
                        underline_button.add (new Image.from_icon_name ("format-text-underline-symbolic", Gtk.IconSize.BUTTON));
94
 
                        styles_buttons.pack_start (underline_button);
95
 
                    strikethrough_button = new Gtk.ToggleButton ();
96
 
                        strikethrough_button.add (new Image.from_icon_name ("format-text-strikethrough-symbolic", Gtk.IconSize.BUTTON));
97
 
                        styles_buttons.pack_start (strikethrough_button);
98
 
                styles_item.add (styles_buttons);
99
 
 
100
 
                
101
 
            var align_item = new ToolItem ();
102
 
                align_button = new ModeButton ();
103
 
                    align_button.append (new Gtk.Image.from_icon_name ("format-justify-left-symbolic", Gtk.IconSize.BUTTON));
104
 
                    align_button.append (new Gtk.Image.from_icon_name ("format-justify-center-symbolic", Gtk.IconSize.BUTTON));
105
 
                    align_button.append (new Gtk.Image.from_icon_name ("format-justify-right-symbolic", Gtk.IconSize.BUTTON));
106
 
                    align_button.append (new Gtk.Image.from_icon_name ("format-justify-fill-symbolic", Gtk.IconSize.BUTTON));
107
 
                align_item.add (align_button);
108
 
            
109
 
            var indent_button = new ButtonGroup ();
110
 
                var indent_more_button = new Button.from_icon_name ("format-indent-more-symbolic", Gtk.IconSize.BUTTON);
111
 
                var indent_less_button = new Button.from_icon_name ("format-indent-less-symbolic", Gtk.IconSize.BUTTON);
112
 
                indent_button.add (indent_more_button);
113
 
                indent_button.add (indent_less_button);
114
 
            var indent_item = new Gtk.ToolItem ();
115
 
                indent_item.add (indent_button);
116
 
            
117
 
            var insert_button = new Gtk.Button.with_label ("Insert");
118
 
            insert_popover = new Gtk.Popover (insert_button);
119
 
            var insert_popover_content = new Gtk.Grid ();
120
 
                insert_popover_content.column_spacing = 6;
121
 
                insert_popover_content.row_spacing = 12;
122
 
                var comment_button = new Gtk.Button.with_label ("Comment");
123
 
                    insert_popover_content.attach (comment_button, 0, 0, 1, 1);
124
 
                var picture_button = new Gtk.Button.with_label ("Picture");
125
 
                    insert_popover_content.attach  (picture_button, 1, 0, 1, 1);
126
 
                var link_button = new Gtk.Button.with_label ("Link");
127
 
                    insert_popover_content.attach  (link_button, 2, 0, 1, 1);
128
 
                var table_chooser = new TableChooser ();
129
 
                    insert_popover_content.attach  (table_chooser, 0, 1, 3, 1);
130
 
                    
131
 
                insert_popover.set_position (Gtk.PositionType.BOTTOM);
132
 
                insert_popover.set_border_width (12);
133
 
                insert_popover.add (insert_popover_content);
134
 
                insert_popover.show_all ();
135
 
                insert_popover.hide ();
136
 
                
137
 
            var insert_item = new Gtk.ToolItem ();
138
 
                insert_item.add (insert_button);
139
 
            
140
 
            
141
 
            //Set border_width on ToolItems
142
 
            paragraph_item.border_width = 5;
143
 
            font_item.border_width = 5;
144
 
            font_color_item.border_width = 5;
145
 
            styles_item.border_width = 5;
146
 
            align_item.border_width = 5;
147
 
            indent_item.border_width = 5;
148
 
            insert_item.border_width = 5;
149
 
            
150
 
            // Add Widgets
151
 
            this.add (paragraph_item);
152
 
            this.add (font_item);
153
 
            this.add (font_color_item);
154
 
            this.add (styles_item);
155
 
            this.add (align_item);
156
 
            this.add (indent_item);
157
 
            this.add (insert_item);
158
 
            
159
 
            
160
 
            
161
 
            // Connect signals
162
 
            
163
 
            insert_button.clicked.connect (() => {
164
 
                insert_popover.show ();
165
 
            });
166
 
            
167
 
            comment_button.clicked.connect (editor.insert_comment);
168
 
            picture_button.clicked.connect (editor.insert_image);
169
 
            link_button.clicked.connect (editor.insert_link);
170
 
            table_chooser.selected.connect (editor.insert_table);
171
 
            
172
 
            align_button.mode_changed.connect (() => {
173
 
                change_align (align_button.selected);
174
 
            });
175
 
            
176
 
            font_button.font_set.connect (() => {
177
 
                editor.set_font_from_string (font_button.get_font_name ());
178
 
            });
179
 
            font_color_button.color_set.connect (() => {
180
 
                Gdk.Color color;
181
 
                font_color_button.get_color (out color);
182
 
                editor.set_font_color (color);
183
 
            });
184
 
            
185
 
            bold_button.button_press_event.connect ((event) => {
186
 
                if (event.type == EventType.BUTTON_PRESS)
187
 
                    editor.toggle_style ("bold");
188
 
                return false;
189
 
            });
190
 
            italic_button.button_press_event.connect ((event) => {
191
 
                if (event.type == EventType.BUTTON_PRESS)
192
 
                    editor.toggle_style ("italic");
193
 
                return false;
194
 
            });
195
 
            underline_button.button_press_event.connect ((event) => {
196
 
                if (event.type == EventType.BUTTON_PRESS)
197
 
                    editor.toggle_style ("underline");
198
 
                return false;
199
 
            });
200
 
            strikethrough_button.button_press_event.connect ((event) => {
201
 
                if (event.type == EventType.BUTTON_PRESS)
202
 
                    editor.toggle_style ("strikethrough");
203
 
                return false;
204
 
            });
205
 
            
206
 
        }
207
 
        
208
 
        
209
 
        
210
 
        
211
 
        /*
212
 
         * Signal callbacks
213
 
         */
214
 
        
215
 
        public void change_align (int index) {
216
 
            switch (index) {
217
 
                case 1:
218
 
                    editor.set_justification ("center"); break;
219
 
                case 2:
220
 
                    editor.set_justification ("right"); break;
221
 
                case 3:
222
 
                    editor.set_justification ("fill"); break;
223
 
                default:
224
 
                    editor.set_justification ("left"); break;
225
 
            }
226
 
        }
227
 
         
228
 
        public void cursor_moved () {
229
 
            bold_button.active = editor.has_style ("bold");
230
 
            italic_button.active = editor.has_style ("italic");
231
 
            underline_button.active = editor.has_style ("underline");
232
 
            strikethrough_button.active = editor.has_style ("strikethrough");
233
 
            
234
 
            align_button.selected = editor.get_justification_as_int ();
235
 
            
236
 
            //TODO
237
 
            // Update font and color buttons
238
 
        }
239
 
 
240
 
    }
241
 
}
 
 
b'\\ No newline at end of file'