]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/NSWindow.mm
don't hang forever in Dispatch() if there is no event loop
[wxWidgets.git] / src / cocoa / NSWindow.mm
CommitLineData
fb896a32
DE
1/////////////////////////////////////////////////////////////////////////////
2// Name: cocoa/NSWindow.mm
3// Purpose: wxCocoaNSWindow
4// Author: David Elliott
5// Modified by:
6// Created: 2003/03/16
7// RCS-ID: $Id:
8// Copyright: (c) 2003 David Elliott
9// Licence: wxWindows license
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
fb45bb1f 26#include "wx/cocoa/ObjcPose.h"
fb896a32
DE
27#include "wx/cocoa/NSWindow.h"
28
29#import <Appkit/NSWindow.h>
720e01c3
DE
30#import <Foundation/NSNotification.h>
31#import <Foundation/NSString.h>
32
33// ============================================================================
78c67cb6 34// @class wxNSWindowDelegate
720e01c3 35// ============================================================================
78c67cb6 36@interface wxNSWindowDelegate : NSObject
720e01c3
DE
37{
38}
39
78c67cb6
DE
40- (void)windowDidBecomeKey: (NSNotification *)notification;
41- (void)windowDidResignKey: (NSNotification *)notification;
8fc821cc
DE
42- (void)windowDidBecomeMain: (NSNotification *)notification;
43- (void)windowDidResignMain: (NSNotification *)notification;
eb3426e7 44- (BOOL)windowShouldClose: (id)sender;
78c67cb6 45@end //interface wxNSWindowDelegate
720e01c3 46
78c67cb6 47@implementation wxNSWindowDelegate : NSObject
720e01c3 48
78c67cb6 49- (void)windowDidBecomeKey: (NSNotification *)notification
720e01c3
DE
50{
51 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
52 wxCHECK_RET(win,"notificationDidBecomeKey received but no wxWindow exists");
aa992c59 53 win->CocoaDelegate_windowDidBecomeKey();
720e01c3
DE
54}
55
78c67cb6 56- (void)windowDidResignKey: (NSNotification *)notification
720e01c3
DE
57{
58 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
59 wxCHECK_RET(win,"notificationDidResignKey received but no wxWindow exists");
aa992c59 60 win->CocoaDelegate_windowDidResignKey();
720e01c3
DE
61}
62
8fc821cc
DE
63- (void)windowDidBecomeMain: (NSNotification *)notification
64{
65 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
66 wxCHECK_RET(win,"notificationDidBecomeMain received but no wxWindow exists");
67 win->CocoaDelegate_windowDidBecomeMain();
68}
69
70- (void)windowDidResignMain: (NSNotification *)notification
71{
72 wxCocoaNSWindow *win = wxCocoaNSWindow::GetFromCocoa([notification object]);
73 wxCHECK_RET(win,"notificationDidResignMain received but no wxWindow exists");
74 win->CocoaDelegate_windowDidResignMain();
75}
76
eb3426e7
DE
77- (BOOL)windowShouldClose: (id)sender
78{
79 wxLogDebug("windowShouldClose");
80 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(sender);
aa992c59 81 if(tlw && !tlw->CocoaDelegate_windowShouldClose())
eb3426e7
DE
82 {
83 wxLogDebug("Window will not be closed");
84 return NO;
85 }
86 wxLogDebug("Window will be closed");
87 return YES;
88}
89
78c67cb6 90@end //implementation wxNSWindowDelegate
720e01c3
DE
91
92// ============================================================================
93// class wxCocoaNSWindow
94// ============================================================================
fb896a32 95
fb896a32
DE
96WX_IMPLEMENT_OBJC_INTERFACE_HASHMAP(NSWindow)
97
78c67cb6 98struct objc_object *wxCocoaNSWindow::sm_cocoaDelegate = [[wxNSWindowDelegate alloc] init];
720e01c3 99
fb896a32
DE
100void wxCocoaNSWindow::AssociateNSWindow(WX_NSWindow cocoaNSWindow)
101{
bac6f234
DE
102 if(cocoaNSWindow)
103 {
104 [cocoaNSWindow setReleasedWhenClosed: NO];
105 sm_cocoaHash.insert(wxCocoaNSWindowHash::value_type(cocoaNSWindow,this));
78c67cb6 106 [cocoaNSWindow setDelegate: sm_cocoaDelegate];
720e01c3
DE
107 }
108}
109
110void wxCocoaNSWindow::DisassociateNSWindow(WX_NSWindow cocoaNSWindow)
111{
112 if(cocoaNSWindow)
113 {
78c67cb6 114 [cocoaNSWindow setDelegate: nil];
720e01c3 115 sm_cocoaHash.erase(cocoaNSWindow);
bac6f234 116 }
fb896a32
DE
117}
118
119// ============================================================================
120// @class wxPoserNSWindow
121// ============================================================================
122@interface wxPoserNSWindow : NSWindow
123{
124}
125
126- (void)close;
8fc821cc 127- (BOOL)canBecomeMainWindow;
fb896a32
DE
128@end // wxPoserNSwindow
129
130WX_IMPLEMENT_POSER(wxPoserNSWindow);
131@implementation wxPoserNSWindow : NSWindow
132
133- (void)close
134{
135 wxLogDebug("close");
136 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
137 if(tlw)
138 tlw->Cocoa_close();
139 [super close];
140}
141
8fc821cc
DE
142- (BOOL)canBecomeMainWindow
143{
144 bool canBecome = false;
145 wxCocoaNSWindow *tlw = wxCocoaNSWindow::GetFromCocoa(self);
146 if(!tlw || !tlw->Cocoa_canBecomeMainWindow(canBecome))
147 canBecome = [super canBecomeMainWindow];
148 return canBecome;
149}
150
fb896a32
DE
151@end // implementation wxPoserNSWindow
152