d8d65bd8a89575c2c649cf739b3e28ebe904a782
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_folder_target.mm
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_folder_target.h"
6
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_model.h"
10 #include "chrome/browser/bookmarks/bookmark_node_data.h"
11 #include "chrome/browser/bookmarks/bookmark_pasteboard_helper_mac.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_folder_controller.h"
14 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h"
15 #import "ui/base/cocoa/cocoa_base_utils.h"
16
17 NSString* kBookmarkButtonDragType = @"ChromiumBookmarkButtonDragType";
18
19 @interface BookmarkFolderTarget()
20 // Copies the given bookmark node to the given pasteboard, declaring appropriate
21 // types (to paste a URL with a title).
22 - (void)copyBookmarkNode:(const BookmarkNode*)node
23         toDragPasteboard:(NSPasteboard*)pboard;
24 @end
25
26 @implementation BookmarkFolderTarget
27
28 - (id)initWithController:(id<BookmarkButtonControllerProtocol>)controller
29                  profile:(Profile*)profile {
30   if ((self = [super init])) {
31     controller_ = controller;
32     profile_ = profile;
33   }
34   return self;
35 }
36
37 // This IBAction is called when the user clicks (mouseUp, really) on a
38 // "folder" bookmark button.  (In this context, "Click" does not
39 // include right-click to open a context menu which follows a
40 // different path).  Scenarios when folder X is clicked:
41 //  *Predicate*        *Action*
42 //  (nothing)          Open Folder X
43 //  Folder X open      Close folder X
44 //  Folder Y open      Close Y, open X
45 //  Cmd-click          Open All with proper disposition
46 //
47 //  Note complication in which a click-drag engages drag and drop, not
48 //  a click-to-open.  Thus the path to get here is a little twisted.
49 - (IBAction)openBookmarkFolderFromButton:(id)sender {
50   DCHECK(sender);
51   // Watch out for a modifier click.  For example, command-click
52   // should open all.
53   //
54   // NOTE: we cannot use [[sender cell] mouseDownFlags] because we
55   // thwart the normal mouse click mechanism to make buttons
56   // draggable.  Thus we must use [NSApp currentEvent].
57   //
58   // Holding command while using the scroll wheel (or moving around
59   // over a bookmark folder) can confuse us.  Unless we check the
60   // event type, we are not sure if this is an "open folder" due to a
61   // hover-open or "open folder" due to a click.  It doesn't matter
62   // (both do the same thing) unless a modifier is held, since
63   // command-click should "open all" but command-move should not.
64   // WindowOpenDispositionFromNSEvent does not consider the event
65   // type; only the modifiers.  Thus the need for an extra
66   // event-type-check here.
67   DCHECK([sender bookmarkNode]->is_folder());
68   NSEvent* event = [NSApp currentEvent];
69   WindowOpenDisposition disposition =
70       ui::WindowOpenDispositionFromNSEvent(event);
71   if (([event type] != NSMouseEntered) &&
72       ([event type] != NSMouseMoved) &&
73       ([event type] != NSScrollWheel) &&
74       (disposition == NEW_BACKGROUND_TAB)) {
75     [controller_ closeAllBookmarkFolders];
76     [controller_ openAll:[sender bookmarkNode] disposition:disposition];
77     return;
78   }
79
80   // If click on same folder, close it and be done.
81   // Else we clicked on a different folder so more work to do.
82   if ([[controller_ folderController] parentButton] == sender) {
83     [controller_ closeBookmarkFolder:controller_];
84     return;
85   }
86
87   [controller_ addNewFolderControllerWithParentButton:sender];
88 }
89
90 - (void)copyBookmarkNode:(const BookmarkNode*)node
91         toDragPasteboard:(NSPasteboard*)pboard {
92   if (!node) {
93     NOTREACHED();
94     return;
95   }
96
97   if (node->is_folder()) {
98     // TODO(viettrungluu): I'm not sure what we should do, so just declare the
99     // "additional" types we're given for now. Maybe we want to add a list of
100     // URLs? Would we then have to recurse if there were subfolders?
101     // In the meanwhile, we *must* set it to a known state.
102     [pboard clearContents];
103   } else {
104     BookmarkNodeData data(node);
105     data.SetOriginatingProfile(profile_);
106     data.WriteToClipboard(ui::CLIPBOARD_TYPE_DRAG);
107   }
108 }
109
110 - (void)fillPasteboard:(NSPasteboard*)pboard
111        forDragOfButton:(BookmarkButton*)button {
112   if (const BookmarkNode* node = [button bookmarkNode]) {
113     // Put the bookmark information into the pasteboard, and then write our own
114     // data for |kBookmarkButtonDragType|.
115     [self copyBookmarkNode:node toDragPasteboard:pboard];
116     [pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]
117                owner:nil];
118     [pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]
119             forType:kBookmarkButtonDragType];
120   } else {
121     NOTREACHED();
122   }
123 }
124
125 @end