]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
applied patch 923858 (fixes crash in zlib streams)
[wxWidgets.git] / src / mac / carbon / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: toolbar.cpp
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: The wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "toolbar.h"
14 #endif
15
16 #include "wx/wx.h"
17
18 #if wxUSE_TOOLBAR
19
20 #include "wx/toolbar.h"
21 #include "wx/notebook.h"
22 #include "wx/tabctrl.h"
23 #include "wx/bitmap.h"
24
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
27
28 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
29 EVT_MOUSE_EVENTS( wxToolBar::OnMouse )
30 EVT_PAINT( wxToolBar::OnPaint )
31 END_EVENT_TABLE()
32 #endif
33
34 #include "wx/mac/uma.h"
35 #include "wx/geometry.h"
36 // ----------------------------------------------------------------------------
37 // private classes
38 // ----------------------------------------------------------------------------
39
40 class wxToolBarTool : public wxToolBarToolBase
41 {
42 public:
43 wxToolBarTool(wxToolBar *tbar,
44 int id,
45 const wxString& label,
46 const wxBitmap& bmpNormal,
47 const wxBitmap& bmpDisabled,
48 wxItemKind kind,
49 wxObject *clientData,
50 const wxString& shortHelp,
51 const wxString& longHelp) ;
52
53 wxToolBarTool(wxToolBar *tbar, wxControl *control)
54 : wxToolBarToolBase(tbar, control)
55 {
56 Init() ;
57 }
58
59 ~wxToolBarTool()
60 {
61 if ( m_controlHandle )
62 DisposeControl( m_controlHandle ) ;
63 }
64
65 WXWidget GetControlHandle() { return (WXWidget) m_controlHandle ; }
66 void SetControlHandle( ControlRef handle ) { m_controlHandle = handle ; }
67
68 void SetSize(const wxSize& size) ;
69 void SetPosition( const wxPoint& position ) ;
70 wxSize GetSize() const
71 {
72 if ( IsControl() )
73 {
74 return GetControl()->GetSize() ;
75 }
76 else if ( IsButton() )
77 {
78 return GetToolBar()->GetToolSize() ;
79 }
80 else
81 {
82 wxSize sz = GetToolBar()->GetToolSize() ;
83 sz.x /= 4 ;
84 sz.y /= 4 ;
85 return sz ;
86 }
87 }
88 wxPoint GetPosition() const
89 {
90 return wxPoint(m_x, m_y);
91 }
92 private :
93 void Init()
94 {
95 m_controlHandle = NULL ;
96 }
97 ControlRef m_controlHandle ;
98
99 wxCoord m_x;
100 wxCoord m_y;
101 };
102
103 static const EventTypeSpec eventList[] =
104 {
105 { kEventClassControl , kEventControlHit } ,
106 } ;
107
108 static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
109 {
110 OSStatus result = eventNotHandledErr ;
111
112 wxMacCarbonEvent cEvent( event ) ;
113
114 ControlRef controlRef ;
115
116 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
117
118 switch( GetEventKind( event ) )
119 {
120 case kEventControlHit :
121 {
122 wxToolBarTool* tbartool = (wxToolBarTool*)data ;
123 if ( tbartool->CanBeToggled() )
124 {
125 tbartool->Toggle( GetControl32BitValue( (ControlRef) tbartool->GetControlHandle() ) ) ;
126 }
127 ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ;
128
129 result = noErr;
130 }
131 break ;
132 default :
133 break ;
134 }
135 return result ;
136 }
137
138 pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
139 {
140 OSStatus result = eventNotHandledErr ;
141
142 switch ( GetEventClass( event ) )
143 {
144 case kEventClassControl :
145 result = wxMacToolBarToolControlEventHandler( handler, event, data ) ;
146 break ;
147 default :
148 break ;
149 }
150 return result ;
151 }
152
153 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler )
154
155 // ============================================================================
156 // implementation
157 // ============================================================================
158
159 // ----------------------------------------------------------------------------
160 // wxToolBarTool
161 // ----------------------------------------------------------------------------
162
163 void wxToolBarTool::SetSize(const wxSize& size)
164 {
165 if ( IsControl() )
166 {
167 GetControl()->SetSize( size ) ;
168 }
169 }
170
171 void wxToolBarTool::SetPosition(const wxPoint& position)
172 {
173 m_x = position.x;
174 m_y = position.y;
175
176 if ( IsButton() )
177 {
178 int x , y ;
179 x = y = 0 ;
180 int mac_x = position.x ;
181 int mac_y = position.y ;
182 #if !TARGET_API_MAC_OSX
183 WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetTopLevelWindowRef() ;
184 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
185 mac_x += x;
186 mac_y += y;
187 #endif
188 Rect contrlRect ;
189 GetControlBounds( m_controlHandle , &contrlRect ) ;
190 int former_mac_x = contrlRect.left ;
191 int former_mac_y = contrlRect.top ;
192 wxSize sz = GetToolBar()->GetToolSize() ;
193
194 if ( mac_x != former_mac_x || mac_y != former_mac_y )
195 {
196 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
197 }
198 }
199 else if ( IsControl() )
200 {
201 GetControl()->Move( position ) ;
202 }
203 }
204
205 const short kwxMacToolBarToolDefaultWidth = 24 ;
206 const short kwxMacToolBarToolDefaultHeight = 22 ;
207 const short kwxMacToolBarTopMargin = 2 ;
208 const short kwxMacToolBarLeftMargin = 2 ;
209
210 wxToolBarTool::wxToolBarTool(wxToolBar *tbar,
211 int id,
212 const wxString& label,
213 const wxBitmap& bmpNormal,
214 const wxBitmap& bmpDisabled,
215 wxItemKind kind,
216 wxObject *clientData,
217 const wxString& shortHelp,
218 const wxString& longHelp)
219 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
220 clientData, shortHelp, longHelp)
221 {
222 Init();
223
224 if (id == wxID_SEPARATOR) return;
225
226 WindowRef window = (WindowRef) tbar->MacGetTopLevelWindowRef() ;
227 wxSize toolSize = tbar->GetToolSize() ;
228 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x } ;
229
230 ControlButtonContentInfo info ;
231 wxMacCreateBitmapButton( &info , GetNormalBitmap() ) ;
232
233 SInt16 behaviour = kControlBehaviorOffsetContents ;
234 if ( CanBeToggled() )
235 behaviour += kControlBehaviorToggles ;
236
237 if ( info.contentType != kControlNoContent )
238 {
239 m_controlHandle = ::NewControl( window , &toolrect , "\p" , true , 0 ,
240 behaviour + info.contentType , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
241
242 ::SetControlData( m_controlHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
243 }
244 else
245 {
246 m_controlHandle = ::NewControl( window , &toolrect , "\p" , true , 0 ,
247 behaviour , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
248 }
249 InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
250 GetEventTypeCount(eventList), eventList, this,NULL);
251 UMAShowControl( m_controlHandle ) ;
252 if ( !IsEnabled() )
253 {
254 UMADeactivateControl( m_controlHandle ) ;
255 }
256 if ( CanBeToggled() && IsToggled() )
257 {
258 ::SetControl32BitValue( m_controlHandle , 1 ) ;
259 }
260 else
261 {
262 ::SetControl32BitValue( m_controlHandle , 0 ) ;
263 }
264
265 ControlRef container = (ControlRef) tbar->GetHandle() ;
266 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
267 ::EmbedControl( m_controlHandle , container ) ;
268 }
269
270
271 wxToolBarToolBase *wxToolBar::CreateTool(int id,
272 const wxString& label,
273 const wxBitmap& bmpNormal,
274 const wxBitmap& bmpDisabled,
275 wxItemKind kind,
276 wxObject *clientData,
277 const wxString& shortHelp,
278 const wxString& longHelp)
279 {
280 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
281 clientData, shortHelp, longHelp);
282 }
283
284 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
285 {
286 return new wxToolBarTool(this, control);
287 }
288
289 void wxToolBar::Init()
290 {
291 m_maxWidth = -1;
292 m_maxHeight = -1;
293 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
294 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
295 }
296
297 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
298 long style, const wxString& name)
299 {
300 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
301 return FALSE ;
302
303 return TRUE;
304 }
305
306 wxToolBar::~wxToolBar()
307 {
308 // we must refresh the frame size when the toolbar is deleted but the frame
309 // is not - otherwise toolbar leaves a hole in the place it used to occupy
310 }
311
312 bool wxToolBar::Realize()
313 {
314 if (m_tools.GetCount() == 0)
315 return FALSE;
316
317 int x = m_xMargin + kwxMacToolBarLeftMargin ;
318 int y = m_yMargin + kwxMacToolBarTopMargin ;
319
320 int tw, th;
321 GetSize(& tw, & th);
322
323 int maxWidth = 0 ;
324 int maxHeight = 0 ;
325
326 int maxToolWidth = 0;
327 int maxToolHeight = 0;
328
329 // Find the maximum tool width and height
330 wxToolBarToolsList::Node *node = m_tools.GetFirst();
331 while ( node )
332 {
333 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
334 wxSize sz = tool->GetSize() ;
335
336 if ( sz.x > maxToolWidth )
337 maxToolWidth = sz.x ;
338 if (sz.y> maxToolHeight)
339 maxToolHeight = sz.y;
340
341 node = node->GetNext();
342 }
343
344 node = m_tools.GetFirst();
345 while (node)
346 {
347 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
348 wxSize cursize = tool->GetSize() ;
349
350 // for the moment we just do a single row/column alignement
351 if ( x + cursize.x > maxWidth )
352 maxWidth = x + cursize.x ;
353 if ( y + cursize.y > maxHeight )
354 maxHeight = y + cursize.y ;
355
356 tool->SetPosition( wxPoint( x , y ) ) ;
357
358 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
359 {
360 y += cursize.y ;
361 }
362 else
363 {
364 x += cursize.x ;
365 }
366
367 node = node->GetNext();
368 }
369
370 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
371 {
372 if ( m_maxRows == 0 )
373 {
374 // if not set yet, only one row
375 SetRows(1);
376 }
377 maxWidth = tw ;
378 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
379 m_maxHeight = maxHeight ;
380 }
381 else
382 {
383 if ( GetToolsCount() > 0 && m_maxRows == 0 )
384 {
385 // if not set yet, have one column
386 SetRows(GetToolsCount());
387 }
388 maxHeight = th ;
389 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
390 m_maxWidth = maxWidth ;
391 }
392
393 SetSize( maxWidth, maxHeight );
394
395 return TRUE;
396 }
397
398 void wxToolBar::SetToolBitmapSize(const wxSize& size)
399 {
400 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
401 }
402
403 // The button size is bigger than the bitmap size
404 wxSize wxToolBar::GetToolSize() const
405 {
406 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
407 }
408
409 void wxToolBar::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED( mouseStillDown ) )
410 {
411 wxToolBarToolsList::Node *node;
412 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
413 {
414 wxToolBarTool* tool = (wxToolBarTool*) node->GetData() ;
415 if ( tool->IsButton() )
416 {
417 if( tool->GetControlHandle() == control )
418 {
419 if ( tool->CanBeToggled() )
420 {
421 tool->Toggle( GetControl32BitValue( (ControlRef) control ) ) ;
422 }
423 OnLeftClick( tool->GetId() , tool -> IsToggled() ) ;
424 break ;
425 }
426 }
427 }
428 }
429
430 void wxToolBar::SetRows(int nRows)
431 {
432 if ( nRows == m_maxRows )
433 {
434 // avoid resizing the frame uselessly
435 return;
436 }
437
438 m_maxRows = nRows;
439 }
440
441 void wxToolBar::MacSuperChangedPosition()
442 {
443 wxWindow::MacSuperChangedPosition() ;
444 Realize() ;
445 }
446
447 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
448 {
449 wxToolBarToolsList::Node *node = m_tools.GetFirst();
450 while (node)
451 {
452 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
453 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
454 if ( r.Contains( wxPoint( x , y ) ) )
455 {
456 return tool;
457 }
458
459 node = node->GetNext();
460 }
461
462 return (wxToolBarToolBase *)NULL;
463 }
464
465 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
466 {
467 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
468 if ( tool )
469 {
470 return tool->GetShortHelp() ;
471 }
472 return wxEmptyString ;
473 }
474
475 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
476 {
477 if (!IsShown())
478 return ;
479
480 wxToolBarTool *tool = (wxToolBarTool *)t;
481 if ( tool->IsControl() )
482 {
483 tool->GetControl()->Enable( enable ) ;
484 }
485 else if ( tool->IsButton() )
486 {
487 if ( enable )
488 UMAActivateControl( (ControlRef) tool->GetControlHandle() ) ;
489 else
490 UMADeactivateControl( (ControlRef) tool->GetControlHandle() ) ;
491 }
492 }
493
494 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
495 {
496 if (!IsShown())
497 return ;
498
499 wxToolBarTool *tool = (wxToolBarTool *)t;
500 if ( tool->IsButton() )
501 {
502 ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ;
503 }
504 }
505
506 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
507 wxToolBarToolBase *tool)
508 {
509 // nothing special to do here - we relayout in Realize() later
510 tool->Attach(this);
511
512 return TRUE;
513 }
514
515 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
516 {
517 wxFAIL_MSG( _T("not implemented") );
518 }
519
520 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
521 {
522 wxToolBarToolsList::Node *node;
523 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
524 {
525 wxToolBarToolBase *tool2 = node->GetData();
526 if ( tool2 == tool )
527 {
528 // let node point to the next node in the list
529 node = node->GetNext();
530
531 break;
532 }
533 }
534
535 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
536
537 tool->Detach();
538
539 // and finally reposition all the controls after this one
540
541 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
542 {
543 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
544 wxPoint pt = tool2->GetPosition() ;
545
546 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
547 {
548 pt.y -= sz.y ;
549 }
550 else
551 {
552 pt.x -= sz.x ;
553 }
554 tool2->SetPosition( pt ) ;
555 }
556
557 return TRUE ;
558 }
559
560 void wxToolBar::OnPaint(wxPaintEvent& event)
561 {
562 wxPaintDC dc(this) ;
563 wxMacPortSetter helper(&dc) ;
564 int w, h ;
565 GetSize( &w , &h ) ;
566
567 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
568 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
569 /*
570 if( toolbarrect.left < 0 )
571 toolbarrect.left = 0 ;
572 if ( toolbarrect.top < 0 )
573 toolbarrect.top = 0 ;
574 */
575 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
576
577 event.Skip() ;
578 }
579
580 void wxToolBar::OnMouse( wxMouseEvent &event )
581 {
582 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
583 {
584
585 int x = event.m_x ;
586 int y = event.m_y ;
587
588 MacClientToRootWindow( &x , &y ) ;
589
590 ControlRef control ;
591 Point localwhere ;
592 SInt16 controlpart ;
593 WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ;
594
595 localwhere.h = x ;
596 localwhere.v = y ;
597
598 short modifiers = 0;
599
600 if ( !event.m_leftDown && !event.m_rightDown )
601 modifiers |= btnState ;
602
603 if ( event.m_shiftDown )
604 modifiers |= shiftKey ;
605
606 if ( event.m_controlDown )
607 modifiers |= controlKey ;
608
609 if ( event.m_altDown )
610 modifiers |= optionKey ;
611
612 if ( event.m_metaDown )
613 modifiers |= cmdKey ;
614
615 controlpart = ::FindControl( localwhere , window , &control ) ;
616 {
617 if ( control && ::IsControlActive( control ) )
618 {
619 {
620 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
621 wxTheApp->s_lastMouseDown = 0 ;
622 if ( control && controlpart != kControlNoPart ) // otherwise we will get the event twice
623 {
624 MacHandleControlClick((WXWidget) control , controlpart , false /* not down anymore */ ) ;
625 }
626 }
627 }
628 }
629 }
630 }
631
632 #endif // wxUSE_TOOLBAR
633