Page.vala 78.5 KB
Newer Older
1
/* Copyright 2009-2010 Yorba Foundation
2 3 4 5
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
 * See the COPYING file in this distribution. 
 */
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
// In order to prevent creating a slew of Pages at app startup, lazily create them as the
// user needs them ... this may be supplemented in the future to discard unused Pages (in
// a lifo).  This class also provides a host of information for the Sidebar and the LibraryWindow
// to use.
public abstract class PageStub : Object, SidebarPage {
    private Page page = null;
    private SidebarMarker marker = null;
    
    protected abstract Page construct_page();
    
    public abstract string get_name();
    
    public virtual bool is_renameable() {
        return false;
    }
    
    public virtual void rename(string new_name) {
        get_page().rename(new_name);
    }
    
    public virtual string? get_icon_name() {
        return null;
    }
    
    public bool has_page() {
        return page != null;
    }
    
    public Page get_page() {
        if (page == null) {
            // create the page and set its marker, if one has been supplied
            page = construct_page();
            debug("Created page %s", page.get_page_name());
            if (marker != null)
                page.set_marker(marker);
            
            // add this to the notebook and tell the notebook to show it (as per DevHelp)
            LibraryWindow.get_app().add_to_notebook(page);
        }

        return page;
    }
    
    public string get_sidebar_text() {
        return (page != null) ? page.get_sidebar_text() : get_name();
    }
    
    public SidebarMarker? get_marker() {
        return (page != null) ? page.get_marker() : marker;
    }
    
    public void set_marker(SidebarMarker marker) {
        this.marker = marker;
        if (page != null)
            page.set_marker(marker);
    }
    
    public void clear_marker() {
        this.marker = null;
        if (page != null)
            page.clear_marker();
    }
    
    public virtual string get_page_name() {
        return (page != null) ? page.get_page_name() : get_name();
    }
    
    public Gtk.Menu? get_page_context_menu() {
        return get_page().get_page_context_menu();
    }
    
    public bool popup_context_menu(Gtk.Menu? context_menu, Gdk.EventButton? event = null) {
        return get_page().popup_context_menu(context_menu, event);
    }
}

83 84
public class InjectionGroup {
    public struct Element {
85 86 87
        public string name;
        public string action;
        public Gtk.UIManagerItemType kind;
88 89
        
        public Element(string name, string? action, Gtk.UIManagerItemType kind) {
90
            this.name = name;
91
            this.action = action != null ? action : name;
92 93 94
            this.kind = kind;
        }
    }
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
    
    private string path;
    private Gee.ArrayList<Element?> elements = new Gee.ArrayList<Element?>();
    
    public InjectionGroup(string path) {
        this.path = path;
    }
    
    public string get_path() {
        return path;
    }
    
    public Gee.List<Element?> get_elements() {
        return elements;
    }
    
    public void add_menu_item(string name, string? action = null) {
        elements.add(Element(name, action, Gtk.UIManagerItemType.MENUITEM));
    }
    
    public void add_menu(string name, string? action = null) {
        elements.add(Element(name, action, Gtk.UIManagerItemType.MENU));
    }
    
    public void add_separator() {
        elements.add(Element("", null, Gtk.UIManagerItemType.SEPARATOR));
    }
}
123

124 125 126
public abstract class Page : Gtk.ScrolledWindow, SidebarPage {
    private const int CONSIDER_CONFIGURE_HALTED_MSEC = 400;
    
127
    public Gtk.UIManager ui = new Gtk.UIManager();
128
    public Gtk.ActionGroup action_group = null;
129
    public Gtk.ActionGroup common_action_group = null;
130
    
131
    private string page_name;
132
    private ViewCollection view = null;
133
    private Gtk.Window container = null;
134
    private Gtk.Toolbar toolbar = new Gtk.Toolbar();
135
    private SidebarMarker marker = null;
136 137 138
    private Gdk.Rectangle last_position = Gdk.Rectangle();
    private Gtk.Widget event_source = null;
    private bool dnd_enabled = false;
139
    private bool in_view = false;
140 141 142
    private ulong last_configure_ms = 0;
    private bool report_move_finished = false;
    private bool report_resize_finished = false;
143
    private Gdk.Point last_down = Gdk.Point();
144
    private bool is_destroyed = false;
145 146 147 148
    private bool ctrl_pressed = false;
    private bool alt_pressed = false;
    private bool shift_pressed = false;
    private bool super_pressed = false;
149
    private Gdk.CursorType last_cursor = Gdk.CursorType.LEFT_PTR;
150 151 152 153
    private bool cursor_hidden = false;
    private int cursor_hide_msec = 0;
    private uint last_timeout_id = 0;
    private int cursor_hide_time_cached = 0;
154
    
155
    protected Page(string page_name) {
156
        this.page_name = page_name;
157 158
        
        view = new ViewCollection("ViewCollection for Page %s".printf(page_name));
159
        
160 161
        last_down = { -1, -1 };
        
162
        set_flags(Gtk.WidgetFlags.CAN_FOCUS);
163

164
        popup_menu.connect(on_context_keypress);
165 166
        
        init_ui();
167
    }
168 169
    
    ~Page() {
170
#if TRACE_DTORS
171
        debug("DTOR: Page %s", page_name);
172 173 174
#endif
    }
    
175
    private void destroy_ui_manager_widgets(Gtk.UIManagerItemType item_type) {
176
        SList<weak Gtk.Widget> toplevels = ui.get_toplevels(item_type);
177 178 179 180
        for (int ctr = 0; ctr < toplevels.length(); ctr++)
            toplevels.nth(ctr).data.destroy();
    }
    
181 182
    // This is called by the page controller when it has removed this page ... pages should override
    // this (or the signal) to clean up
183
    public override void destroy() {
184 185
        debug("Page %s Destroyed", get_page_name());

186
        // untie signals
187
        detach_event_source();
188
        view.close();
189 190 191 192
        
        // remove refs to external objects which may be pointing to the Page
        clear_marker();
        clear_container();
193 194 195 196 197 198 199 200 201 202 203 204 205 206
        
        // Without destroying the menubar, Gtk spits out an assertion related to
        // a Gtk.AccelLabel being unable to disconnect a signal from the UI Manager's
        // Gtk.AccelGroup because the AccelGroup has already been finalized.  This only
        // happens during a drag-and-drop operation where the Page is destroyed.
        // Destroying the menubar explicitly solves this problem.  These calls go through and
        // ensure *all* widgets created by the UI manager are destroyed.
        destroy_ui_manager_widgets(Gtk.UIManagerItemType.MENUBAR);
        destroy_ui_manager_widgets(Gtk.UIManagerItemType.TOOLBAR);
        destroy_ui_manager_widgets(Gtk.UIManagerItemType.POPUP);
        
        // toolbars (as of yet) are not created by the UI Manager and need to be destroyed
        // explicitly
        toolbar.destroy();
207 208

        is_destroyed = true;
209 210
        
        base.destroy();
211 212
    }
    
213 214 215 216
    public virtual string? get_icon_name() {
        return null;
    }

217 218 219 220
    public string get_page_name() {
        return page_name;
    }
    
221 222 223 224 225 226 227
    public virtual bool is_renameable() {
        return false;
    }
    
    public virtual void rename(string new_name) {
    }
    
228
    public virtual void set_page_name(string page_name) {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        this.page_name = page_name;
    }
    
    public ViewCollection get_view() {
        return view;
    }
    
    // Usually when a controller is needed to iterate through a page's ViewCollection, the page's
    // own ViewCollection is the right choice.  Some pages may keep their own ViewCollection but
    // are actually referring to another ViewController for input (such as PhotoPage, whose own
    // ViewCollection merely refers to what's currently on the page while it uses another 
    // ViewController to flip through a collection of thumbnails).
    public virtual ViewCollection get_controller() {
        return view;
    }
    
245 246 247 248 249 250 251 252 253 254 255 256 257 258
    public Gtk.Window? get_container() {
        return container;
    }
    
    public virtual void set_container(Gtk.Window container) {
        assert(this.container == null);
        
        this.container = container;
    }
    
    public virtual void clear_container() {
        container = null;
    }
    
259 260 261 262
    public void set_event_source(Gtk.Widget event_source) {
        assert(this.event_source == null);

        this.event_source = event_source;
263
        event_source.set_flags(Gtk.WidgetFlags.CAN_FOCUS);
264

265 266 267 268
        // interested in mouse button and motion events on the event source
        event_source.add_events(Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK
            | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.POINTER_MOTION_HINT_MASK
            | Gdk.EventMask.BUTTON_MOTION_MASK);
269 270 271 272
        event_source.button_press_event.connect(on_button_pressed_internal);
        event_source.button_release_event.connect(on_button_released_internal);
        event_source.motion_notify_event.connect(on_motion_internal);
        event_source.scroll_event.connect(on_mousewheel_internal);
273
        event_source.realize.connect(on_event_source_realize);
274 275
    }
    
276 277 278 279
    private void detach_event_source() {
        if (event_source == null)
            return;
        
280 281 282 283
        event_source.button_press_event.disconnect(on_button_pressed_internal);
        event_source.button_release_event.disconnect(on_button_released_internal);
        event_source.motion_notify_event.disconnect(on_motion_internal);
        event_source.scroll_event.disconnect(on_mousewheel_internal);
284 285
        
        disable_drag_source();
286 287
        
        event_source = null;
288 289
    }
    
290 291
    public Gtk.Widget? get_event_source() {
        return event_source;
292 293
    }
    
294 295 296 297
    public string get_sidebar_text() {
        return page_name;
    }
    
298
    public void set_marker(SidebarMarker marker) {
299
        this.marker = marker;
300 301
    }
    
302
    public SidebarMarker? get_marker() {
303
        return marker;
304 305
    }
    
306
    public void clear_marker() {
307
        marker = null;
308 309
    }
    
310
    public virtual Gtk.MenuBar get_menubar() {
311
        assert(get_menubar_path() != null);
312
        
313
        return (Gtk.MenuBar) ui.get_widget(get_menubar_path());
314
    }
315

316 317 318 319 320 321 322
    public virtual Gtk.Toolbar get_toolbar() {
        return toolbar;
    }
    
    public virtual Gtk.Menu? get_page_context_menu() {
        return null;
    }
323 324
    
    public virtual void switching_from() {
325
        in_view = false;
326 327 328
    }
    
    public virtual void switched_to() {
329
        in_view = true;
330
        update_modifiers();
331 332 333 334
    }
    
    public bool is_in_view() {
        return in_view;
335 336
    }
    
337
    public virtual void switching_to_fullscreen(FullscreenWindow fsw) {
338 339
    }
    
340
    public virtual void returning_from_fullscreen(FullscreenWindow fsw) {
341 342
    }
    
343 344
    public Gtk.Action? get_action(string name) {
        Gtk.Action? action = action_group.get_action(name);
345 346 347
        if (action == null)
            action = common_action_group.get_action(name);
        
348 349
        if (action == null)
            warning("Page %s: Unable to locate action %s", get_page_name(), name);
350
        
351
        return action;
352
    }
353
    
354 355 356 357
    public void set_action_sensitive(string name, bool sensitive) {
        Gtk.Action? action = get_action(name);
        if (action != null)
            action.sensitive = sensitive;
358 359
    }
    
360 361 362 363 364 365 366 367 368
    public void set_action_important(string name, bool important) {
        Gtk.Action? action = get_action(name);
        if (action != null)
            action.is_important = important;
    }
    
    public void set_action_visible(string name, bool visible) {
        Gtk.Action? action = get_action(name);
        if (action == null)
Jim Nelson's avatar
Jim Nelson committed
369 370
            return;
        
371 372
        action.visible = visible;
        action.sensitive = visible;
Jim Nelson's avatar
Jim Nelson committed
373 374
    }
    
375 376 377
    public void set_action_details(string name, string? label, string? tooltip, bool sensitive) {
        Gtk.Action? action = get_action(name);
        if (action == null)
378 379
            return;
        
380 381 382 383 384 385
        if (label != null)
            action.label = label;
        
        if (tooltip != null)
            action.tooltip = tooltip;
        
386 387 388
        action.sensitive = sensitive;
    }
    
389 390 391 392
    public Gtk.Action? get_common_action(string name) {
        Gtk.Action? action = common_action_group.get_action(name);
        if (action == null)
            warning("Page %s: Unable to locate common action %s", get_page_name(), name);
393
        
394 395 396 397 398 399 400 401
        return action;
    }
    
    public void set_common_action_sensitive(string name, bool sensitive) {
        Gtk.Action? action = get_common_action(name);
        if (action != null)
            action.sensitive = sensitive;
    }
402 403 404 405 406 407

    public void set_common_action_label(string name, string label) {
        Gtk.Action? action = get_common_action(name);
        if (action != null)
            action.set_label(label);
    }
408 409 410 411 412
    
    public void set_common_action_important(string name, bool important) {
        Gtk.Action? action = get_common_action(name);
        if (action != null)
            action.is_important = important;
413
    }
414
    
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
    public bool get_ctrl_pressed() {
        return ctrl_pressed;
    }
    
    public bool get_alt_pressed() {
        return alt_pressed;
    }
    
    public bool get_shift_pressed() {
        return shift_pressed;
    }
    
    public bool get_super_pressed() {
        return super_pressed;
    }
430
    
431 432
    private void get_modifiers(out bool ctrl, out bool alt, out bool shift, out bool super) {
        int x, y;
433
        Gdk.ModifierType mask;
434
        AppWindow.get_instance().window.get_pointer(out x, out y, out mask);
435

436 437 438
        ctrl = (mask & Gdk.ModifierType.CONTROL_MASK) != 0;
        alt = (mask & Gdk.ModifierType.MOD1_MASK) != 0;
        shift = (mask & Gdk.ModifierType.SHIFT_MASK) != 0;
439
        super = (mask & Gdk.ModifierType.MOD4_MASK) != 0; // not SUPER_MASK
440
    }
441

442
    private void update_modifiers() {
443 444
        bool ctrl_currently_pressed, alt_currently_pressed, shift_currently_pressed,
            super_currently_pressed;
445
        get_modifiers(out ctrl_currently_pressed, out alt_currently_pressed,
446
            out shift_currently_pressed, out super_currently_pressed);
447
        
448 449 450 451 452 453 454 455 456 457 458 459 460 461
        if (ctrl_pressed && !ctrl_currently_pressed)
            on_ctrl_released(null);
        else if (!ctrl_pressed && ctrl_currently_pressed)
            on_ctrl_pressed(null);

        if (alt_pressed && !alt_currently_pressed)
            on_alt_released(null);
        else if (!alt_pressed && alt_currently_pressed)
            on_alt_pressed(null);

        if (shift_pressed && !shift_currently_pressed)
            on_shift_released(null);
        else if (!shift_pressed && shift_currently_pressed)
            on_shift_pressed(null);
462 463 464 465 466

        if(super_pressed && !super_currently_pressed)
            on_super_released(null);
        else if (!super_pressed && super_currently_pressed)
            on_super_pressed(null);
467 468 469 470
        
        ctrl_pressed = ctrl_currently_pressed;
        alt_pressed = alt_currently_pressed;
        shift_pressed = shift_currently_pressed;
471
        super_pressed = super_currently_pressed;
472
    }
473
    
474 475 476 477 478 479 480 481 482 483 484 485
    public PageWindow? get_page_window() {
        Gtk.Widget p = parent;
        while (p != null) {
            if (p is PageWindow)
                return (PageWindow) p;
            
            p = p.parent;
        }
        
        return null;
    }
    
486 487 488 489
    public CommandManager get_command_manager() {
        return AppWindow.get_command_manager();
    }
    
490 491 492 493 494 495
    private void init_ui() {
        // Collect all UI filenames and load them into the UI manager
        Gee.List<string> ui_filenames = new Gee.ArrayList<string>();
        init_collect_ui_filenames(ui_filenames);
        if (ui_filenames.size == 0)
            warning("No UI file specified for %s", get_page_name());
496
        
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
        foreach (string ui_filename in ui_filenames)
            init_load_ui(ui_filename);
        
        action_group = new Gtk.ActionGroup("PageActionGroup");
        
        // Collect all Gtk.Actions and add them to the Page's Gtk.ActionGroup
        Gtk.ActionEntry[] action_entries = init_collect_action_entries();
        if (action_entries.length > 0)
            action_group.add_actions(action_entries, this);
        
        // Collect all Gtk.ToggleActionEntries and add them to the Gtk.ActionGroup
        Gtk.ToggleActionEntry[] toggle_entries = init_collect_toggle_action_entries();
        if (toggle_entries.length > 0)
            action_group.add_toggle_actions(toggle_entries, this);
        
        // Collect all Gtk.RadioActionEntries and add them to the Gtk.ActionGroup
        // (Would use a similar collection scheme as the other calls, but there is a binding
        // problem with Gtk.RadioActionCallback that doesn't allow it to be stored in a struct)
        register_radio_actions(action_group);
        
        common_action_group = new Gtk.ActionGroup("CommonActionGroup");
        
        // Add common actions to the Page's common ActionGroup
        AppWindow.get_instance().add_common_actions(common_action_group);
        
        // Add both ActionGroups to the UIManager
        ui.insert_action_group(action_group, 0);
        ui.insert_action_group(common_action_group, 0);
        
        // Collect injected UI elements and add them to the UI manager
        InjectionGroup[] injection_groups = init_collect_injection_groups();
        foreach (InjectionGroup group in injection_groups) {
            foreach (InjectionGroup.Element element in group.get_elements()) {
                ui.add_ui(ui.new_merge_id(), group.get_path(), element.name, element.action,
                    element.kind, false);
            }
533
        }
534 535
        
        ui.ensure_update();
536 537
    }
    
538 539 540 541 542 543 544 545 546 547 548 549 550 551
    public override void map() {
        base.map();
        
        // initialize the Gtk.Actions according to current state
        int selected_count = get_view().get_selected_count();
        int count = get_view().get_count();
        init_actions(selected_count, count);
        update_actions(selected_count, count);
        
        // monitor state changes to update actions
        view.items_state_changed.connect(on_update_actions);
        view.selection_group_altered.connect(on_update_actions);
        view.items_visibility_changed.connect(on_update_actions);
        view.contents_altered.connect(on_update_actions);
552 553
    }
    
554 555
    private void on_update_actions() {
        update_actions(get_view().get_selected_count(), get_view().get_count());
Jim Nelson's avatar
Jim Nelson committed
556 557
    }
    
558
    private void init_load_ui(string ui_filename) {
559
        File ui_file = Resources.get_ui(ui_filename);
560
        
561
        try {
562
            ui.add_ui_from_file(ui_file.get_path());
563 564 565 566
        } catch (Error err) {
            AppWindow.error_message("Error loading UI file %s: %s".printf(
                ui_file.get_path(), err.message));
            Application.get_instance().panic();
567
        }
568 569
    }
    
570 571 572 573
    // This is called during init_ui() to collect all the UI files to be loaded into the UI
    // manager.  Because order is important here, call the base method *first*, then add the
    // classes' filename.
    protected virtual void init_collect_ui_filenames(Gee.List<string> ui_filenames) {
574
    }
575 576 577 578 579 580 581 582 583
    
    // This is called during construction for the menubar path to use for the menubar widget
    protected virtual string? get_menubar_path() {
        return null;
    }
    
    // This is called during init_ui() to collect all Gtk.ActionEntries for the page.
    protected virtual Gtk.ActionEntry[] init_collect_action_entries() {
        return new Gtk.ActionEntry[0];
Jim Nelson's avatar
Jim Nelson committed
584 585
    }
    
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
    // This is called during init_ui() to collect all Gtk.ToggleActionEntries for the page
    protected virtual Gtk.ToggleActionEntry[] init_collect_toggle_action_entries() {
        return new Gtk.ToggleActionEntry[0];
    }
    
    // This is called during init_ui() to collect all Gtk.RadioActionEntries for the page
    protected virtual void register_radio_actions(Gtk.ActionGroup action_group) {
    }
    
    // This is called during init_ui() to collect all Page.InjectedUIElements for the page.  They
    // should be added to the MultiSet using the injection path as the key.
    protected virtual InjectionGroup[] init_collect_injection_groups() {
        return new InjectionGroup[0];
    }
    
    // This is called during "map" allowing for Gtk.Actions to be updated at
    // initialization time.
Jim Nelson's avatar
Jim Nelson committed
603 604 605
    protected virtual void init_actions(int selected_count, int count) {
    }
    
606 607 608 609
    // This is called during "map" and during ViewCollection selection, visibility,
    // and collection content altered events.  This can be used to both initialize Gtk.Actions and
    // update them when selection or visibility has been altered.
    protected virtual void update_actions(int selected_count, int count) {
610 611
    }
    
612 613
    // This method enables drag-and-drop on the event source and routes its events through this
    // object
614
    public void enable_drag_source(Gdk.DragAction actions, Gtk.TargetEntry[] source_target_entries) {
615 616 617 618 619
        if (dnd_enabled)
            return;
            
        assert(event_source != null);
        
620 621 622 623
        Gtk.drag_source_set(event_source, Gdk.ModifierType.BUTTON1_MASK, source_target_entries, actions);
        
        // hook up handlers which route the event_source's DnD signals to the Page's (necessary
        // because Page is a NO_WINDOW widget and cannot support DnD on its own).
624 625 626 627 628
        event_source.drag_begin.connect(on_drag_begin);
        event_source.drag_data_get.connect(on_drag_data_get);
        event_source.drag_data_delete.connect(on_drag_data_delete);
        event_source.drag_end.connect(on_drag_end);
        event_source.drag_failed.connect(on_drag_failed);
629 630 631 632
        
        dnd_enabled = true;
    }
    
633 634 635 636 637 638
    public void disable_drag_source() {
        if (!dnd_enabled)
            return;

        assert(event_source != null);
        
639 640 641 642 643
        event_source.drag_begin.disconnect(on_drag_begin);
        event_source.drag_data_get.disconnect(on_drag_data_get);
        event_source.drag_data_delete.disconnect(on_drag_data_delete);
        event_source.drag_end.disconnect(on_drag_end);
        event_source.drag_failed.disconnect(on_drag_failed);
644 645 646 647 648
        Gtk.drag_source_unset(event_source);
        
        dnd_enabled = false;
    }
    
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
    public bool is_dnd_enabled() {
        return dnd_enabled;
    }
    
    private void on_drag_begin(Gdk.DragContext context) {
        drag_begin(context);
    }
    
    private void on_drag_data_get(Gdk.DragContext context, Gtk.SelectionData selection_data,
        uint info, uint time) {
        drag_data_get(context, selection_data, info, time);
    }
    
    private void on_drag_data_delete(Gdk.DragContext context) {
        drag_data_delete(context);
    }
    
    private void on_drag_end(Gdk.DragContext context) {
        drag_end(context);
    }
    
    // wierdly, Gtk 2.16.1 doesn't supply a drag_failed virtual method in the GtkWidget impl ...
    // Vala binds to it, but it's not available in gtkwidget.h, and so gcc complains.  Have to
    // makeshift one for now.
673
    // https://bugzilla.gnome.org/show_bug.cgi?id=584247
674 675 676 677 678 679 680 681
    public virtual bool source_drag_failed(Gdk.DragContext context, Gtk.DragResult drag_result) {
        return false;
    }
    
    private bool on_drag_failed(Gdk.DragContext context, Gtk.DragResult drag_result) {
        return source_drag_failed(context, drag_result);
    }
    
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    // Use this function rather than GDK or GTK's get_pointer, especially if called during a 
    // button-down mouse drag (i.e. a window grab).
    //
    // For more information, see: https://bugzilla.gnome.org/show_bug.cgi?id=599937
    public bool get_event_source_pointer(out int x, out int y, out Gdk.ModifierType mask) {
        if (event_source == null)
            return false;
        
        event_source.window.get_pointer(out x, out y, out mask);
        
        if (last_down.x < 0 || last_down.y < 0)
            return true;
            
        // check for bogus values inside a drag which goes outside the window
        // caused by (most likely) X windows signed 16-bit int overflow and fixup
        // (https://bugzilla.gnome.org/show_bug.cgi?id=599937)
        
        if ((x - last_down.x).abs() >= 0x7FFF)
            x += 0xFFFF;
        
        if ((y - last_down.y).abs() >= 0x7FFF)
            y += 0xFFFF;
        
        return true;
    }
    
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
    protected virtual bool on_left_click(Gdk.EventButton event) {
        return false;
    }
    
    protected virtual bool on_middle_click(Gdk.EventButton event) {
        return false;
    }
    
    protected virtual bool on_right_click(Gdk.EventButton event) {
        return false;
    }
    
    protected virtual bool on_left_released(Gdk.EventButton event) {
        return false;
    }
    
    protected virtual bool on_middle_released(Gdk.EventButton event) {
        return false;
    }
    
    protected virtual bool on_right_released(Gdk.EventButton event) {
        return false;
    }
    
    private bool on_button_pressed_internal(Gdk.EventButton event) {
733 734
        switch (event.button) {
            case 1:
735 736 737
                if (event_source != null)
                    event_source.grab_focus();
                
738 739 740 741
                // stash location of mouse down for drag fixups
                last_down.x = (int) event.x;
                last_down.y = (int) event.y;
                
742
                return on_left_click(event);
743

744 745 746 747 748 749 750 751 752 753
            case 2:
                return on_middle_click(event);
            
            case 3:
                return on_right_click(event);
            
            default:
                return false;
        }
    }
754 755 756 757
    
    private bool on_button_released_internal(Gdk.EventButton event) {
        switch (event.button) {
            case 1:
758 759 760
                // clear when button released, only for drag fixups
                last_down = { -1, -1 };
                
761 762 763 764 765 766 767 768 769 770 771 772
                return on_left_released(event);
            
            case 2:
                return on_middle_released(event);
            
            case 3:
                return on_right_released(event);
            
            default:
                return false;
        }
    }
773

774
    protected virtual bool on_ctrl_pressed(Gdk.EventKey? event) {
775
        return false;
776 777
    }
    
778
    protected virtual bool on_ctrl_released(Gdk.EventKey? event) {
779
        return false;
780 781
    }
    
782
    protected virtual bool on_alt_pressed(Gdk.EventKey? event) {
783
        return false;
784 785
    }
    
786
    protected virtual bool on_alt_released(Gdk.EventKey? event) {
787 788 789
        return false;
    }
    
790
    protected virtual bool on_shift_pressed(Gdk.EventKey? event) {
791
        return false;
792 793
    }
    
794
    protected virtual bool on_shift_released(Gdk.EventKey? event) {
795
        return false;
796
    }
797 798 799 800 801 802 803 804

    protected virtual bool on_super_pressed(Gdk.EventKey? event) {
        return false;
    }
    
    protected virtual bool on_super_released(Gdk.EventKey? event) {
        return false;
    }
805
    
806 807 808 809 810 811 812 813
    protected virtual bool on_app_key_pressed(Gdk.EventKey event) {
        return false;
    }
    
    protected virtual bool on_app_key_released(Gdk.EventKey event) {
        return false;
    }
    
814
    public bool notify_app_key_pressed(Gdk.EventKey event) {
815 816
        bool ctrl_currently_pressed, alt_currently_pressed, shift_currently_pressed,
            super_currently_pressed;
817
        get_modifiers(out ctrl_currently_pressed, out alt_currently_pressed,
818
            out shift_currently_pressed, out super_currently_pressed);
819

820 821 822
        switch (Gdk.keyval_name(event.keyval)) {
            case "Control_L":
            case "Control_R":
823 824 825
                if (!ctrl_currently_pressed || ctrl_pressed)
                    return false;

826 827 828
                ctrl_pressed = true;
                
                return on_ctrl_pressed(event);
829 830 831

            case "Meta_L":
            case "Meta_R":
832 833
            case "Alt_L":
            case "Alt_R":
834 835 836
                if (!alt_currently_pressed || alt_pressed)
                    return false;

837 838 839 840 841 842
                alt_pressed = true;
                
                return on_alt_pressed(event);
            
            case "Shift_L":
            case "Shift_R":
843 844 845
                if (!shift_currently_pressed || shift_pressed)
                    return false;

846 847 848
                shift_pressed = true;
                
                return on_shift_pressed(event);
849 850 851 852 853 854 855 856
            
            case "Super_L":
            case "Super_R":
                if (!super_currently_pressed || super_pressed)
                    return false;
                
                super_pressed = true;
                
857
                return on_super_pressed(event);
858
        }
859
        
860
        return on_app_key_pressed(event);
861
    }
862
    
863
    public bool notify_app_key_released(Gdk.EventKey event) {
864 865
        bool ctrl_currently_pressed, alt_currently_pressed, shift_currently_pressed,
            super_currently_pressed;
866
        get_modifiers(out ctrl_currently_pressed, out alt_currently_pressed,
867
            out shift_currently_pressed, out super_currently_pressed);
868

869 870 871
        switch (Gdk.keyval_name(event.keyval)) {
            case "Control_L":
            case "Control_R":
872 873 874
                if (ctrl_currently_pressed || !ctrl_pressed)
                    return false;

875 876 877 878
                ctrl_pressed = false;
                
                return on_ctrl_released(event);
            
879 880
            case "Meta_L":
            case "Meta_R":
881 882
            case "Alt_L":
            case "Alt_R":
883 884 885
                if (alt_currently_pressed || !alt_pressed)
                    return false;

886 887 888 889 890 891
                alt_pressed = false;
                
                return on_alt_released(event);
            
            case "Shift_L":
            case "Shift_R":
892 893 894
                if (shift_currently_pressed || !shift_pressed)
                    return false;

895 896 897
                shift_pressed = false;
                
                return on_shift_released(event);
898 899 900 901 902 903 904 905 906

            case "Super_L":
            case "Super_R":
                if (super_currently_pressed || !super_pressed)
                    return false;

                super_pressed = false;
                
                return on_super_released(event);
907
        }
908
        
909
        return on_app_key_released(event);
910
    }
911
    
912 913
    public bool notify_app_focus_in(Gdk.EventFocus event) {
        update_modifiers();
914
        
915 916 917 918 919 920
        return false;
    }

    public bool notify_app_focus_out(Gdk.EventFocus event) {
        return false;
    }
921 922 923 924
    
    protected virtual void on_move(Gdk.Rectangle rect) {
    }
    
925 926 927 928 929 930
    protected virtual void on_move_start(Gdk.Rectangle rect) {
    }
    
    protected virtual void on_move_finished(Gdk.Rectangle rect) {
    }
    
931
    protected virtual void on_resize(Gdk.Rectangle rect) {
932
    }
933
    
934 935 936 937 938 939
    protected virtual void on_resize_start(Gdk.Rectangle rect) {
    }
    
    protected virtual void on_resize_finished(Gdk.Rectangle rect) {
    }
    
940 941 942 943 944
    protected virtual bool on_configure(Gdk.EventConfigure event, Gdk.Rectangle rect) {
        return false;
    }
    
    public bool notify_configure_event(Gdk.EventConfigure event) {
945 946 947 948 949 950
        Gdk.Rectangle rect = Gdk.Rectangle();
        rect.x = event.x;
        rect.y = event.y;
        rect.width = event.width;
        rect.height = event.height;
        
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
        // special case events, to report when a configure first starts (and appears to end)
        if (last_configure_ms == 0) {
            if (last_position.x != rect.x || last_position.y != rect.y) {
                on_move_start(rect);
                report_move_finished = true;
            }
            
            if (last_position.width != rect.width || last_position.height != rect.height) {
                on_resize_start(rect);
                report_resize_finished = true;
            }

            // need to check more often then the timeout, otherwise it could be up to twice the
            // wait time before it's noticed
            Timeout.add(CONSIDER_CONFIGURE_HALTED_MSEC / 8, check_configure_halted);
        }
967 968 969 970 971 972 973
        
        if (last_position.x != rect.x || last_position.y != rect.y)
            on_move(rect);
        
        if (last_position.width != rect.width || last_position.height != rect.height)
            on_resize(rect);
        
974
        last_position = rect;
975 976
        last_configure_ms = now_ms();

977
        return on_configure(event, rect);
978
    }
979
    
980
    private bool check_configure_halted() {
981 982 983
        if (is_destroyed)
            return false;

984 985
        if ((now_ms() - last_configure_ms) < CONSIDER_CONFIGURE_HALTED_MSEC)
            return true;
986
        
987 988 989 990 991 992 993 994 995 996 997 998 999
        if (report_move_finished)
            on_move_finished((Gdk.Rectangle) allocation);
        
        if (report_resize_finished)
            on_resize_finished((Gdk.Rectangle) allocation);
        
        last_configure_ms = 0;
        report_move_finished = false;
        report_resize_finished = false;
        
        return false;
    }
    
1000
    protected virtual bool on_motion(Gdk.EventMotion event, int x, int y, Gdk.ModifierType mask) {
1001 1002
        check_cursor_hiding();

1003 1004 1005 1006 1007 1008 1009
        return false;
    }
    
    private bool on_motion_internal(Gdk.EventMotion event) {
        int x, y;
        Gdk.ModifierType mask;
        if (event.is_hint) {
1010
            get_event_source_pointer(out x, out y, out mask);
1011 1012 1013 1014 1015 1016 1017 1018
        } else {
            x = (int) event.x;
            y = (int) event.y;
            mask = event.state;
        }
        
        return on_motion(event, x, y, mask);
    }
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    private bool on_mousewheel_internal(Gdk.EventScroll event) {
        switch (event.direction) {
            case Gdk.ScrollDirection.UP:
                return on_mousewheel_up(event);

            case Gdk.ScrollDirection.DOWN:
                return on_mousewheel_down(event);
            
            case Gdk.ScrollDirection.LEFT:
                return on_mousewheel_left(event);

            case Gdk.ScrollDirection.RIGHT:
                return on_mousewheel_right(event);
           
            default:
                return false;
        }
    }
    
    protected virtual bool on_mousewheel_up(Gdk.EventScroll event) {
        return false;
    }
    
    protected virtual bool on_mousewheel_down(Gdk.EventScroll event) {
        return false;
    }
    
    protected virtual bool on_mousewheel_left(Gdk.EventScroll event) {
        return false;
    }
    
    protected virtual bool on_mousewheel_right(Gdk.EventScroll event) {
        return false;
    }
    
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    protected virtual bool on_context_keypress() {
        return false;
    }
    
    protected virtual bool on_context_buttonpress(Gdk.EventButton event) {
        return false;
    }
    
    protected virtual bool on_context_invoked() {
        return true;
    }

    protected bool popup_context_menu(Gtk.Menu? context_menu,
        Gdk.EventButton? event = null) {

1070
        if (context_menu == null || !on_context_invoked())
1071 1072 1073 1074 1075 1076 1077 1078 1079
            return false;

        if (event == null)
            context_menu.popup(null, null, null, 0, Gtk.get_current_event_time());
        else
            context_menu.popup(null, null, null, event.button, event.time);

        return true;
    }
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154

    protected void on_event_source_realize() {
        assert(event_source.window != null); // the realize event means the Widget has a window

        if (event_source.window.get_cursor() != null) {
            last_cursor = event_source.window.get_cursor().type;
            return;
        }

        // no custom cursor defined, check parents
        Gdk.Window? parent_window = event_source.window;
        do {
            parent_window = parent_window.get_parent();
        } while (parent_window != null && parent_window.get_cursor() == null);
        
        if (parent_window != null)
            last_cursor = parent_window.get_cursor().type;
    }

    public void set_cursor_hide_time(int hide_time) {
        cursor_hide_msec = hide_time;
    }

    public void start_cursor_hiding() {
        check_cursor_hiding();
    }

    public void stop_cursor_hiding() {
        if (last_timeout_id != 0)
            Source.remove(last_timeout_id);
    }

    public void suspend_cursor_hiding() {
        cursor_hide_time_cached = cursor_hide_msec;

        if (last_timeout_id != 0)
            Source.remove(last_timeout_id);

        cursor_hide_msec = 0;
    }

    public void restore_cursor_hiding() {
        cursor_hide_msec = cursor_hide_time_cached;
        check_cursor_hiding();
    }

    // Use this method to set the cursor for a page, NOT window.set_cursor(...)
    protected virtual void set_page_cursor(Gdk.CursorType cursor_type) {
        last_cursor = cursor_type;

        if (!cursor_hidden && event_source != null)
            event_source.window.set_cursor(new Gdk.Cursor(cursor_type));
    }

    private void check_cursor_hiding() {
        if (cursor_hidden) {
            cursor_hidden = false;
            set_page_cursor(last_cursor);
        }

        if (cursor_hide_msec != 0) {
            if (last_timeout_id != 0)
                Source.remove(last_timeout_id);
            last_timeout_id = Timeout.add(cursor_hide_msec, on_hide_cursor);
        }
    }

    private bool on_hide_cursor() {
        cursor_hidden = true;

        if (event_source != null)
            event_source.window.set_cursor(new Gdk.Cursor(Gdk.CursorType.BLANK_CURSOR));

        return false;
    }
1155 1156 1157
}

public abstract class CheckerboardPage : Page {
1158 1159
    private const int AUTOSCROLL_PIXELS = 50;
    private const int AUTOSCROLL_TICKS_MSEC = 50;
1160
    
1161
    private CheckerboardLayout layout;
1162 1163
    private Gtk.Menu item_context_menu = null;
    private Gtk.Menu page_context_menu = null;
1164
    private Gtk.Viewport viewport = new Gtk.Viewport(null, null);
1165 1166 1167
    protected CheckerboardItem anchor = null;
    protected CheckerboardItem cursor = null;
    private CheckerboardItem highlighted = null;
1168
    private bool autoscroll_scheduled = false;
1169
    private CheckerboardItem activated_item = null;
1170
    private Gee.ArrayList<CheckerboardItem> previously_selected = null;
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
    public enum Activator {
        KEYBOARD,
        MOUSE
    }

    public struct KeyboardModifiers {
        public KeyboardModifiers(Page page) {
            ctrl_pressed = page.get_ctrl_pressed();
            alt_pressed = page.get_alt_pressed();
            shift_pressed = page.get_shift_pressed();
            super_pressed = page.get_super_pressed();
        }

        public bool ctrl_pressed;
        public bool alt_pressed;
        public bool shift_pressed;
        public bool super_pressed;
    }

Jim Nelson's avatar
Jim Nelson committed
1191
    public CheckerboardPage(string page_name) {
1192
        base (page_name);
Jim Nelson's avatar
Jim Nelson committed
1193
        
1194
        layout = new CheckerboardLayout(get_view());
1195
        layout.set_name(page_name);
1196
        
1197
        set_event_source(layout);
1198

1199 1200 1201
        set_border_width(0);
        set_shadow_type(Gtk.ShadowType.NONE);
        
1202 1203 1204 1205 1206
        viewport.set_border_width(0);
        viewport.set_shadow_type(Gtk.ShadowType.NONE);
        
        viewport.add(layout);
        
1207 1208 1209
        // want to set_adjustments before adding to ScrolledWindow to let our signal handlers
        // run first ... otherwise, the thumbnails draw late
        layout.set_adjustments(get_hadjustment(), get_vadjustment());
1210
        
1211
        add(viewport);
1212 1213
        
        // need to monitor items going hidden when dealing with anchor/cursor/highlighted items
1214
        get_view().items_hidden.connect(on_items_hidden);
Jim Nelson's avatar
Jim Nelson committed
1215 1216 1217
        
        // scrollbar policy
        set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
1218 1219
    }
    
1220 1221
    public void init_item_context_menu(string path) {
        item_context_menu = (Gtk.Menu) ui.get_widget(path);
1222
    }
1223 1224 1225 1226 1227 1228 1229

    public void init_page_context_menu(string path) {
        page_context_menu = (Gtk.Menu) ui.get_widget(path);
    }
   
    public Gtk.Menu? get_context_menu() {
        // show page context menu if nothing is selected
1230 1231
        return (get_view().get_selected_count() != 0) ? get_item_context_menu() :
            get_page_context_menu();
1232 1233 1234 1235
    }
    
    public virtual Gtk.Menu? get_item_context_menu() {
        return item_context_menu;
1236
    }
1237
    
1238 1239
    public override Gtk.Menu? get_page_context_menu() {
        return page_context_menu;
1240 1241
    }
    
1242 1243
    protected override bool on_context_keypress() {
        return popup_context_menu(get_context_menu());
1244 1245
    }
    
1246 1247
    protected virtual void on_item_activated(CheckerboardItem item, Activator activator, 
        KeyboardModifiers modifiers) {
1248 1249
    }
    
1250 1251 1252 1253
    public CheckerboardLayout get_checkerboard_layout() {
        return layout;
    }
    
1254 1255
    public override void switching_from() {
        layout.set_in_view(false);
1256 1257 1258

        // unselect everything so selection won't persist after page loses focus 
        get_view().unselect_all();
1259 1260 1261 1262 1263
        
        base.switching_from();
    }
    
    public override void switched_to() {
1264
        layout.set_in_view(true);       
1265

1266 1267 1268
        base.switched_to();
    }
    
1269
    public abstract CheckerboardItem? get_fullscreen_photo();
1270
    
1271 1272
    public void set_page_message(string message) {
        layout.set_message(message);
1273 1274
        if (is_in_view())
            layout.queue_draw();
Jim Nelson's avatar
Jim Nelson committed
1275 1276
    }
    
1277 1278 1279 1280 1281 1282
    public override void set_page_name(string name) {
        base.set_page_name(name);
        
        layout.set_name(name);
    }
    
1283
    public CheckerboardItem? get_item_at_pixel(double x, double y) {
1284
        return layout.get_item_at_pixel(x, y);
1285
    }
1286 1287 1288
    
    private void on_items_hidden(Gee.Iterable<DataView> hidden) {
        foreach (DataView view in hidden) {
1289
            CheckerboardItem item = (CheckerboardItem) view;
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300
            
            if (anchor == item)
                anchor = null;
            
            if (cursor == item)
                cursor = null;
            
            if (highlighted == item)
                highlighted = null;
        }
    }
1301

1302 1303
    protected override bool key_press_event(Gdk.EventKey event) {
        bool handled = true;
1304 1305 1306

        // mask out the modifiers we're interested in
        uint state = event.state & Gdk.ModifierType.SHIFT_MASK;
1307
        
1308 1309 1310 1311
        switch (Gdk.keyval_name(event.keyval)) {
            case "Up":
            case "KP_Up":
                move_cursor(CompassPoint.NORTH);
1312
                select_anchor_to_cursor(state);
1313 1314 1315 1316 1317
            break;
            
            case "Down":
            case "KP_Down":
                move_cursor(CompassPoint.SOUTH);
1318
                select_anchor_to_cursor(state);
1319 1320 1321 1322 1323
            break;
            
            case "Left":
            case "KP_Left":
                move_cursor(CompassPoint.WEST);
1324
                select_anchor_to_cursor(state);
1325 1326 1327 1328 1329
            break;
            
            case "Right":
            case "KP_Right":
                move_cursor(CompassPoint.EAST);
1330
                select_anchor_to_cursor(state);
1331 1332 1333 1334
            break;
            
            case "Home":
            case "KP_Home":
1335
                CheckerboardItem? first = (CheckerboardItem?) get_view().get_first();
1336 1337
                if (first != null)
                    cursor_to_item(first);
1338
                select_anchor_to_cursor(state);
1339 1340 1341 1342
            break;
            
            case "End":
            case "KP_End":
1343
                CheckerboardItem? last = (CheckerboardItem?) get_view().get_last();
1344 1345
                if (last != null)
                    cursor_to_item(last);
1346
                select_anchor_to_cursor(state);
1347 1348 1349 1350
            break;
            
            case "Return":
            case "KP_Enter":
1351
                if (get_view().get_selected_count() == 1)
1352 1353
                    on_item_activated((CheckerboardItem) get_view().get_selected_at(0),
                        Activator.KEYBOARD, KeyboardModifiers(this));
1354
                else
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
                    handled = false;
            break;
            
            default:
                handled = false;
            break;
        }
        
        if (handled)
            return true;
        
        return (base.key_press_event != null) ? base.key_press_event(event) : true;
    }
    
    protected override bool on_left_click(Gdk.EventButton event) {
1370
        // only interested in single-click and double-clicks for now
1371
        if ((event.type != Gdk.EventType.BUTTON_PRESS) && (event.type != Gdk.EventType.2BUTTON_PRESS))
1372 1373 1374 1375 1376
            return false;
        
        // mask out the modifiers we're interested in
        uint state = event.state & (Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK);
        
1377 1378
        // use clicks for multiple selection and activation only; single selects are handled by
        // button release, to allow for multiple items to be selected then dragged
1379
        CheckerboardItem item = get_item_at_pixel(event.x, event.y);
1380 1381
        if (item != null) {
            switch (state) {
1382
                case Gdk.ModifierType.CONTROL_MASK:
1383 1384
                    // with only Ctrl pressed, multiple selections are possible ... chosen item
                    // is toggled
1385 1386
                    Marker marker = get_view().mark(item);
                    get_view().toggle_marked(marker);
1387

1388 1389 1390 1391
                    if (item.is_selected()) {
                        anchor = item;
                        cursor = item;
                    }
1392
                break;
1393
                
1394
                case Gdk.ModifierType.SHIFT_MASK:
1395
                    get_view().unselect_all();
1396 1397 1398 1399
                    
                    if (anchor == null)
                        anchor = item;
                    
1400
                    select_between_items(anchor, item);
1401
                    
1402
                    cursor = item;
1403
                break;
1404
                
1405
                case Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK:
1406
                    // TODO
1407
                break;
1408
                
1409
                default:
1410
                    if (event.type == Gdk.EventType.2BUTTON_PRESS) {
1411
                        activated_item = item;
1412
                    } else {
1413 1414 1415 1416 1417
                        // if the user has selected one or more items and is preparing for a drag,
                        // don't want to blindly unselect: if they've clicked on an unselected item
                        // unselect all and select that one; if they've clicked on a previously
                        // selected item, do nothing
                        if (!item.is_selected()) {
1418
                            get_view().unselect_all();
1419 1420
                            get_view().select_marked(get_view().mark(item));
                        }
1421
                    }
1422

1423 1424
                    anchor = item;
                    cursor = item;
1425
                break;
1426 1427
            }
        } else {
1428 1429 1430 1431
            // user clicked on "dead" area; only unselect if control is not pressed
            // do we want similar behavior for shift as well?
            if (state != Gdk.ModifierType.CONTROL_MASK)
                get_view().unselect_all();
1432

1433 1434 1435 1436
            // grab previously marked items
            previously_selected = new Gee.ArrayList<CheckerboardItem>();
            foreach (DataView view in get_view().get_selected())
                previously_selected.add((CheckerboardItem) view);
1437

1438
            layout.set_drag_select_origin((int) event.x, (int) event.y);
1439 1440 1441

            return true;
        }
1442

1443 1444 1445
        // need to determine if the signal should be passed to the DnD handlers
        // Return true to block the DnD handler, false otherwise

1446
        return get_view().get_selected_count() == 0;
1447 1448
    }
    
1449
    protected override bool on_left_released(Gdk.EventButton event) {
1450 1451
        previously_selected = null;

1452
        // if drag-selecting, stop here and do nothing else
1453
        if (layout.is_drag_select_active()) {
1454
            layout.clear_drag_select();
1455
            anchor = cursor;
1456