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 #if wxUSE_PREFERENCES_EDITOR
28 #include "wx/private/preferences.h"
30 #ifdef wxHAS_PREF_EDITOR_NATIVE
33 #include "wx/sharedptr.h"
34 #include "wx/toolbar.h"
35 #include "wx/vector.h"
36 #include "wx/weakref.h"
37 #include "wx/windowid.h"
38 #include "wx/osx/private.h"
40 #import <AppKit/NSWindow.h>
43 wxBitmap wxStockPreferencesPage::GetLargeIcon() const
48 return wxBitmap([NSImage imageNamed:NSImageNamePreferencesGeneral]);
50 return wxBitmap([NSImage imageNamed:NSImageNameAdvanced]);
52 return wxBitmap(); // silence compiler warning
56 class wxCocoaPrefsWindow : public wxFrame
59 wxCocoaPrefsWindow(const wxString& title)
60 : wxFrame(NULL, wxID_ANY, title,
61 wxDefaultPosition, wxDefaultSize,
62 wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX | wxMINIMIZE_BOX)),
63 m_toolbarRealized(false),
66 m_toolbar = new wxToolBar(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
67 wxTB_FLAT | wxTB_TEXT);
68 m_toolbar->SetToolBitmapSize(wxSize(32,32));
69 m_toolbar->OSXSetSelectableTools(true);
70 SetToolBar(m_toolbar);
72 m_toolbar->Bind(wxEVT_TOOL,
73 &wxCocoaPrefsWindow::OnPageChanged, this);
74 Bind(wxEVT_CLOSE_WINDOW, &wxCocoaPrefsWindow::OnClose, this);
77 void AddPage(wxPreferencesPage *page)
79 wxASSERT_MSG( !m_toolbarRealized,
80 "can't add more preferences pages after showing the window" );
82 const wxString title = page->GetName();
83 wxBitmap bmp(page->GetLargeIcon());
84 wxASSERT_MSG( bmp.IsOk(), "OS X requires valid bitmap for preference page" );
86 int toolId = wxIdManager::ReserveId();
87 wxToolBarToolBase *tool = m_toolbar->AddTool(toolId, title, bmp);
89 wxSharedPtr<PageInfo> info(new PageInfo(page));
90 m_pages.push_back(info);
92 tool->SetClientData(info.get());
95 virtual bool Show(bool show)
97 if ( show && !m_toolbarRealized )
100 m_toolbarRealized = true;
102 const wxToolBarToolBase *first = m_toolbar->GetToolByPos(0);
103 wxCHECK_MSG( first, false, "no preferences panels" );
104 OnSelectPageForTool(first);
105 m_toolbar->OSXSelectTool(first->GetId());
108 return wxFrame::Show(show);
111 virtual bool ShouldPreventAppExit() const { return false; }
114 // Native preferences windows resize when the selected panel changes and
115 // the resizing is animated, so we need to override DoMoveWindow.
116 virtual void DoMoveWindow(int x, int y, int width, int height)
118 NSRect r = wxToNSRect(NULL, wxRect(x, y, width, height));
119 NSWindow *win = (NSWindow*)GetWXWindow();
120 [win setFrame:r display:YES animate:YES];
125 void OnSelectPageForTool(const wxToolBarToolBase *tool)
127 PageInfo *info = static_cast<PageInfo*>(tool->GetClientData());
128 wxCHECK_RET( info, "toolbar item lacks client data" );
132 info->win = info->page->CreateWindow(this);
135 // fill the page with data using wxEVT_INIT_DIALOG/TransferDataToWindow:
136 info->win->InitDialog();
139 // When the page changes in a native preferences dialog, the sequence
140 // of events is thus:
142 // 1. the old page is hidden, only gray background remains
144 m_visiblePage->Hide();
145 m_visiblePage = info->win;
147 // 2. window is resized to fix the new page, with animation
148 // (in our case, using overriden DoMoveWindow())
149 SetClientSize(info->win->GetSize());
151 // 3. new page is shown and the title updated.
153 SetTitle(info->page->GetName());
155 // TODO: Preferences window may have some pages resizeable and some
156 // non-resizable on OS X; the whole window is or is not resizable
157 // depending on which page is selected.
159 // We'll need to add wxPreferencesPage::IsResizable() virtual
160 // method to implement this.
163 void OnPageChanged(wxCommandEvent& event)
165 wxToolBarToolBase *tool = m_toolbar->FindById(event.GetId());
166 wxCHECK_RET( tool, "invalid tool ID" );
167 OnSelectPageForTool(tool);
170 void OnClose(wxCloseEvent& e)
172 // Instead of destroying the window, just hide it, it could be
173 // reused again by another invocation of the editor.
178 struct PageInfo : public wxObject
180 PageInfo(wxPreferencesPage *p) : page(p), win(NULL) {}
182 wxSharedPtr<wxPreferencesPage> page;
185 // All pages. Use shared pointer to be able to get pointers to PageInfo structs
186 wxVector< wxSharedPtr<PageInfo> > m_pages;
188 wxToolBar *m_toolbar;
189 bool m_toolbarRealized;
190 wxWindow *m_visiblePage;
194 class wxCocoaPreferencesEditorImpl : public wxPreferencesEditorImpl
197 wxCocoaPreferencesEditorImpl(const wxString& title)
198 : m_win(NULL), m_title(title)
202 virtual ~wxCocoaPreferencesEditorImpl()
204 // m_win may already be destroyed if this destructor is called from
205 // wxApp's destructor. In that case, all windows -- including this
206 // one -- would already be destroyed by now.
211 virtual void AddPage(wxPreferencesPage* page)
213 GetWin()->AddPage(page);
216 virtual void Show(wxWindow* WXUNUSED(parent))
218 // OS X preferences windows don't have parents, they are independent
219 // windows, so we just ignore the 'parent' argument.
220 wxWindow *win = GetWin();
225 virtual void Dismiss()
227 // Don't destroy the window, only hide it, because OS X preferences
228 // window typically remember their state even when closed. Reopening
229 // the window should show it in the exact same state the user left it.
234 // Use this function to access m_win, so that the window is only created on
235 // demand when actually needed.
236 wxCocoaPrefsWindow* GetWin()
240 if ( m_title.empty() )
241 m_title = _("Preferences");
243 m_win = new wxCocoaPrefsWindow(m_title);
249 wxWeakRef<wxCocoaPrefsWindow> m_win;
255 wxPreferencesEditorImpl* wxPreferencesEditorImpl::Create(const wxString& title)
257 return new wxCocoaPreferencesEditorImpl(title);
260 #endif // wxHAS_PREF_EDITOR_NATIVE
262 #endif // wxUSE_PREFERENCES_EDITOR