]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/dialog.mm
fix for a warning in DLL build (patch 966823)
[wxWidgets.git] / src / cocoa / dialog.mm
CommitLineData
fb896a32
DE
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
065e208e 9// Licence: wxWidgets licence
fb896a32
DE
10/////////////////////////////////////////////////////////////////////////////
11
449c5673
DE
12#include "wx/wxprec.h"
13#ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/dialog.h"
17 #include "wx/settings.h"
18#endif //WX_PRECOMP
fb896a32 19
7fc77f30 20#include "wx/cocoa/autorelease.h"
35812955 21#include "wx/cocoa/string.h"
7fc77f30 22
fb896a32
DE
23#import <AppKit/NSPanel.h>
24#import <AppKit/NSApplication.h>
47885968
DE
25#import <AppKit/NSEvent.h>
26#import <Foundation/NSRunLoop.h>
fb896a32
DE
27
28// Lists to keep track of windows, so we can disable/enable them
29// for modal dialogs
30static wxWindowList wxModalDialogs;
31
32IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
33
34BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
35 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
36 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
37 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
38 EVT_CLOSE(wxDialog::OnCloseWindow)
39END_EVENT_TABLE()
40
41WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
42
43void wxDialog::Init()
44{
90c10896 45 m_isModal = false;
fb896a32
DE
46 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
47}
48
49bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
50 const wxString& title,
51 const wxPoint& pos,
52 const wxSize& size,
53 long style,
54 const wxString& name)
55{
7fc77f30 56 wxAutoNSAutoreleasePool pool;
fb896a32
DE
57 wxTopLevelWindows.Append(this);
58
59 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
60 return false;
61
62 if (parent)
63 parent->AddChild(this);
64
65 NSRect cocoaRect = NSMakeRect(300,300,200,200);
66
67 unsigned int cocoaStyle = 0;
68 cocoaStyle |= NSTitledWindowMask;
69 cocoaStyle |= NSClosableWindowMask;
70 cocoaStyle |= NSMiniaturizableWindowMask;
71 cocoaStyle |= NSResizableWindowMask;
72
73 m_cocoaNSWindow = NULL;
74 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
75 // NOTE: SetNSWindow has retained the Cocoa object for this object.
76 // Because we do not release on close, the following release matches the
77 // above alloc and thus the retain count will be 1.
78 [m_cocoaNSWindow release];
48580976 79 wxLogTrace(wxTRACE_COCOA_RetainRelease,wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
35812955
DE
80 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
81 [m_cocoaNSWindow setHidesOnDeactivate:NO];
fb896a32
DE
82
83 return true;
84}
85
86wxDialog::~wxDialog()
87{
162c4aad 88 DisassociateNSPanel(GetNSPanel());
fb896a32
DE
89}
90
9692f42b 91void wxDialog::CocoaDelegate_windowWillClose(void)
fb896a32
DE
92{
93 m_closed = true;
94 /* Actually, this isn't true anymore */
48580976 95 wxLogTrace(wxTRACE_COCOA,wxT("Woah: Dialogs are not generally closed"));
fb896a32
DE
96}
97
98void wxDialog::SetModal(bool flag)
99{
90c10896 100 wxFAIL_MSG( wxT("wxDialog:SetModal obsolete now") );
fb896a32
DE
101}
102
103bool wxDialog::Show(bool show)
104{
0411b864
DE
105 if(m_isShown == show)
106 return false;
90c10896 107
fb896a32 108 if(show)
fb896a32 109 {
90c10896
DE
110 wxAutoNSAutoreleasePool pool;
111 InitDialog();
112 if(IsModal())
113 { // ShowModal() will show the dialog
114 m_isShown = true;
115 return true;
fb896a32 116 }
90c10896
DE
117 }
118 else
119 {
120 if(IsModal())
121 { // this doesn't hide the dialog, base class Show(false) does.
48580976 122 wxLogTrace(wxTRACE_COCOA,wxT("abortModal"));
fb896a32
DE
123 [wxTheApp->GetNSApplication() abortModal];
124 wxModalDialogs.DeleteObject(this);
90c10896 125 m_isModal = false;
fb896a32
DE
126 }
127 }
90c10896 128 return wxTopLevelWindow::Show(show);
fb896a32
DE
129}
130
90c10896
DE
131// Shows the dialog and begins a modal event loop. When the event loop
132// is stopped (via EndModal()) it returns the exit code.
fb896a32
DE
133int wxDialog::ShowModal()
134{
90c10896
DE
135 wxCHECK_MSG(!IsModal(),GetReturnCode(),wxT("wxDialog::ShowModal called within its own modal loop"));
136
137 // Show(true) will set m_isShown = true
138 m_isShown = false;
139 m_isModal = true;
140 wxModalDialogs.Append(this);
141
142 wxLogTrace(wxTRACE_COCOA,wxT("runModal"));
143 NSApplication *theNSApp = wxTheApp->GetNSApplication();
144 // If the app hasn't started, flush the event queue
145 // If we don't do this, the Dock doesn't get the message that
146 // the app has started so will refuse to activate it.
147 if(![theNSApp isRunning])
148 {
149 // We should only do a few iterations so one pool should be okay
150 wxAutoNSAutoreleasePool pool;
151 while(NSEvent *event = [theNSApp
152 nextEventMatchingMask:NSAnyEventMask
153 untilDate:[NSDate distantPast]
154 inMode:NSDefaultRunLoopMode
155 dequeue: YES])
156 {
157 [theNSApp sendEvent: event];
158 }
159 }
160
fb896a32 161 Show(true);
90c10896
DE
162 do {
163 wxAutoNSAutoreleasePool pool;
164 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
165 } while(0);
166 wxLogTrace(wxTRACE_COCOA,wxT("runModal END"));
167
fb896a32
DE
168 return GetReturnCode();
169}
170
fb896a32
DE
171void wxDialog::EndModal(int retCode)
172{
32794053 173 wxASSERT_MSG(IsModal(), wxT("EndModal() should only be used within ShowModal()"));
fb896a32
DE
174 SetReturnCode(retCode);
175 Show(false);
176}
177
fb896a32
DE
178void wxDialog::OnCloseWindow(wxCloseEvent& event)
179{
180 // We'll send a Cancel message by default,
181 // which may close the dialog.
182 // Check for looping if the Cancel event handler calls Close().
183
184 // Note that if a cancel button and handler aren't present in the dialog,
185 // nothing will happen when you close the dialog via the window manager, or
186 // via Close().
187 // We wouldn't want to destroy the dialog by default, since the dialog may have been
188 // created on the stack.
189 // However, this does mean that calling dialog->Close() won't delete the dialog
190 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
191 // sure to destroy the dialog.
192 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
193 // ALWAYS VETO THIS EVENT!!!!
194 event.Veto();
195
196 static wxList closing;
197
198 if ( closing.Member(this) )
199 {
2b030203 200 wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog"));
fb896a32
DE
201 return;
202 }
203
204 closing.Append(this);
205
48580976 206 wxLogTrace(wxTRACE_COCOA,wxT("Sending Cancel Event"));
fb896a32
DE
207 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
208 cancelEvent.SetEventObject( this );
209 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
210
211 closing.DeleteObject(this);
212}
213
214// Standard buttons
215void wxDialog::OnOK(wxCommandEvent& event)
216{
217 if ( Validate() && TransferDataFromWindow() )
218 {
219 EndModal(wxID_OK);
220 }
221}
222
223void wxDialog::OnApply(wxCommandEvent& event)
224{
225 if (Validate())
226 TransferDataFromWindow();
227 // TODO probably need to disable the Apply button until things change again
228}
229
230void wxDialog::OnCancel(wxCommandEvent& event)
231{
48580976 232 wxLogTrace(wxTRACE_COCOA,wxT("Cancelled!"));
fb896a32
DE
233 EndModal(wxID_CANCEL);
234}
235