]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/dialog.mm
1. added abstract interface to wxHtmlWindow for use by wxHtmlWinParser and implemente...
[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
ec157c8f 7// RCS-ID: $Id:
fb896a32 8// Copyright: 2002 David Elliott
ec157c8f 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;
ec157c8f 46 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
fb896a32
DE
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
acb96ac2
DE
178void wxDialog::EndDialog(int retCode)
179{
180 if(IsModal())
181 EndModal(retCode);
182 else
183 Show(false);
184}
185
fb896a32
DE
186void wxDialog::OnCloseWindow(wxCloseEvent& event)
187{
188 // We'll send a Cancel message by default,
189 // which may close the dialog.
190 // Check for looping if the Cancel event handler calls Close().
191
192 // Note that if a cancel button and handler aren't present in the dialog,
193 // nothing will happen when you close the dialog via the window manager, or
194 // via Close().
195 // We wouldn't want to destroy the dialog by default, since the dialog may have been
196 // created on the stack.
197 // However, this does mean that calling dialog->Close() won't delete the dialog
198 // unless the handler for wxID_CANCEL does so. So use Destroy() if you want to be
199 // sure to destroy the dialog.
200 // The default OnCancel (above) simply ends a modal dialog, and hides a modeless dialog.
201 // ALWAYS VETO THIS EVENT!!!!
202 event.Veto();
203
204 static wxList closing;
ec157c8f 205
fb896a32
DE
206 if ( closing.Member(this) )
207 {
2b030203 208 wxLogDebug(wxT("WARNING: Attempting to recursively call Close for dialog"));
fb896a32
DE
209 return;
210 }
ec157c8f 211
fb896a32 212 closing.Append(this);
ec157c8f 213
48580976 214 wxLogTrace(wxTRACE_COCOA,wxT("Sending Cancel Event"));
fb896a32
DE
215 wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL);
216 cancelEvent.SetEventObject( this );
217 GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog
218
219 closing.DeleteObject(this);
220}
221
222// Standard buttons
223void wxDialog::OnOK(wxCommandEvent& event)
224{
225 if ( Validate() && TransferDataFromWindow() )
226 {
acb96ac2 227 EndDialog(wxID_OK);
fb896a32
DE
228 }
229}
230
231void wxDialog::OnApply(wxCommandEvent& event)
232{
ec157c8f
WS
233 if (Validate())
234 TransferDataFromWindow();
235 // TODO probably need to disable the Apply button until things change again
fb896a32
DE
236}
237
238void wxDialog::OnCancel(wxCommandEvent& event)
239{
48580976 240 wxLogTrace(wxTRACE_COCOA,wxT("Cancelled!"));
acb96ac2 241 EndDialog(wxID_CANCEL);
fb896a32
DE
242}
243