1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/preferences.cpp
3 // Purpose: Native OS X implementation of wxPreferencesEditor.
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/private/preferences.h"
28 #ifdef wxHAS_PREF_EDITOR_NATIVE
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"
38 #import <AppKit/NSWindow.h>
41 wxBitmap wxStockPreferencesPage::GetLargeIcon() const
46 return wxBitmap([NSImage imageNamed:NSImageNamePreferencesGeneral]);
48 return wxBitmap([NSImage imageNamed:NSImageNameAdvanced]);
50 return wxBitmap(); // silence compiler warning
54 class wxCocoaPrefsWindow : public wxFrame
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),
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);
70 m_toolbar->Bind(wxEVT_TOOL,
71 &wxCocoaPrefsWindow::OnPageChanged, this);
72 Bind(wxEVT_CLOSE_WINDOW, &wxCocoaPrefsWindow::OnClose, this);
75 void AddPage(wxPreferencesPage *page)
77 wxASSERT_MSG( !m_toolbarRealized,
78 "can't add more preferences pages after showing the window" );
80 const wxString title = page->GetName();
81 wxBitmap bmp(page->GetLargeIcon());
82 wxASSERT_MSG( bmp.IsOk(), "OS X requires valid bitmap for preference page" );
84 int toolId = wxIdManager::ReserveId();
85 wxToolBarToolBase *tool = m_toolbar->AddTool(toolId, title, bmp);
87 wxSharedPtr<PageInfo> info(new PageInfo(page));
88 m_pages.push_back(info);
90 tool->SetClientData(info.get());
93 virtual bool Show(bool show)
95 if ( show && !m_toolbarRealized )
98 m_toolbarRealized = true;
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());
106 return wxFrame::Show(show);
109 virtual bool ShouldPreventAppExit() const { return false; }
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)
116 NSRect r = wxToNSRect(NULL, wxRect(x, y, width, height));
117 NSWindow *win = (NSWindow*)GetWXWindow();
118 [win setFrame:r display:YES animate:YES];
123 void OnSelectPageForTool(const wxToolBarToolBase *tool)
125 PageInfo *info = static_cast<PageInfo*>(tool->GetClientData());
126 wxCHECK_RET( info, "toolbar item lacks client data" );
130 info->win = info->page->CreateWindow(this);
133 // fill the page with data using wxEVT_INIT_DIALOG/TransferDataToWindow:
134 info->win->InitDialog();
137 // When the page changes in a native preferences dialog, the sequence
138 // of events is thus:
140 // 1. the old page is hidden, only gray background remains
142 m_visiblePage->Hide();
143 m_visiblePage = info->win;
145 // 2. window is resized to fix the new page, with animation
146 // (in our case, using overriden DoMoveWindow())
147 SetClientSize(info->win->GetSize());
149 // 3. new page is shown and the title updated.
151 SetTitle(info->page->GetName());
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.
157 // We'll need to add wxPreferencesPage::IsResizable() virtual
158 // method to implement this.
161 void OnPageChanged(wxCommandEvent& event)
163 wxToolBarToolBase *tool = m_toolbar->FindById(event.GetId());
164 wxCHECK_RET( tool, "invalid tool ID" );
165 OnSelectPageForTool(tool);
168 void OnClose(wxCloseEvent& e)
170 // Instead of destroying the window, just hide it, it could be
171 // reused again by another invocation of the editor.
176 struct PageInfo : public wxObject
178 PageInfo(wxPreferencesPage *p) : page(p), win(NULL) {}
180 wxSharedPtr<wxPreferencesPage> page;
183 // All pages. Use shared pointer to be able to get pointers to PageInfo structs
184 wxVector< wxSharedPtr<PageInfo> > m_pages;
186 wxToolBar *m_toolbar;
187 bool m_toolbarRealized;
188 wxWindow *m_visiblePage;
192 class wxCocoaPreferencesEditorImpl : public wxPreferencesEditorImpl
195 wxCocoaPreferencesEditorImpl(const wxString& title)
196 : m_win(NULL), m_title(title)
200 virtual ~wxCocoaPreferencesEditorImpl()
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.
209 virtual void AddPage(wxPreferencesPage* page)
211 GetWin()->AddPage(page);
214 virtual void Show(wxWindow* WXUNUSED(parent))
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();
223 virtual void Dismiss()
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.
232 // Use this function to access m_win, so that the window is only created on
233 // demand when actually needed.
234 wxCocoaPrefsWindow* GetWin()
238 if ( m_title.empty() )
239 m_title = _("Preferences");
241 m_win = new wxCocoaPrefsWindow(m_title);
247 wxWeakRef<wxCocoaPrefsWindow> m_win;
253 wxPreferencesEditorImpl* wxPreferencesEditorImpl::Create(const wxString& title)
255 return new wxCocoaPreferencesEditorImpl(title);
258 #endif // wxHAS_PREF_EDITOR_NATIVE