Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #import "base/mac_util.h" | 7 #import "base/mac_util.h" |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
| 10 #import "chrome/browser/cocoa/edit_search_engine_cocoa_controller.h" | 10 #import "chrome/browser/cocoa/edit_search_engine_cocoa_controller.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 const std::wstring& url) { | 50 const std::wstring& url) { |
| 51 KeywordEditorController* controller = [controller_ controller]; | 51 KeywordEditorController* controller = [controller_ controller]; |
| 52 if (template_url) { | 52 if (template_url) { |
| 53 controller->ModifyTemplateURL(template_url, title, keyword, url); | 53 controller->ModifyTemplateURL(template_url, title, keyword, url); |
| 54 } else { | 54 } else { |
| 55 controller->AddTemplateURL(title, keyword, url); | 55 controller->AddTemplateURL(title, keyword, url); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 | 58 |
| 59 void KeywordEditorModelObserver::OnModelChanged() { | 59 void KeywordEditorModelObserver::OnModelChanged() { |
| 60 InvalidateIconCache(0, [iconImages_ count]); | |
| 61 int count = [controller_ controller]->table_model()->RowCount(); | 60 int count = [controller_ controller]->table_model()->RowCount(); |
| 61 [iconImages_ setCount:0]; | |
| 62 [iconImages_ setCount:count]; | 62 [iconImages_ setCount:count]; |
| 63 [controller_ modelChanged]; | 63 [controller_ modelChanged]; |
| 64 } | 64 } |
| 65 | 65 |
| 66 void KeywordEditorModelObserver::OnItemsChanged(int start, int length) { | 66 void KeywordEditorModelObserver::OnItemsChanged(int start, int length) { |
| 67 InvalidateIconCache(start, length); | 67 DCHECK_LE(start + length, static_cast<int>([iconImages_ count])); |
|
pink (ping after 24hrs)
2009/10/23 14:11:00
Seems odd to move this out of a separate function
Scott Hess - ex-Googler
2009/10/23 14:24:28
InvalidateIconCache() was a private function only
| |
| 68 for (int i = start; i < (start + length); ++i) { | |
| 69 [iconImages_ replacePointerAtIndex:i withPointer:NULL]; | |
| 70 } | |
| 71 DCHECK_EQ([controller_ controller]->table_model()->RowCount(), | |
| 72 static_cast<int>([iconImages_ count])); | |
| 68 [controller_ modelChanged]; | 73 [controller_ modelChanged]; |
| 69 } | 74 } |
| 70 | 75 |
| 71 void KeywordEditorModelObserver::OnItemsAdded(int start, int length) { | 76 void KeywordEditorModelObserver::OnItemsAdded(int start, int length) { |
| 72 DCHECK_LE(start, static_cast<int>([iconImages_ count])); | 77 DCHECK_LE(start, static_cast<int>([iconImages_ count])); |
| 73 for (int i = 0; i < length; ++i) { | 78 |
| 74 [iconImages_ insertPointer:NULL atIndex:start]; // Values slide down. | 79 // -[NSPointerArray insertPointer:atIndex:] throws if index == count. |
| 80 // Instead expand the array with NULLs. | |
| 81 if (start == static_cast<int>([iconImages_ count])) { | |
|
pink (ping after 24hrs)
2009/10/23 14:11:00
if we're concerned about throwing, why not wrap in
Scott Hess - ex-Googler
2009/10/23 14:24:28
Catching the exception would still leave the array
| |
| 82 [iconImages_ setCount:start + length]; | |
| 83 } else { | |
| 84 for (int i = 0; i < length; ++i) { | |
| 85 [iconImages_ insertPointer:NULL atIndex:start]; // Values slide up. | |
| 86 } | |
| 75 } | 87 } |
| 88 DCHECK_EQ([controller_ controller]->table_model()->RowCount(), | |
| 89 static_cast<int>([iconImages_ count])); | |
| 76 [controller_ modelChanged]; | 90 [controller_ modelChanged]; |
| 77 } | 91 } |
| 78 | 92 |
| 79 void KeywordEditorModelObserver::OnItemsRemoved(int start, int length) { | 93 void KeywordEditorModelObserver::OnItemsRemoved(int start, int length) { |
| 80 DCHECK_LE(start + length, static_cast<int>([iconImages_ count])); | 94 DCHECK_LE(start + length, static_cast<int>([iconImages_ count])); |
| 81 for (int i = 0; i < length; ++i) { | 95 for (int i = 0; i < length; ++i) { |
| 82 [iconImages_ removePointerAtIndex:start]; // Values slide up. | 96 [iconImages_ removePointerAtIndex:start]; // Values slide down. |
| 83 } | 97 } |
| 98 DCHECK_EQ([controller_ controller]->table_model()->RowCount(), | |
| 99 static_cast<int>([iconImages_ count])); | |
| 84 [controller_ modelChanged]; | 100 [controller_ modelChanged]; |
| 85 } | 101 } |
| 86 | 102 |
| 87 NSImage* KeywordEditorModelObserver::GetImageForRow(int row) { | 103 NSImage* KeywordEditorModelObserver::GetImageForRow(int row) { |
| 88 DCHECK(row >= 0 && row < static_cast<int>([iconImages_ count])); | 104 DCHECK_EQ([controller_ controller]->table_model()->RowCount(), |
| 105 static_cast<int>([iconImages_ count])); | |
| 106 DCHECK_GE(row, 0); | |
| 107 DCHECK_LT(row, static_cast<int>([iconImages_ count])); | |
| 89 NSImage* image = static_cast<NSImage*>([iconImages_ pointerAtIndex:row]); | 108 NSImage* image = static_cast<NSImage*>([iconImages_ pointerAtIndex:row]); |
| 90 if (!image) { | 109 if (!image) { |
| 91 const SkBitmap bitmapIcon = | 110 const SkBitmap bitmapIcon = |
| 92 [controller_ controller]->table_model()->GetIcon(row); | 111 [controller_ controller]->table_model()->GetIcon(row); |
| 93 if (!bitmapIcon.isNull()) { | 112 if (!bitmapIcon.isNull()) { |
| 94 image = gfx::SkBitmapToNSImage(bitmapIcon); | 113 image = gfx::SkBitmapToNSImage(bitmapIcon); |
| 95 DCHECK(image); | 114 DCHECK(image); |
| 96 [iconImages_ replacePointerAtIndex:row withPointer:image]; | 115 [iconImages_ replacePointerAtIndex:row withPointer:image]; |
| 97 } | 116 } |
| 98 } | 117 } |
| 99 return image; | 118 return image; |
| 100 } | 119 } |
| 101 | 120 |
| 102 void KeywordEditorModelObserver::InvalidateIconCache(int start, int length) { | |
| 103 DCHECK_LE(start + length, static_cast<int>([iconImages_ count])); | |
| 104 for (int i = start; i < (start + length); ++i) { | |
| 105 [iconImages_ replacePointerAtIndex:i withPointer:NULL]; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // KeywordEditorCocoaController ----------------------------------------------- | 121 // KeywordEditorCocoaController ----------------------------------------------- |
| 110 | 122 |
| 111 namespace { | 123 namespace { |
| 112 | 124 |
| 113 typedef std::map<Profile*,KeywordEditorCocoaController*> ProfileControllerMap; | 125 typedef std::map<Profile*,KeywordEditorCocoaController*> ProfileControllerMap; |
| 114 | 126 |
| 115 } // namespace | 127 } // namespace |
| 116 | 128 |
| 117 @implementation KeywordEditorCocoaController | 129 @implementation KeywordEditorCocoaController |
| 118 | 130 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 observer_.reset(new KeywordEditorModelObserver(self)); | 175 observer_.reset(new KeywordEditorModelObserver(self)); |
| 164 controller_->table_model()->SetObserver(observer_.get()); | 176 controller_->table_model()->SetObserver(observer_.get()); |
| 165 controller_->url_model()->AddObserver(observer_.get()); | 177 controller_->url_model()->AddObserver(observer_.get()); |
| 166 } | 178 } |
| 167 return self; | 179 return self; |
| 168 } | 180 } |
| 169 | 181 |
| 170 - (void)dealloc { | 182 - (void)dealloc { |
| 171 controller_->table_model()->SetObserver(NULL); | 183 controller_->table_model()->SetObserver(NULL); |
| 172 controller_->url_model()->RemoveObserver(observer_.get()); | 184 controller_->url_model()->RemoveObserver(observer_.get()); |
| 185 [tableView_ setTarget:nil]; | |
| 186 observer_.reset(); | |
| 173 [super dealloc]; | 187 [super dealloc]; |
| 174 } | 188 } |
| 175 | 189 |
| 176 - (void)awakeFromNib { | 190 - (void)awakeFromNib { |
| 177 // Make sure the button fits its label, but keep it the same height as the | 191 // Make sure the button fits its label, but keep it the same height as the |
| 178 // other two buttons. | 192 // other two buttons. |
| 179 [GTMUILocalizerAndLayoutTweaker sizeToFitView:makeDefaultButton_]; | 193 [GTMUILocalizerAndLayoutTweaker sizeToFitView:makeDefaultButton_]; |
| 180 NSSize size = [makeDefaultButton_ frame].size; | 194 NSSize size = [makeDefaultButton_ frame].size; |
| 181 size.height = NSHeight([addButton_ frame]); | 195 size.height = NSHeight([addButton_ frame]); |
| 182 [makeDefaultButton_ setFrameSize:size]; | 196 [makeDefaultButton_ setFrameSize:size]; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 - (void)windowDidMove:(NSNotification*)notif { | 228 - (void)windowDidMove:(NSNotification*)notif { |
| 215 if (g_browser_process && g_browser_process->local_state()) { | 229 if (g_browser_process && g_browser_process->local_state()) { |
| 216 NSWindow* window = [self window]; | 230 NSWindow* window = [self window]; |
| 217 [window saveWindowPositionToPrefs:g_browser_process->local_state() | 231 [window saveWindowPositionToPrefs:g_browser_process->local_state() |
| 218 withPath:prefs::kKeywordEditorWindowPlacement]; | 232 withPath:prefs::kKeywordEditorWindowPlacement]; |
| 219 } | 233 } |
| 220 } | 234 } |
| 221 | 235 |
| 222 - (void)modelChanged { | 236 - (void)modelChanged { |
| 223 [tableView_ reloadData]; | 237 [tableView_ reloadData]; |
| 238 [self adjustEditingButtons]; | |
| 224 } | 239 } |
| 225 | 240 |
| 226 - (KeywordEditorController*)controller { | 241 - (KeywordEditorController*)controller { |
| 227 return controller_.get(); | 242 return controller_.get(); |
| 228 } | 243 } |
| 229 | 244 |
| 230 - (void)sheetDidEnd:(NSWindow*)sheet | 245 - (void)sheetDidEnd:(NSWindow*)sheet |
| 231 returnCode:(NSInteger)code | 246 returnCode:(NSInteger)code |
| 232 context:(void*)context { | 247 context:(void*)context { |
| 233 [sheet orderOut:self]; | 248 [sheet orderOut:self]; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 if ([selection count] != 1) { | 381 if ([selection count] != 1) { |
| 367 [makeDefaultButton_ setEnabled:NO]; | 382 [makeDefaultButton_ setEnabled:NO]; |
| 368 } else { | 383 } else { |
| 369 const TemplateURL& url = | 384 const TemplateURL& url = |
| 370 controller_->table_model()->GetTemplateURL([selection firstIndex]); | 385 controller_->table_model()->GetTemplateURL([selection firstIndex]); |
| 371 [makeDefaultButton_ setEnabled:controller_->CanMakeDefault(&url)]; | 386 [makeDefaultButton_ setEnabled:controller_->CanMakeDefault(&url)]; |
| 372 } | 387 } |
| 373 } | 388 } |
| 374 | 389 |
| 375 @end | 390 @end |
| OLD | NEW |