]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/preferences.mm
guarding open combo box against AppDefined NSEvents issued by wxEventLoop::WakeUp...
[wxWidgets.git] / src / osx / cocoa / preferences.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/preferences.cpp
3 // Purpose: Native OS X implementation of wxPreferencesEditor.
4 // Author: Vaclav Slavik
5 // Created: 2013-02-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/private/preferences.h"
27
28 #ifdef wxHAS_PREF_EDITOR_NATIVE
29
30 #include "wx/frame.h"
31 #include "wx/sharedptr.h"
32 #include "wx/toolbar.h"
33 #include "wx/vector.h"
34 #include "wx/weakref.h"
35 #include "wx/windowid.h"
36 #include "wx/osx/private.h"
37
38 #import <AppKit/NSWindow.h>
39
40
41 wxBitmap wxStockPreferencesPage::GetLargeIcon() const
42 {
43 switch ( m_kind )
44 {
45 case Kind_General:
46 return wxBitmap([NSImage imageNamed:NSImageNamePreferencesGeneral]);
47 case Kind_Advanced:
48 return wxBitmap([NSImage imageNamed:NSImageNameAdvanced]);
49 }
50 return wxBitmap(); // silence compiler warning
51 }
52
53
54 class wxCocoaPrefsWindow : public wxFrame
55 {
56 public:
57 wxCocoaPrefsWindow(const wxString& title)
58 : wxFrame(NULL, wxID_ANY, title,
59 wxDefaultPosition, wxDefaultSize,
60 wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX | wxMINIMIZE_BOX)),
61 m_toolbarRealized(false),
62 m_visiblePage(NULL)
63 {
64 m_toolbar = new wxToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
65 wxTB_FLAT | wxTB_TEXT);
66 m_toolbar->SetToolBitmapSize(wxSize(32,32));
67 m_toolbar->OSXSetSelectableTools(true);
68 SetToolBar(m_toolbar);
69
70 m_toolbar->Bind(wxEVT_TOOL,
71 &wxCocoaPrefsWindow::OnPageChanged, this);
72 Bind(wxEVT_CLOSE_WINDOW, &wxCocoaPrefsWindow::OnClose, this);
73 }
74
75 void AddPage(wxPreferencesPage *page)
76 {
77 wxASSERT_MSG( !m_toolbarRealized,
78 "can't add more preferences pages after showing the window" );
79
80 const wxString title = page->GetName();
81 wxBitmap bmp(page->GetLargeIcon());
82 wxASSERT_MSG( bmp.IsOk(), "OS X requires valid bitmap for preference page" );
83
84 int toolId = wxIdManager::ReserveId();
85 wxToolBarToolBase *tool = m_toolbar->AddTool(toolId, title, bmp);
86
87 wxSharedPtr<PageInfo> info(new PageInfo(page));
88 m_pages.push_back(info);
89
90 tool->SetClientData(info.get());
91 }
92
93 virtual bool Show(bool show)
94 {
95 if ( show && !m_toolbarRealized )
96 {
97 m_toolbar->Realize();
98 m_toolbarRealized = true;
99
100 const wxToolBarToolBase *first = m_toolbar->GetToolByPos(0);
101 wxCHECK_MSG( first, false, "no preferences panels" );
102 OnSelectPageForTool(first);
103 m_toolbar->OSXSelectTool(first->GetId());
104 }
105
106 return wxFrame::Show(show);
107 }
108
109 virtual bool ShouldPreventAppExit() const { return false; }
110
111 protected:
112 // Native preferences windows resize when the selected panel changes and
113 // the resizing is animated, so we need to override DoMoveWindow.
114 virtual void DoMoveWindow(int x, int y, int width, int height)
115 {
116 NSRect r = wxToNSRect(NULL, wxRect(x, y, width, height));
117 NSWindow *win = (NSWindow*)GetWXWindow();
118 [win setFrame:r display:YES animate:YES];
119 }
120
121
122 private:
123 void OnSelectPageForTool(const wxToolBarToolBase *tool)
124 {
125 PageInfo *info = static_cast<PageInfo*>(tool->GetClientData());
126 wxCHECK_RET( info, "toolbar item lacks client data" );
127
128 if ( !info->win )
129 {
130 info->win = info->page->CreateWindow(this);
131 info->win->Hide();
132 info->win->Fit();
133 // fill the page with data using wxEVT_INIT_DIALOG/TransferDataToWindow:
134 info->win->InitDialog();
135 }
136
137 // When the page changes in a native preferences dialog, the sequence
138 // of events is thus:
139
140 // 1. the old page is hidden, only gray background remains
141 if ( m_visiblePage )
142 m_visiblePage->Hide();
143 m_visiblePage = info->win;
144
145 // 2. window is resized to fix the new page, with animation
146 // (in our case, using overriden DoMoveWindow())
147 SetClientSize(info->win->GetSize());
148
149 // 3. new page is shown and the title updated.
150 info->win->Show();
151 SetTitle(info->page->GetName());
152
153 // TODO: Preferences window may have some pages resizeable and some
154 // non-resizable on OS X; the whole window is or is not resizable
155 // depending on which page is selected.
156 //
157 // We'll need to add wxPreferencesPage::IsResizable() virtual
158 // method to implement this.
159 }
160
161 void OnPageChanged(wxCommandEvent& event)
162 {
163 wxToolBarToolBase *tool = m_toolbar->FindById(event.GetId());
164 wxCHECK_RET( tool, "invalid tool ID" );
165 OnSelectPageForTool(tool);
166 }
167
168 void OnClose(wxCloseEvent& e)
169 {
170 // Instead of destroying the window, just hide it, it could be
171 // reused again by another invocation of the editor.
172 Hide();
173 }
174
175 private:
176 struct PageInfo : public wxObject
177 {
178 PageInfo(wxPreferencesPage *p) : page(p), win(NULL) {}
179
180 wxSharedPtr<wxPreferencesPage> page;
181 wxWindow *win;
182 };
183 // All pages. Use shared pointer to be able to get pointers to PageInfo structs
184 wxVector< wxSharedPtr<PageInfo> > m_pages;
185
186 wxToolBar *m_toolbar;
187 bool m_toolbarRealized;
188 wxWindow *m_visiblePage;
189 };
190
191
192 class wxCocoaPreferencesEditorImpl : public wxPreferencesEditorImpl
193 {
194 public:
195 wxCocoaPreferencesEditorImpl(const wxString& title)
196 : m_win(NULL), m_title(title)
197 {
198 }
199
200 virtual ~wxCocoaPreferencesEditorImpl()
201 {
202 // m_win may already be destroyed if this destructor is called from
203 // wxApp's destructor. In that case, all windows -- including this
204 // one -- would already be destroyed by now.
205 if ( m_win )
206 m_win->Destroy();
207 }
208
209 virtual void AddPage(wxPreferencesPage* page)
210 {
211 GetWin()->AddPage(page);
212 }
213
214 virtual void Show(wxWindow* WXUNUSED(parent))
215 {
216 // OS X preferences windows don't have parents, they are independent
217 // windows, so we just ignore the 'parent' argument.
218 wxWindow *win = GetWin();
219 win->Show();
220 win->Raise();
221 }
222
223 virtual void Dismiss()
224 {
225 // Don't destroy the window, only hide it, because OS X preferences
226 // window typically remember their state even when closed. Reopening
227 // the window should show it in the exact same state the user left it.
228 GetWin()->Hide();
229 }
230
231 private:
232 // Use this function to access m_win, so that the window is only created on
233 // demand when actually needed.
234 wxCocoaPrefsWindow* GetWin()
235 {
236 if ( !m_win )
237 {
238 if ( m_title.empty() )
239 m_title = _("Preferences");
240
241 m_win = new wxCocoaPrefsWindow(m_title);
242 }
243
244 return m_win;
245 }
246
247 wxWeakRef<wxCocoaPrefsWindow> m_win;
248
249 wxString m_title;
250 };
251
252 /*static*/
253 wxPreferencesEditorImpl* wxPreferencesEditorImpl::Create(const wxString& title)
254 {
255 return new wxCocoaPreferencesEditorImpl(title);
256 }
257
258 #endif // wxHAS_PREF_EDITOR_NATIVE