1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/toolbar.cpp
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: toolbar.cpp 54954 2008-08-03 11:27:03Z VZ $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/toolbar.h"
23 #include "wx/osx/private.h"
24 #include "wx/geometry.h"
25 #include "wx/sysopt.h"
28 #pragma mark Tool Implementation
30 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
39 @interface wxUIToolbar : UIToolbar
41 NSMutableArray* mutableBarItems;
44 - (void)clickedAction:(id)sender;
46 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos;
48 - (void)removeTool:(size_t) pos;
55 class wxToolBarTool : public wxToolBarToolBase
61 const wxString& label,
62 const wxBitmap& bmpNormal,
63 const wxBitmap& bmpDisabled,
66 const wxString& shortHelp,
67 const wxString& longHelp );
69 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label);
71 virtual ~wxToolBarTool();
75 wxToolBar *tbar = (wxToolBar*) GetToolBar();
80 shouldToggle = !IsToggled();
81 tbar->ToggleTool( GetId(), shouldToggle );
84 tbar->OnLeftClick( GetId(), IsToggled() );
87 UIBarButtonItem* GetUIBarButtonItem() const {return m_toolbarItem;}
96 UIBarButtonItem* m_toolbarItem;
97 // position in its toolbar, -1 means not inserted
101 WX_DECLARE_HASH_MAP(WX_NSObject, wxToolBarTool*, wxPointerHash, wxPointerEqual, ToolBarToolMap);
102 static ToolBarToolMap wxToolBarToolList;
104 wxToolBarTool::wxToolBarTool(
107 const wxString& label,
108 const wxBitmap& bmpNormal,
109 const wxBitmap& bmpDisabled,
111 wxObject *clientData,
112 const wxString& shortHelp,
113 const wxString& longHelp )
116 tbar, id, label, bmpNormal, bmpDisabled, kind,
117 clientData, shortHelp, longHelp )
120 UIBarButtonItem* bui = [UIBarButtonItem alloc];
121 UIBarButtonItemStyle style = UIBarButtonItemStylePlain;
122 wxUIToolbar* toolbar = (wxUIToolbar*) tbar->GetHandle();
124 if ( bmpNormal.Ok() )
126 [bui initWithImage:bmpNormal.GetUIImage() style:UIBarButtonItemStylePlain target:toolbar
127 action:@selector(clickedAction:)];
132 style = UIBarButtonItemStyleDone;
134 style = UIBarButtonItemStyleBordered;
136 [bui initWithTitle:wxCFStringRef(label).AsNSString() style:style target:toolbar
137 action:@selector(clickedAction:)];
141 wxToolBarToolList[bui] = this;
144 wxToolBarTool::wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
145 : wxToolBarToolBase(tbar, control, label)
148 UIBarButtonItem* bui = [UIBarButtonItem alloc];
150 [bui initWithCustomView:control->GetHandle() ];
153 wxToolBarToolList[bui] = this;
156 wxToolBarTool::~wxToolBarTool()
162 ToolBarToolMap::iterator it;
163 for ( it = wxToolBarToolList.begin(); it != wxToolBarToolList.end(); ++it )
165 if ( it->second == this )
167 wxToolBarToolList.erase(it);
176 wxToolBarToolBase *wxToolBar::CreateTool(
178 const wxString& label,
179 const wxBitmap& bmpNormal,
180 const wxBitmap& bmpDisabled,
182 wxObject *clientData,
183 const wxString& shortHelp,
184 const wxString& longHelp )
186 return new wxToolBarTool(
187 this, id, label, bmpNormal, bmpDisabled, kind,
188 clientData, shortHelp, longHelp );
192 wxToolBar::CreateTool(wxControl *control, const wxString& label)
194 return new wxToolBarTool(this, control, label);
197 void wxToolBar::Init()
205 // also for the toolbar we have the dual implementation:
206 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
208 bool wxToolBar::Create(
214 const wxString& name )
216 m_macIsUserPane = false ;
218 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
223 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
225 wxUIToolbar* toolbar = [[wxUIToolbar alloc] init];
228 switch ( [[UIApplication sharedApplication] statusBarStyle] )
230 case UIStatusBarStyleBlackOpaque:
231 toolbar.barStyle = UIBarStyleBlack;
233 case UIStatusBarStyleBlackTranslucent:
234 toolbar.barStyle = UIBarStyleBlack;
235 toolbar.translucent = YES;
238 toolbar.barStyle = UIBarStyleDefault;
241 m_macToolbar = toolbar;
243 m_peer = new wxWidgetIPhoneImpl( this, toolbar );
244 MacPostControlCreate(pos, size) ;
245 NSLog(@"toolbar was created %@",toolbar);
248 wxToolBar::~wxToolBar()
253 bool wxToolBar::Realize()
255 if ( !wxToolBarBase::Realize() )
262 void wxToolBar::SetToolBitmapSize(const wxSize& size)
264 m_defaultWidth = size.x;
265 m_defaultHeight = size.y;
268 // The button size is bigger than the bitmap size
269 wxSize wxToolBar::GetToolSize() const
271 return wxSize(m_defaultWidth, m_defaultHeight);
274 void wxToolBar::SetRows(int nRows)
276 // avoid resizing the frame uselessly
277 if ( nRows != m_maxRows )
281 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
283 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
286 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
288 tool->SetNormalBitmap(bitmap);
292 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
294 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
297 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
299 tool->SetDisabledBitmap(bitmap);
301 // TODO: what to do for this one?
305 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
310 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
314 ((wxToolBarTool*)t)->DoEnable( enable );
318 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
321 wxToolBarTool *tool = (wxToolBarTool *)t;
322 if ( ( tool != NULL ) && tool->IsButton() )
323 tool->UpdateToggleImage( toggle );
327 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
329 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
333 wxSize toolSize = GetToolSize();
335 switch (tool->GetStyle())
337 case wxTOOL_STYLE_SEPARATOR:
340 case wxTOOL_STYLE_BUTTON:
343 case wxTOOL_STYLE_CONTROL:
344 // right now there's nothing to do here
351 [(wxUIToolbar*)m_macToolbar insertTool:tool->GetUIBarButtonItem() atIndex:pos];
352 InvalidateBestSize();
358 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
360 wxFAIL_MSG( wxT("not implemented") );
363 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolbase)
365 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
367 [(wxUIToolbar*)m_macToolbar removeTool:pos];
372 void wxToolBar::SetWindowStyleFlag( long style )
374 wxToolBarBase::SetWindowStyleFlag( style );
378 @implementation wxUIToolbar
382 if (!(self = [super init]))
385 mutableBarItems = [NSMutableArray arrayWithCapacity:5];
389 - (void)clickedAction:(id)sender
391 ToolBarToolMap::iterator node = wxToolBarToolList.find(sender);
393 if ( node != wxToolBarToolList.end() )
394 node->second->Action();
397 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos
399 [mutableBarItems insertObject:item atIndex:pos];
400 [super setItems:mutableBarItems];
403 - (void)removeTool:(size_t) pos
405 [mutableBarItems removeObjectAtIndex:pos];
406 [super setItems:mutableBarItems];
411 #endif // wxUSE_TOOLBAR