]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dialog.mm
missing commit
[wxWidgets.git] / src / cocoa / dialog.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/dialog.mm
3 // Purpose: wxDialog class
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2002/12/15
7 // RCS-ID: $Id$
8 // Copyright: 2002 David Elliott
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #include "wx/dialog.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/log.h"
18 #include "wx/app.h"
19 #include "wx/settings.h"
20 #endif //WX_PRECOMP
21
22 #include "wx/modalhook.h"
23 #include "wx/cocoa/autorelease.h"
24 #include "wx/cocoa/string.h"
25
26 #import <AppKit/NSPanel.h>
27 #import <AppKit/NSApplication.h>
28 #import <AppKit/NSEvent.h>
29 #import <Foundation/NSRunLoop.h>
30
31 // Lists to keep track of windows, so we can disable/enable them
32 // for modal dialogs
33 static wxWindowList wxModalDialogs;
34
35 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
36
37 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
38
39 void wxDialog::Init()
40 {
41 m_isModal = false;
42 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
43 }
44
45 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
46 const wxString& title,
47 const wxPoint& pos,
48 const wxSize& size,
49 long style,
50 const wxString& name)
51 {
52 wxAutoNSAutoreleasePool pool;
53 wxTopLevelWindows.Append(this);
54
55 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
56 return false;
57
58 if (parent)
59 parent->AddChild(this);
60
61 unsigned int cocoaStyle = NSWindowStyleForWxStyle(style);
62
63 NSRect cocoaRect = MakeInitialNSWindowContentRect(pos,size,cocoaStyle);
64
65 m_cocoaNSWindow = NULL;
66 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
67 // NOTE: SetNSWindow has retained the Cocoa object for this object.
68 // Because we do not release on close, the following release matches the
69 // above alloc and thus the retain count will be 1.
70 [m_cocoaNSWindow release];
71 wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
72 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
73 [m_cocoaNSWindow setHidesOnDeactivate:NO];
74
75 return true;
76 }
77
78 wxDialog::~wxDialog()
79 {
80 DisassociateNSPanel(GetNSPanel());
81 }
82
83 void wxDialog::CocoaDelegate_windowWillClose(void)
84 {
85 m_closed = true;
86 /* Actually, this isn't true anymore */
87 wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed"));
88 }
89
90 void wxDialog::SetModal(bool flag)
91 {
92 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
93 }
94
95 bool wxDialog::Show(bool show)
96 {
97 if(m_isShown == show)
98 return false;
99
100 if(show)
101 {
102 wxAutoNSAutoreleasePool pool;
103
104 if (CanDoLayoutAdaptation())
105 DoLayoutAdaptation();
106
107 InitDialog();
108 if(IsModal())
109 { // ShowModal() will show the dialog
110 m_isShown = true;
111 return true;
112 }
113 }
114 else
115 {
116 if(IsModal())
117 { // this doesn't hide the dialog, base class Show(false) does.
118 wxLogTrace(wxTRACE_COCOA,wxT("abortModal"));
119 [wxTheApp->GetNSApplication() abortModal];
120 wxModalDialogs.DeleteObject(this);
121 m_isModal = false;
122 }
123 }
124 return wxTopLevelWindow::Show(show);
125 }
126
127 // Shows the dialog and begins a modal event loop. When the event loop
128 // is stopped (via EndModal()) it returns the exit code.
129 int wxDialog::ShowModal()
130 {
131 WX_HOOK_MODAL_DIALOG();
132
133 wxCHECK_MSG(!IsModal(),GetReturnCode(),wxT("wxDialog::ShowModal called within its own modal loop"));
134
135 // Show(true) will set m_isShown = true
136 m_isShown = false;
137 m_isModal = true;
138 wxModalDialogs.Append(this);
139
140 wxLogTrace(wxTRACE_COCOA,wxT("runModal"));
141 NSApplication *theNSApp = wxTheApp->GetNSApplication();
142 // If the app hasn't started, flush the event queue
143 // If we don't do this, the Dock doesn't get the message that
144 // the app has started so will refuse to activate it.
145 if(![theNSApp isRunning])
146 {
147 // We should only do a few iterations so one pool should be okay
148 wxAutoNSAutoreleasePool pool;
149 while(NSEvent *event = [theNSApp
150 nextEventMatchingMask:NSAnyEventMask
151 untilDate:[NSDate distantPast]
152 inMode:NSDefaultRunLoopMode
153 dequeue: YES])
154 {
155 [theNSApp sendEvent: event];
156 }
157 }
158
159 Show(true);
160 do {
161 wxAutoNSAutoreleasePool pool;
162 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
163 } while(0);
164 wxLogTrace(wxTRACE_COCOA,wxT("runModal END"));
165
166 return GetReturnCode();
167 }
168
169 void wxDialog::EndModal(int retCode)
170 {
171 wxASSERT_MSG(IsModal(), wxT("EndModal() should only be used within ShowModal()"));
172 SetReturnCode(retCode);
173 Show(false);
174 }