From: Václav Slavík <vslavik@fastmail.fm>
Date: Thu, 28 Feb 2013 16:28:56 +0000 (+0000)
Subject: Add support for Cocoa's selectable toolbar items.
X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/d52a1abad31942f32364bb40808150c4be6c56cd

Add support for Cocoa's selectable toolbar items.

The support is currently limited to making all toolbar items selectable
and is only available in wxOSX with Cocoa and native toolbars.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73575 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---

diff --git a/include/wx/osx/toolbar.h b/include/wx/osx/toolbar.h
index 4bed1ab647..c40b709e05 100644
--- a/include/wx/osx/toolbar.h
+++ b/include/wx/osx/toolbar.h
@@ -72,6 +72,10 @@ class WXDLLIMPEXP_CORE wxToolBar: public wxToolBarBase
 #endif
 
 #if wxOSX_USE_NATIVE_TOOLBAR
+    // make all tools selectable
+    void OSXSetSelectableTools(bool set);
+    void OSXSelectTool(int toolId);
+
     bool MacInstallNativeToolbar(bool usesNative);
     void MacUninstallNativeToolbar();
     bool MacWantsNativeToolbar();
diff --git a/src/osx/cocoa/toolbar.mm b/src/osx/cocoa/toolbar.mm
index 4293801329..1a989ddd6c 100644
--- a/src/osx/cocoa/toolbar.mm
+++ b/src/osx/cocoa/toolbar.mm
@@ -305,8 +305,11 @@ private:
 
 @interface wxNSToolbarDelegate : NSObject wxOSX_10_6_AND_LATER(<NSToolbarDelegate>)
 {
+    bool m_isSelectable;
 }
 
+- (void)setSelectable:(bool) value;
+
 - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag;
 
 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar;
@@ -363,6 +366,17 @@ private:
 
 @implementation wxNSToolbarDelegate
 
+- (id)init
+{
+    m_isSelectable = false;
+    return [super init];
+}
+
+- (void)setSelectable:(bool) value
+{
+    m_isSelectable = true;
+}
+
 - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
 {
     wxUnusedVar(toolbar);
@@ -377,8 +391,10 @@ private:
 
 - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
 {
-    wxUnusedVar(toolbar);
-    return nil;
+  if ( m_isSelectable )
+      return [[toolbar items] valueForKey:@"itemIdentifier"];
+  else
+      return nil;
 }
 
 - (NSToolbarItem*) toolbar:(NSToolbar*) toolbar itemForItemIdentifier:(NSString*) itemIdentifier willBeInsertedIntoToolbar:(BOOL) flag
@@ -1609,4 +1625,23 @@ void wxToolBar::OnPaint(wxPaintEvent& event)
     event.Skip();
 }
 
+#if wxOSX_USE_NATIVE_TOOLBAR
+void wxToolBar::OSXSetSelectableTools(bool set)
+{
+    wxCHECK_RET( m_macToolbar, "toolbar must be non-NULL" );
+    [(wxNSToolbarDelegate*)[(NSToolbar*)m_macToolbar delegate] setSelectable:set];
+}
+
+void wxToolBar::OSXSelectTool(int toolId)
+{
+    wxToolBarToolBase *tool = FindById(toolId);
+    wxCHECK_RET( tool, "invalid tool ID" );
+    wxCHECK_RET( m_macToolbar, "toolbar must be non-NULL" );
+
+    wxString identifier = wxString::Format(wxT("%ld"), (long)tool);
+    wxCFStringRef cfidentifier(identifier, wxFont::GetDefaultEncoding());
+    [(NSToolbar*)m_macToolbar setSelectedItemIdentifier:cfidentifier.AsNSString()];
+}
+#endif // wxOSX_USE_NATIVE_TOOLBAR
+
 #endif // wxUSE_TOOLBAR