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