summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa
diff options
context:
space:
mode:
authoravi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-14 20:08:00 +0000
committeravi@google.com <avi@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-14 20:08:00 +0000
commit7bea1c588a1951e65f525cdab0e99c0839be691c (patch)
tree872e6a34522220a976c15b45759e665137deb5d1 /chrome/browser/cocoa
parent106c901495fac4d0a09656144e37c6c5abe7a6e0 (diff)
downloadchromium_src-7bea1c588a1951e65f525cdab0e99c0839be691c.zip
chromium_src-7bea1c588a1951e65f525cdab0e99c0839be691c.tar.gz
chromium_src-7bea1c588a1951e65f525cdab0e99c0839be691c.tar.bz2
Add a Cocoa file picker.
Review URL: http://codereview.chromium.org/73044 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13695 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/cocoa')
-rw-r--r--chrome/browser/cocoa/bookmark_bar_state_controller.mm2
-rw-r--r--chrome/browser/cocoa/browser_window_controller.mm13
-rw-r--r--chrome/browser/cocoa/shell_dialogs_mac.mm333
-rw-r--r--chrome/browser/cocoa/tab_strip_controller.mm11
4 files changed, 349 insertions, 10 deletions
diff --git a/chrome/browser/cocoa/bookmark_bar_state_controller.mm b/chrome/browser/cocoa/bookmark_bar_state_controller.mm
index 69d7aa2..e4f9d11a 100644
--- a/chrome/browser/cocoa/bookmark_bar_state_controller.mm
+++ b/chrome/browser/cocoa/bookmark_bar_state_controller.mm
@@ -3,7 +3,9 @@
// found in the LICENSE file.
#import "chrome/browser/cocoa/bookmark_bar_state_controller.h"
+
#include "chrome/browser/bookmarks/bookmark_utils.h"
+#include "chrome/browser/browser.h"
#include "chrome/browser/profile.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm
index 47b3ef9..7a1cbc4 100644
--- a/chrome/browser/cocoa/browser_window_controller.mm
+++ b/chrome/browser/cocoa/browser_window_controller.mm
@@ -36,6 +36,11 @@ const int kWindowGradientHeight = 24;
// heuristic.
- (void)fixWindowGradient;
+// We need to adjust where sheets come out of the window, as by default they
+// erupt from the omnibox, which is rather weird.
+- (NSRect)window:(NSWindow *)window
+willPositionSheet:(NSWindow *)sheet
+ usingRect:(NSRect)defaultSheetRect;
@end
@@ -413,4 +418,12 @@ const int kWindowGradientHeight = 24;
}
}
+- (NSRect)window:(NSWindow *)window
+willPositionSheet:(NSWindow *)sheet
+ usingRect:(NSRect)defaultSheetRect {
+ NSRect windowFrame = [window frame];
+ defaultSheetRect.origin.y = windowFrame.size.height - 10;
+ return defaultSheetRect;
+}
+
@end
diff --git a/chrome/browser/cocoa/shell_dialogs_mac.mm b/chrome/browser/cocoa/shell_dialogs_mac.mm
index f14beac..2cb35aa 100644
--- a/chrome/browser/cocoa/shell_dialogs_mac.mm
+++ b/chrome/browser/cocoa/shell_dialogs_mac.mm
@@ -4,14 +4,337 @@
#include "chrome/browser/shell_dialogs.h"
+#include <CoreServices/CoreServices.h>
+#import <Cocoa/Cocoa.h>
+#include <map>
+#include <set>
+
#include "base/logging.h"
+#include "base/scoped_cftyperef.h"
+#include "base/scoped_nsobject.h"
+#include "base/sys_string_conversions.h"
+
+static const int kFileTypePopupTag = 1234;
+
+class SelectFileDialogImpl;
+
+// A bridge class to act as the modal delegate to the save/open sheet and send
+// the results to the C++ class.
+@interface SelectFileDialogBridge : NSObject {
+ @private
+ SelectFileDialogImpl* selectFileDialogImpl_; // WEAK; owns us
+}
+
+- (id)initWithSelectFileDialogImpl:(SelectFileDialogImpl*)s;
+- (void)endedPanel:(NSSavePanel *)panel
+ withReturn:(int)returnCode
+ context:(void *)context;
+
+@end
+
+// Implementation of SelectFileDialog that shows Cocoa dialogs for choosing a
+// file or folder.
+class SelectFileDialogImpl : public SelectFileDialog {
+ public:
+ explicit SelectFileDialogImpl(Listener* listener);
+ virtual ~SelectFileDialogImpl();
+
+ // BaseShellDialog implementation.
+ virtual bool IsRunning(gfx::NativeWindow parent_window) const;
+ virtual void ListenerDestroyed();
+
+ // SelectFileDialog implementation.
+ // |params| is user data we pass back via the Listener interface.
+ virtual void SelectFile(Type type,
+ const string16& title,
+ const FilePath& default_path,
+ const FileTypeInfo* file_types,
+ int file_type_index,
+ const FilePath::StringType& default_extension,
+ gfx::NativeWindow owning_window,
+ void* params);
+
+ // Callback from ObjC bridge.
+ void FileWasSelected(NSPanel* dialog,
+ NSWindow* parent_window,
+ bool was_cancelled,
+ bool is_multi,
+ const std::vector<FilePath>& files,
+ int index);
+
+ struct SheetContext {
+ Type type;
+ NSWindow* owning_window;
+ };
+
+ private:
+ // Gets the accessory view for the save dialog.
+ NSView* GetAccessoryView(const FileTypeInfo* file_types,
+ int file_type_index);
+
+ // The listener to be notified of selection completion.
+ Listener* listener_;
+
+ // The bridge for results from Cocoa to return to us.
+ scoped_nsobject<SelectFileDialogBridge> bridge_;
+
+ // A map from file dialogs to the |params| user data associated with them.
+ std::map<NSPanel*, void*> params_map_;
+ // The set of all parent windows for which we are currently running dialogs.
+ std::set<NSWindow*> parents_;
+
+ DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl);
+};
+
+// static
SelectFileDialog* SelectFileDialog::Create(Listener* listener) {
- NOTREACHED() << "Implement me.";
- return NULL;
+ return new SelectFileDialogImpl(listener);
+}
+
+SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener)
+ : listener_(listener),
+ bridge_([[SelectFileDialogBridge alloc]
+ initWithSelectFileDialogImpl:this]) {
+}
+
+SelectFileDialogImpl::~SelectFileDialogImpl() {
+}
+
+bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow parent_window) const {
+ return parents_.find(parent_window) != parents_.end();
+}
+
+void SelectFileDialogImpl::ListenerDestroyed() {
+ listener_ = NULL;
+}
+
+void SelectFileDialogImpl::SelectFile(
+ Type type,
+ const string16& title,
+ const FilePath& default_path,
+ const FileTypeInfo* file_types,
+ int file_type_index,
+ const FilePath::StringType& default_extension,
+ gfx::NativeWindow owning_window,
+ void* params) {
+ DCHECK(type == SELECT_FOLDER ||
+ type == SELECT_OPEN_FILE ||
+ type == SELECT_OPEN_MULTI_FILE ||
+ type == SELECT_SAVEAS_FILE);
+ DCHECK(owning_window);
+ parents_.insert(owning_window);
+
+ NSSavePanel* dialog;
+ if (type == SELECT_SAVEAS_FILE)
+ dialog = [NSSavePanel savePanel];
+ else
+ dialog = [NSOpenPanel openPanel];
+
+ if (!title.empty())
+ [dialog setTitle:base::SysUTF16ToNSString(title)];
+
+ NSString* default_dir = nil;
+ NSString* default_filename = nil;
+ if (!default_path.empty()) {
+ default_dir = base::SysUTF8ToNSString(default_path.DirName().value());
+ default_filename = base::SysUTF8ToNSString(default_path.BaseName().value());
+ }
+
+ NSMutableArray* allowed_file_types = nil;
+ if (file_types) {
+ if (!file_types->extensions.empty()) {
+ allowed_file_types = [NSMutableArray array];
+ for (size_t i=0; i < file_types->extensions.size(); ++i) {
+ const std::vector<FilePath::StringType>& ext_list =
+ file_types->extensions[i];
+ for (size_t j=0; j < ext_list.size(); ++j) {
+ [allowed_file_types addObject:base::SysUTF8ToNSString(ext_list[j])];
+ }
+ }
+ }
+ if (type == SELECT_SAVEAS_FILE)
+ [dialog setAllowedFileTypes:allowed_file_types];
+ // else we'll pass it in when we run the open panel
+
+ if (file_types->include_all_files)
+ [dialog setAllowsOtherFileTypes:YES];
+
+ if (!file_types->extension_description_overrides.empty()) {
+ NSView* accessory_view = GetAccessoryView(file_types, file_type_index);
+ [dialog setAccessoryView:accessory_view];
+ }
+ } else {
+ // If no type info is specified, anything goes.
+ [dialog setAllowsOtherFileTypes:YES];
+ }
+
+ if (!default_extension.empty())
+ [dialog setRequiredFileType:base::SysUTF8ToNSString(default_extension)];
+
+ params_map_[dialog] = params;
+
+ SheetContext* context = new SheetContext;
+ context->type = type;
+ context->owning_window = owning_window;
+
+ if (type == SELECT_SAVEAS_FILE) {
+ [dialog beginSheetForDirectory:default_dir
+ file:default_filename
+ modalForWindow:owning_window
+ modalDelegate:bridge_.get()
+ didEndSelector:@selector(endedPanel:withReturn:context:)
+ contextInfo:context];
+ } else {
+ NSOpenPanel* open_dialog = (NSOpenPanel*)dialog;
+
+ if (type == SELECT_OPEN_MULTI_FILE)
+ [open_dialog setAllowsMultipleSelection:YES];
+ else
+ [open_dialog setAllowsMultipleSelection:NO];
+
+ if (type == SELECT_FOLDER) {
+ [open_dialog setCanChooseFiles:NO];
+ [open_dialog setCanChooseDirectories:YES];
+ } else {
+ [open_dialog setCanChooseFiles:YES];
+ [open_dialog setCanChooseDirectories:NO];
+ }
+
+ [open_dialog beginSheetForDirectory:default_dir
+ file:default_filename
+ types:allowed_file_types
+ modalForWindow:owning_window
+ modalDelegate:bridge_.get()
+ didEndSelector:@selector(endedPanel:withReturn:context:)
+ contextInfo:context];
+ }
}
-SelectFontDialog* SelectFontDialog::Create(Listener* listener) {
- NOTREACHED() << "Implement me.";
- return NULL;
+void SelectFileDialogImpl::FileWasSelected(NSPanel* dialog,
+ NSWindow* parent_window,
+ bool was_cancelled,
+ bool is_multi,
+ const std::vector<FilePath>& files,
+ int index) {
+ void* params = params_map_[dialog];
+ params_map_.erase(dialog);
+ parents_.erase(parent_window);
+
+ if (!listener_)
+ return;
+
+ if (was_cancelled) {
+ listener_->FileSelectionCanceled(params);
+ } else {
+ if (is_multi) {
+ listener_->MultiFilesSelected(files, params);
+ } else {
+ listener_->FileSelected(files[0], index, params);
+ }
+ }
}
+
+NSView* SelectFileDialogImpl::GetAccessoryView(const FileTypeInfo* file_types,
+ int file_type_index) {
+ DCHECK(file_types);
+ scoped_nsobject<NSNib> nib (
+ [[NSNib alloc] initWithNibNamed:@"SaveAccessoryView" bundle:nil]);
+ if (!nib)
+ return nil;
+
+ NSArray* objects;
+ BOOL success = [nib instantiateNibWithOwner:nil
+ topLevelObjects:&objects];
+ if (!success)
+ return nil;
+
+ // This is a one-object nib, but IB insists on creating a second object, the
+ // NSApplication. I don't know why.
+ size_t view_index = 0;
+ while (view_index < [objects count] &&
+ ![[objects objectAtIndex:view_index] isKindOfClass:[NSView class]])
+ ++view_index;
+ DCHECK(view_index < [objects count]);
+ NSView* accessory_view = [objects objectAtIndex:view_index];
+
+ NSPopUpButton* popup = [accessory_view viewWithTag:kFileTypePopupTag];
+ DCHECK(popup);
+
+ size_t type_count = file_types->extensions.size();
+ for (size_t type = 0; type<type_count; ++type) {
+ NSString* type_description;
+ if (type < file_types->extension_description_overrides.size()) {
+ type_description = base::SysUTF16ToNSString(
+ file_types->extension_description_overrides[type]);
+ } else {
+ const std::vector<FilePath::StringType>& ext_list =
+ file_types->extensions[type];
+ DCHECK(!ext_list.empty());
+ NSString* type_extension = base::SysUTF8ToNSString(ext_list[0]);
+ scoped_cftyperef<CFStringRef> uti(
+ UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
+ (CFStringRef)type_extension,
+ NULL));
+ scoped_cftyperef<CFStringRef> description(
+ UTTypeCopyDescription(uti.get()));
+
+ type_description =
+ [NSString stringWithString:(NSString*)description.get()];
+ }
+ [popup addItemWithTitle:type_description];
+ }
+
+ [popup selectItemAtIndex:file_type_index-1]; // 1-based
+ return accessory_view;
+}
+
+@implementation SelectFileDialogBridge
+
+- (id)initWithSelectFileDialogImpl:(SelectFileDialogImpl*)s {
+ self = [super init];
+ if (self != nil) {
+ selectFileDialogImpl_ = s;
+ }
+ return self;
+}
+
+- (void)endedPanel:(id)panel
+ withReturn:(int)returnCode
+ context:(void *)context {
+ int index = 0;
+ SelectFileDialogImpl::SheetContext* context_struct =
+ (SelectFileDialogImpl::SheetContext*)context;
+
+ SelectFileDialog::Type type = context_struct->type;
+ NSWindow* parentWindow = context_struct->owning_window;
+ delete context_struct;
+
+ bool isMulti = type == SelectFileDialog::SELECT_OPEN_MULTI_FILE;
+
+ std::vector<FilePath> paths;
+ if (type == SelectFileDialog::SELECT_SAVEAS_FILE) {
+ paths.push_back(FilePath(base::SysNSStringToUTF8([panel filename])));
+
+ NSView* accessoryView = [panel accessoryView];
+ if (accessoryView) {
+ NSPopUpButton* popup = [accessoryView viewWithTag:kFileTypePopupTag];
+ if (popup) {
+ index = [popup indexOfSelectedItem]+1; // file type indexes are 1-based
+ }
+ }
+ } else {
+ NSArray* filenames = [panel filenames];
+ for (NSString* filename in filenames)
+ paths.push_back(FilePath(base::SysNSStringToUTF8(filename)));
+ }
+
+ selectFileDialogImpl_->FileWasSelected(panel,
+ parentWindow,
+ returnCode==NSCancelButton,
+ isMulti,
+ paths,
+ index);
+}
+
+@end
diff --git a/chrome/browser/cocoa/tab_strip_controller.mm b/chrome/browser/cocoa/tab_strip_controller.mm
index 920438c..8528ef1 100644
--- a/chrome/browser/cocoa/tab_strip_controller.mm
+++ b/chrome/browser/cocoa/tab_strip_controller.mm
@@ -4,17 +4,18 @@
#import "chrome/browser/cocoa/tab_strip_controller.h"
-#import "base/sys_string_conversions.h"
-#import "chrome/app/chrome_dll_resource.h"
-#import "chrome/browser/browser.h"
+#include "base/sys_string_conversions.h"
+#include "chrome/app/chrome_dll_resource.h"
+#include "chrome/browser/browser.h"
+#include "chrome/browser/profile.h"
#import "chrome/browser/cocoa/tab_strip_view.h"
#import "chrome/browser/cocoa/tab_cell.h"
#import "chrome/browser/cocoa/tab_contents_controller.h"
#import "chrome/browser/cocoa/tab_controller.h"
#import "chrome/browser/cocoa/tab_strip_model_observer_bridge.h"
#import "chrome/browser/cocoa/tab_view.h"
-#import "chrome/browser/tab_contents/tab_contents.h"
-#import "chrome/browser/tabs/tab_strip_model.h"
+#include "chrome/browser/tab_contents/tab_contents.h"
+#include "chrome/browser/tabs/tab_strip_model.h"
@implementation TabStripController