Debian Codegraph @

src/widgets/ItemView.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 public class DesktopFolder.ItemView : Gtk.EventBox {
19
20 // NOT SURE ABOUT THESE CONSTANTS!!! TODO!!!!!
21 public const int PADDING_X = 10;
22 public const int PADDING_Y = 44;
23 // DEFAULT SIZES
24 private const int ICON_WIDTH = 48;
25 private const int DEFAULT_WIDTH = 90;
26 private const int DEFAULT_HEIGHT = 90;
27 private const int MAX_CHARACTERS = 13;
28 /** flag to know that the item has been moved */
29 private bool flagModified = false;
30 private bool flagMoved = false;
31 /** the manager of this item icon */
32 private ItemManager manager;
33 /** the context menu for this item */
34 private Gtk.Menu menu = null;
35
36 /** flag to know if we should hide the extension of the file */
37 private bool hide_extension = false;
38 /** the hidden extension of the file, it it was hidden, @see hide_extension*/
39 private string hidden_extension = "";
40
41 /** the container of this item view */
42 private Gtk.Box container;
43 /** the label of the icon */
44 private DesktopFolder.EditableLabel label;
45 /** the image shown */
46 private Gtk.Image icon = null;
47 /** flag to know whether the drag drop event was started */
48 private bool flag_dragdrop_started = false;
49 /** the last drag_context used to allow canceling it */
50 private Gdk.DragContext drag_context;
51
52
53 /** set of variables to allow move the widget */
54 private int offsetx;
55 private int offsety;
56 private int px;
57 private int py;
58 private int maxx;
59 private int maxy;
60 /** ----------------------------------------- */
61
62 // this is the link image loaded
63 static Gdk.Pixbuf LINK_PIXBUF = null;
64 static construct {
65 try {
66 int scale = DesktopFolder.ICON_SIZE / 3;
67 ItemView.LINK_PIXBUF = new Gdk.Pixbuf.from_resource ("/com/github/spheras/desktopfolder/link.svg");
68 ItemView.LINK_PIXBUF = LINK_PIXBUF.scale_simple (scale, scale, Gdk.InterpType.BILINEAR);
69 } catch (Error e) {
70 stderr.printf ("Error: %s\n", e.message);
71 Util.show_error_dialog ("Error", e.message);
72 }
73 }
74
75 /**
76 * @constructor
77 * @param ItemManager manager the manager of this item view
78 */
79 public ItemView (ItemManager manager) {
80 this.manager = manager;
81
82 // we set the default size
83 this.set_size_request (DEFAULT_WIDTH, DEFAULT_HEIGHT);
84 // class for this widget
85 this.get_style_context ().add_class ("df_item");
86
87 this.get_style_context ().add_class ("df_fadingwindow");
88 this.get_style_context ().add_class ("df_fadeout");
89
90 // we connect the enter and leave events
91 this.enter_notify_event.connect (this.on_enter);
92 this.leave_notify_event.connect (this.on_leave);
93 this.button_press_event.connect (this.on_press);
94 this.button_release_event.connect (this.on_release);
95 this.motion_notify_event.connect (this.on_motion);
96
97 // we create the components to put inside the item (icon and label)
98 this.container = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
99 this.container.margin = 0;
100 this.container.set_size_request (DEFAULT_WIDTH, DEFAULT_HEIGHT);
101
102 this.refresh_icon ();
103 string slabel = this.get_correct_label (this.manager.get_file_name ());
104 this.create_label (slabel);
105
106 this.add (this.container);
107
108 }
109
110 /**
111 * @name create_headerbar
112 * @description create the header bar
113 */
114 protected virtual void create_label (string slabel) {
115 try {
116 // debug ("Create label for %s", this.manager.get_file_name ());
117
118 this.label = new DesktopFolder.EditableLabel (slabel);
119 this.container.pack_end (label, true, true, 0);
120 this.check_ellipse ();
121
122 label.changed.connect ((new_name) => {
123 if (this.manager.rename (new_name + this.hidden_extension)) {
124 label.text = new_name;
125 this.check_ellipse ();
126 }
127 });
128
129 label.on_start_editing.connect (() => {
130 this.manager.get_folder ().get_view ().on_start_editing ();
131 });
132 label.on_stop_editing.connect (() => {
133 this.manager.get_folder ().get_view ().on_end_editing ();
134 });
135 } catch (Error e) {
136 stderr.printf ("Error: %s\n", e.message);
137 Util.show_error_dialog ("Error", e.message);
138 }
139 }
140
141 /**
142 * @name rename
143 * @description change the label of the item to the new label
144 */
145 public void rename (string new_name) {
146 this.label.text = new_name;
147 this.refresh_icon ();
148 }
149
150 /**
151 * @name refresh
152 * @description refresh the window
153 */
154 public void refresh () {
155 if (this.manager.get_folder ().get_application ().get_desktop_visibility ()) {
156 this.fade_in ();
157 this.show_all ();
158 }
159 }
160
161 /**
162 * @name refresh_icon
163 * @description force to refresh the icon imagen shown
164 */
165 public void refresh_icon (string ? forced = null) {
166 try {
167 Gtk.Image newImage;
168
169 if (forced == null) {
170 newImage = this.calculate_icon ();
171 } else {
172 GLib.Icon gicon = GLib.Icon.new_for_string ("folder-open");
173 if (this.manager.is_link ()) {
174 newImage = this.draw_link_mark_gicon (gicon);
175 } else {
176 newImage = new Gtk.Image.from_gicon (gicon, Gtk.IconSize.DIALOG);
177 }
178 }
179
180 if (this.icon != null) {
181 this.container.remove (this.icon);
182 }
183
184 this.icon = newImage;
185 this.icon.set_size_request (ICON_WIDTH, ICON_WIDTH);
186 this.icon.get_style_context ().add_class ("df_icon");
187 this.container.pack_start (this.icon, true, true);
188 this.refresh ();
189 } catch (Error e) {
190 stderr.printf ("Error: %s\n", e.message);
191 Util.show_error_dialog ("Error", e.message);
192 }
193 }
194
195 /**
196 * @name force_opened_folder_icon
197 * @description put the folder-open icon over the item
198 */
199 public void force_opened_folder_icon () {
200 this.refresh_icon ("folder-open");
201 }
202
203 /**
204 * @return the image icon
205 */
206 public Gtk.Image get_image () {
207 return this.icon;
208 }
209
210 /**
211 * @name calculate_icon
212 * @description calculate the icon image to show
213 * @return Gtk.Image the image to be shown
214 */
215 private Gtk.Image calculate_icon () {
216 try {
217 Gtk.Image icon;
218 var fileInfo = this.manager.get_file ().query_info ("standard::icon", FileQueryInfoFlags.NONE);
219 if (manager.get_settings ().icon != null && manager.get_settings ().icon.length > 0) {
220 // We have a custom icon
221 int scale = DesktopFolder.ICON_SIZE;
222 Gdk.Pixbuf custom = new Gdk.Pixbuf.from_file_at_scale (manager.get_settings ().icon, scale, scale, true);
223 // custom = custom.scale_simple (scale, scale, Gdk.InterpType.BILINEAR);
224 if (this.manager.is_link ()) {
225 icon = this.draw_link_mark_pixbuf (custom);
226 } else {
227 icon = new Gtk.Image.from_pixbuf (custom);
228 }
229 } else {
230 // checking if the file still exists... we need to check following symlinks!
231 if (!this.manager.check_exist_cached ()) {
232 // the file doesn't exist
233 GLib.ThemedIcon themed = new ThemedIcon.with_default_fallbacks ("text-x-generic");
234 var info = Gtk.IconTheme.get_default ().lookup_by_gicon (themed, ICON_WIDTH, 0);
235 var pixbuf = info.load_icon ();
236 if (pixbuf.height != ICON_WIDTH) {
237 // Some icons don't return the requested size, so we need to scale them
238 pixbuf = pixbuf.scale_simple (ICON_WIDTH, ICON_WIDTH, Gdk.InterpType.BILINEAR);
239 }
240 // drawing a cross over the icon
241 icon = draw_not_exist_mark_pixbuf (pixbuf);
242
243 } else if (this.manager.is_desktop_file ()) {
244 GLib.DesktopAppInfo desktopApp = new GLib.DesktopAppInfo.from_filename (this.manager.get_absolute_path ());
245 GLib.Icon gicon = desktopApp.get_icon ();
246 if (this.manager.is_link ()) {
247 icon = this.draw_link_mark_gicon (gicon);
248 } else {
249 GLib.ThemedIcon themed = new GLib.ThemedIcon.with_default_fallbacks (gicon.to_string ());
250 var info = Gtk.IconTheme.get_default ().lookup_by_gicon (themed, ICON_WIDTH, 0);
251 Gdk.Pixbuf pixbuf = null;
252 if (info == null) {
253 // force a large icon load to ensure icons like svg's are scaled correctly later
254 pixbuf = new Gdk.Pixbuf.from_file_at_scale (gicon.to_string (), ICON_WIDTH, ICON_WIDTH, true);
255 } else {
256 pixbuf = info.load_icon ();
257 }
258
259 if (pixbuf.height != ICON_WIDTH) {
260 // Some icons don't return the requested size, so we need to scale them
261 pixbuf = pixbuf.scale_simple (ICON_WIDTH, ICON_WIDTH, Gdk.InterpType.BILINEAR);
262 icon = new Gtk.Image.from_pixbuf (pixbuf);
263 } else {
264 icon = new Gtk.Image.from_gicon (gicon, Gtk.IconSize.DIALOG);
265 }
266 }
267 } else {
268 var ctypeInfo = this.manager.get_file ().query_info ("standard::content-type", FileQueryInfoFlags.NONE);
269 string contentType = ctypeInfo.get_content_type ();
270 string mimeType = GLib.ContentType.get_mime_type (contentType);
271 var isImage = GLib.ContentType.is_a (mimeType, "image/*");
272 if (mimeType != null && isImage) {
273 // reading the image to show it
274 int scale = DesktopFolder.ICON_SIZE;
275 Gdk.Pixbuf custom = new Gdk.Pixbuf.from_file (this.manager.get_file ().get_path ());
276 custom = custom.scale_simple (scale, scale, Gdk.InterpType.BILINEAR);
277 if (this.manager.is_link ()) {
278 icon = this.draw_link_mark_pixbuf (custom);
279 } else {
280 icon = new Gtk.Image.from_pixbuf (custom);
281 }
282 } else {
283 // we get the default icon from the system
284 GLib.Icon gicon = fileInfo.get_icon ();
285 if (this.manager.is_link ()) {
286 icon = this.draw_link_mark_gicon (gicon);
287 } else {
288 icon = new Gtk.Image.from_gicon (gicon, Gtk.IconSize.DIALOG);
289 }
290 }
291 }
292 }
293 return icon;
294 } catch (Error e) {
295 try {
296 stderr.printf ("Error: %s\n", e.message);
297 Gtk.Image icon;
298 icon = new Gtk.Image.from_icon_name ("image-x-generic", Gtk.IconSize.DIALOG);
299 return icon;
300 } catch (Error ee) {
301 stderr.printf ("Error: %s\n", ee.message);
302 Util.show_error_dialog ("Error", ee.message);
303 }
304 }
305 }
306
307 /**
308 * @name draw_not_exist_mark_pixbuf
309 * @description draw a CROSS mark to indicate that the file doesn't exist
310 * @param pixbuf {Gdk.Pixbuf} the pixbuf to modify
311 * @result {Gtk.Image} the image produced
312 */
313 private Gtk.Image draw_not_exist_mark_pixbuf (Gdk.Pixbuf pixbuf) {
314 try {
315 var surface = Gdk.cairo_surface_create_from_pixbuf (pixbuf, 1, null);
316 var context = new Cairo.Context (surface);
317
318 context.set_source_rgba (1, 0, 0, 1);
319 context.move_to (0, 0);
320 context.line_to (ICON_SIZE, ICON_SIZE);
321 context.move_to (ICON_SIZE, 0);
322 context.line_to (0, ICON_SIZE);
323 context.stroke ();
324
325 var pixbuf2 = Gdk.pixbuf_get_from_surface (surface, 0, 0, ICON_SIZE, ICON_SIZE);
326 Gtk.Image icon = new Gtk.Image.from_pixbuf (pixbuf2);
327 return icon;
328 } catch (Error e) {
329 stderr.printf ("Error: %s\n", e.message);
330 Util.show_error_dialog ("Error", e.message);
331 }
332 }
333
334 /**
335 * @name draw_link_mark_gicon
336 * @description draw a link mark over the image of an icon
337 * @param gicon {GLib.Icon} the icon we want to modify and add the mark
338 * @result {Gtk.Image} the image produced
339 */
340 private Gtk.Image draw_link_mark_gicon (GLib.Icon gicon) {
341 try {
342 Gtk.IconTheme theme = Gtk.IconTheme.get_default ();
343 Gtk.IconInfo iconInfo = theme.lookup_by_gicon (gicon, ICON_SIZE, 0);
344 Gdk.Pixbuf pixbuf = iconInfo.load_icon ();
345
346 if (pixbuf.height != ICON_WIDTH) {
347 // some icons doesn't return the asked size, so we need to scale them
348 pixbuf = pixbuf.scale_simple (ICON_WIDTH, ICON_WIDTH, Gdk.InterpType.BILINEAR);
349 }
350
351 return this.draw_link_mark_pixbuf (pixbuf);
352 } catch (Error e) {
353 stderr.printf ("Error: %s\n", e.message);
354 Util.show_error_dialog ("Error", e.message);
355 }
356 return null as Gtk.Image;
357 }
358
359 /**
360 * @name draw_link_mark_pixbuf
361 * @description draw a link mark over a pixbuf
362 * @param pixbuf {Gdk.Pixbuf} the pixbuf to modify
363 * @result {Gtk.Image} the image produced
364 */
365 private Gtk.Image draw_link_mark_pixbuf (Gdk.Pixbuf pixbuf) {
366 try {
367 var surface = Gdk.cairo_surface_create_from_pixbuf (pixbuf, 1, null);
368 var context = new Cairo.Context (surface);
369
370 int scale = DesktopFolder.ICON_SIZE / 3;
371 var links = Gdk.cairo_surface_create_from_pixbuf (ItemView.LINK_PIXBUF, 1, null);
372 context.set_source_surface (links, ICON_SIZE - scale, ICON_SIZE - scale);
373 context.paint ();
374
375 var pixbuf2 = Gdk.pixbuf_get_from_surface (surface, 0, 0, ICON_SIZE, ICON_SIZE);
376 Gtk.Image icon = new Gtk.Image.from_pixbuf (pixbuf2);
377 return icon;
378 } catch (Error e) {
379 stderr.printf ("Error: %s\n", e.message);
380 Util.show_error_dialog ("Error", e.message);
381 }
382 }
383
384 /**
385 * @name get_correct_label
386 * @description return the correct label to be shown
387 */
388 private string get_correct_label (string file_name) {
389 if (this.hide_extension) {
390 int index = file_name.index_of (".", 0);
391 if (index > 0) {
392 hidden_extension = file_name.substring (index);
393 return file_name.substring (0, index);
394 }
395 hidden_extension = "";
396 return file_name;
397 } else {
398 int index = file_name.index_of (".desktop", 0);
399 if (index > 0) {
400 // is a desktop file!
401 this.hide_extension = true;
402 hidden_extension = file_name.substring (index);
403 return file_name.substring (0, index);
404 }
405 hidden_extension = "";
406 return file_name;
407 }
408 }
409
410 /**
411 * @name check_ellipse
412 * @description check if the ellipse should be shown
413 * @param string name the name to be shown in order to calculate if the ellipse will be shown
414 */
415 private void check_ellipse () {
416 // debug ("check ellipse!");
417 if (this.label.text.length > MAX_CHARACTERS) {
418 this.label.set_lines (2);
419 this.label.set_ellipsize (Pango.EllipsizeMode.END);
420 } else {
421 this.label.set_lines (1);
422 this.label.set_ellipsize (Pango.EllipsizeMode.NONE);
423 }
424 }
425
426 /**
427 * @name on_enter
428 * @description on_enter signal to highlight the icon
429 * @param eventCrossing EventCrossing @see on_enter signal
430 * @return bool @see the on_enter signal
431 */
432 private bool on_enter (Gdk.EventCrossing eventCrossing) {
433 this.get_style_context ().add_class ("df_item_over");
434 // debug("enter item");
435 bool single_click = this.manager.get_folder ().get_application ().get_single_click ();
436
437 if (single_click && !this.flagModified) {
438 get_window ().set_cursor (new Gdk.Cursor.from_name (Gdk.Display.get_default (), "pointer"));
439 }
440
441 return true;
442 }
443
444 /**
445 * @name cancel_movement_and_start_drag_action
446 * @description cancel the current icon movement and start a drag action instead
447 */
448 private void cancel_movement_and_start_drag_action (Gdk.Event event) {
449 this.flagModified = false;
450
451 this.drag_context = Gtk.drag_begin_with_coordinates (this,
452 new Gtk.TargetList (DesktopFolder.DragnDrop.drag_targets),
453 DesktopFolder.DragnDrop.file_drag_actions,
454 Gdk.BUTTON_PRIMARY,
455 event, -1, -1);
456 this.flag_dragdrop_started = true;
457
458 // restoring the icon position
459 // debug("restoring to: %d,%d",manager.get_settings ().x,manager.get_settings ().y);
460 // now, moving the rest of selected items to replicate the main movement
461 Gee.List <ItemView> selecteds = this.manager.get_folder ().get_selected_items ();
462 for (int i = 0; i < selecteds.size; i++) {
463 ItemView selected = selecteds.@get (i);
464 UtilGtkAnimation.animate_move (selected, selected.start_drag_point, 500, UtilFx.AnimationMode.EASE_OUT_BACK);
465 var settings = selected.manager.get_settings ();
466 settings.x = selected.start_drag_point.x;
467 settings.y = selected.start_drag_point.y;
468 selected.manager.save_settings (settings);
469 selected.manager.get_folder ().get_view ().on_item_moving (false);
470
471 }
472 }
473
474 /**
475 * @name on_leave
476 * @description on_leave signal to remove highlight of the icon
477 * @param eventCrossing EventCrossing @see on_enter signal
478 * @return bool @see the on_leave signal
479 */
480 public bool on_leave (Gdk.EventCrossing ? eventCrossing) {
481 // debug ("detail: %d", eventCrossing.detail);
482 // we remove the highlight class
483 this.get_style_context ().remove_class ("df_item_over");
484
485
486 if (this.flagMoved) {
487 if (this.manager.get_folder ().get_view () is DesktopWindow && eventCrossing.y < 0) {
488 return true;
489 }
490 // if the item is outside the current windows, we cancel the movement, and start dragging
491 Gtk.Allocation allocation;
492 this.get_allocation (out allocation);
493 FolderWindow window = this.manager.get_folder ().get_view ();
494 int window_width;
495 int window_height;
496 window.get_size (out window_width, out window_height);
497 if (eventCrossing.detail == Gdk.NotifyType.NONLINEAR ||
498 eventCrossing.detail == Gdk.NotifyType.NONLINEAR_VIRTUAL ||
499 (
500 !(this.manager.get_folder ().get_view () is DesktopWindow)
501 &&
502 (
503 allocation.x < 0 ||
504 allocation.y < 0 ||
505 allocation.x > window_width - DesktopFolder.ICON_DEFAULT_WIDTH ||
506 allocation.y > window_height - DesktopFolder.ICON_DEFAULT_WIDTH
507 )
508 )
509 ) {
510 this.cancel_movement_and_start_drag_action (eventCrossing);
511
512 }
513 } else if (this.flagModified) {
514 this.manager.save_current_position ();
515 this.flagModified = false;
516 }
517 return true;
518 }
519
520 /**
521 * @name start_editing
522 * @description action of rename the item selected by the user
523 * @param string new_name the new name for this item
524 */
525 public void start_editing () {
526 // debug ("renaming ItemView");
527 this.label.start_editing ();
528 }
529
530 ///**
531 // * @name force_adjust_label
532 // * @description util function to force the label to readjust
533 // * sometimes, the label change its appareance, but it is not adjusted automatically
534 // * this function force the adjust
535 // */
536 // public void force_adjust_label () {
537 // this.label.title_label.set_label (this.label.title_label.get_text ());
538 // }
539
540 /**
541 * @name select
542 * @description the user select the icon
543 */
544 public void select_only () {
545 Gtk.Window window = (Gtk.Window) this.get_toplevel ();
546 ((FolderWindow) window).unselect_all ();
547 this.manager.select_only ();
548 this.get_style_context ().add_class ("df_selected");
549
550 }
551
552 public void select_add () {
553 this.manager.select_add ();
554 this.get_style_context ().add_class ("df_selected");
555 }
556
557 /**
558 * @name unselect
559 * @description unselect the icon
560 */
561 public void unselect () {
562 this.manager.unselect ();
563 this.get_style_context ().remove_class ("df_selected");
564 }
565
566 /**
567 * @name is_selected
568 * @description check whether the item is selected or not
569 * @return bool true->is selected
570 */
571 public bool is_selected () {
572 return this.manager.is_selected ();
573 }
574
575 /**
576 * @name fade_in
577 * @description fade the view in. does not call show
578 */
579 public void fade_in () {
580 this.get_style_context ().remove_class ("df_fadeout");
581 this.get_style_context ().add_class ("df_fadein");
582 }
583
584 /**
585 * @name fade_out
586 * @description fade the view out. does not call hide
587 */
588 public void fade_out () {
589 this.get_style_context ().remove_class ("df_fadein");
590 this.get_style_context ().add_class ("df_fadeout");
591 }
592
593 /**
594 * @name on_release
595 * @description the release button event
596 * @param EventButton event the event produced
597 * @return bool @see on_release signal
598 */
599 private bool on_release (Gdk.EventButton event) {
600 /*
601 //TODO IF WE DO THIS ON PRESS EVENT, THE MOTION IS ALTERED
602 //I'VE NOT FOUND A WAY TO ALTER THE Z ORDER WITHOUT REMOVING AND ADDING AGAIN FROM CONTAINER :(
603 //we will move to top this icon
604 Gtk.Allocation allocation;
605 this.get_allocation(out allocation);
606 //HELP! don't know why these constants?? maybe padding??
607 int x=allocation.x - PADDING_X;
608 int y=allocation.y - PADDING_Y;
609 ((FolderWindow)this.manager.get_application_window()).raise(this,x,y);
610
611 return true;
612 */
613 // if the icon wasnt moved, maybe we must execute it
614 // depending if the files preferences single-click was activated
615
616 var mods = event.state & Gtk.accelerator_get_default_mod_mask ();
617 bool control_pressed = ((mods & Gdk.ModifierType.CONTROL_MASK) != 0);
618
619 this.flag_dragdrop_started = false;
620
621 // Change cursor
622 get_window ().set_cursor (new Gdk.Cursor.from_name (Gdk.Display.get_default (), "default"));
623
624 // we notify that we are stopping to move
625 this.manager.get_folder ().get_view ().on_item_moving (false);
626
627 if (!this.flagMoved) {
628 bool single_click = this.manager.get_folder ().get_application ().get_single_click ();
629
630 if (single_click && !control_pressed && event.type == Gdk.EventType.BUTTON_RELEASE && event.button == Gdk.BUTTON_PRIMARY) {
631 on_double_click ();
632 }
633 } else {
634 this.flagMoved = false;
635 Gtk.Allocation allocation;
636 this.get_allocation (out allocation);
637 // debug("release(%d,%d)",allocation.x,allocation.y);
638 // HELP! don't know why these constants?? maybe padding??
639 int x = allocation.x; // - PADDING_X;
640 int y = allocation.y; // - PADDING_Y;
641
642 FolderArrangement arrangement = this.manager.get_folder ().get_arrangement ();
643 int sensitivity = arrangement.get_sensitivity ();
644 int padding = this.manager.get_folder ().get_settings ().arrangement_padding;
645 x = RoundToNearestMultiple (int.max (int.min (x, this.maxx), 0), sensitivity + padding);
646 y = RoundToNearestMultiple (int.max (int.min (y, this.maxy), 0), sensitivity + padding);
647
648 if (this.manager.get_folder ().get_arrangement ().have_margin ()) {
649 x = x + FolderArrangement.DEFAULT_EXTERNAL_MARGIN;
650 }
651
652 Gtk.Window window = (Gtk.Window) this.get_toplevel ();
653 ((FolderWindow) window).move_item (this, x, y);
654
655 int diff_x = allocation.x - x;
656 int diff_y = allocation.y - y;
657
658
659 // notifying to the arrangement
660 this.manager.get_folder ().get_arrangement ().end_drag ();
661 ItemSettings my_settings = this.manager.get_settings ();
662 my_settings.x = x;
663 my_settings.y = y;
664 this.manager.save_settings (my_settings);
665
666 // now, moving the rest of selected items to replicate the main movement
667 Gee.List <ItemView> selecteds = this.manager.get_folder ().get_selected_items ();
668 for (int i = 0; i < selecteds.size; i++) {
669 ItemView selected = selecteds.@get (i);
670 if (selected != this) {
671 Gtk.Allocation allocation_selected;
672 selected.get_allocation (out allocation_selected);
673 int sel_x = allocation_selected.x - diff_x;
674 int sel_y = allocation_selected.y - diff_y;
675 ((FolderWindow) window).move_item (selected, sel_x, sel_y);
676
677 // we simulate a drag to move the conflict items in that position of the grid
678 this.manager.get_folder ().get_arrangement ().start_drag (selected);
679 this.manager.get_folder ().get_arrangement ().motion_drag (sel_x + PADDING_X, sel_y);
680 this.manager.get_folder ().get_arrangement ().end_drag ();
681
682 ItemSettings sel_settings = selected.manager.get_settings ();
683 sel_settings.x = sel_x;
684 sel_settings.y = sel_y;
685 selected.manager.save_settings (sel_settings);
686
687 }
688 }
689
690 }
691
692 return false;
693 }
694
695 /**
696 * @name get_manager
697 * @description return the manager of this view
698 * @return {ItemManager} the manager
699 */
700 public ItemManager get_manager () {
701 return this.manager;
702 }
703
704 /**
705 * @name is_dragdrop_started
706 * @description return whether the item has started a drag drop event
707 * @return true -> yes, it was started, false otherwise
708 */
709 public bool is_dragdrop_started () {
710 return this.flag_dragdrop_started;
711 }
712
713 /**
714 * @name on_drag_end
715 * @description drag end event
716 */
717 public void on_drag_end () {
718 this.flag_dragdrop_started = false;
719 }
720
721 /** start drag point to restore if the drag was cancelled */
722 private Gdk.Point start_drag_point = Gdk.Point ();
723
724 /**
725 * @name on_press
726 * @description the mouse press event captured
727 * @param EventButton event the event produced
728 * @return bool @see on_press signal
729 */
730 private bool on_press (Gdk.EventButton event) {
731 // debug("press:%i",(int)event.button);
732 manager.get_folder ().get_view ().present ();
733 // Change cursor to closed hand
734 get_window ().set_cursor (new Gdk.Cursor.from_name (Gdk.Display.get_default (), "grabbing"));
735
736 // this code is to allow the drag'ndrop of files inside the folder window
737 var mods = event.state & Gtk.accelerator_get_default_mod_mask ();
738 bool control_pressed = ((mods & Gdk.ModifierType.CONTROL_MASK) != 0);
739 bool shift_pressed = ((mods & Gdk.ModifierType.SHIFT_MASK) != 0);
740 bool can_drag = this.manager.get_folder ().get_arrangement ().can_drag ();
741 bool locked = this.manager.get_folder ().are_items_locked ();
742
743 if (event.type == Gdk.EventType.BUTTON_PRESS &&
744 event.button == Gdk.BUTTON_PRIMARY &&
745 (control_pressed || !can_drag || locked) &&
746 this.is_selected ()) {
747 this.flag_dragdrop_started = true;
748 return false;
749 }
750
751 if (event.type == Gdk.EventType.BUTTON_PRESS &&
752 event.button == Gdk.BUTTON_PRIMARY &&
753 (control_pressed || shift_pressed)) {
754
755 if (!this.is_selected ()) {
756 this.select_add ();
757 } else {
758 this.unselect ();
759 }
760 return false;
761 }
762
763
764
765 if (event.type == Gdk.EventType.BUTTON_PRESS && event.button == Gdk.BUTTON_PRIMARY) {
766 // first we must select the item
767 if (!this.is_selected ()) {
768 this.select_only ();
769 }
770 this.flagMoved = false;
771
772 if (!this.manager.get_folder ().are_items_locked () &&
773 this.manager.get_folder ().get_arrangement ().can_drag ()) {
774 Gee.List <ItemView> selecteds = this.manager.get_folder ().get_selected_items ();
775 for (int i = 0; i < selecteds.size; i++) {
776 ItemView selected = selecteds.@get (i);
777 selected.start_drag_point.x = selected.manager.get_settings ().x;
778 selected.start_drag_point.y = selected.manager.get_settings ().y;
779
780 }
781
782 Gtk.Widget p = this.parent;
783 // offset == distance of parent widget from edge of screen ...
784 p.get_window ().get_position (out this.offsetx, out this.offsety);
785 // debug ("offset_1: %i,%i", this.offsetx, this.offsety);
786 // plus distance from pointer to edge of widget
787 this.offsetx += (int) event.x + PADDING_X + PADDING_X;
788 this.offsety += (int) event.y + PADDING_Y;
789 // debug ("offset_2: %i,%i", this.offsetx, this.offsety);
790
791 // if it was grabed the title_label, the event position (y) need to be recalculated
792 if (event.window == this.label.title_label.get_window ()) {
793 int my_x = 0;
794 int my_y = 0;
795 this.translate_coordinates (this.label.title_label, (int) event.x, (int) event.y, out my_x, out my_y);
796 this.offsety -= my_y;
797 // debug ("offset_3 %i,%i", this.offsetx, this.offsety);
798 }
799
800 // creating a map of the current grid structure
801 this.manager.get_folder ().get_arrangement ().start_drag (this);
802
803 // maxx, maxy both relative to the parent
804 // note that we're rounding down now so that these max values don't get
805 // rounded upward later and push the widget off the edge of its parent.
806 Gtk.Allocation pAllocation;
807 p.get_allocation (out pAllocation);
808 Gtk.Allocation thisAllocation;
809 this.get_allocation (out thisAllocation);
810 this.maxx = RoundDownToMultiple (pAllocation.width - thisAllocation.width, this.manager.get_folder ().get_arrangement ().get_sensitivity ());
811 this.maxy = RoundDownToMultiple (pAllocation.height - thisAllocation.height, this.manager.get_folder ().get_arrangement ().get_sensitivity ());
812 } else {
813 // we must ensure the event is not processed by the folder window parent to avoid unselections
814 return true;
815 }
816 } else if (event.type == Gdk.EventType.@2BUTTON_PRESS) {
817 if (!this.is_selected ()) {
818 this.select_only ();
819 }
820 on_double_click ();
821 } else if (event.type == Gdk.EventType.BUTTON_PRESS && event.button == Gdk.BUTTON_SECONDARY) {
822 if (!this.is_selected ()) {
823 this.select_only ();
824 }
825 this.show_popup (event);
826 }
827
828 return true;
829 }
830
831 /**
832 * @name on_double_click
833 * @description double click event captured
834 */
835 private void on_double_click () {
836 // debug("doble click! %s",this.fileName);
837 this.execute ();
838 }
839
840 /**
841 * @name execute
842 * @description execute the action associated with the item
843 */
844 public void execute () {
845 this.manager.execute ();
846 }
847
848 /**
849 * @name show_popup
850 * @description show the context popup for this item
851 * @param EventButton event the event button that originates this context menu
852 */
853 private void show_popup (Gdk.EventButton event) {
854
855 // debug("evento:%f,%f",event.x,event.y);
856 // if(this.menu==null) { //we need the event coordinates for the menu, we need to recreate?!
857
858 // building the menu
859 this.menu = new Gtk.Menu ();
860
861 var label = DesktopFolder.Lang.ITEM_MENU_OPEN;
862 if (this.manager.is_executable ()) {
863 label = DesktopFolder.Lang.ITEM_MENU_EXECUTE;
864 }
865
866 Gtk.MenuItem item = new Gtk.MenuItem.with_label (label);
867 item.activate.connect ((item) => {
868 this.manager.execute ();
869 });
870
871 item.show ();
872 menu.append (item);
873
874 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_MENU_OPEN_WITH);
875 string path = this.manager.get_absolute_path ();
876 item.activate.connect ((item) => { this.manager.openwith (path); });
877 item.show ();
878 menu.append (item);
879
880 item = new MenuItemSeparator ();
881 item.show ();
882 menu.append (item);
883
884 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_MENU_CUT);
885 item.activate.connect ((item) => { this.manager.cut (); });
886 item.show ();
887 menu.append (item);
888
889 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_MENU_COPY);
890 item.activate.connect ((item) => { this.manager.copy (); });
891 item.show ();
892 menu.append (item);
893
894 item = new MenuItemSeparator ();
895 item.show ();
896 menu.append (item);
897
898 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_MENU_RENAME);
899 item.activate.connect ((item) => { this.label.start_editing (); });
900 item.show ();
901 menu.append (item);
902
903 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_MENU_TRASH);
904 item.activate.connect ((item) => { this.manager.trash_selected (); });
905 item.show ();
906 menu.append (item);
907
908 // if (this.manager.is_executable ()) { //only allowing this to executable files
909 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_MENU_CHANGEICON);
910 item.activate.connect ((item) => { this.change_icon (); });
911 item.show ();
912 menu.append (item);
913 // }
914
915 item = new MenuItemSeparator ();
916 item.show ();
917 menu.append (item);
918
919 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.ITEM_PROPSWINDOW_SHOW_FILEINFO);
920 string info_path = this.manager.get_absolute_path ();
921 item.activate.connect ((item) => { this.manager.show_info (info_path); });
922 item.show ();
923 menu.append (item);
924
925 // only add open-folder-in-terminal to menu if the item is a folder
926 if (this.manager.is_folder ()) {
927 item = new MenuItemSeparator ();
928 item.show ();
929 menu.append (item);
930
931 item = new Gtk.MenuItem.with_label (DesktopFolder.Lang.DESKTOPFOLDER_MENU_OPEN_IN_TERMINAL);
932 item.activate.connect ((item) => { this.manager.open_in_terminal (path); });
933 item.show ();
934 menu.append (item);
935 }
936
937 menu.show_all ();
938 // }
939
940 // showing the menu
941 menu.popup_at_pointer (null);
942 }
943
944 /**
945 * @name cut
946 * @description cut the file to the clipboard
947 */
948 public void cut () {
949 this.manager.cut ();
950 }
951
952 /**
953 * @name copy
954 * @description copy the file to the clipboard
955 */
956 public void copy () {
957 this.manager.copy ();
958 }
959
960 /**
961 * @name copy
962 * @description copy the file to the clipboard
963 */
964 public void trash () {
965 this.manager.trash ();
966 }
967
968 /**
969 * @name delete_dialog
970 * @description the user wants to delete the item. It should be confirmed before the deletion occurs
971 */
972 public void delete_dialog () {
973 string message = DesktopFolder.Lang.ITEM_DELETE_FOLDER_MESSAGE;
974 Gtk.Window window = (Gtk.Window) this.get_toplevel ();
975 bool isdir = this.manager.is_folder ();
976 if (!isdir) {
977 message = DesktopFolder.Lang.ITEM_DELETE_FILE_MESSAGE;
978 }
979 if (this.manager.is_link ()) {
980 message = DesktopFolder.Lang.ITEM_DELETE_LINK_MESSAGE;
981 }
982
983 Gtk.MessageDialog msg = new Gtk.MessageDialog (window, Gtk.DialogFlags.MODAL,
984 Gtk.MessageType.WARNING, Gtk.ButtonsType.OK_CANCEL, message);
985 msg.use_markup = true;
986 msg.response.connect ((response_id) => {
987 switch (response_id) {
988 case Gtk.ResponseType.OK:
989 msg.destroy ();
990 if (isdir) {
991 this.manager.trash ();
992 break;
993 } else {
994 this.manager.trash ();
995 }
996 break;
997 default:
998 msg.destroy ();
999 break;
1000 // uff
1001 }
1002 });
1003 msg.show ();
1004 }
1005
1006 /**
1007 * @name change_icon
1008 * @description change the icon of an executable item
1009 */
1010 private void change_icon () {
1011 Gtk.FileChooserDialog chooser = new Gtk.FileChooserDialog (
1012 DesktopFolder.Lang.ITEM_CHANGEICON_MESSAGE, this.manager.get_application_window (),
1013 Gtk.FileChooserAction.OPEN,
1014 DesktopFolder.Lang.DIALOG_CANCEL,
1015 Gtk.ResponseType.CANCEL,
1016 DesktopFolder.Lang.DIALOG_SELECT,
1017 Gtk.ResponseType.ACCEPT);
1018
1019 Gtk.FileFilter filter = new Gtk.FileFilter ();
1020 filter.set_name ("Images");
1021 filter.add_mime_type ("image");
1022 filter.add_mime_type ("image/png");
1023 filter.add_mime_type ("image/jpeg");
1024 filter.add_mime_type ("image/gif");
1025 filter.add_mime_type ("image/svg+xml");
1026 filter.add_pattern ("*.png");
1027 filter.add_pattern ("*.jpg");
1028 filter.add_pattern ("*.svg");
1029 filter.add_pattern ("*.gif");
1030 filter.add_pattern ("*.tif");
1031 filter.add_pattern ("*.xpm");
1032 chooser.add_filter (filter);
1033
1034 // Process response:
1035 if (chooser.run () == Gtk.ResponseType.ACCEPT) {
1036 var filename = chooser.get_filename ();
1037 this.manager.change_icon (filename);
1038 }
1039 chooser.close ();
1040 }
1041
1042 /**
1043 * @name modify
1044 * @description raise the flat modify
1045 */
1046 public void modify () {
1047 this.flagModified = true;
1048 }
1049
1050 public Gdk.Rectangle get_bounding_box () {
1051 Gtk.Allocation allocation;
1052 this.get_allocation (out allocation);
1053 allocation.y = allocation.y + PADDING_Y; // damn magic numbers!
1054 allocation.x = allocation.x + PADDING_X; // damn magic numbers!
1055 return allocation;
1056 }
1057
1058 /**
1059 * @name on_motion
1060 * @description the on_motion event captured to allow the movement of the icon
1061 * @param EventMotion event @see the on_motion signal
1062 * @return bool @see the on_motion signal
1063 */
1064 private bool on_motion (Gdk.EventMotion event) {
1065 if (!this.manager.get_folder ().are_items_locked () && this.manager.get_folder ().get_arrangement ().can_drag ()) {
1066
1067 // To prevent moving the itemView when editing the label
1068 if (this.label.editing) {
1069 // debug("ItemView has the focus but is editing the label");
1070 return false;
1071 }
1072 var mods = event.state & Gtk.accelerator_get_default_mod_mask ();
1073 bool control_pressed = ((mods & Gdk.ModifierType.CONTROL_MASK) != 0);
1074 if (control_pressed) {
1075 return false;
1076 }
1077
1078 // debug("on_motion");
1079 this.flagModified = true;
1080
1081 if (!this.flagMoved) {
1082 // we notify that we are starting to move
1083 this.manager.get_folder ().get_view ().on_item_moving (true);
1084 this.flagMoved = true;
1085 }
1086
1087 // x_root,x_root relative to screen
1088 // x,y relative to parent (fixed widget)
1089 // px,py stores previous values of x,y
1090
1091 // get starting values for x,y
1092 int x = (int) event.x_root - this.offsetx;
1093 int y = (int) event.y_root - this.offsety;
1094
1095
1096 // debug("motion: %d,%d",(int) event.x_root,(int) event.y_root);
1097 // debug("offset_motion: %d,%d",this.offsetx,this.offsety);
1098
1099
1100 // removing parent absolute position due to scroll
1101 // if (!(this.manager.get_folder ().get_view () is DesktopWindow)) {
1102 FolderSettings folder_settings = this.manager.get_folder ().get_settings ();
1103 x = x - folder_settings.x + DesktopFolder.WINDOW_DECORATION_MARGIN;
1104 y = y - folder_settings.y + DesktopFolder.WINDOW_DECORATION_MARGIN;
1105 // }
1106
1107
1108 Gdk.Rectangle icon_rectangle = Gdk.Rectangle ();
1109 int qwidth = DesktopFolder.ICON_DEFAULT_WIDTH / 2;
1110 icon_rectangle.x = x + qwidth;
1111 icon_rectangle.y = y + qwidth;
1112 icon_rectangle.width = DesktopFolder.ICON_DEFAULT_WIDTH - qwidth;
1113 icon_rectangle.height = DesktopFolder.ICON_DEFAULT_WIDTH - qwidth;
1114 Gee.List <ItemManager> ims = this.manager.get_folder ().get_items_at (icon_rectangle);
1115 for (int i = 0; i < ims.size; i++) {
1116 ItemManager im = ims.@get (i);
1117 // if(im!=null){
1118 // debug("im is folder? %s",(im.is_folder()?"true":"false"));
1119 // }
1120 if (im != null && im != this.manager && im.is_folder () && !im.is_selected ()) {
1121 this.cancel_movement_and_start_drag_action (event);
1122 break;
1123 }
1124 }
1125
1126 // debug("-------------");
1127 // debug ("offset(%d,%d)", this.offsetx, this.offsety);
1128 // debug ("event-xy(%f, %f)", event.x, event.y);
1129 // debug ("root(%f, %f)", event.x_root, event.y_root);
1130 // debug ("x,y=(%d,%d)", x, y);
1131
1132 // make sure the potential coordinates x,y:
1133 // 1) will not push any part of the widget outside of its parent container
1134 // 2) is a multiple of Sensitivity
1135 // x = RoundToNearestMultiple (int.max (int.min (x, this.maxx), 0), FreeArrangement.SENSITIVITY_WITHOUT_GRID);
1136 // y = RoundToNearestMultiple (int.max (int.min (y, this.maxy), 0), FreeArrangement.SENSITIVITY_WITHOUT_GRID);
1137 if (x != this.px || y != this.py) {
1138 this.px = x;
1139 this.py = y;
1140
1141 Gtk.Allocation allocation0;
1142 this.get_allocation (out allocation0);
1143 FolderWindow window = this.manager.get_folder ().get_view ();
1144 window.move_item (this, x + PADDING_X, y);
1145 int diff_x = allocation0.x - (x + PADDING_X);
1146 int diff_y = allocation0.y - y;
1147
1148 // now, moving the rest of selected items to replicate the main movement
1149 Gee.List <ItemView> selecteds = this.manager.get_folder ().get_selected_items ();
1150 for (int i = 0; i < selecteds.size; i++) {
1151 ItemView selected = selecteds.@get (i);
1152 if (selected != this) {
1153 Gtk.Allocation allocation;
1154 selected.get_allocation (out allocation);
1155 window.move_item (selected, allocation.x - diff_x, allocation.y - diff_y);
1156 }
1157 }
1158
1159 // debug("moving to: %d,%d",x+PADDING_X,y);
1160
1161 // notifying to the arrangement
1162 this.manager.get_folder ().get_arrangement ().motion_drag (x + PADDING_X, y);
1163 }
1164
1165 return true;
1166 } else {
1167 return false;
1168 }
1169 }
1170
1171 /**
1172 * @name RoundDownToMultiple
1173 * @description util function for the movement of icons
1174 */
1175 inline static int RoundDownToMultiple (int i, int m) {
1176 return i / m * m;
1177 }
1178
1179 /**
1180 * @name RoundToNearestMultiple
1181 * @description util function for the movement of icons
1182 */
1183 public static int RoundToNearestMultiple (int i, int m) {
1184 if (i % m > (double) m / 2.0d)
1185 return (i / m + 1) * m;
1186 return i / m * m;
1187 }
1188
1189 }