]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/NSWindow.mm
guard against nullptr
[wxWidgets.git] / src / cocoa / NSWindow.mm
CommitLineData
fb896a32 1/////////////////////////////////////////////////////////////////////////////
1c4e8f38 2// Name: src/cocoa/NSWindow.mm
fb896a32
DE
3// Purpose: wxCocoaNSWindow
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
1c4e8f38 7// RCS-ID: $Id$
fb896a32 8// Copyright: (c) 2003 David Elliott
526954c5 9// Licence: wxWindows licence
fb896a32
DE
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#include "wx/wxprec.h"
21#ifndef WX_PRECOMP
22 #include "wx/log.h"
23 #include "wx/menuitem.h"
24#endif // WX_PRECOMP
25
26#include "wx/cocoa/NSWindow.h"
27
e7e1ad7d
DE
28#include "wx/cocoa/objc/objc_uniquifying.h"
29
720e01c3
DE
30#import <Foundation/NSNotification.h>
31#import <Foundation/NSString.h>
ad3628fa 32#include "wx/cocoa/objc/NSWindow.h"
720e01c3
DE
33
34// ============================================================================
78c67cb6 35// @class wxNSWindowDelegate
720e01c3 36// ============================================================================
78c67cb6 37@interface wxNSWindowDelegate : NSObject
720e01c3 38{
c6f73d05 39 wxCocoaNSWindow *m_wxCocoaInterface;
720e01c3
DE
40}
41
c6f73d05
DE
42- (id)init;
43- (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface;
44- (wxCocoaNSWindow *)wxCocoaInterface;
45
46// Delegate message handlers
78c67cb6
DE
47- (void)windowDidBecomeKey: (NSNotification *)notification;
48- (void)windowDidResignKey: (NSNotification *)notification;
8fc821cc
DE
49- (void)windowDidBecomeMain: (NSNotification *)notification;
50- (void)windowDidResignMain: (NSNotification *)notification;
eb3426e7 51- (BOOL)windowShouldClose: (id)sender;
9692f42b 52- (void)windowWillClose: (NSNotification *)notification;
86adc758
DE
53
54// Menu item handlers
7dd8b1ea
DE
55- (void)wxMenuItemAction: (NSMenuItem *)menuItem;
56- (BOOL)validateMenuItem: (NSMenuItem *)menuItem;
78c67cb6 57@end //interface wxNSWindowDelegate
e7e1ad7d 58WX_DECLARE_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
720e01c3 59
78c67cb6 60@implementation wxNSWindowDelegate : NSObject
720e01c3 61
c6f73d05
DE
62- (id)init
63{
64 m_wxCocoaInterface = NULL;
65 return [super init];
66}
67
68- (void)setWxCocoaInterface: (wxCocoaNSWindow *)wxCocoaInterface
69{
70 m_wxCocoaInterface = wxCocoaInterface;
71}
72
73- (wxCocoaNSWindow *)wxCocoaInterface
74{
75 return m_wxCocoaInterface;
76}
77
86adc758 78// Delegate message handlers
78c67cb6 79- (void)windowDidBecomeKey: (NSNotification *)notification
720e01c3
DE
80{
81 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
c6f73d05 82 wxASSERT(win==m_wxCocoaInterface);
2b030203 83 wxCHECK_RET(win,wxT("notificationDidBecomeKey received but no wxWindow exists"));
aa992c59 84 win->CocoaDelegate_windowDidBecomeKey();
720e01c3
DE
85}
86
78c67cb6 87- (void)windowDidResignKey: (NSNotification *)notification
720e01c3
DE
88{
89 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
c6f73d05 90 wxASSERT(win==m_wxCocoaInterface);
2b030203 91 wxCHECK_RET(win,wxT("notificationDidResignKey received but no wxWindow exists"));
aa992c59 92 win->CocoaDelegate_windowDidResignKey();
720e01c3
DE
93}
94
8fc821cc
DE
95- (void)windowDidBecomeMain: (NSNotification *)notification
96{
97 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
c6f73d05 98 wxASSERT(win==m_wxCocoaInterface);
2b030203 99 wxCHECK_RET(win,wxT("notificationDidBecomeMain received but no wxWindow exists"));
8fc821cc
DE
100 win->CocoaDelegate_windowDidBecomeMain();
101}
102
103- (void)windowDidResignMain: (NSNotification *)notification
104{
105 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
c6f73d05 106 wxASSERT(win==m_wxCocoaInterface);
2b030203 107 wxCHECK_RET(win,wxT("notificationDidResignMain received but no wxWindow exists"));
8fc821cc
DE
108 win->CocoaDelegate_windowDidResignMain();
109}
110
eb3426e7
DE
111- (BOOL)windowShouldClose: (id)sender
112{
48580976 113 wxLogTrace(wxTRACE_COCOA,wxT("windowShouldClose"));
eb3426e7 114 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
c6f73d05 115 wxASSERT(tlw==m_wxCocoaInterface);
aa992c59 116 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
eb3426e7 117 {
48580976 118 wxLogTrace(wxTRACE_COCOA,wxT("Window will not be closed"));
eb3426e7
DE
119 return NO;
120 }
48580976 121 wxLogTrace(wxTRACE_COCOA,wxT("Window will be closed"));
eb3426e7
DE
122 return YES;
123}
124
9692f42b
DE
125- (void)windowWillClose: (NSNotification *)notification
126{
127 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
c6f73d05 128 wxASSERT(win==m_wxCocoaInterface);
2b030203 129 wxCHECK_RET(win,wxT("windowWillClose received but no wxWindow exists"));
9692f42b
DE
130 win->CocoaDelegate_windowWillClose();
131}
132
86adc758 133// Menu item handlers
7dd8b1ea 134- (void)wxMenuItemAction: (NSMenuItem *)sender
86adc758
DE
135{
136 wxASSERT(m_wxCocoaInterface);
137 m_wxCocoaInterface->CocoaDelegate_wxMenuItemAction(sender);
138}
139
7dd8b1ea 140- (BOOL)validateMenuItem: (NSMenuItem *)sender
86adc758
DE
141{
142 wxASSERT(m_wxCocoaInterface);
143 return m_wxCocoaInterface->CocoaDelegate_validateMenuItem(sender);
144}
145
78c67cb6 146@end //implementation wxNSWindowDelegate
e7e1ad7d 147WX_IMPLEMENT_GET_OBJC_CLASS(wxNSWindowDelegate,NSObject)
720e01c3
DE
148
149// ============================================================================
150// class wxCocoaNSWindow
151// ============================================================================
fb896a32 152
fb896a32
DE
153WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
154
0092ef0d
DE
155wxCocoaNSWindow::wxCocoaNSWindow(wxTopLevelWindowCocoa *tlw)
156: m_wxTopLevelWindowCocoa(tlw)
c6f73d05 157{
e7e1ad7d 158 m_cocoaDelegate = [[WX_GET_OBJC_CLASS(wxNSWindowDelegate) alloc] init];
c6f73d05
DE
159 [m_cocoaDelegate setWxCocoaInterface: this];
160}
161
162wxCocoaNSWindow::~wxCocoaNSWindow()
163{
164 [m_cocoaDelegate setWxCocoaInterface: NULL];
165 [m_cocoaDelegate release];
166}
720e01c3 167
fb896a32
DE
168void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
169{
bac6f234
DE
170 if(cocoaNSWindow)
171 {
172 [cocoaNSWindow setReleasedWhenClosed: NO];
173 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
c6f73d05 174 [cocoaNSWindow setDelegate: m_cocoaDelegate];
720e01c3
DE
175 }
176}
177
178void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
179{
180 if(cocoaNSWindow)
181 {
78c67cb6 182 [cocoaNSWindow setDelegate: nil];
720e01c3 183 sm_cocoaHash.erase(cocoaNSWindow);
bac6f234 184 }
fb896a32
DE
185}
186
8ded703d 187wxMenuBar* wxCocoaNSWindow::GetAppMenuBar(wxCocoaNSWindow *win)
360be3c0
DE
188{
189 return NULL;
190}
191
fb896a32 192// ============================================================================
829a2e95 193// @class WXNSWindow
fb896a32 194// ============================================================================
829a2e95 195@implementation WXNSWindow : NSWindow
fb896a32 196
3d012d1b
DE
197- (BOOL)canBecomeKeyWindow
198{
199 bool canBecome = false;
200 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
201 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
202 canBecome = [super canBecomeKeyWindow];
203 return canBecome;
204}
205
8fc821cc
DE
206- (BOOL)canBecomeMainWindow
207{
208 bool canBecome = false;
209 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
210 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
211 canBecome = [super canBecomeMainWindow];
212 return canBecome;
213}
214
829a2e95 215@end // implementation WXNSWindow
dc834029 216WX_IMPLEMENT_GET_OBJC_CLASS(WXNSWindow,NSWindow)
829a2e95
DE
217
218// ============================================================================
219// @class WXNSPanel
220// ============================================================================
221@implementation WXNSPanel : NSPanel
222
223- (BOOL)canBecomeKeyWindow
224{
225 bool canBecome = false;
226 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
227 if(!tlw || !tlw->Cocoa_canBecomeKeyWindow(canBecome))
228 canBecome = [super canBecomeKeyWindow];
229 return canBecome;
230}
231
232- (BOOL)canBecomeMainWindow
233{
234 bool canBecome = false;
235 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
236 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
237 canBecome = [super canBecomeMainWindow];
238 return canBecome;
239}
240
241@end // implementation WXNSPanel
dc834029 242WX_IMPLEMENT_GET_OBJC_CLASS(WXNSPanel,NSPanel)