]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/dialog.mm
Add wxCocoa-specific trace masks
[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
9// Licence: wxWindows license
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{
45 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
46}
47
48bool wxDialog::Create(wxWindow *parent, wxWindowID winid,
49 const wxString& title,
50 const wxPoint& pos,
51 const wxSize& size,
52 long style,
53 const wxString& name)
54{
7fc77f30 55 wxAutoNSAutoreleasePool pool;
fb896a32
DE
56 wxTopLevelWindows.Append(this);
57
58 if(!CreateBase(parent,winid,pos,size,style,wxDefaultValidator,name))
59 return false;
60
61 if (parent)
62 parent->AddChild(this);
63
64 NSRect cocoaRect = NSMakeRect(300,300,200,200);
65
66 unsigned int cocoaStyle = 0;
67 cocoaStyle |= NSTitledWindowMask;
68 cocoaStyle |= NSClosableWindowMask;
69 cocoaStyle |= NSMiniaturizableWindowMask;
70 cocoaStyle |= NSResizableWindowMask;
71
72 m_cocoaNSWindow = NULL;
73 SetNSPanel([[NSPanel alloc] initWithContentRect:cocoaRect styleMask:cocoaStyle backing:NSBackingStoreBuffered defer:NO]);
74 // NOTE: SetNSWindow has retained the Cocoa object for this object.
75 // Because we do not release on close, the following release matches the
76 // above alloc and thus the retain count will be 1.
77 [m_cocoaNSWindow release];
2b030203 78 wxLogDebug(wxT("wxDialog m_cocoaNSWindow retainCount=%d"),[m_cocoaNSWindow retainCount]);
35812955
DE
79 [m_cocoaNSWindow setTitle:wxNSStringWithWxString(title)];
80 [m_cocoaNSWindow setHidesOnDeactivate:NO];
fb896a32
DE
81
82 return true;
83}
84
85wxDialog::~wxDialog()
86{
2b030203 87 wxLogDebug(wxT("Destroying"));
fb896a32
DE
88 // setReleasedWhenClosed: NO
89 [m_cocoaNSWindow close];
162c4aad 90 DisassociateNSPanel(GetNSPanel());
fb896a32
DE
91}
92
9692f42b 93void wxDialog::CocoaDelegate_windowWillClose(void)
fb896a32
DE
94{
95 m_closed = true;
96 /* Actually, this isn't true anymore */
2b030203 97 wxLogDebug(wxT("Woah: Dialogs are not generally closed"));
fb896a32
DE
98}
99
100void wxDialog::SetModal(bool flag)
101{
102 if ( flag )
103 {
104 wxModelessWindows.DeleteObject(this);
105 m_windowStyle |= wxDIALOG_MODAL ;
106 }
107 else
108 {
109 m_windowStyle &= ~wxDIALOG_MODAL ;
110 wxModelessWindows.Append(this);
111 }
112}
113
114bool wxDialog::Show(bool show)
115{
0411b864
DE
116 if(m_isShown == show)
117 return false;
fb896a32
DE
118 if(show)
119 InitDialog();
120 if(IsModal())
121 {
0411b864 122 m_isShown = show;
fb896a32
DE
123 if(show)
124 {
7fc77f30 125 wxAutoNSAutoreleasePool pool;
fb896a32 126 wxModalDialogs.Append(this);
2b030203 127 wxLogDebug(wxT("runModal"));
47885968
DE
128 NSApplication *theNSApp = wxTheApp->GetNSApplication();
129 // If the app hasn't started, flush the event queue
130 // If we don't do this, the Dock doesn't get the message that
131 // the app has started so will refuse to activate it.
132 if(![theNSApp isRunning])
133 {
134 while(NSEvent *event = [theNSApp
135 nextEventMatchingMask:NSAnyEventMask
136 untilDate:[NSDate distantPast]
137 inMode:NSDefaultRunLoopMode
138 dequeue: YES])
139 {
140 [theNSApp sendEvent: event];
141 }
142 }
fb896a32 143 [wxTheApp->GetNSApplication() runModalForWindow:m_cocoaNSWindow];
2b030203 144 wxLogDebug(wxT("runModal END"));
fb896a32
DE
145 }
146 else
147 {
2b030203 148 wxLogDebug(wxT("abortModal"));
fb896a32
DE
149 [wxTheApp->GetNSApplication() abortModal];
150 wxModalDialogs.DeleteObject(this);
151 }
152 }
0411b864
DE
153 else
154 return wxTopLevelWindow::Show(show);
fb896a32
DE
155 return true;
156}
157
158// Replacement for Show(TRUE) for modal dialogs - returns return code
159int wxDialog::ShowModal()
160{
161 if(!IsModal())
162 SetModal(true);
163 Show(true);
164 return GetReturnCode();
165}
166
167// EndModal will work for any dialog
168void wxDialog::EndModal(int retCode)
169{
170 SetReturnCode(retCode);
171 Show(false);
172}
173
174bool wxDialog::IsModal() const
175{
176 return (GetWindowStyleFlag() & wxDIALOG_MODAL);
177}
178
179void wxDialog::OnCloseWindow(wxCloseEvent& event)
180{
181 // We'll send a Cancel message by default,
182 // which may close the dialog.
183 // Check for looping if the Cancel event handler calls Close().
184
185 // Note that if a cancel button and handler aren't present in the dialog,
186 // nothing will happen when you close the dialog via the window manager, or
187 // via Close().
188 // We wouldn't want to destroy the dialog by default, since the dialog may have been
189 // created on the stack.
190 // However, this does mean that calling dialog->Close() won't delete the dialog
191 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
192 // sure to destroy the dialog.
193 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
194 // ALWAYS VETO THIS EVENT!!!!
195 event.Veto();
196
197 static wxList closing;
198
199 if ( closing.Member(this) )
200 {
2b030203 201 wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog"));
fb896a32
DE
202 return;
203 }
204
205 closing.Append(this);
206
2b030203 207 wxLogDebug(wxT("Sending Cancel Event"));
fb896a32
DE
208 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
209 cancelEvent.SetEventObject( this );
210 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
211
212 closing.DeleteObject(this);
213}
214
215// Standard buttons
216void wxDialog::OnOK(wxCommandEvent& event)
217{
218 if ( Validate() && TransferDataFromWindow() )
219 {
220 EndModal(wxID_OK);
221 }
222}
223
224void wxDialog::OnApply(wxCommandEvent& event)
225{
226 if (Validate())
227 TransferDataFromWindow();
228 // TODO probably need to disable the Apply button until things change again
229}
230
231void wxDialog::OnCancel(wxCommandEvent& event)
232{
2b030203 233 wxLogDebug(wxT("Cancelled!"));
fb896a32
DE
234 EndModal(wxID_CANCEL);
235}
236