]> git.saurik.com Git - wxWidgets.git/blob - src/osx/iphone/toolbar.mm
no existing on 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 }
246
247 wxToolBar::~wxToolBar()
248 {
249 m_macToolbar = NULL;
250 }
251
252 bool wxToolBar::Realize()
253 {
254 if ( !wxToolBarBase::Realize() )
255 return false;
256
257
258 return true;
259 }
260
261 void wxToolBar::SetToolBitmapSize(const wxSize& size)
262 {
263 m_defaultWidth = size.x;
264 m_defaultHeight = size.y;
265 }
266
267 // The button size is bigger than the bitmap size
268 wxSize wxToolBar::GetToolSize() const
269 {
270 return wxSize(m_defaultWidth, m_defaultHeight);
271 }
272
273 void wxToolBar::SetRows(int nRows)
274 {
275 // avoid resizing the frame uselessly
276 if ( nRows != m_maxRows )
277 m_maxRows = nRows;
278 }
279
280 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
281 {
282 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
283 if ( tool )
284 {
285 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
286
287 tool->SetNormalBitmap(bitmap);
288 }
289 }
290
291 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
292 {
293 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
294 if ( tool )
295 {
296 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
297
298 tool->SetDisabledBitmap(bitmap);
299
300 // TODO: what to do for this one?
301 }
302 }
303
304 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
305 {
306 return NULL;
307 }
308
309 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
310 {
311 /*
312 if ( t != NULL )
313 ((wxToolBarTool*)t)->DoEnable( enable );
314 */
315 }
316
317 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
318 {
319 /*
320 wxToolBarTool *tool = (wxToolBarTool *)t;
321 if ( ( tool != NULL ) && tool->IsButton() )
322 tool->UpdateToggleImage( toggle );
323 */
324 }
325
326 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
327 {
328 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
329 if (tool == NULL)
330 return false;
331
332 wxSize toolSize = GetToolSize();
333
334 switch (tool->GetStyle())
335 {
336 case wxTOOL_STYLE_SEPARATOR:
337 break;
338
339 case wxTOOL_STYLE_BUTTON:
340 break;
341
342 case wxTOOL_STYLE_CONTROL:
343 // right now there's nothing to do here
344 break;
345
346 default:
347 break;
348 }
349
350 [(wxUIToolbar*)m_macToolbar insertTool:tool->GetUIBarButtonItem() atIndex:pos];
351 InvalidateBestSize();
352
353 return true;
354
355 }
356
357 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
358 {
359 wxFAIL_MSG( wxT("not implemented") );
360 }
361
362 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolbase)
363 {
364 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
365
366 [(wxUIToolbar*)m_macToolbar removeTool:pos];
367
368 return true;
369 }
370
371 void wxToolBar::SetWindowStyleFlag( long style )
372 {
373 wxToolBarBase::SetWindowStyleFlag( style );
374
375 }
376
377 @implementation wxUIToolbar
378
379 - (id)init
380 {
381 if (!(self = [super init]))
382 return nil;
383
384 mutableBarItems = [NSMutableArray arrayWithCapacity:5];
385 return self;
386 }
387
388 - (void)clickedAction:(id)sender
389 {
390 ToolBarToolMap::iterator node = wxToolBarToolList.find(sender);
391
392 if ( node != wxToolBarToolList.end() )
393 node->second->Action();
394 }
395
396 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos
397 {
398 [mutableBarItems insertObject:item atIndex:pos];
399 [super setItems:mutableBarItems];
400 }
401
402 - (void)removeTool:(size_t) pos
403 {
404 [mutableBarItems removeObjectAtIndex:pos];
405 [super setItems:mutableBarItems];
406 }
407
408 @end
409
410 #endif // wxUSE_TOOLBAR