]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/dialog.mm
unicode compilation fix
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/dialog.h"
13 #include "wx/app.h"
14 #include "wx/settings.h"
15 #include "wx/log.h"
16
17 #include "wx/cocoa/autorelease.h"
18
19 #import <AppKit/NSPanel.h>
20 #import <AppKit/NSApplication.h>
21
22 // Lists to keep track of windows, so we can disable/enable them
23 // for modal dialogs
24 static wxWindowList wxModalDialogs;
25
26 IMPLEMENT_DYNAMIC_CLASS(wxDialog, wxTopLevelWindow)
27
28 BEGIN_EVENT_TABLE(wxDialog, wxDialogBase)
29 EVT_BUTTON(wxID_OK, wxDialog::OnOK)
30 EVT_BUTTON(wxID_APPLY, wxDialog::OnApply)
31 EVT_BUTTON(wxID_CANCEL, wxDialog::OnCancel)
32 EVT_CLOSE(wxDialog::OnCloseWindow)
33 END_EVENT_TABLE()
34
35 WX_IMPLEMENT_COCOA_OWNER(wxDialog,NSPanel,NSWindow,NSWindow)
36
37 void wxDialog::Init()
38 {
39 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
40 }
41
42 bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
43 const wxString& title,
44 const wxPoint& pos,
45 const wxSize& size,
46 long style,
47 const wxString& name)
48 {
49 wxAutoNSAutoreleasePool pool;
50 wxTopLevelWindows.Append(this);
51
52 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
53 return false;
54
55 if (parent)
56 parent->AddChild(this);
57
58 NSRect cocoaRect = NSMakeRect(300,300,200,200);
59
60 unsigned int cocoaStyle = 0;
61 cocoaStyle |= NSTitledWindowMask;
62 cocoaStyle |= NSClosableWindowMask;
63 cocoaStyle |= NSMiniaturizableWindowMask;
64 cocoaStyle |= NSResizableWindowMask;
65
66 m_cocoaNSWindow = NULL;
67 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
68 // NOTE: SetNSWindow has retained the Cocoa object for this object.
69 // Because we do not release on close, the following release matches the
70 // above alloc and thus the retain count will be 1.
71 [m_cocoaNSWindow release];
72 wxLogDebug("wxDialog m_cocoaNSWindow retainCount=%d",[m_cocoaNSWindow retainCount]);
73
74 return true;
75 }
76
77 wxDialog::~wxDialog()
78 {
79 wxLogDebug("Destroying");
80 // setReleasedWhenClosed: NO
81 [m_cocoaNSWindow close];
82 DisassociateNSPanel(m_cocoaNSWindow);
83 }
84
85 void wxDialog::Cocoa_close(void)
86 {
87 m_closed = true;
88 /* Actually, this isn't true anymore */
89 wxLogDebug("Woah: Dialogs are not generally closed");
90 }
91
92 void wxDialog::SetModal(bool flag)
93 {
94 if ( flag )
95 {
96 wxModelessWindows.DeleteObject(this);
97 m_windowStyle |= wxDIALOG_MODAL ;
98 }
99 else
100 {
101 m_windowStyle &= ~wxDIALOG_MODAL ;
102 wxModelessWindows.Append(this);
103 }
104 }
105
106 bool wxDialog::Show(bool show)
107 {
108 if(show)
109 InitDialog();
110 if(IsModal())
111 {
112 if(show)
113 {
114 wxAutoNSAutoreleasePool pool;
115 wxModalDialogs.Append(this);
116 wxLogDebug("runModal");
117 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
118 wxLogDebug("runModal END");
119 }
120 else
121 {
122 wxLogDebug("abortModal");
123 [wxTheApp->GetNSApplication() abortModal];
124 wxModalDialogs.DeleteObject(this);
125 }
126 }
127 return true;
128 }
129
130 // Replacement for Show(TRUE) for modal dialogs - returns return code
131 int wxDialog::ShowModal()
132 {
133 if(!IsModal())
134 SetModal(true);
135 Show(true);
136 return GetReturnCode();
137 }
138
139 // EndModal will work for any dialog
140 void wxDialog::EndModal(int retCode)
141 {
142 SetReturnCode(retCode);
143 Show(false);
144 }
145
146 bool wxDialog::IsModal() const
147 {
148 return (GetWindowStyleFlag() & wxDIALOG_MODAL);
149 }
150
151 void wxDialog::OnCloseWindow(wxCloseEvent& event)
152 {
153 // We'll send a Cancel message by default,
154 // which may close the dialog.
155 // Check for looping if the Cancel event handler calls Close().
156
157 // Note that if a cancel button and handler aren't present in the dialog,
158 // nothing will happen when you close the dialog via the window manager, or
159 // via Close().
160 // We wouldn't want to destroy the dialog by default, since the dialog may have been
161 // created on the stack.
162 // However, this does mean that calling dialog->Close() won't delete the dialog
163 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
164 // sure to destroy the dialog.
165 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
166 // ALWAYS VETO THIS EVENT!!!!
167 event.Veto();
168
169 static wxList closing;
170
171 if ( closing.Member(this) )
172 {
173 wxLogDebug("WARNING: Attempting to recursively call Close for dialog");
174 return;
175 }
176
177 closing.Append(this);
178
179 wxLogDebug("Sending Cancel Event");
180 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
181 cancelEvent.SetEventObject( this );
182 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
183
184 closing.DeleteObject(this);
185 }
186
187 // Standard buttons
188 void wxDialog::OnOK(wxCommandEvent& event)
189 {
190 if ( Validate() && TransferDataFromWindow() )
191 {
192 EndModal(wxID_OK);
193 }
194 }
195
196 void wxDialog::OnApply(wxCommandEvent& event)
197 {
198 if (Validate())
199 TransferDataFromWindow();
200 // TODO probably need to disable the Apply button until things change again
201 }
202
203 void wxDialog::OnCancel(wxCommandEvent& event)
204 {
205 wxLogDebug("Cancelled!");
206 EndModal(wxID_CANCEL);
207 }
208