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