constrained_window_mac.h revision dc0f95d653279beabeb9817299e2902918ba123e
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 "content/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  virtual ~ConstrainedWindowMacDelegateSystemSheet();
56
57 protected:
58  void set_sheet(id sheet);
59  id sheet() { return systemSheet_; }
60
61  // Returns an NSArray to be passed as parameters to GTMWindowSheetController.
62  // Array's contents should be the arguments passed to the system sheet's
63  // beginSheetForWindow:... method. The window argument must be [NSNull null].
64  //
65  // The default implementation returns
66  //     [null window, delegate, didEndSelector, null contextInfo]
67  // Subclasses may override this if they show a system sheet which takes
68  // different parameters.
69  virtual NSArray* GetSheetParameters(id delegate, SEL didEndSelector);
70
71 private:
72  virtual void RunSheet(GTMWindowSheetController* sheetController,
73                        NSView* view);
74  scoped_nsobject<id> systemSheet_;
75  scoped_nsobject<id> delegate_;
76  SEL didEndSelector_;
77};
78
79// Subclass this for a dialog delegate that displays a custom sheet, e.g. loaded
80// from a nib file.
81class ConstrainedWindowMacDelegateCustomSheet
82    : public ConstrainedWindowMacDelegate {
83 public:
84  ConstrainedWindowMacDelegateCustomSheet();
85  ConstrainedWindowMacDelegateCustomSheet(id delegate, SEL didEndSelector);
86  ~ConstrainedWindowMacDelegateCustomSheet();
87
88 protected:
89  // For when you need to delay initalization after the constructor call.
90  void init(NSWindow* sheet, id delegate, SEL didEndSelector);
91  void set_sheet(NSWindow* sheet);
92  NSWindow* sheet() { return customSheet_; }
93
94 private:
95  virtual void RunSheet(GTMWindowSheetController* sheetController,
96                        NSView* view);
97  scoped_nsobject<NSWindow> customSheet_;
98  scoped_nsobject<id> delegate_;
99  SEL didEndSelector_;
100};
101
102// Constrained window implementation for the Mac port. A constrained window
103// is a per-tab sheet on OS X.
104//
105// Constrained windows work slightly differently on OS X than on the other
106// platforms:
107// 1. A constrained window is bound to both a tab and window on OS X.
108// 2. The delegate is responsible for closing the sheet again when it is
109//    deleted.
110class ConstrainedWindowMac : public ConstrainedWindow {
111 public:
112  virtual ~ConstrainedWindowMac();
113
114  // Overridden from ConstrainedWindow:
115  virtual void ShowConstrainedWindow();
116  virtual void CloseConstrainedWindow();
117
118  // Returns the TabContents that constrains this Constrained Window.
119  TabContents* owner() const { return owner_; }
120
121  // Returns the window's delegate.
122  ConstrainedWindowMacDelegate* delegate() { return delegate_; }
123
124  // Makes the constrained window visible, if it is not yet visible.
125  void Realize(BrowserWindowController* controller);
126
127 private:
128  friend class ConstrainedWindow;
129
130  ConstrainedWindowMac(TabContents* owner,
131                       ConstrainedWindowMacDelegate* delegate);
132
133  // The TabContents that owns and constrains this ConstrainedWindow.
134  TabContents* owner_;
135
136  // Delegate that provides the contents of this constrained window.
137  ConstrainedWindowMacDelegate* delegate_;
138
139  // Controller of the window that contains this sheet.
140  BrowserWindowController* controller_;
141
142  // Stores if |ShowConstrainedWindow()| was called.
143  bool should_be_visible_;
144
145  DISALLOW_COPY_AND_ASSIGN(ConstrainedWindowMac);
146};
147
148#endif  // CHROME_BROWSER_UI_COCOA_CONSTRAINED_WINDOW_MAC_H_
149
150