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