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