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