]> git.saurik.com Git - wxWidgets.git/blob - src/osx/iphone/toolbar.mm
adding toolbar implementation for iphone
[wxWidgets.git] / src / osx / iphone / toolbar.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/toolbar.cpp
3 // Purpose: wxToolBar
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id: toolbar.cpp 54954 2008-08-03 11:27:03Z VZ $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_TOOLBAR
15
16 #include "wx/toolbar.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21
22 #include "wx/app.h"
23 #include "wx/osx/private.h"
24 #include "wx/geometry.h"
25 #include "wx/sysopt.h"
26
27 #pragma mark -
28 #pragma mark Tool Implementation
29
30 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
31 END_EVENT_TABLE()
32
33 // ----------------------------------------------------------------------------
34 // private classes
35 // ----------------------------------------------------------------------------
36
37 class wxToolBarTool;
38
39 @interface wxUIToolbar : UIToolbar
40 {
41 NSMutableArray* mutableBarItems;
42 }
43
44 - (void)clickedAction:(id)sender;
45
46 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos;
47
48 - (void)removeTool:(size_t) pos;
49
50 - (id)init;
51
52 @end
53
54
55 class wxToolBarTool : public wxToolBarToolBase
56 {
57 public:
58 wxToolBarTool(
59 wxToolBar *tbar,
60 int id,
61 const wxString& label,
62 const wxBitmap& bmpNormal,
63 const wxBitmap& bmpDisabled,
64 wxItemKind kind,
65 wxObject *clientData,
66 const wxString& shortHelp,
67 const wxString& longHelp );
68
69 wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label);
70
71 virtual ~wxToolBarTool();
72
73 void Action()
74 {
75 wxToolBar *tbar = (wxToolBar*) GetToolBar();
76 if (CanBeToggled())
77 {
78 bool shouldToggle;
79
80 shouldToggle = !IsToggled();
81 tbar->ToggleTool( GetId(), shouldToggle );
82 }
83
84 tbar->OnLeftClick( GetId(), IsToggled() );
85 }
86
87 UIBarButtonItem* GetUIBarButtonItem() const {return m_toolbarItem;}
88 private:
89
90 void Init()
91 {
92 m_toolbarItem = NULL;
93 m_index = -1;
94 }
95
96 UIBarButtonItem* m_toolbarItem;
97 // position in its toolbar, -1 means not inserted
98 CFIndex m_index;
99 };
100
101 WX_DECLARE_HASH_MAP(WX_NSObject, wxToolBarTool*, wxPointerHash, wxPointerEqual, ToolBarToolMap);
102 static ToolBarToolMap wxToolBarToolList;
103
104 wxToolBarTool::wxToolBarTool(
105 wxToolBar *tbar,
106 int id,
107 const wxString& label,
108 const wxBitmap& bmpNormal,
109 const wxBitmap& bmpDisabled,
110 wxItemKind kind,
111 wxObject *clientData,
112 const wxString& shortHelp,
113 const wxString& longHelp )
114 :
115 wxToolBarToolBase(
116 tbar, id, label, bmpNormal, bmpDisabled, kind,
117 clientData, shortHelp, longHelp )
118 {
119 Init();
120 UIBarButtonItem* bui = [UIBarButtonItem alloc];
121 UIBarButtonItemStyle style = UIBarButtonItemStylePlain;
122 wxUIToolbar* toolbar = (wxUIToolbar*) tbar->GetHandle();
123
124 if ( bmpNormal.Ok() )
125 {
126 [bui initWithImage:bmpNormal.GetUIImage() style:UIBarButtonItemStylePlain target:toolbar
127 action:@selector(clickedAction:)];
128 }
129 else
130 {
131 if ( id == wxID_OK )
132 style = UIBarButtonItemStyleDone;
133 else
134 style = UIBarButtonItemStyleBordered;
135
136 [bui initWithTitle:wxCFStringRef(label).AsNSString() style:style target:toolbar
137 action:@selector(clickedAction:)];
138 }
139
140 m_toolbarItem = bui;
141 wxToolBarToolList[bui] = this;
142 }
143
144 wxToolBarTool::wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
145 : wxToolBarToolBase(tbar, control, label)
146 {
147 Init();
148 UIBarButtonItem* bui = [UIBarButtonItem alloc];
149
150 [bui initWithCustomView:control->GetHandle() ];
151
152 m_toolbarItem = bui;
153 wxToolBarToolList[bui] = this;
154 }
155
156 wxToolBarTool::~wxToolBarTool()
157 {
158 bool found = true ;
159 while ( found )
160 {
161 found = false ;
162 ToolBarToolMap::iterator it;
163 for ( it = wxToolBarToolList.begin(); it != wxToolBarToolList.end(); ++it )
164 {
165 if ( it->second == this )
166 {
167 wxToolBarToolList.erase(it);
168 found = true ;
169 break;
170 }
171 }
172 }
173 }
174
175
176 wxToolBarToolBase *wxToolBar::CreateTool(
177 int id,
178 const wxString& label,
179 const wxBitmap& bmpNormal,
180 const wxBitmap& bmpDisabled,
181 wxItemKind kind,
182 wxObject *clientData,
183 const wxString& shortHelp,
184 const wxString& longHelp )
185 {
186 return new wxToolBarTool(
187 this, id, label, bmpNormal, bmpDisabled, kind,
188 clientData, shortHelp, longHelp );
189 }
190
191 wxToolBarToolBase *
192 wxToolBar::CreateTool(wxControl *control, const wxString& label)
193 {
194 return new wxToolBarTool(this, control, label);
195 }
196
197 void wxToolBar::Init()
198 {
199 m_maxWidth = -1;
200 m_maxHeight = -1;
201
202 m_macToolbar = NULL;
203 }
204
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
207
208 bool wxToolBar::Create(
209 wxWindow *parent,
210 wxWindowID id,
211 const wxPoint& pos,
212 const wxSize& size,
213 long style,
214 const wxString& name )
215 {
216 m_macIsUserPane = false ;
217
218 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
219 return false;
220
221 FixupStyle();
222
223 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
224
225 wxUIToolbar* toolbar = [[wxUIToolbar alloc] init];
226 [toolbar sizeToFit];
227
228 switch ( [[UIApplication sharedApplication] statusBarStyle] )
229 {
230 case UIStatusBarStyleBlackOpaque:
231 toolbar.barStyle = UIBarStyleBlack;
232 break;
233 case UIStatusBarStyleBlackTranslucent:
234 toolbar.barStyle = UIBarStyleBlack;
235 toolbar.translucent = YES;
236 break;
237 default:
238 toolbar.barStyle = UIBarStyleDefault;
239 break;
240 }
241 m_macToolbar = toolbar;
242
243 m_peer = new wxWidgetIPhoneImpl( this, toolbar );
244 MacPostControlCreate(pos, size) ;
245 NSLog(@"toolbar was created %@",toolbar);
246 }
247
248 wxToolBar::~wxToolBar()
249 {
250 m_macToolbar = NULL;
251 }
252
253 bool wxToolBar::Realize()
254 {
255 if ( !wxToolBarBase::Realize() )
256 return false;
257
258
259 return true;
260 }
261
262 void wxToolBar::SetToolBitmapSize(const wxSize& size)
263 {
264 m_defaultWidth = size.x;
265 m_defaultHeight = size.y;
266 }
267
268 // The button size is bigger than the bitmap size
269 wxSize wxToolBar::GetToolSize() const
270 {
271 return wxSize(m_defaultWidth, m_defaultHeight);
272 }
273
274 void wxToolBar::SetRows(int nRows)
275 {
276 // avoid resizing the frame uselessly
277 if ( nRows != m_maxRows )
278 m_maxRows = nRows;
279 }
280
281 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
282 {
283 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
284 if ( tool )
285 {
286 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
287
288 tool->SetNormalBitmap(bitmap);
289 }
290 }
291
292 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
293 {
294 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
295 if ( tool )
296 {
297 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
298
299 tool->SetDisabledBitmap(bitmap);
300
301 // TODO: what to do for this one?
302 }
303 }
304
305 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
306 {
307 return NULL;
308 }
309
310 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
311 {
312 /*
313 if ( t != NULL )
314 ((wxToolBarTool*)t)->DoEnable( enable );
315 */
316 }
317
318 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
319 {
320 /*
321 wxToolBarTool *tool = (wxToolBarTool *)t;
322 if ( ( tool != NULL ) && tool->IsButton() )
323 tool->UpdateToggleImage( toggle );
324 */
325 }
326
327 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
328 {
329 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
330 if (tool == NULL)
331 return false;
332
333 wxSize toolSize = GetToolSize();
334
335 switch (tool->GetStyle())
336 {
337 case wxTOOL_STYLE_SEPARATOR:
338 break;
339
340 case wxTOOL_STYLE_BUTTON:
341 break;
342
343 case wxTOOL_STYLE_CONTROL:
344 // right now there's nothing to do here
345 break;
346
347 default:
348 break;
349 }
350
351 [(wxUIToolbar*)m_macToolbar insertTool:tool->GetUIBarButtonItem() atIndex:pos];
352 InvalidateBestSize();
353
354 return true;
355
356 }
357
358 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
359 {
360 wxFAIL_MSG( wxT("not implemented") );
361 }
362
363 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolbase)
364 {
365 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
366
367 [(wxUIToolbar*)m_macToolbar removeTool:pos];
368
369 return true;
370 }
371
372 void wxToolBar::SetWindowStyleFlag( long style )
373 {
374 wxToolBarBase::SetWindowStyleFlag( style );
375
376 }
377
378 @implementation wxUIToolbar
379
380 - (id)init
381 {
382 if (!(self = [super init]))
383 return nil;
384
385 mutableBarItems = [NSMutableArray arrayWithCapacity:5];
386 return self;
387 }
388
389 - (void)clickedAction:(id)sender
390 {
391 ToolBarToolMap::iterator node = wxToolBarToolList.find(sender);
392
393 if ( node != wxToolBarToolList.end() )
394 node->second->Action();
395 }
396
397 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos
398 {
399 [mutableBarItems insertObject:item atIndex:pos];
400 [super setItems:mutableBarItems];
401 }
402
403 - (void)removeTool:(size_t) pos
404 {
405 [mutableBarItems removeObjectAtIndex:pos];
406 [super setItems:mutableBarItems];
407 }
408
409 @end
410
411 #endif // wxUSE_TOOLBAR