OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 #include "chrome/browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/sys_string_conversions.h" | |
10 #include "grit/generated_resources.h" | |
11 #include "ui/base/l10n/l10n_util.h" | |
12 | |
13 namespace { | |
Nico
2012/08/13 19:40:24
"const has implicit internal linkage, no need for
sail
2012/08/13 20:40:33
Done.
| |
14 | |
15 const CGFloat kCheckboxMargin = 5; | |
16 const CGFloat kCheckboxMaxWidth = 350; | |
17 | |
18 } // namespace | |
19 | |
20 @interface MediaGalleriesCocoaBridge : NSObject { | |
21 chrome::MediaGalleriesDialogCocoa* dialog_; | |
22 } | |
23 | |
24 @property(nonatomic, readwrite) chrome::MediaGalleriesDialogCocoa* dialog; | |
25 | |
26 @end | |
27 | |
28 @implementation MediaGalleriesCocoaBridge | |
29 | |
30 @synthesize dialog = dialog_; | |
31 | |
32 - (void)onAddFolderClicked:(id)sender { | |
33 DCHECK(dialog_); | |
34 dialog_->OnAddFolderClicked(); | |
35 } | |
36 | |
37 - (void)onCheckboxToggled:(id)sender { | |
38 DCHECK(dialog_); | |
39 dialog_->OnCheckboxToggled(sender); | |
40 } | |
41 | |
42 - (void)sheetDidEnd:(NSWindow*)parent | |
43 returnCode:(NSInteger)returnCode | |
44 context:(void*)context { | |
45 DCHECK(dialog_); | |
46 dialog_->SheetDidEnd(returnCode); | |
47 } | |
48 | |
49 @end | |
50 | |
51 namespace chrome { | |
52 | |
53 MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa( | |
54 MediaGalleriesDialogController* controller, | |
55 MediaGalleriesCocoaBridge* cocoa_bridge) | |
56 : ConstrainedWindowMacDelegateSystemSheet( | |
57 cocoa_bridge, @selector(sheetDidEnd:returnCode:context:)), | |
58 controller_(controller), | |
59 window_(NULL), | |
60 accepted_(false), | |
61 cocoa_bridge_([cocoa_bridge retain]) { | |
62 alert_.reset([[NSAlert alloc] init]); | |
63 [alert_ setMessageText:base::SysUTF16ToNSString(controller_->GetHeader())]; | |
64 [alert_ setInformativeText:SysUTF16ToNSString(controller_->GetSubtext())]; | |
65 [alert_ addButtonWithTitle:l10n_util::GetNSString( | |
66 IDS_MEDIA_GALLERIES_DIALOG_CONFIRM)]; | |
67 [alert_ addButtonWithTitle:l10n_util::GetNSString( | |
68 IDS_MEDIA_GALLERIES_DIALOG_CANCEL)]; | |
69 [alert_ addButtonWithTitle:l10n_util::GetNSString( | |
70 IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY)]; | |
71 | |
72 // Override the add button click handler to prevent the alert from closing. | |
73 NSButton* add_button = [[alert_ buttons] objectAtIndex:2]; | |
74 [add_button setTarget:cocoa_bridge_]; | |
75 [add_button setAction:@selector(onAddFolderClicked:)]; | |
76 | |
77 // Add gallery permission checkboxes inside an accessory view. | |
78 checkbox_container_.reset([[NSView alloc] initWithFrame:NSZeroRect]); | |
79 checkboxes_.reset([[NSMutableArray alloc] init]); | |
80 const MediaGalleriesDialogController::KnownGalleryPermissions& permissions = | |
81 controller_->permissions(); | |
82 bool has_permitted_galleries = false; | |
83 for (MediaGalleriesDialogController::KnownGalleryPermissions:: | |
84 const_reverse_iterator iter = permissions.rbegin(); | |
85 iter != permissions.rend(); iter++) { | |
86 const MediaGalleriesDialogController::GalleryPermission& permission = | |
87 iter->second; | |
88 UpdateGalleryCheckbox(nil, &permission.pref_info, permission.allowed); | |
89 has_permitted_galleries = has_permitted_galleries || permission.allowed; | |
Nico
2012/08/13 19:40:24
The controller should have a function HasPermitted
sail
2012/08/13 20:40:33
Done.
Will updated the views implementation in thi
| |
90 } | |
91 UpdateCheckboxContainerFrame(); | |
92 [alert_ setAccessoryView:checkbox_container_]; | |
93 | |
94 // As a safeguard against the user skipping reading over the dialog and just | |
95 // confirming, the button will be unavailable for dialogs without any checks | |
96 // until the user toggles something. | |
97 [[[alert_ buttons] objectAtIndex:0] setEnabled:has_permitted_galleries]; | |
98 | |
99 set_sheet(alert_); | |
100 window_ = new ConstrainedWindowMac(controller->tab_contents(), this); | |
101 } | |
102 | |
103 MediaGalleriesDialogCocoa::~MediaGalleriesDialogCocoa() { | |
104 } | |
105 | |
106 void MediaGalleriesDialogCocoa::OnAddFolderClicked() { | |
107 controller_->OnAddFolderClicked(); | |
108 } | |
109 | |
110 void MediaGalleriesDialogCocoa::OnCheckboxToggled(NSButton* checkbox) { | |
111 const MediaGalleriesDialogController::KnownGalleryPermissions& permissions = | |
112 controller_->permissions(); | |
113 [[[alert_ buttons] objectAtIndex:0] setEnabled:YES]; | |
114 | |
115 for (MediaGalleriesDialogController::KnownGalleryPermissions:: | |
116 const_reverse_iterator iter = permissions.rbegin(); | |
117 iter != permissions.rend(); iter++) { | |
118 const MediaGalleryPrefInfo* gallery = &iter->second.pref_info; | |
119 NSString* device_id = base::SysUTF8ToNSString(gallery->device_id); | |
120 if ([[[checkbox cell] representedObject] isEqual:device_id]) { | |
121 controller_->GalleryToggled(gallery, [checkbox state] == NSOnState); | |
Nico
2012/08/13 19:40:24
DidToggleGallery() is chromier naming
sail
2012/08/13 20:40:33
Done.
Once the views code lands I'll update this
| |
122 break; | |
123 } | |
124 } | |
125 } | |
126 | |
127 void MediaGalleriesDialogCocoa::SheetDidEnd(NSInteger result) { | |
128 switch (result) { | |
129 case NSAlertFirstButtonReturn: | |
130 accepted_ = true; | |
131 window_->CloseConstrainedWindow(); | |
132 break; | |
133 case NSAlertSecondButtonReturn: | |
134 window_->CloseConstrainedWindow(); | |
135 break; | |
136 default: | |
137 NOTREACHED(); | |
138 break; | |
139 } | |
140 } | |
141 | |
142 void MediaGalleriesDialogCocoa::UpdateGalleryCheckbox( | |
143 NSButton* checkbox, | |
144 const MediaGalleryPrefInfo* gallery, | |
145 bool permitted) { | |
146 CGFloat y_pos = 0; | |
147 if (checkbox) { | |
148 y_pos = NSMaxY([checkbox frame]); | |
149 } else { | |
150 y_pos = NSMaxY([[checkboxes_ lastObject] frame]); | |
151 if ([checkboxes_ count] > 0) | |
152 y_pos += kCheckboxMargin; | |
153 | |
154 scoped_nsobject<NSButton> new_checkbox( | |
155 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
156 NSString* device_id = base::SysUTF8ToNSString(gallery->device_id); | |
157 [[new_checkbox cell] setRepresentedObject:device_id]; | |
158 [[new_checkbox cell] setLineBreakMode:NSLineBreakByTruncatingMiddle]; | |
159 [new_checkbox setButtonType:NSSwitchButton]; | |
160 [new_checkbox setTarget:cocoa_bridge_]; | |
161 [new_checkbox setAction:@selector(onCheckboxToggled:)]; | |
162 | |
163 [checkbox_container_ addSubview:new_checkbox]; | |
164 [checkboxes_ addObject:new_checkbox]; | |
165 checkbox = new_checkbox.get(); | |
166 } | |
167 | |
168 [checkbox setTitle:base::SysUTF16ToNSString(gallery->display_name)]; | |
169 [checkbox setToolTip: | |
170 base::SysUTF16ToNSString(gallery->path.LossyDisplayName())]; | |
171 [checkbox setState:permitted ? NSOnState : NSOffState]; | |
172 | |
173 [checkbox sizeToFit]; | |
174 NSRect rect = [checkbox bounds]; | |
175 rect.origin.y = y_pos; | |
176 rect.size.width = std::min(rect.size.width, kCheckboxMaxWidth); | |
177 [checkbox setFrame:rect]; | |
178 } | |
179 | |
180 void MediaGalleriesDialogCocoa::UpdateCheckboxContainerFrame() { | |
181 NSRect rect = NSMakeRect( | |
182 0, 0, kCheckboxMaxWidth, NSMaxY([[checkboxes_ lastObject] frame])); | |
183 [checkbox_container_ setFrame:rect]; | |
184 } | |
185 | |
186 void MediaGalleriesDialogCocoa::UpdateGallery( | |
187 const MediaGalleryPrefInfo* gallery, | |
188 bool permitted) { | |
189 NSButton* checkbox = nil; | |
190 NSString* device_id = base::SysUTF8ToNSString(gallery->device_id); | |
191 | |
192 for (NSButton* button in checkboxes_.get()) { | |
193 if ([[[button cell] representedObject] isEqual:device_id]) { | |
194 checkbox = button; | |
195 break; | |
196 } | |
197 } | |
198 | |
199 UpdateGalleryCheckbox(checkbox, gallery, permitted); | |
200 UpdateCheckboxContainerFrame(); | |
201 [alert_ layout]; | |
202 } | |
203 | |
204 void MediaGalleriesDialogCocoa::DeleteDelegate() { | |
205 // As required by ConstrainedWindowMacDelegate, close the sheet if | |
206 // it's still open. | |
207 if (is_sheet_open()) | |
208 [NSApp endSheet:sheet()]; | |
209 | |
210 controller_->DialogFinished(accepted_); | |
211 } | |
212 | |
213 // static | |
214 MediaGalleriesDialog* MediaGalleriesDialog::Create( | |
215 MediaGalleriesDialogController* controller) { | |
216 scoped_nsobject<MediaGalleriesCocoaBridge> cocoa_bridge( | |
217 [[MediaGalleriesCocoaBridge alloc] init]); | |
218 scoped_ptr<MediaGalleriesDialogCocoa> dialog(new MediaGalleriesDialogCocoa( | |
219 controller, cocoa_bridge)); | |
220 [cocoa_bridge setDialog:dialog.get()]; | |
Nico
2012/08/13 19:40:24
Could the constructor do this?
sail
2012/08/13 20:40:33
Done.
| |
221 return dialog.release(); | |
222 } | |
223 | |
224 } // namespace chrome | |
OLD | NEW |