constrained_window_mac.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
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 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_UI_COCOA_CONSTRAINED_WINDOW_MAC_H_ 6#define CHROME_BROWSER_UI_COCOA_CONSTRAINED_WINDOW_MAC_H_ 7#pragma once 8 9#import <Cocoa/Cocoa.h> 10 11#include "chrome/browser/tab_contents/constrained_window.h" 12 13#include "base/basictypes.h" 14#include "base/logging.h" 15#include "base/scoped_nsobject.h" 16 17@class BrowserWindowController; 18@class GTMWindowSheetController; 19@class NSView; 20@class NSWindow; 21class TabContents; 22 23// Base class for constrained dialog delegates. Never inherit from this 24// directly. 25class ConstrainedWindowMacDelegate { 26 public: 27 ConstrainedWindowMacDelegate() : is_sheet_open_(false) { } 28 virtual ~ConstrainedWindowMacDelegate(); 29 30 // Tells the delegate to either delete itself or set up a task to delete 31 // itself later. Note that you MUST close the sheet belonging to your delegate 32 // in this method. 33 virtual void DeleteDelegate() = 0; 34 35 // Called by the tab controller, you do not need to do anything yourself 36 // with this method. 37 virtual void RunSheet(GTMWindowSheetController* sheetController, 38 NSView* view) = 0; 39 protected: 40 // Returns true if this delegate's sheet is currently showing. 41 bool is_sheet_open() { return is_sheet_open_; } 42 43 private: 44 bool is_sheet_open_; 45 void set_sheet_open(bool is_open) { is_sheet_open_ = is_open; } 46 friend class ConstrainedWindowMac; 47}; 48 49// Subclass this for a dialog delegate that displays a system sheet such as 50// an NSAlert, an open or save file panel, etc. 51class ConstrainedWindowMacDelegateSystemSheet 52 : public ConstrainedWindowMacDelegate { 53 public: 54 ConstrainedWindowMacDelegateSystemSheet(id delegate, SEL didEndSelector) 55 : systemSheet_(nil), 56 delegate_([delegate retain]), 57 didEndSelector_(didEndSelector) { } 58 59 protected: 60 void set_sheet(id sheet) { systemSheet_.reset([sheet retain]); } 61 id sheet() { return systemSheet_; } 62 63 // Returns an NSArray to be passed as parameters to GTMWindowSheetController. 64 // Array's contents should be the arguments passed to the system sheet's 65 // beginSheetForWindow:... method. The window argument must be [NSNull null]. 66 // 67 // The default implementation returns 68 // [null window, delegate, didEndSelector, null contextInfo] 69 // Subclasses may override this if they show a system sheet which takes 70 // different parameters. 71 virtual NSArray* GetSheetParameters(id delegate, SEL didEndSelector); 72 73 private: 74 virtual void RunSheet(GTMWindowSheetController* sheetController, 75 NSView* view); 76 scoped_nsobject<id> systemSheet_; 77 scoped_nsobject<id> delegate_; 78 SEL didEndSelector_; 79}; 80 81// Subclass this for a dialog delegate that displays a custom sheet, e.g. loaded 82// from a nib file. 83class ConstrainedWindowMacDelegateCustomSheet 84 : public ConstrainedWindowMacDelegate { 85 public: 86 ConstrainedWindowMacDelegateCustomSheet() 87 : customSheet_(nil), 88 delegate_(nil), 89 didEndSelector_(NULL) { } 90 91 ConstrainedWindowMacDelegateCustomSheet(id delegate, SEL didEndSelector) 92 : customSheet_(nil), 93 delegate_([delegate retain]), 94 didEndSelector_(didEndSelector) { } 95 96 protected: 97 // For when you need to delay initalization after the constructor call. 98 void init(NSWindow* sheet, id delegate, SEL didEndSelector) { 99 DCHECK(!delegate_.get()); 100 DCHECK(!didEndSelector_); 101 customSheet_.reset([sheet retain]); 102 delegate_.reset([delegate retain]); 103 didEndSelector_ = didEndSelector; 104 DCHECK(delegate_.get()); 105 DCHECK(didEndSelector_); 106 } 107 void set_sheet(NSWindow* sheet) { customSheet_.reset([sheet retain]); } 108 NSWindow* sheet() { return customSheet_; } 109 110 private: 111 virtual void RunSheet(GTMWindowSheetController* sheetController, 112 NSView* view); 113 scoped_nsobject<NSWindow> customSheet_; 114 scoped_nsobject<id> delegate_; 115 SEL didEndSelector_; 116}; 117 118// Constrained window implementation for the Mac port. A constrained window 119// is a per-tab sheet on OS X. 120// 121// Constrained windows work slightly differently on OS X than on the other 122// platforms: 123// 1. A constrained window is bound to both a tab and window on OS X. 124// 2. The delegate is responsible for closing the sheet again when it is 125// deleted. 126class ConstrainedWindowMac : public ConstrainedWindow { 127 public: 128 virtual ~ConstrainedWindowMac(); 129 130 // Overridden from ConstrainedWindow: 131 virtual void ShowConstrainedWindow(); 132 virtual void CloseConstrainedWindow(); 133 134 // Returns the TabContents that constrains this Constrained Window. 135 TabContents* owner() const { return owner_; } 136 137 // Returns the window's delegate. 138 ConstrainedWindowMacDelegate* delegate() { return delegate_; } 139 140 // Makes the constrained window visible, if it is not yet visible. 141 void Realize(BrowserWindowController* controller); 142 143 private: 144 friend class ConstrainedWindow; 145 146 ConstrainedWindowMac(TabContents* owner, 147 ConstrainedWindowMacDelegate* delegate); 148 149 // The TabContents that owns and constrains this ConstrainedWindow. 150 TabContents* owner_; 151 152 // Delegate that provides the contents of this constrained window. 153 ConstrainedWindowMacDelegate* delegate_; 154 155 // Controller of the window that contains this sheet. 156 BrowserWindowController* controller_; 157 158 // Stores if |ShowConstrainedWindow()| was called. 159 bool should_be_visible_; 160 161 DISALLOW_COPY_AND_ASSIGN(ConstrainedWindowMac); 162}; 163 164#endif // CHROME_BROWSER_UI_COCOA_CONSTRAINED_WINDOW_MAC_H_ 165 166