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
58 : wxFrame(NULL, wxID_ANY, _("Preferences"),
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);
132 // fill the page with data using wxEVT_INIT_DIALOG/TransferDataToWindow:
133 info->win->InitDialog();
136 // When the page changes in a native preferences dialog, the sequence
137 // of events is thus:
139 // 1. the old page is hidden, only gray background remains
141 m_visiblePage->Hide();
142 m_visiblePage = info->win;
144 // 2. window is resized to fix the new page, with animation
145 // (in our case, using overriden DoMoveWindow())
146 SetClientSize(info->win->GetSize());
148 // 3. new page is shown and the title updated.
150 SetTitle(info->page->GetName());
152 // TODO: Preferences window may have some pages resizeable and some
153 // non-resizable on OS X; the whole window is or is not resizable
154 // depending on which page is selected.
156 // We'll need to add wxPreferencesPage::IsResizable() virtual
157 // method to implement this.
160 void OnPageChanged(wxCommandEvent& event)
162 wxToolBarToolBase *tool = m_toolbar->FindById(event.GetId());
163 wxCHECK_RET( tool, "invalid tool ID" );
164 OnSelectPageForTool(tool);
167 void OnClose(wxCloseEvent& e)
169 // Instead of destroying the window, just hide it, it could be
170 // reused again by another invocation of the editor.
175 struct PageInfo : public wxObject
177 PageInfo(wxPreferencesPage *p) : page(p), win(NULL) {}
179 wxSharedPtr<wxPreferencesPage> page;
182 // All pages. Use shared pointer to be able to get pointers to PageInfo structs
183 wxVector< wxSharedPtr<PageInfo> > m_pages;
185 wxToolBar *m_toolbar;
186 bool m_toolbarRealized;
187 wxWindow *m_visiblePage;
191 class wxCocoaPreferencesEditorImpl : public wxPreferencesEditorImpl
194 wxCocoaPreferencesEditorImpl() : m_win(NULL)
198 virtual ~wxCocoaPreferencesEditorImpl()
200 // m_win may already be destroyed if this destructor is called from
201 // wxApp's destructor. In that case, all windows -- including this
202 // one -- would already be destroyed by now.
207 virtual void AddPage(wxPreferencesPage* page)
209 GetWin()->AddPage(page);
212 virtual void Show(wxWindow* WXUNUSED(parent))
214 // OS X preferences windows don't have parents, they are independent
215 // windows, so we just ignore the 'parent' argument.
216 wxWindow *win = GetWin();
221 virtual void Dismiss()
223 // Don't destroy the window, only hide it, because OS X preferences
224 // window typically remember their state even when closed. Reopening
225 // the window should show it in the exact same state the user left it.
230 // Use this function to access m_win, so that the window is only created on
231 // demand when actually needed.
232 wxCocoaPrefsWindow* GetWin()
235 m_win = new wxCocoaPrefsWindow();
239 wxWeakRef<wxCocoaPrefsWindow> m_win;
243 wxPreferencesEditorImpl* wxPreferencesEditorImpl::Create()
245 return new wxCocoaPreferencesEditorImpl();
248 #endif // wxHAS_PREF_EDITOR_NATIVE