]> git.saurik.com Git - wxWidgets.git/blob - src/osx/iphone/toolbar.mm
Do not use GTKIsUsingGlobalMenu when compiled against gtk1.x
[wxWidgets.git] / src / osx / iphone / toolbar.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/toolbar.mm
3 // Purpose: wxToolBar
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
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 ( id == wxID_SEPARATOR )
125 {
126 [bui initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
127 bui.width = 25.0f;
128 }
129 else if ( bmpNormal.IsOk() )
130 {
131 [bui initWithImage:bmpNormal.GetUIImage() style:UIBarButtonItemStylePlain target:toolbar
132 action:@selector(clickedAction:)];
133 }
134 else
135 {
136 if ( id == wxID_OK )
137 style = UIBarButtonItemStyleDone;
138 else
139 style = UIBarButtonItemStyleBordered;
140
141 [bui initWithTitle:wxCFStringRef(label).AsNSString() style:style target:toolbar
142 action:@selector(clickedAction:)];
143 }
144
145 m_toolbarItem = bui;
146 wxToolBarToolList[bui] = this;
147 }
148
149 wxToolBarTool::wxToolBarTool(wxToolBar *tbar, wxControl *control, const wxString& label)
150 : wxToolBarToolBase(tbar, control, label)
151 {
152 Init();
153 UIBarButtonItem* bui = [UIBarButtonItem alloc];
154
155 [bui initWithCustomView:control->GetHandle() ];
156
157 m_toolbarItem = bui;
158 wxToolBarToolList[bui] = this;
159 }
160
161 wxToolBarTool::~wxToolBarTool()
162 {
163 bool found = true ;
164 while ( found )
165 {
166 found = false ;
167 ToolBarToolMap::iterator it;
168 for ( it = wxToolBarToolList.begin(); it != wxToolBarToolList.end(); ++it )
169 {
170 if ( it->second == this )
171 {
172 wxToolBarToolList.erase(it);
173 found = true ;
174 break;
175 }
176 }
177 }
178 }
179
180
181 wxToolBarToolBase *wxToolBar::CreateTool(
182 int id,
183 const wxString& label,
184 const wxBitmap& bmpNormal,
185 const wxBitmap& bmpDisabled,
186 wxItemKind kind,
187 wxObject *clientData,
188 const wxString& shortHelp,
189 const wxString& longHelp )
190 {
191 return new wxToolBarTool(
192 this, id, label, bmpNormal, bmpDisabled, kind,
193 clientData, shortHelp, longHelp );
194 }
195
196 wxToolBarToolBase *
197 wxToolBar::CreateTool(wxControl *control, const wxString& label)
198 {
199 return new wxToolBarTool(this, control, label);
200 }
201
202 void wxToolBar::Init()
203 {
204 m_maxWidth = -1;
205 m_maxHeight = -1;
206
207 m_macToolbar = NULL;
208 }
209
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
212
213 bool wxToolBar::Create(
214 wxWindow *parent,
215 wxWindowID id,
216 const wxPoint& pos,
217 const wxSize& size,
218 long style,
219 const wxString& name )
220 {
221 DontCreatePeer();
222
223 if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) )
224 return false;
225
226 FixupStyle();
227
228 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
229
230 wxUIToolbar* toolbar = [[wxUIToolbar alloc] init];
231 [toolbar sizeToFit];
232
233 switch ( [[UIApplication sharedApplication] statusBarStyle] )
234 {
235 #ifdef __IPHONE_3_0
236 case UIStatusBarStyleBlackOpaque:
237 toolbar.barStyle = UIBarStyleBlack;
238 break;
239 case UIStatusBarStyleBlackTranslucent:
240 toolbar.barStyle = UIBarStyleBlack;
241 toolbar.translucent = YES;
242 break;
243 #endif
244 default:
245 toolbar.barStyle = UIBarStyleDefault;
246 break;
247 }
248 m_macToolbar = toolbar;
249
250 SetPeer(new wxWidgetIPhoneImpl( this, toolbar ));
251 MacPostControlCreate(pos, size) ;
252 return true;
253 }
254
255 wxToolBar::~wxToolBar()
256 {
257 m_macToolbar = NULL;
258 }
259
260 bool wxToolBar::Realize()
261 {
262 if ( !wxToolBarBase::Realize() )
263 return false;
264
265
266 return true;
267 }
268
269 void wxToolBar::DoLayout()
270 {
271 // TODO port back osx_cocoa layout solution
272 }
273
274 void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags)
275 {
276 wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags);
277
278 DoLayout();
279 }
280
281 void wxToolBar::SetToolBitmapSize(const wxSize& size)
282 {
283 m_defaultWidth = size.x;
284 m_defaultHeight = size.y;
285 }
286
287 // The button size is bigger than the bitmap size
288 wxSize wxToolBar::GetToolSize() const
289 {
290 return wxSize(m_defaultWidth, m_defaultHeight);
291 }
292
293 void wxToolBar::SetRows(int nRows)
294 {
295 // avoid resizing the frame uselessly
296 if ( nRows != m_maxRows )
297 m_maxRows = nRows;
298 }
299
300 void wxToolBar::SetToolNormalBitmap( int id, const wxBitmap& bitmap )
301 {
302 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
303 if ( tool )
304 {
305 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
306
307 tool->SetNormalBitmap(bitmap);
308 }
309 }
310
311 void wxToolBar::SetToolDisabledBitmap( int id, const wxBitmap& bitmap )
312 {
313 wxToolBarTool* tool = static_cast<wxToolBarTool*>(FindById(id));
314 if ( tool )
315 {
316 wxCHECK_RET( tool->IsButton(), wxT("Can only set bitmap on button tools."));
317
318 tool->SetDisabledBitmap(bitmap);
319
320 // TODO: what to do for this one?
321 }
322 }
323
324 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
325 {
326 return NULL;
327 }
328
329 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
330 {
331 /*
332 if ( t != NULL )
333 ((wxToolBarTool*)t)->DoEnable( enable );
334 */
335 }
336
337 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
338 {
339 /*
340 wxToolBarTool *tool = (wxToolBarTool *)t;
341 if ( ( tool != NULL ) && tool->IsButton() )
342 tool->UpdateToggleImage( toggle );
343 */
344 }
345
346 bool wxToolBar::DoInsertTool(size_t pos, wxToolBarToolBase *toolBase)
347 {
348 wxToolBarTool *tool = static_cast< wxToolBarTool*>(toolBase );
349 if (tool == NULL)
350 return false;
351
352 wxSize toolSize = GetToolSize();
353
354 switch (tool->GetStyle())
355 {
356 case wxTOOL_STYLE_SEPARATOR:
357 break;
358
359 case wxTOOL_STYLE_BUTTON:
360 break;
361
362 case wxTOOL_STYLE_CONTROL:
363 // right now there's nothing to do here
364 break;
365
366 default:
367 break;
368 }
369
370 [(wxUIToolbar*)m_macToolbar insertTool:tool->GetUIBarButtonItem() atIndex:pos];
371 InvalidateBestSize();
372
373 return true;
374
375 }
376
377 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
378 {
379 wxFAIL_MSG( wxT("not implemented") );
380 }
381
382 bool wxToolBar::DoDeleteTool(size_t pos, wxToolBarToolBase *toolbase)
383 {
384 wxToolBarTool* tool = static_cast< wxToolBarTool*>(toolbase );
385
386 [(wxUIToolbar*)m_macToolbar removeTool:pos];
387
388 return true;
389 }
390
391 void wxToolBar::SetWindowStyleFlag( long style )
392 {
393 wxToolBarBase::SetWindowStyleFlag( style );
394
395 }
396
397 @implementation wxUIToolbar
398
399 - (id)init
400 {
401 if (!(self = [super init]))
402 return nil;
403
404 mutableBarItems = [NSMutableArray arrayWithCapacity:5];
405 return self;
406 }
407
408 - (void)clickedAction:(id)sender
409 {
410 ToolBarToolMap::iterator node = wxToolBarToolList.find(sender);
411
412 if ( node != wxToolBarToolList.end() )
413 node->second->Action();
414 }
415
416 - (void)insertTool:(UIBarButtonItem*) item atIndex:(size_t) pos
417 {
418 [mutableBarItems insertObject:item atIndex:pos];
419 [super setItems:mutableBarItems];
420 }
421
422 - (void)removeTool:(size_t) pos
423 {
424 [mutableBarItems removeObjectAtIndex:pos];
425 [super setItems:mutableBarItems];
426 }
427
428 @end
429
430 #endif // wxUSE_TOOLBAR