summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/constrained_window_mac.h
blob: 83331c7e4bd8e3c73913ce5b6e2d1164fa2a07e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_COCOA_CONSTRAINED_WINDOW_MAC_H_
#define CHROME_BROWSER_COCOA_CONSTRAINED_WINDOW_MAC_H_

#import <Cocoa/Cocoa.h>

#include "chrome/browser/tab_contents/constrained_window.h"

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/scoped_nsobject.h"

@class BrowserWindowController;
@class GTMWindowSheetController;
@class NSView;
@class NSWindow;
class TabContents;

// Base class for constrained dialog delegates. Never inherit from this
// directly.
class ConstrainedWindowMacDelegate {
 public:
  ConstrainedWindowMacDelegate() : is_sheet_open_(false) { }
  virtual ~ConstrainedWindowMacDelegate();

  // Tells the delegate to either delete itself or set up a task to delete
  // itself later. Note that you MUST close the sheet belonging to your delegate
  // in this method.
  virtual void DeleteDelegate() = 0;

  // Called by the tab controller, you do not need to do anything yourself
  // with this method.
  virtual void RunSheet(GTMWindowSheetController* sheetController,
                        NSView* view) = 0;
 protected:
  // Returns true if this delegate's sheet is currently showing.
  bool is_sheet_open() { return is_sheet_open_; }

 private:
  bool is_sheet_open_;
  void set_sheet_open(bool is_open) { is_sheet_open_ = is_open; }
  friend class ConstrainedWindowMac;
};

// Subclass this for a dialog delegate that displays a system sheet such as
// an NSAlert, an open or save file panel, etc.
class ConstrainedWindowMacDelegateSystemSheet
    : public ConstrainedWindowMacDelegate {
 public:
  ConstrainedWindowMacDelegateSystemSheet(id delegate, SEL didEndSelector)
    : systemSheet_(nil),
      delegate_([delegate retain]),
      didEndSelector_(didEndSelector) { }

 protected:
  void set_sheet(id sheet) { systemSheet_.reset([sheet retain]); }
  id sheet() { return systemSheet_; }

 private:
  virtual void RunSheet(GTMWindowSheetController* sheetController,
                        NSView* view);
  scoped_nsobject<id> systemSheet_;
  scoped_nsobject<id> delegate_;
  SEL didEndSelector_;
};

// Subclass this for a dialog delegate that displays a custom sheet, e.g. loaded
// from a nib file.
class ConstrainedWindowMacDelegateCustomSheet
    : public ConstrainedWindowMacDelegate {
 public:
  ConstrainedWindowMacDelegateCustomSheet()
    : customSheet_(nil),
      delegate_(nil),
      didEndSelector_(NULL) { }

  ConstrainedWindowMacDelegateCustomSheet(id delegate, SEL didEndSelector)
    : customSheet_(nil),
      delegate_([delegate retain]),
      didEndSelector_(didEndSelector) { }

 protected:
  // For when you need to delay initalization after the constructor call.
  void init(NSWindow* sheet, id delegate, SEL didEndSelector) {
    DCHECK(!delegate_.get());
    DCHECK(!didEndSelector_);
    customSheet_.reset([sheet retain]);
    delegate_.reset([delegate retain]);
    didEndSelector_ = didEndSelector;
    DCHECK(delegate_.get());
    DCHECK(didEndSelector_);
  }
  void set_sheet(NSWindow* sheet) { customSheet_.reset([sheet retain]); }
  NSWindow* sheet() { return customSheet_; }

 private:
  virtual void RunSheet(GTMWindowSheetController* sheetController,
                        NSView* view);
  scoped_nsobject<NSWindow> customSheet_;
  scoped_nsobject<id> delegate_;
  SEL didEndSelector_;
};

// Constrained window implementation for the Mac port. A constrained window
// is a per-tab sheet on OS X.
//
// Constrained windows work slightly differently on OS X than on the other
// platforms:
// 1. A constrained window is bound to both a tab and window on OS X.
// 2. The delegate is responsible for closing the sheet again when it is
//    deleted.
class ConstrainedWindowMac : public ConstrainedWindow {
 public:
  virtual ~ConstrainedWindowMac();

  // Overridden from ConstrainedWindow:
  virtual void CloseConstrainedWindow();

  // Returns the TabContents that constrains this Constrained Window.
  TabContents* owner() const { return owner_; }

  // Returns the window's delegate.
  ConstrainedWindowMacDelegate* delegate() { return delegate_; }

  // Tells |controller_| that the sheet would like to be displayed.
  void Realize(BrowserWindowController* controller);

  // Called by |controller_| to inform the sheet that it now is visible.
  void SetVisible();

 private:
  friend class ConstrainedWindow;

  ConstrainedWindowMac(TabContents* owner,
                       ConstrainedWindowMacDelegate* delegate);

  // The TabContents that owns and constrains this ConstrainedWindow.
  TabContents* owner_;

  // Delegate that provides the contents of this constrained window.
  ConstrainedWindowMacDelegate* delegate_;

  // Controller of the window that contains this sheet.
  BrowserWindowController* controller_;

  DISALLOW_COPY_AND_ASSIGN(ConstrainedWindowMac);
};

#endif  // CHROME_BROWSER_COCOA_CONSTRAINED_WINDOW_MAC_H_