Debian Codegraph @

src/widgets/NoteWindow.vala

raw
1 /*
2 * Copyright (c) 2017-2019 José Amuedo (https://github.com/spheras)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /**
19 * @class
20 * Folder Window that is shown above the desktop to manage files and folders
21 */
22 public class DesktopFolder.NoteWindow : Gtk.ApplicationWindow {
23 private NoteManager manager = null;
24 private Gtk.Menu menu = null; // Context menu
25 private Gtk.TextView text = null;
26 private Cairo.Pattern texture_pattern = null;
27 private Cairo.Surface clip_surface = null; // The clip image
28 private Gtk.Button trash_button = null;
29 private DesktopFolder.EditableLabel label = null;
30
31 private const string HEAD_TAGS_COLORS[3] = { null, "#ffffff", "#000000" };
32 private const string HEAD_TAGS_COLORS_CLASS[3] = { "df_headless", "df_light", "df_dark" };
33 private const string BODY_TAGS_COLORS[10] = { null, "#ffe16b", "#ffa154", "#795548", "#9bdb4d", "#64baff", "#ad65d6", "#ed5353", "#d4d4d4", "#000000" };
34 private const string BODY_TAGS_COLORS_CLASS[10] = { "df_transparent", "df_yellow", "df_orange", "df_brown", "df_green", "df_blue", "df_purple", "df_red", "df_gray", "df_black" };
35 private string last_custom_color = "#FF0000";
36 private Gtk.CssProvider custom_color_provider = new Gtk.CssProvider ();
37
38 /** flag to know if the window was painted /packed already */
39 private bool flag_realized = false;
40
41 construct {
42 this.hide_titlebar_when_maximized = false;
43
44 stick ();
45 }
46
47 /**
48 * @constructor
49 * @param NoteManager manager The manager of this window
50 */
51 public NoteWindow (NoteManager manager) {
52 Object (
53 application: manager.get_application (),
54 icon_name: "com.github.spheras.desktopfolder",
55 resizable: true,
56 accept_focus: true,
57 decorated: true,
58 title: (manager.get_note_name ()),
59 type_hint: Gdk.WindowTypeHint.NORMAL,
60 deletable: false,
61 height_request: 140,
62 width_request: 160
63 );
64
65 this.check_window_type ();
66 this.manager = manager;
67 this.name = manager.get_application ().get_next_id ();
68 DesktopManager desktop_manager = manager.get_application ().get_fake_desktop ();
69 if (desktop_manager != null) {
70 this.set_transient_for (desktop_manager.get_view ());
71 }
72
73 this.trash_button = new Gtk.Button.from_icon_name ("edit-delete-symbolic");
74 trash_button.has_tooltip = true;
75 trash_button.tooltip_text = DesktopFolder.Lang.DESKTOPFOLDER_DELETE_TOOLTIP;
76 trash_button.get_image ().get_style_context ().add_class ("df_titlebar_button");
77 trash_button.get_image ().get_style_context ().add_class ("df_titlebar_button_hidden");
78 this.trash_button.enter_notify_event.connect (() => {
79 this.trash_button.get_image ().get_style_context ().add_class ("df_titlebar_button_hover");
80 return true;
81 });
82 this.trash_button.leave_notify_event.connect (() => {
83 this.trash_button.get_image ().get_style_context ().remove_class ("df_titlebar_button_hover");
84 return true;
85 });
86
87 this.create_headerbar ();
88
89 Gtk.Box box = new Gtk.Box (Gtk.Orientation.VERTICAL, 1);
90 box.get_style_context ().add_class ("df_note_container");
91 box.set_border_width (20);
92 this.add (box);
93
94 Gtk.ScrolledWindow scrolled = new Gtk.ScrolledWindow (null, null);
95 scrolled.get_style_context ().add_class ("df_note_scroll");
96 box.pack_start (scrolled, true, true, 0);
97
98 this.text = new Gtk.SourceView (); // Gtk.TextView ();
99 this.text.set_wrap_mode (Gtk.WrapMode.WORD);
100 this.text.get_style_context ().add_class ("df_note_text");
101 this.text.buffer.text = this.manager.get_settings ().text;
102 scrolled.add (this.text);
103
104 // important to load settings 2 times, now and after realized event
105 this.reload_settings ();
106 this.realize.connect (() => {
107 if (!this.flag_realized) {
108 this.flag_realized = true;
109 // we need to reload settings to ensure that it get the real sizes and positiions
110 this.reload_settings ();
111 }
112 });
113
114 this.show_all ();
115
116 this.check_on_top ();
117
118 // this.configure_event.connect (this.on_configure);
119 this.button_press_event.connect (this.on_press);
120 this.button_release_event.connect (this.on_release);
121 this.draw.connect (this.draw_background);
122
123 this.enter_notify_event.connect (this.on_enter_notify);
124 this.leave_notify_event.connect (this.on_leave_notify);
125 this.text.add_events (Gdk.EventMask.ENTER_NOTIFY_MASK);
126 this.text.add_events (Gdk.EventMask.LEAVE_NOTIFY_MASK);
127 this.text.enter_notify_event.connect (this.on_enter_notify);
128 this.text.leave_notify_event.connect (this.on_leave_notify);
129
130 trash_button.clicked.connect (this.manager.trash);
131
132 this.text.focus_out_event.connect (this.on_focus_out);
133 // this.key_release_event.connect(this.on_key);
134
135 // TODO: Does the GTK window have any active signal or css :active state?
136 Wnck.Screen screen = Wnck.Screen.get_default ();
137 screen.active_window_changed.connect (on_active_change);
138
139 NoteSettings settings = this.manager.get_settings ();
140
141 if (settings.edit_label_on_creation) {
142 GLib.Timeout.add (50, () => {
143 this.label.start_editing ();
144 settings.edit_label_on_creation = false;
145 return false;
146 });
147 }
148 }
149
150
151 /**
152 * @name create_headerbar
153 * @description create the header bar
154 */
155 protected virtual void create_headerbar () {
156 // debug("Create headerbar for %s",this.manager.get_folder_name ());
157
158 var header = new Gtk.HeaderBar ();
159 header.height_request = DesktopFolder.HEADERBAR_HEIGHT;
160 header.has_subtitle = false;
161 this.label = new DesktopFolder.EditableLabel (manager.get_note_name ());
162 this.label.set_margin_top (10);
163 this.label.set_margin_end (40);
164 this.label.show_popup.connect ((event) => { this.show_popup (event); return true; });
165 this.label.get_style_context ().add_class ("title");
166 header.set_custom_title (this.label);
167 header.pack_start (trash_button);
168 header.set_decoration_layout ("");
169 this.set_titlebar (header);
170
171 this.label.changed.connect ((new_name) => {
172 if (this.manager.rename (new_name)) {
173 this.label.text = new_name;
174 }
175 });
176
177 header.button_press_event.connect ((event) => {
178 this.type_hint = Gdk.WindowTypeHint.NORMAL;
179 this.begin_move_drag ((int) event.button, (int) event.x_root, (int) event.y_root, event.time);
180 return false;
181 });
182 }
183
184 /**
185 * @name fade_in
186 * @description fade the view in. does not call show
187 */
188 public void fade_in () {
189 this.get_style_context ().remove_class ("df_fadeout");
190 this.get_style_context ().add_class ("df_fadein");
191 }
192
193 /**
194 * @name fade_out
195 * @description fade the view out. does not call hide
196 */
197 public void fade_out () {
198 this.get_style_context ().remove_class ("df_fadein");
199 this.get_style_context ().add_class ("df_fadeout");
200 }
201
202 /**
203 * @name move_to
204 * @description move the window to other position
205 */
206 protected virtual void move_to (int x, int y) {
207 // debug ("MOVE_TO: %d,%d", x, y);
208 this.move (x, y);
209 }
210
211 /**
212 * @name resize_to
213 * @description resize the window to other position
214 */
215 protected virtual void resize_to (int width, int height) {
216 // debug ("RESIZE_TO: %d,%d", width, height);
217 this.set_default_size (width, height);
218 this.resize (width, height);
219 }
220
221 /**
222 * @name reload_settings
223 * @description reload the window style in general
224 */
225 public void reload_settings () {
226 NoteSettings settings = this.manager.get_settings ();
227 if (settings.w > 0) {
228 this.resize_to (settings.w, settings.h);
229 }
230 if (settings.x > 0 || settings.y > 0) {
231 this.move_to (settings.x, settings.y);
232 }
233
234 List <unowned string> classes = this.get_style_context ().list_classes ();
235 foreach (string class in classes) {
236 if (class.has_prefix ("df_")) {
237 this.get_style_context ().remove_class (class);
238 }
239 }
240
241 this.get_style_context ().add_class ("df_folder");
242 this.get_style_context ().add_class ("df_note");
243 this.get_style_context ().add_class ("df_shadow");
244
245 this.get_style_context ().add_class ("df_fadingwindow");
246 if (this.manager.get_application ().get_desktop_visibility ()) {
247 this.get_style_context ().add_class ("df_fadein");
248 // setting opacity to stop the folder window flashing at startup
249 this.opacity = 1;
250 } else {
251 this.get_style_context ().add_class ("df_fadeout");
252 // ditto
253 this.opacity = 0;
254 }
255
256 // applying existing colors configuration
257 if (settings.bgcolor.has_prefix ("rgb")) {
258 string custom = settings.bgcolor;
259 this.set_custom_color (custom);
260 } else {
261 Gdk.RGBA rgba = Gdk.RGBA ();
262 rgba.parse (this.get_color_for_class (settings.bgcolor));
263 rgba.alpha = 0.6;
264 this.last_custom_color = rgba.to_string ();
265 this.get_style_context ().add_class (settings.bgcolor);
266 }
267 this.get_style_context ().add_class (settings.fgcolor);
268
269 this.check_on_top ();
270 }
271
272 /**
273 * @name get_color_for_class
274 * @description return the correct color for a certain class
275 * @param {string} class the class to obtain (@see BODY_TAGS_COLORS_CLASS)
276 * @return {string} the color for the class passed
277 */
278 private string get_color_for_class (string class) {
279 if (class == "df_transparent") {
280 return "rgba(0,0,0,0)";
281 } else {
282 for (int i = 0; i < BODY_TAGS_COLORS_CLASS.length; i++) {
283 if (BODY_TAGS_COLORS_CLASS[i] == class) {
284 return BODY_TAGS_COLORS[i];
285 }
286 }
287 }
288 return "rgba(0,0,0)";
289 }
290
291 /**
292 * @name set_custom_color
293 * @description utility function to set a custom color for the window
294 * @param {string} custom the custom color to set (rgba(....))
295 * @return {string} the real custom color applied
296 */
297 private string set_custom_color (string custom) {
298 for (int i = 0; i < BODY_TAGS_COLORS_CLASS.length; i++) {
299 string scolor = BODY_TAGS_COLORS_CLASS[i];
300 this.get_style_context ().remove_class (scolor);
301 }
302
303 Gdk.RGBA rgba = Gdk.RGBA ();
304 rgba.parse (custom);
305 string mycustom = custom;
306 if (rgba.alpha == 1) {
307 // this solves a bug wen setting an opaque color to gtk and vice
308 rgba.alpha = 0.999;
309 mycustom = rgba.to_string ();
310 }
311
312 Gtk.StyleContext.remove_provider_for_screen (Gdk.Screen.get_default (), this.custom_color_provider);
313 try {
314 string css =
315 "#" + this.name + """.df_folder.window-frame, #""" + this.name + """.df_folder.window-frame:backdrop {
316 border-color: """ + mycustom + """;
317 }
318 #""" + this.name + """.df_folder.background, #""" + this.name + """.df_folder.background:backdrop {
319 background-color: """ + mycustom + """;
320 }
321 #""" + this.name + """.df_folder .titlebar, #""" + this.name + """.df_folder .titlebar:backdrop{
322 background-color: """ + mycustom + """;
323 border-color: """ + mycustom + """;
324 }""";
325 // debug("applying css:\n %s",css);
326 this.custom_color_provider.load_from_data (css);
327 } catch (Error e) {
328 stderr.printf ("Error: %s\n", e.message);
329 DesktopFolder.Util.show_error_dialog ("Error", e.message);
330 }
331 Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), this.custom_color_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);
332 this.last_custom_color = mycustom;
333 return mycustom;
334 }
335
336 /**
337 * @name on_active_change
338 * @description the screen actived window has change signal
339 * @param {Wnck.Window} the previous actived window
340 */
341 private void on_active_change (Wnck.Window ? previous) {
342 string sclass = "df_active";
343 Gtk.StyleContext style = this.get_style_context ();
344 if (this.is_active) {
345 if (!style.has_class (sclass)) {
346 // debug("active");
347 style.add_class ("df_active");
348 this.check_window_type ();
349 // we need to force a queue_draw
350 this.queue_draw ();
351 this.text.queue_draw ();
352 }
353 } else {
354 if (style.has_class (sclass)) {
355 // debug("inactive");
356 style.remove_class ("df_active");
357 // we need to force a queue_draw
358 this.queue_draw ();
359 this.text.queue_draw ();
360 }
361 }
362
363 this.save_current_position_and_size ();
364 }
365
366 /**
367 * @name save_position_and_size
368 * @description save the current position and size of the window
369 */
370 public void save_current_position_and_size () {
371 // we are saving here the last position and size
372 // we avoid doing it at on_configure because it launches a lot of events
373 int w = 0;
374 int h = 0;
375 this.get_size (out w, out h);
376
377 int x = 0;
378 int y = 0;
379 this.get_position (out x, out y);
380
381 // debug ("set_new_shape:%i,%i,%i,%i", x, y, w, h);
382 this.manager.set_new_shape (x, y, w, h);
383 }
384
385 /**
386 * @name on_focus_out
387 * @description focus out event
388 * @param {Gdk.EventFocus} event the event launched
389 * @return bool @see focus_out_event signal
390 */
391 private bool on_focus_out (Gdk.EventFocus event) {
392 // This is to avoid minimization when Show Desktop shortcut is used
393 // TODO: Is there a way to make a desktop window resizable and movable?
394 this.check_window_type ();
395
396 var buffer = this.text.get_buffer ();
397 var text = buffer.text;
398 var saved_text = this.manager.get_settings ().text;
399 if (text != saved_text) {
400 this.manager.on_text_change (text);
401 }
402 return false;
403 }
404
405 /**
406 * @name on_enter_notify
407 * @description On mouse entering the window
408 */
409 private bool on_enter_notify (Gdk.EventCrossing event) {
410 // debug("NOTEWINDOW ENTER notify");
411 trash_button.get_image ().get_style_context ().remove_class ("df_titlebar_button_hidden");
412 return true;
413 }
414
415 /**
416 * @name on_enter_leave
417 * @description On mouse leaving the window
418 */
419 private bool on_leave_notify (Gdk.EventCrossing event) {
420 // debug("NOTEWINDOW LEAVE notify");
421 if (event.detail == Gdk.NotifyType.ANCESTOR || event.detail == Gdk.NotifyType.VIRTUAL || event.detail == Gdk.NotifyType.INFERIOR) {
422 return false;
423 }
424 trash_button.get_image ().get_style_context ().add_class ("df_titlebar_button_hidden");
425 return true;
426 }
427
428 /**
429 * @name on_release
430 * @description release event captured.
431 * @return bool @see widget on_release signal
432 */
433 private bool on_release (Gdk.EventButton event) {
434 // debug ("window release");
435 // This is to avoid minimization when Show Desktop shortcut is used
436 // TODO: Is there a way to make a desktop window resizable and movable?
437 this.check_window_type ();
438 return false;
439 }
440
441 /**
442 * @name on_press
443 * @description press event captured. The Window should show the popup on right button
444 * @return bool @see widget on_press signal
445 */
446 private bool on_press (Gdk.EventButton event) {
447 // debug("window press");
448
449 // This is to allow moving and resizing the panel
450 // TODO: Is there a way to make a desktop window resizable and movable?
451 this.type_hint = Gdk.WindowTypeHint.NORMAL;
452
453 // debug("press:%i,%i",(int)event.button,(int)event.y);
454 if (event.type == Gdk.EventType.BUTTON_PRESS &&
455 (event.button == Gdk.BUTTON_SECONDARY)) {
456 this.show_popup (event);
457 } else if (event.type == Gdk.EventType.BUTTON_PRESS && (event.button == Gdk.BUTTON_PRIMARY)) {
458 // int width = this.get_allocated_width ();
459 // int height = this.get_allocated_height ();
460 // debug("x:%d,y:%d,width:%d,height:%d",(int)event.x,(int) event.y,width,height);
461
462 /*
463 if (event.x > 11 && event.y > 11) {
464 // the corner need some extra space
465 if (!(event.x < 31 && event.y < 31)) {
466 this.flag_dragging=true;
467 this.begin_move_drag ((int) event.button, (int) event.x_root, (int) event.y_root, event.time);
468 }
469 }
470 */
471 }
472 return false;
473 }
474
475 /**
476 * @name show_popup
477 * @description build and show the popup menu
478 * @param event EventButton the origin event, needed to position the menu
479 */
480 private void show_popup (Gdk.EventButton event) {
481 this.check_window_type ();
482
483 this.menu = new Gtk.Menu ();
484
485 if (!this.manager.get_application ().get_desktoppanel_enabled ()) {
486 Gtk.MenuItem item_new = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_SUBMENU);
487 menu.append (item_new);
488
489 Gtk.Menu newmenu = new Gtk.Menu ();
490 item_new.set_submenu (newmenu);
491
492 Gtk.MenuItem item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_DESKTOP_FOLDER);
493 item.activate.connect ((item) => {
494 this.new_desktop_folder ((int) event.x, (int) event.y);
495 });
496 newmenu.append (item);
497
498 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_LINK_PANEL);
499 item.activate.connect ((item) => {
500 this.new_link_panel ((int) event.x, (int) event.y);
501 });
502 newmenu.append (item);
503
504 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_NOTE);
505 item.activate.connect ((item) => {
506 this.new_note ((int) event.x, (int) event.y);
507 });
508 newmenu.append (item);
509
510 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_NEW_PHOTO);
511 item.activate.connect ((item) => {
512 this.new_photo ((int) event.x, (int) event.y);
513 });
514 newmenu.append (item);
515
516 menu.append (new MenuItemSeparator ());
517 }
518
519 Gtk.MenuItem item = new Gtk.CheckMenuItem.with_label (DesktopFolder.Lang.NOTE_MENU_PAPER_NOTE);
520 (item as Gtk.CheckMenuItem).set_active (this.manager.get_settings ().texture == "square_paper");
521 (item as Gtk.CheckMenuItem).toggled.connect ((item) => {
522 this.on_texture ("square_paper");
523 });
524 menu.append (item);
525
526 menu.append (new MenuItemSeparator ());
527
528 item = new Gtk.CheckMenuItem.with_label (DesktopFolder.Lang.NOTE_MENU_ON_TOP);
529 (item as Gtk.CheckMenuItem).set_active (this.manager.get_settings ().on_top);
530 (item as Gtk.CheckMenuItem).toggled.connect ((item) => {
531 this.on_toggle_on_top ();
532 });
533 menu.append (item);
534
535 item = new Gtk.CheckMenuItem.with_label (DesktopFolder.Lang.NOTE_MENU_ON_BACK);
536 (item as Gtk.CheckMenuItem).set_active (this.manager.get_settings ().on_back);
537 (item as Gtk.CheckMenuItem).toggled.connect ((item) => {
538 this.on_toggle_on_back ();
539 });
540 menu.append (item);
541
542 menu.append (new MenuItemSeparator ());
543
544 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.NOTE_MENU_DELETE_NOTE);
545 item.activate.connect ((item) => { this.manager.trash (); });
546 menu.append (item);
547
548 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.NOTE_MENU_RENAME_NOTE);
549 item.activate.connect (this.label.start_editing);
550 menu.append (item);
551
552 item = new MenuItemColor (HEAD_TAGS_COLORS, null);
553 ((MenuItemColor) item).color_changed.connect (change_head_color);
554 menu.append (item);
555
556 item = new MenuItemColor (BODY_TAGS_COLORS, this.last_custom_color);
557 ((MenuItemColor) item).color_changed.connect (change_body_color);
558 ((MenuItemColor) item).custom_changed.connect (change_body_color_custom);
559 menu.append (item);
560
561 menu.show_all ();
562 menu.popup_at_pointer (null);
563 }
564
565 /**
566 * @name on_texture
567 * @description set the texture for the note window
568 * @param {string} texture the texture to apply
569 */
570 private void on_texture (string texture) {
571 string current_texture = this.manager.get_settings ().texture;
572 if (current_texture == texture) {
573 this.manager.get_settings ().texture = "";
574 } else {
575 this.manager.get_settings ().texture = texture;
576 }
577 this.manager.get_settings ().save ();
578 this.queue_draw ();
579 }
580
581 /**
582 * @name new_desktop_folder
583 * @description create a new desktop folder
584 */
585 private void new_desktop_folder (int x, int y) {
586 DesktopFolder.Util.create_new_desktop_folder (this, x, y);
587 }
588
589 /**
590 * @name new_link_panel
591 * @description create a new link panel
592 */
593 private void new_link_panel (int x, int y) {
594 DesktopFolder.Util.create_new_link_panel (this, x, y);
595 }
596
597 /*
598 * @name new_note
599 * @description create a new note
600 */
601 private void new_note (int x, int y) {
602 DesktopFolder.Util.create_new_note (this, x, y);
603 }
604
605 /**
606 * @name new_photo
607 * @description create a new photo
608 */
609 private void new_photo (int x, int y) {
610 DesktopFolder.Util.create_new_photo (this, x, y);
611 }
612
613 /**
614 * @name change_head_color
615 * @description change event captured from the popup for a new color to the head window
616 * @param ncolor int the new color for the head window
617 */
618 private void change_head_color (int ncolor) {
619 string color = HEAD_TAGS_COLORS_CLASS[ncolor];
620 for (int i = 0; i < HEAD_TAGS_COLORS_CLASS.length; i++) {
621 string scolor = HEAD_TAGS_COLORS_CLASS[i];
622 this.get_style_context ().remove_class (scolor);
623 }
624 this.get_style_context ().add_class (color);
625 this.manager.save_head_color (color);
626 }
627
628 /**
629 * @name change_body_color
630 * @description change event captured from the popup for a new color to the body window
631 * @param ncolor int the new color for the body window
632 */
633 private void change_body_color (int ncolor) {
634 string color = BODY_TAGS_COLORS_CLASS[ncolor];
635 for (int i = 0; i < BODY_TAGS_COLORS_CLASS.length; i++) {
636 string scolor = BODY_TAGS_COLORS_CLASS[i];
637 this.get_style_context ().remove_class (scolor);
638 }
639 if (this.custom_color_provider != null) {
640 Gtk.StyleContext.remove_provider_for_screen (Gdk.Screen.get_default (), this.custom_color_provider);
641 }
642
643 if (ncolor > 0) {
644 Gdk.RGBA rgba = Gdk.RGBA ();
645 rgba.parse (BODY_TAGS_COLORS[ncolor]);
646 this.last_custom_color = rgba.to_string ();
647 } else {
648 this.last_custom_color = "rgba(0,0,0,0)";
649 }
650
651 this.get_style_context ().add_class (color);
652 this.manager.save_body_color (color);
653 // debug("color:%d,%s",ncolor,color);
654 }
655
656 /**
657 * @name change_body_color_custom
658 * @description change event captured from the popup for a new color to the body window
659 * @param custom string the new custom color
660 */
661 public void change_body_color_custom (string custom) {
662 string mycustom = this.set_custom_color (custom);
663 this.manager.save_body_color (mycustom);
664 }
665
666 /**
667 * @name draw_backgorund
668 * @description draw the note window background intercepting the draw signal
669 * @param {Cairo.Context} cr the cairo context
670 * @bool @see draw signal
671 */
672 private bool draw_background (Cairo.Context cr) {
673 // util code to draw the whole window background (which contains also the decoration and you can size it)
674 // cr.rectangle(0,0,10000,10000);
675 // cr.set_source_rgba (1, 1, 1, 0.2);
676 // cr.fill();
677
678 int width = this.get_allocated_width ();
679 int height = this.get_allocated_height ();
680
681 cr.set_operator (Cairo.Operator.CLEAR);
682 cr.paint ();
683 cr.set_operator (Cairo.Operator.OVER);
684
685 /*
686 var background_style = this.get_style_context ();
687 background_style.render_background (cr, 0, 0, width, height);
688 background_style.render_frame (cr, 0, 0, width, height);
689 */
690
691 // shaping
692 shape (cr, width, height);
693 cr.clip ();
694
695 if (this.manager.get_settings ().texture == "square_paper") {
696 try {
697 if (this.texture_pattern == null) {
698 var pixbuf = new Gdk.Pixbuf.from_resource ("/com/github/spheras/desktopfolder/hip-square.png");
699 var surface = Gdk.cairo_surface_create_from_pixbuf (pixbuf, 1, null);
700 this.texture_pattern = new Cairo.Pattern.for_surface (surface);
701 this.texture_pattern.set_extend (Cairo.Extend.REPEAT);
702 }
703
704 cr.set_source (this.texture_pattern);
705 cr.paint_with_alpha (0.9);
706 } catch (Error e) {
707 // error! ??
708 stderr.printf ("Error: %s\n", e.message);
709 }
710 }
711
712 // drawing border
713 shape (cr, width, height);
714 cr.set_line_width (3.0);
715 cr.set_source_rgba (0, 0, 0, 0.2);
716 cr.stroke ();
717
718 // drawing corner
719 draw_corner (cr, width, height);
720 cr.set_source_rgba (0, 0, 0, 0.4);
721 cr.stroke ();
722
723 base.draw (cr);
724
725 cr.reset_clip ();
726
727 // drawing clip
728 try {
729 if (this.clip_surface == null) {
730 int clipcolor = this.manager.get_settings ().clipcolor;
731 var color = "";
732 switch (clipcolor) {
733 case 1:
734 color = "blue";
735 break;
736 case 2:
737 color = "green";
738 break;
739 case 3:
740 color = "orange";
741 break;
742 case 4:
743 color = "pink";
744 break;
745 case 5:
746 color = "red";
747 break;
748 default:
749 case 6:
750 color = "yellow";
751 break;
752 }
753 var pixbuf = new Gdk.Pixbuf.from_resource ("/com/github/spheras/desktopfolder/clip-" + color + ".png");
754 this.clip_surface = Gdk.cairo_surface_create_from_pixbuf (pixbuf, 1, null);
755 }
756 cr.set_source_surface (this.clip_surface, 5, 5);
757 cr.paint ();
758 } catch (Error e) {
759 // error! ??
760 stderr.printf ("Error: %s\n", e.message);
761 }
762
763 return true;
764 }
765
766 private void draw_corner (Cairo.Context cr, double width, double height) {
767 int margin = 15;
768 int rightRadius = 25;
769 cr.move_to (width - margin - rightRadius, margin);
770 cr.line_to (width - margin - rightRadius, margin + rightRadius);
771 cr.line_to (width - margin, margin + rightRadius);
772 }
773
774 /**
775 * @name shape
776 * @description shape the window with the shape of a note
777 * @param {Cairo.Context} cr the context to draw
778 * @param double width the width of the window
779 * @param double height the height of the window
780 */
781 private void shape (Cairo.Context cr, double width, double height) {
782 int margin = 15;
783 int radius = 2;
784 int rightRadius = 25;
785
786 cr.move_to (margin, margin + radius);
787
788 ///
789 cr.line_to (margin + radius, margin);
790 // -
791 cr.line_to (width - margin - rightRadius, margin);
792 // \ (top right corner)
793 cr.line_to (width - margin, margin + rightRadius);
794 // |
795 cr.line_to (width - margin, height - margin - radius);
796 ///
797 cr.line_to (width - margin - radius, height - margin);
798 // -
799 cr.line_to (margin + radius, height - margin);
800 // \ (top right corner)
801 cr.line_to (margin, height - margin - radius);
802 // |
803 cr.line_to (margin, margin + radius);
804 cr.close_path ();
805 }
806
807 /**
808 * @name on_toggle_on_top
809 * @description toggle the on top setting
810 */
811 protected void on_toggle_on_top () {
812 this.manager.get_settings ().on_top = !this.manager.get_settings ().on_top;
813 if (this.manager.get_settings ().on_top) {
814 this.manager.get_settings ().on_back = false;
815 }
816 this.manager.get_settings ().save ();
817 this.check_on_top ();
818 this.manager.reopen ();
819 }
820
821 /**
822 * @name on_toggle_on_back
823 * @description toggle the on back setting
824 */
825 protected void on_toggle_on_back () {
826 this.manager.get_settings ().on_back = !this.manager.get_settings ().on_back;
827 if (this.manager.get_settings ().on_back) {
828 this.manager.get_settings ().on_top = false;
829 }
830 this.manager.get_settings ().save ();
831 this.check_on_top ();
832 this.manager.reopen ();
833 }
834
835 /**
836 * @name check_on_top
837 * @description check the current settings to put the window above or not
838 */
839 private void check_on_top () {
840 this.check_window_type ();
841 if (this.manager.get_settings ().on_top) {
842 this.set_keep_above (true);
843 } else {
844 this.set_keep_above (false);
845 }
846 }
847
848 /**
849 * @name check_window_type
850 * @description check whether the window should have a normal or desktop type
851 */
852 private void check_window_type () {
853 if (this.manager != null) {
854 var settings = this.manager.get_settings ();
855 if (settings == null || settings.on_back) {
856 this.type_hint = Gdk.WindowTypeHint.DESKTOP;
857 } else {
858 this.type_hint = Gdk.WindowTypeHint.NORMAL;
859 }
860 }
861 }
862
863 }