summaryrefslogtreecommitdiff
path: root/src/gui/preferencesWindow.vala
blob: 933919ba51dbf083e46922ba9bd99e01532995ab (plain)
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
/* 
Copyright (c) 2011 by Simon Schneegans

This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

You should have received a copy of the GNU General Public License along with
this program.  If not, see <http://www.gnu.org/licenses/>. 
*/

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////    
/// The settings menu of Gnome-Pie.
/////////////////////////////////////////////////////////////////////////

public class PreferencesWindow : GLib.Object {
    
    /////////////////////////////////////////////////////////////////////
    /// The ID of the currently selected Pie.
    /////////////////////////////////////////////////////////////////////
    
    private string selected_id = "";
    
    /////////////////////////////////////////////////////////////////////
    /// Some Gtk widgets used by this window.
    /////////////////////////////////////////////////////////////////////
    
    private Gtk.Window? window = null;
    private Gtk.Label? id_label = null;
    private Gtk.Label? name_label = null;
    private Gtk.Label? hotkey_label = null;
    private Gtk.Label? no_pie_label = null;
    private Gtk.Label? no_slice_label = null;
    private Gtk.VBox? preview_box = null;
    private Gtk.Image? icon = null;
    private Gtk.EventBox? preview_background = null;
    private Gtk.Button? icon_button = null;
    private Gtk.Button? name_button = null;
    private Gtk.Button? hotkey_button = null;
    private Gtk.ToolButton? remove_pie_button = null;
    
    /////////////////////////////////////////////////////////////////////
    /// Some custom widgets and dialogs used by this window.
    /////////////////////////////////////////////////////////////////////
    
    private PiePreview? preview = null;
    private PieList? pie_list = null;
    private SettingsWindow? settings_window = null;
    private TriggerSelectWindow? trigger_window = null;
    private IconSelectWindow? icon_window = null;
    private RenameWindow? rename_window = null;
    
    /////////////////////////////////////////////////////////////////////
    /// C'tor, creates the window.
    /////////////////////////////////////////////////////////////////////
    
    public PreferencesWindow() {
        try {
            var builder = new Gtk.Builder();

            builder.add_from_file (Paths.ui_files + "/preferences.ui");

            this.window = builder.get_object("window") as Gtk.Window;
            
            this.window.add_events(Gdk.EventMask.BUTTON_RELEASE_MASK |
                        Gdk.EventMask.KEY_RELEASE_MASK |
                        Gdk.EventMask.KEY_PRESS_MASK |
                        Gdk.EventMask.POINTER_MOTION_MASK);
            
            #if HAVE_GTK_3
                var toolbar = builder.get_object ("toolbar") as Gtk.Widget;
                toolbar.get_style_context().add_class("primary-toolbar");
                
                var inline_toolbar = builder.get_object ("pies-toolbar") as Gtk.Widget;
                inline_toolbar.get_style_context().add_class("inline-toolbar");
            #endif
            
            this.pie_list = new PieList();
            this.pie_list.on_select.connect(this.on_pie_select);
            
            var scroll_area = builder.get_object("pies-scrolledwindow") as Gtk.ScrolledWindow;
            scroll_area.add(this.pie_list);
            
            this.preview = new PiePreview();
            this.preview.on_first_slice_added.connect(() => {
                this.no_slice_label.hide();
            });
            
            this.preview.on_last_slice_removed.connect(() => {
                this.no_slice_label.show();
            });
            
            preview_box = builder.get_object("preview-box") as Gtk.VBox;
            this.preview_box.pack_start(preview, true, true);
            
            this.id_label = builder.get_object("id-label") as Gtk.Label;
            this.name_label = builder.get_object("pie-name-label") as Gtk.Label;
            this.hotkey_label = builder.get_object("hotkey-label") as Gtk.Label;
            this.no_pie_label = builder.get_object("no-pie-label") as Gtk.Label;
            this.no_slice_label = builder.get_object("no-slice-label") as Gtk.Label;
            this.icon = builder.get_object("icon") as Gtk.Image;
            this.preview_background = builder.get_object("preview-background") as Gtk.EventBox;
                    
            (builder.get_object("settings-button") as Gtk.ToolButton).clicked.connect(on_settings_button_clicked);
            
            this.hotkey_button = builder.get_object("key-button") as Gtk.Button;
            this.hotkey_button.clicked.connect(on_key_button_clicked);
            
            this.icon_button = builder.get_object("icon-button") as Gtk.Button;
            this.icon_button.clicked.connect(on_icon_button_clicked);
            
            this.name_button = builder.get_object("rename-button") as Gtk.Button;
            this.name_button.clicked.connect(on_rename_button_clicked);
            
            this.remove_pie_button = builder.get_object("remove-pie-button") as Gtk.ToolButton;
            this.remove_pie_button.clicked.connect(on_remove_pie_button_clicked);
            
            (builder.get_object("add-pie-button") as Gtk.ToolButton).clicked.connect(on_add_pie_button_clicked);
            
            this.window.hide.connect(() => {
                // save settings on close
                Config.global.save();
                Pies.save();
                
                Timeout.add(100, () => {
                    IconSelectWindow.clear_icons();
                    return false;
                });
            });
            
            this.window.delete_event.connect(this.window.hide_on_delete);
                
        } catch (GLib.Error e) {
            error("Could not load UI: %s\n", e.message);
        }
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Shows the window.
    /////////////////////////////////////////////////////////////////////
    
    public void show() {
        this.preview.draw_loop();
        this.window.show_all();
        this.pie_list.select_first();
        this.preview_background.modify_bg(Gtk.StateType.NORMAL, Gtk.rc_get_style(this.window).light[0]);
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when a new Pie is selected in the PieList.
    /////////////////////////////////////////////////////////////////////
    
    private void on_pie_select(string id) {
        selected_id = id;
        
        this.no_slice_label.hide();
        this.no_pie_label.hide();
        this.preview_box.hide();
        
        this.name_button.sensitive = false;
        this.hotkey_button.sensitive = false;
        this.icon_button.sensitive = false;
        this.remove_pie_button.sensitive = false;
        
        if (id == "") {
            this.id_label.label = "";
            this.name_label.label = _("No Pie selected.");
            this.hotkey_label.set_markup("");
            this.icon.icon_name = "application-default-icon";

            this.no_pie_label.show();
        } else {
            var pie = PieManager.all_pies[selected_id];
            this.id_label.label = ("ID: %s").printf(pie.id);
            this.name_label.label = PieManager.get_name_of(pie.id);
            this.hotkey_label.set_markup(PieManager.get_accelerator_label_of(pie.id));
            
            if (pie.icon.contains("/"))
                try {
                    this.icon.pixbuf = new Gdk.Pixbuf.from_file_at_scale(pie.icon, 
                                            this.icon.get_pixel_size(), this.icon.get_pixel_size(), true);
                } catch (GLib.Error error) {
                    warning(error.message);
                }
            else
                this.icon.icon_name = pie.icon;
            
            this.preview.set_pie(id);
            this.preview_box.show();
            
            if (pie.action_groups.size == 0) {
                this.no_slice_label.show();
            }
            
            this.name_button.sensitive = true;
            this.hotkey_button.sensitive = true;
            this.icon_button.sensitive = true;
            this.remove_pie_button.sensitive = true;
        }
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when the add Pie button is clicked.
    /////////////////////////////////////////////////////////////////////
    
    private void on_add_pie_button_clicked(Gtk.ToolButton button) {
        var new_pie = PieManager.create_persistent_pie(_("New Pie"), "application-default-icon", null);
        this.pie_list.reload_all();
        this.pie_list.select(new_pie.id);
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when the remove Pie button is clicked.
    /////////////////////////////////////////////////////////////////////
    
    private void on_remove_pie_button_clicked(Gtk.ToolButton button) {
        if (this.selected_id != "") {
            var dialog = new Gtk.MessageDialog((Gtk.Window)this.window.get_toplevel(), Gtk.DialogFlags.MODAL,
                         Gtk.MessageType.QUESTION, Gtk.ButtonsType.YES_NO,
                         _("Do you really want to delete the selected Pie with all contained Slices?"));
                                                     
            dialog.response.connect((response) => {
                if (response == Gtk.ResponseType.YES) {
                    PieManager.remove_pie(selected_id);
                    this.pie_list.reload_all();
                    this.pie_list.select_first();
                }
            });
            
            dialog.run();
            dialog.destroy();
        }
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when rename Pie button is clicked.
    /////////////////////////////////////////////////////////////////////
    
    private void on_rename_button_clicked(Gtk.Button button) {
        if (this.rename_window == null) {
            this.rename_window = new RenameWindow();
            this.rename_window.set_parent(window);
            this.rename_window.on_ok.connect((name) => {
                var pie = PieManager.all_pies[selected_id];
                pie.name = name;
                PieManager.create_launcher(pie.id);
                this.name_label.label = name;
                this.pie_list.reload_all();
            });
        }
        
        this.rename_window.set_pie(selected_id);
        this.rename_window.show();
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when the hotkey button is clicked.
    /////////////////////////////////////////////////////////////////////
    
    private void on_key_button_clicked(Gtk.Button button) {
        if (this.trigger_window == null) {
            this.trigger_window = new TriggerSelectWindow();
            this.trigger_window.set_parent(window);
            this.trigger_window.on_ok.connect((trigger) => {
                PieManager.bind_trigger(trigger, selected_id);
                this.hotkey_label.set_markup(trigger.label_with_specials);
            });
        }
        
        this.trigger_window.set_pie(selected_id);
        this.trigger_window.show();
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when the general settings button is clicked.
    /////////////////////////////////////////////////////////////////////
    
    private void on_settings_button_clicked(Gtk.ToolButton button) {
        if (this.settings_window == null) {
            this.settings_window = new SettingsWindow();
            this.settings_window.set_parent(this.window.get_toplevel() as Gtk.Window);
        }
        
        this.settings_window.show();
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when the icon button is clicked.
    /////////////////////////////////////////////////////////////////////
    
    private void on_icon_button_clicked(Gtk.Button button) {
        if (this.icon_window == null) {
            this.icon_window = new IconSelectWindow(this.window);
            this.icon_window.on_ok.connect((icon) => {
                var pie = PieManager.all_pies[selected_id];
                pie.icon = icon;
                PieManager.create_launcher(pie.id);
                this.pie_list.reload_all();
            });
        }
        
        this.icon_window.show();
        this.icon_window.set_icon(PieManager.all_pies[selected_id].icon);
    }
}

}