1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/toolbar.mm
4 // Author: Stefan Csomor
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 ( id == wxID_SEPARATOR )
126 [bui initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
129 else if ( bmpNormal.IsOk() )
131 [bui initWithImage:bmpNormal.GetUIImage() style:UIBarButtonItemStylePlain target:toolbar
132 action:@selector(clickedAction:)];
137 style = UIBarButtonItemStyleDone;
139 style = UIBarButtonItemStyleBordered;
141 [bui initWithTitle:wxCFStringRef(label).AsNSString() style:style target:toolbar
142 action:@selector(clickedAction:)];
146 wxToolBarToolList[bui] = this;
149 wxToolBarTool::wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
150 : wxToolBarToolBase(tbar, control, label)
153 UIBarButtonItem* bui = [UIBarButtonItem alloc];
155 [bui initWithCustomView:control->GetHandle() ];
158 wxToolBarToolList[bui] = this;
161 wxToolBarTool::~wxToolBarTool()
167 ToolBarToolMap::iterator it;
168 for ( it = wxToolBarToolList.begin(); it != wxToolBarToolList.end(); ++it )
170 if ( it->second == this )
172 wxToolBarToolList.erase(it);
181 wxToolBarToolBase *wxToolBar::CreateTool(
183 const wxString& label,
184 const wxBitmap& bmpNormal,
185 const wxBitmap& bmpDisabled,
187 wxObject *clientData,
188 const wxString& shortHelp,
189 const wxString& longHelp )
191 return new wxToolBarTool(
192 this, id, label, bmpNormal, bmpDisabled, kind,
193 clientData, shortHelp, longHelp );
197 wxToolBar::CreateTool(wxControl *control, const wxString& label)
199 return new wxToolBarTool(this, control, label);
202 void wxToolBar::Init()
210 // also for the toolbar we have the dual implementation:
211 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
213 bool wxToolBar::Create(
219 const wxString& name )
223 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
228 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
230 wxUIToolbar* toolbar = [[wxUIToolbar alloc] init];
233 switch ( [[UIApplication sharedApplication] statusBarStyle] )
236 case UIStatusBarStyleBlackOpaque:
237 toolbar.barStyle = UIBarStyleBlack;
239 case UIStatusBarStyleBlackTranslucent:
240 toolbar.barStyle = UIBarStyleBlack;
241 toolbar.translucent = YES;
245 toolbar.barStyle = UIBarStyleDefault;
248 m_macToolbar = toolbar;
250 SetPeer(new wxWidgetIPhoneImpl( this, toolbar ));
251 MacPostControlCreate(pos, size) ;
255 wxToolBar::~wxToolBar()
260 bool wxToolBar::Realize()
262 if ( !wxToolBarBase::Realize() )
269 void wxToolBar::DoLayout()
271 // TODO port back osx_cocoa layout solution
274 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
276 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
281 void wxToolBar::SetToolBitmapSize(const wxSize& size)
283 m_defaultWidth = size.x;
284 m_defaultHeight = size.y;
287 // The button size is bigger than the bitmap size
288 wxSize wxToolBar::GetToolSize() const
290 return wxSize(m_defaultWidth, m_defaultHeight);
293 void wxToolBar::SetRows(int nRows)
295 // avoid resizing the frame uselessly
296 if ( nRows != m_maxRows )
300 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
302 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
305 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
307 tool->SetNormalBitmap(bitmap);
311 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
313 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
316 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
318 tool->SetDisabledBitmap(bitmap);
320 // TODO: what to do for this one?
324 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
329 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
333 ((wxToolBarTool*)t)->DoEnable( enable );
337 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
340 wxToolBarTool *tool = (wxToolBarTool *)t;
341 if ( ( tool != NULL ) && tool->IsButton() )
342 tool->UpdateToggleImage( toggle );
346 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
348 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
352 wxSize toolSize = GetToolSize();
354 switch (tool->GetStyle())
356 case wxTOOL_STYLE_SEPARATOR:
359 case wxTOOL_STYLE_BUTTON:
362 case wxTOOL_STYLE_CONTROL:
363 // right now there's nothing to do here
370 [(wxUIToolbar*)m_macToolbar insertTool:tool->GetUIBarButtonItem() atIndex:pos];
371 InvalidateBestSize();
377 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
379 wxFAIL_MSG( wxT("not implemented") );
382 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolbase)
384 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
386 [(wxUIToolbar*)m_macToolbar removeTool:pos];
391 void wxToolBar::SetWindowStyleFlag( long style )
393 wxToolBarBase::SetWindowStyleFlag( style );
397 @implementation wxUIToolbar
401 if (!(self = [super init]))
404 mutableBarItems = [NSMutableArray arrayWithCapacity:5];
408 - (void)clickedAction:(id)sender
410 ToolBarToolMap::iterator node = wxToolBarToolList.find(sender);
412 if ( node != wxToolBarToolList.end() )
413 node->second->Action();
416 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos
418 [mutableBarItems insertObject:item atIndex:pos];
419 [super setItems:mutableBarItems];
422 - (void)removeTool:(size_t) pos
424 [mutableBarItems removeObjectAtIndex:pos];
425 [super setItems:mutableBarItems];
430 #endif // wxUSE_TOOLBAR