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