]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/toolbar.cpp
Added wxFopen to the MSLU code.
[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_PAINT( wxToolBar::OnPaint )
30 END_EVENT_TABLE()
31 #endif
32
33 #include "wx/mac/uma.h"
34 #include "wx/geometry.h"
35 // ----------------------------------------------------------------------------
36 // private classes
37 // ----------------------------------------------------------------------------
38
39 class wxToolBarTool : public wxToolBarToolBase
40 {
41 public:
42 wxToolBarTool(wxToolBar *tbar,
43 int id,
44 const wxString& label,
45 const wxBitmap& bmpNormal,
46 const wxBitmap& bmpDisabled,
47 wxItemKind kind,
48 wxObject *clientData,
49 const wxString& shortHelp,
50 const wxString& longHelp) ;
51
52 wxToolBarTool(wxToolBar *tbar, wxControl *control)
53 : wxToolBarToolBase(tbar, control)
54 {
55 Init() ;
56 }
57
58 ~wxToolBarTool()
59 {
60 if ( m_controlHandle )
61 DisposeControl( m_controlHandle ) ;
62 }
63
64 WXWidget GetControlHandle() { return (WXWidget) m_controlHandle ; }
65 void SetControlHandle( ControlRef handle ) { m_controlHandle = handle ; }
66
67 void SetSize(const wxSize& size) ;
68 void SetPosition( const wxPoint& position ) ;
69
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 bool DoEnable( bool enable ) ;
93 private :
94 void Init()
95 {
96 m_controlHandle = NULL ;
97 }
98 ControlRef m_controlHandle ;
99
100 wxCoord m_x;
101 wxCoord m_y;
102 };
103
104 static const EventTypeSpec eventList[] =
105 {
106 { kEventClassControl , kEventControlHit } ,
107 } ;
108
109 static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
110 {
111 OSStatus result = eventNotHandledErr ;
112
113 wxMacCarbonEvent cEvent( event ) ;
114
115 ControlRef controlRef ;
116
117 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
118
119 switch( GetEventKind( event ) )
120 {
121 case kEventControlHit :
122 {
123 wxToolBarTool* tbartool = (wxToolBarTool*)data ;
124 if ( tbartool->CanBeToggled() )
125 {
126 ((wxToolBar*)tbartool->GetToolBar())->ToggleTool(tbartool->GetId(), GetControl32BitValue((ControlRef)tbartool->GetControlHandle()));
127 }
128 ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ;
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 bool wxToolBarTool::DoEnable(bool enable)
164 {
165 if ( IsControl() )
166 {
167 GetControl()->Enable( enable ) ;
168 }
169 else if ( IsButton() )
170 {
171 #if TARGET_API_MAC_OSX
172 if ( enable )
173 EnableControl( m_controlHandle ) ;
174 else
175 DisableControl( m_controlHandle ) ;
176 #else
177 if ( enable )
178 ActivateControl( m_controlHandle ) ;
179 else
180 DeactivateControl( m_controlHandle ) ;
181 #endif
182 }
183 return true ;
184 }
185 void wxToolBarTool::SetSize(const wxSize& size)
186 {
187 if ( IsControl() )
188 {
189 GetControl()->SetSize( size ) ;
190 }
191 }
192
193 void wxToolBarTool::SetPosition(const wxPoint& position)
194 {
195 m_x = position.x;
196 m_y = position.y;
197
198 if ( IsButton() )
199 {
200 int x , y ;
201 x = y = 0 ;
202 int mac_x = position.x ;
203 int mac_y = position.y ;
204 #if !TARGET_API_MAC_OSX
205 WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetTopLevelWindowRef() ;
206 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
207 mac_x += x;
208 mac_y += y;
209 #endif
210 Rect contrlRect ;
211 GetControlBounds( m_controlHandle , &contrlRect ) ;
212 int former_mac_x = contrlRect.left ;
213 int former_mac_y = contrlRect.top ;
214 GetToolBar()->GetToolSize() ;
215
216 if ( mac_x != former_mac_x || mac_y != former_mac_y )
217 {
218 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
219 }
220 }
221 else if ( IsControl() )
222 {
223 GetControl()->Move( position ) ;
224 }
225 }
226
227 const short kwxMacToolBarToolDefaultWidth = 24 ;
228 const short kwxMacToolBarToolDefaultHeight = 22 ;
229 const short kwxMacToolBarTopMargin = 2 ;
230 const short kwxMacToolBarLeftMargin = 2 ;
231
232 wxToolBarTool::wxToolBarTool(wxToolBar *tbar,
233 int id,
234 const wxString& label,
235 const wxBitmap& bmpNormal,
236 const wxBitmap& bmpDisabled,
237 wxItemKind kind,
238 wxObject *clientData,
239 const wxString& shortHelp,
240 const wxString& longHelp)
241 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
242 clientData, shortHelp, longHelp)
243 {
244 Init();
245
246 if (id == wxID_SEPARATOR) return;
247
248 WindowRef window = (WindowRef) tbar->MacGetTopLevelWindowRef() ;
249 wxSize toolSize = tbar->GetToolSize() ;
250 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x } ;
251
252 ControlButtonContentInfo info ;
253 wxMacCreateBitmapButton( &info , GetNormalBitmap() ) ;
254
255 SInt16 behaviour = kControlBehaviorOffsetContents ;
256 if ( CanBeToggled() )
257 behaviour += kControlBehaviorToggles ;
258
259 CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info ,
260 0 , 0 , 0 , &m_controlHandle ) ;
261
262 InstallControlEventHandler( (ControlRef) m_controlHandle, GetwxMacToolBarToolEventHandlerUPP(),
263 GetEventTypeCount(eventList), eventList, this,NULL);
264
265 UMAShowControl( m_controlHandle ) ;
266
267 if ( CanBeToggled() && IsToggled() )
268 ::SetControl32BitValue( m_controlHandle , 1 ) ;
269 else
270 ::SetControl32BitValue( m_controlHandle , 0 ) ;
271
272 ControlRef container = (ControlRef) tbar->GetHandle() ;
273 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
274 ::EmbedControl( m_controlHandle , container ) ;
275 }
276
277
278 wxToolBarToolBase *wxToolBar::CreateTool(int id,
279 const wxString& label,
280 const wxBitmap& bmpNormal,
281 const wxBitmap& bmpDisabled,
282 wxItemKind kind,
283 wxObject *clientData,
284 const wxString& shortHelp,
285 const wxString& longHelp)
286 {
287 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
288 clientData, shortHelp, longHelp);
289 }
290
291 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
292 {
293 return new wxToolBarTool(this, control);
294 }
295
296 void wxToolBar::Init()
297 {
298 m_maxWidth = -1;
299 m_maxHeight = -1;
300 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
301 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
302 }
303
304 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
305 long style, const wxString& name)
306 {
307 if ( !wxToolBarBase::Create( parent , id , pos , size , style ) )
308 return FALSE ;
309
310 return TRUE;
311 }
312
313 wxToolBar::~wxToolBar()
314 {
315 // we must refresh the frame size when the toolbar is deleted but the frame
316 // is not - otherwise toolbar leaves a hole in the place it used to occupy
317 }
318
319 bool wxToolBar::Realize()
320 {
321 if (m_tools.GetCount() == 0)
322 return FALSE;
323
324 int x = m_xMargin + kwxMacToolBarLeftMargin ;
325 int y = m_yMargin + kwxMacToolBarTopMargin ;
326
327 int tw, th;
328 GetSize(& tw, & th);
329
330 int maxWidth = 0 ;
331 int maxHeight = 0 ;
332
333 int maxToolWidth = 0;
334 int maxToolHeight = 0;
335
336 // Find the maximum tool width and height
337 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
338 while ( node )
339 {
340 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
341 wxSize sz = tool->GetSize() ;
342
343 if ( sz.x > maxToolWidth )
344 maxToolWidth = sz.x ;
345 if (sz.y> maxToolHeight)
346 maxToolHeight = sz.y;
347
348 node = node->GetNext();
349 }
350
351 bool lastWasRadio = FALSE;
352 node = m_tools.GetFirst();
353 while (node)
354 {
355 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
356 wxSize cursize = tool->GetSize() ;
357
358 bool isRadio = FALSE;
359
360 if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO )
361 {
362 if ( !lastWasRadio )
363 {
364 if (tool->Toggle(true))
365 {
366 DoToggleTool(tool, true);
367 }
368 }
369 isRadio = TRUE;
370 }
371 else
372 {
373 isRadio = FALSE;
374 }
375 lastWasRadio = isRadio;
376
377 // for the moment we just do a single row/column alignement
378 if ( x + cursize.x > maxWidth )
379 maxWidth = x + cursize.x ;
380 if ( y + cursize.y > maxHeight )
381 maxHeight = y + cursize.y ;
382
383 tool->SetPosition( wxPoint( x , y ) ) ;
384
385 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
386 {
387 y += cursize.y ;
388 }
389 else
390 {
391 x += cursize.x ;
392 }
393
394 node = node->GetNext();
395 }
396
397 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
398 {
399 if ( m_maxRows == 0 )
400 {
401 // if not set yet, only one row
402 SetRows(1);
403 }
404 m_minWidth = maxWidth;
405 maxWidth = tw ;
406 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
407 m_minHeight = m_maxHeight = maxHeight ;
408 }
409 else
410 {
411 if ( GetToolsCount() > 0 && m_maxRows == 0 )
412 {
413 // if not set yet, have one column
414 SetRows(GetToolsCount());
415 }
416 m_minHeight = maxHeight;
417 maxHeight = th ;
418 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
419 m_minWidth = m_maxWidth = maxWidth ;
420 }
421
422 SetSize( maxWidth, maxHeight );
423 InvalidateBestSize();
424
425 return TRUE;
426 }
427
428 void wxToolBar::SetToolBitmapSize(const wxSize& size)
429 {
430 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
431 }
432
433 // The button size is bigger than the bitmap size
434 wxSize wxToolBar::GetToolSize() const
435 {
436 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
437 }
438
439 void wxToolBar::SetRows(int nRows)
440 {
441 if ( nRows == m_maxRows )
442 {
443 // avoid resizing the frame uselessly
444 return;
445 }
446
447 m_maxRows = nRows;
448 }
449
450 void wxToolBar::MacSuperChangedPosition()
451 {
452 wxWindow::MacSuperChangedPosition() ;
453 Realize() ;
454 }
455
456 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
457 {
458 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
459 while (node)
460 {
461 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
462 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
463 if ( r.Contains( wxPoint( x , y ) ) )
464 {
465 return tool;
466 }
467
468 node = node->GetNext();
469 }
470
471 return (wxToolBarToolBase *)NULL;
472 }
473
474 wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
475 {
476 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
477 if ( tool )
478 {
479 return tool->GetShortHelp() ;
480 }
481 return wxEmptyString ;
482 }
483
484 void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
485 {
486 ((wxToolBarTool*)t)->DoEnable( enable ) ;
487 }
488
489 void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
490 {
491 wxToolBarTool *tool = (wxToolBarTool *)t;
492 if ( tool->IsButton() )
493 {
494 ::SetControl32BitValue( (ControlRef) tool->GetControlHandle() , toggle ) ;
495 }
496 }
497
498 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
499 wxToolBarToolBase *tool)
500 {
501 // nothing special to do here - we relayout in Realize() later
502 tool->Attach(this);
503 InvalidateBestSize();
504
505 return TRUE;
506 }
507
508 void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
509 {
510 wxFAIL_MSG( _T("not implemented") );
511 }
512
513 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
514 {
515 wxToolBarToolsList::compatibility_iterator node;
516 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
517 {
518 wxToolBarToolBase *tool2 = node->GetData();
519 if ( tool2 == tool )
520 {
521 // let node point to the next node in the list
522 node = node->GetNext();
523
524 break;
525 }
526 }
527
528 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
529
530 tool->Detach();
531
532 // and finally reposition all the controls after this one
533
534 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
535 {
536 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
537 wxPoint pt = tool2->GetPosition() ;
538
539 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
540 {
541 pt.y -= sz.y ;
542 }
543 else
544 {
545 pt.x -= sz.x ;
546 }
547 tool2->SetPosition( pt ) ;
548 }
549
550 InvalidateBestSize();
551 return TRUE ;
552 }
553
554 void wxToolBar::OnPaint(wxPaintEvent& event)
555 {
556 wxPaintDC dc(this) ;
557 wxMacPortSetter helper(&dc) ;
558 int w, h ;
559 GetSize( &w , &h ) ;
560
561 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
562 dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ;
563 /*
564 if( toolbarrect.left < 0 )
565 toolbarrect.left = 0 ;
566 if ( toolbarrect.top < 0 )
567 toolbarrect.top = 0 ;
568 */
569 if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() )
570 {
571 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
572 }
573 else
574 {
575 #if TARGET_API_MAC_OSX
576 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
577 if ( UMAGetSystemVersion() >= 0x1030 )
578 {
579 HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
580 dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) );
581 CGContextRef cgContext ;
582 Rect bounds ;
583 GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ;
584 QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
585 CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ;
586 CGContextScaleCTM( cgContext , 1 , -1 ) ;
587
588 {
589 HIThemeBackgroundDrawInfo drawInfo ;
590 drawInfo.version = 0 ;
591 drawInfo.state = kThemeStateActive ;
592 drawInfo.kind = kThemeBackgroundMetal ;
593 HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ;
594 }
595 QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ;
596 }
597 else
598 #endif
599 {
600 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
601 }
602 #endif
603 }
604 event.Skip() ;
605 }
606
607 #endif // wxUSE_TOOLBAR
608