1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/toolbar.mm
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/toolbar.h"
22 #include "wx/osx/private.h"
23 #include "wx/geometry.h"
24 #include "wx/sysopt.h"
27 #pragma mark Tool Implementation
29 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
38 @interface wxUIToolbar : UIToolbar
40 NSMutableArray* mutableBarItems;
43 - (void)clickedAction:(id)sender;
45 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos;
47 - (void)removeTool:(size_t) pos;
54 class wxToolBarTool : public wxToolBarToolBase
60 const wxString& label,
61 const wxBitmap& bmpNormal,
62 const wxBitmap& bmpDisabled,
65 const wxString& shortHelp,
66 const wxString& longHelp );
68 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label);
70 virtual ~wxToolBarTool();
74 wxToolBar *tbar = (wxToolBar*) GetToolBar();
79 shouldToggle = !IsToggled();
80 tbar->ToggleTool( GetId(), shouldToggle );
83 tbar->OnLeftClick( GetId(), IsToggled() );
86 UIBarButtonItem* GetUIBarButtonItem() const {return m_toolbarItem;}
95 UIBarButtonItem* m_toolbarItem;
96 // position in its toolbar, -1 means not inserted
100 WX_DECLARE_HASH_MAP(WX_NSObject, wxToolBarTool*, wxPointerHash, wxPointerEqual, ToolBarToolMap);
101 static ToolBarToolMap wxToolBarToolList;
103 wxToolBarTool::wxToolBarTool(
106 const wxString& label,
107 const wxBitmap& bmpNormal,
108 const wxBitmap& bmpDisabled,
110 wxObject *clientData,
111 const wxString& shortHelp,
112 const wxString& longHelp )
115 tbar, id, label, bmpNormal, bmpDisabled, kind,
116 clientData, shortHelp, longHelp )
119 UIBarButtonItem* bui = [UIBarButtonItem alloc];
120 UIBarButtonItemStyle style = UIBarButtonItemStylePlain;
121 wxUIToolbar* toolbar = (wxUIToolbar*) tbar->GetHandle();
123 if ( id == wxID_SEPARATOR )
125 [bui initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
128 else if ( bmpNormal.IsOk() )
130 [bui initWithImage:bmpNormal.GetUIImage() style:UIBarButtonItemStylePlain target:toolbar
131 action:@selector(clickedAction:)];
136 style = UIBarButtonItemStyleDone;
138 style = UIBarButtonItemStyleBordered;
140 [bui initWithTitle:wxCFStringRef(label).AsNSString() style:style target:toolbar
141 action:@selector(clickedAction:)];
145 wxToolBarToolList[bui] = this;
148 wxToolBarTool::wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
149 : wxToolBarToolBase(tbar, control, label)
152 UIBarButtonItem* bui = [UIBarButtonItem alloc];
154 [bui initWithCustomView:control->GetHandle() ];
157 wxToolBarToolList[bui] = this;
160 wxToolBarTool::~wxToolBarTool()
166 ToolBarToolMap::iterator it;
167 for ( it = wxToolBarToolList.begin(); it != wxToolBarToolList.end(); ++it )
169 if ( it->second == this )
171 wxToolBarToolList.erase(it);
180 wxToolBarToolBase *wxToolBar::CreateTool(
182 const wxString& label,
183 const wxBitmap& bmpNormal,
184 const wxBitmap& bmpDisabled,
186 wxObject *clientData,
187 const wxString& shortHelp,
188 const wxString& longHelp )
190 return new wxToolBarTool(
191 this, id, label, bmpNormal, bmpDisabled, kind,
192 clientData, shortHelp, longHelp );
196 wxToolBar::CreateTool(wxControl *control, const wxString& label)
198 return new wxToolBarTool(this, control, label);
201 void wxToolBar::Init()
209 // also for the toolbar we have the dual implementation:
210 // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar
212 bool wxToolBar::Create(
218 const wxString& name )
222 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
227 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
229 wxUIToolbar* toolbar = [[wxUIToolbar alloc] init];
232 switch ( [[UIApplication sharedApplication] statusBarStyle] )
235 case UIStatusBarStyleBlackOpaque:
236 toolbar.barStyle = UIBarStyleBlack;
238 case UIStatusBarStyleBlackTranslucent:
239 toolbar.barStyle = UIBarStyleBlack;
240 toolbar.translucent = YES;
244 toolbar.barStyle = UIBarStyleDefault;
247 m_macToolbar = toolbar;
249 SetPeer(new wxWidgetIPhoneImpl( this, toolbar ));
250 MacPostControlCreate(pos, size) ;
254 wxToolBar::~wxToolBar()
259 bool wxToolBar::Realize()
261 if ( !wxToolBarBase::Realize() )
268 void wxToolBar::DoLayout()
270 // TODO port back osx_cocoa layout solution
273 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
275 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
280 void wxToolBar::SetToolBitmapSize(const wxSize& size)
282 m_defaultWidth = size.x;
283 m_defaultHeight = size.y;
286 // The button size is bigger than the bitmap size
287 wxSize wxToolBar::GetToolSize() const
289 return wxSize(m_defaultWidth, m_defaultHeight);
292 void wxToolBar::SetRows(int nRows)
294 // avoid resizing the frame uselessly
295 if ( nRows != m_maxRows )
299 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
301 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
304 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
306 tool->SetNormalBitmap(bitmap);
310 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
312 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
315 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
317 tool->SetDisabledBitmap(bitmap);
319 // TODO: what to do for this one?
323 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
328 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
332 ((wxToolBarTool*)t)->DoEnable( enable );
336 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
339 wxToolBarTool *tool = (wxToolBarTool *)t;
340 if ( ( tool != NULL ) && tool->IsButton() )
341 tool->UpdateToggleImage( toggle );
345 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
347 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
351 wxSize toolSize = GetToolSize();
353 switch (tool->GetStyle())
355 case wxTOOL_STYLE_SEPARATOR:
358 case wxTOOL_STYLE_BUTTON:
361 case wxTOOL_STYLE_CONTROL:
362 // right now there's nothing to do here
369 [(wxUIToolbar*)m_macToolbar insertTool:tool->GetUIBarButtonItem() atIndex:pos];
370 InvalidateBestSize();
376 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
378 wxFAIL_MSG( wxT("not implemented") );
381 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolbase)
383 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
385 [(wxUIToolbar*)m_macToolbar removeTool:pos];
390 void wxToolBar::SetWindowStyleFlag( long style )
392 wxToolBarBase::SetWindowStyleFlag( style );
396 @implementation wxUIToolbar
400 if (!(self = [super init]))
403 mutableBarItems = [NSMutableArray arrayWithCapacity:5];
407 - (void)clickedAction:(id)sender
409 ToolBarToolMap::iterator node = wxToolBarToolList.find(sender);
411 if ( node != wxToolBarToolList.end() )
412 node->second->Action();
415 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos
417 [mutableBarItems insertObject:item atIndex:pos];
418 [super setItems:mutableBarItems];
421 - (void)removeTool:(size_t) pos
423 [mutableBarItems removeObjectAtIndex:pos];
424 [super setItems:mutableBarItems];
429 #endif // wxUSE_TOOLBAR