]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/mac/carbon/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: 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/mac/uma.h" | |
23 | #include "wx/geometry.h" | |
24 | ||
25 | ||
26 | #ifdef __WXMAC_OSX__ | |
27 | const short kwxMacToolBarToolDefaultWidth = 16; | |
28 | const short kwxMacToolBarToolDefaultHeight = 16; | |
29 | const short kwxMacToolBarTopMargin = 4; | |
30 | const short kwxMacToolBarLeftMargin = 4; | |
31 | const short kwxMacToolBorder = 0; | |
32 | const short kwxMacToolSpacing = 6; | |
33 | #else | |
34 | const short kwxMacToolBarToolDefaultWidth = 24; | |
35 | const short kwxMacToolBarToolDefaultHeight = 22; | |
36 | const short kwxMacToolBarTopMargin = 2; | |
37 | const short kwxMacToolBarLeftMargin = 2; | |
38 | const short kwxMacToolBorder = 4; | |
39 | const short kwxMacToolSpacing = 0; | |
40 | #endif | |
41 | ||
42 | ||
43 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) | |
44 | ||
45 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
46 | EVT_PAINT( wxToolBar::OnPaint ) | |
47 | END_EVENT_TABLE() | |
48 | ||
49 | ||
50 | #pragma mark - | |
51 | #pragma mark Tool Implementation | |
52 | ||
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // private classes | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef | |
59 | ||
60 | class wxToolBarTool : public wxToolBarToolBase | |
61 | { | |
62 | public: | |
63 | wxToolBarTool( | |
64 | wxToolBar *tbar, | |
65 | int id, | |
66 | const wxString& label, | |
67 | const wxBitmap& bmpNormal, | |
68 | const wxBitmap& bmpDisabled, | |
69 | wxItemKind kind, | |
70 | wxObject *clientData, | |
71 | const wxString& shortHelp, | |
72 | const wxString& longHelp ); | |
73 | ||
74 | wxToolBarTool(wxToolBar *tbar, wxControl *control) | |
75 | : wxToolBarToolBase(tbar, control) | |
76 | { | |
77 | Init(); | |
78 | if (control != NULL) | |
79 | SetControlHandle( (ControlRef) control->GetHandle() ); | |
80 | } | |
81 | ||
82 | ~wxToolBarTool() | |
83 | { | |
84 | ClearControl(); | |
85 | ||
86 | #if wxMAC_USE_NATIVE_TOOLBAR | |
87 | if ( m_toolbarItemRef ) | |
88 | CFRelease( m_toolbarItemRef ); | |
89 | #endif | |
90 | } | |
91 | ||
92 | WXWidget GetControlHandle() | |
93 | { | |
94 | return (WXWidget) m_controlHandle; | |
95 | } | |
96 | ||
97 | void SetControlHandle( ControlRef handle ) | |
98 | { | |
99 | m_controlHandle = handle; | |
100 | } | |
101 | ||
102 | void SetPosition( const wxPoint& position ); | |
103 | ||
104 | void ClearControl() | |
105 | { | |
106 | m_control = NULL; | |
107 | if ( m_controlHandle ) | |
108 | { | |
109 | DisposeControl( m_controlHandle ); | |
110 | m_controlHandle = NULL ; | |
111 | } | |
112 | ||
113 | #if wxMAC_USE_NATIVE_TOOLBAR | |
114 | m_toolbarItemRef = NULL; | |
115 | #endif | |
116 | } | |
117 | ||
118 | wxSize GetSize() const | |
119 | { | |
120 | wxSize curSize; | |
121 | ||
122 | if ( IsControl() ) | |
123 | { | |
124 | curSize = GetControl()->GetSize(); | |
125 | } | |
126 | else if ( IsButton() ) | |
127 | { | |
128 | curSize = GetToolBar()->GetToolSize(); | |
129 | } | |
130 | else | |
131 | { | |
132 | // separator size | |
133 | curSize = GetToolBar()->GetToolSize(); | |
134 | if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL ) | |
135 | curSize.y /= 4; | |
136 | else | |
137 | curSize.x /= 4; | |
138 | } | |
139 | ||
140 | return curSize; | |
141 | } | |
142 | ||
143 | wxPoint GetPosition() const | |
144 | { | |
145 | return wxPoint( m_x, m_y ); | |
146 | } | |
147 | ||
148 | bool DoEnable( bool enable ); | |
149 | ||
150 | void UpdateToggleImage( bool toggle ); | |
151 | ||
152 | #if wxMAC_USE_NATIVE_TOOLBAR | |
153 | void SetToolbarItemRef( HIToolbarItemRef ref ) | |
154 | { | |
155 | if ( m_controlHandle ) | |
156 | HideControl( m_controlHandle ); | |
157 | if ( m_toolbarItemRef ) | |
158 | CFRelease( m_toolbarItemRef ); | |
159 | ||
160 | m_toolbarItemRef = ref; | |
161 | if ( m_toolbarItemRef ) | |
162 | { | |
163 | HIToolbarItemSetHelpText( | |
164 | m_toolbarItemRef, | |
165 | wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ), | |
166 | wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) ); | |
167 | } | |
168 | } | |
169 | ||
170 | HIToolbarItemRef GetToolbarItemRef() const | |
171 | { | |
172 | return m_toolbarItemRef; | |
173 | } | |
174 | ||
175 | void SetIndex( CFIndex idx ) | |
176 | { | |
177 | m_index = idx; | |
178 | } | |
179 | ||
180 | CFIndex GetIndex() const | |
181 | { | |
182 | return m_index; | |
183 | } | |
184 | #endif | |
185 | ||
186 | private: | |
187 | void Init() | |
188 | { | |
189 | m_controlHandle = NULL; | |
190 | ||
191 | #if wxMAC_USE_NATIVE_TOOLBAR | |
192 | m_toolbarItemRef = NULL; | |
193 | m_index = -1; | |
194 | #endif | |
195 | } | |
196 | ||
197 | ControlRef m_controlHandle; | |
198 | wxCoord m_x; | |
199 | wxCoord m_y; | |
200 | ||
201 | #if wxMAC_USE_NATIVE_TOOLBAR | |
202 | HIToolbarItemRef m_toolbarItemRef; | |
203 | // position in its toolbar, -1 means not inserted | |
204 | CFIndex m_index; | |
205 | #endif | |
206 | }; | |
207 | ||
208 | static const EventTypeSpec eventList[] = | |
209 | { | |
210 | { kEventClassControl, kEventControlHit }, | |
211 | #ifdef __WXMAC_OSX__ | |
212 | { kEventClassControl, kEventControlHitTest }, | |
213 | #endif | |
214 | }; | |
215 | ||
216 | static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) | |
217 | { | |
218 | OSStatus result = eventNotHandledErr; | |
219 | ControlRef controlRef; | |
220 | wxMacCarbonEvent cEvent( event ); | |
221 | ||
222 | cEvent.GetParameter( kEventParamDirectObject, &controlRef ); | |
223 | ||
224 | switch ( GetEventKind( event ) ) | |
225 | { | |
226 | case kEventControlHit: | |
227 | { | |
228 | wxToolBarTool *tbartool = (wxToolBarTool*)data; | |
229 | wxToolBar *tbar = tbartool != NULL ? (wxToolBar*) (tbartool->GetToolBar()) : NULL; | |
230 | if ((tbartool != NULL) && tbartool->CanBeToggled()) | |
231 | { | |
232 | bool shouldToggle; | |
233 | ||
234 | #ifdef __WXMAC_OSX__ | |
235 | shouldToggle = !tbartool->IsToggled(); | |
236 | #else | |
237 | shouldToggle = (GetControl32BitValue( (ControlRef)(tbartool->GetControlHandle()) ) != 0); | |
238 | #endif | |
239 | ||
240 | tbar->ToggleTool( tbartool->GetId(), shouldToggle ); | |
241 | } | |
242 | ||
243 | if (tbartool != NULL) | |
244 | tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() ); | |
245 | result = noErr; | |
246 | } | |
247 | break; | |
248 | ||
249 | #ifdef __WXMAC_OSX__ | |
250 | case kEventControlHitTest: | |
251 | { | |
252 | HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation); | |
253 | HIRect rect; | |
254 | HIViewGetBounds( controlRef, &rect ); | |
255 | ||
256 | ControlPartCode pc = kControlNoPart; | |
257 | if ( CGRectContainsPoint( rect, pt ) ) | |
258 | pc = kControlIconPart; | |
259 | cEvent.SetParameter( kEventParamControlPart, typeControlPartCode, pc ); | |
260 | result = noErr; | |
261 | } | |
262 | break; | |
263 | #endif | |
264 | ||
265 | default: | |
266 | break; | |
267 | } | |
268 | ||
269 | return result; | |
270 | } | |
271 | ||
272 | static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) | |
273 | { | |
274 | OSStatus result = eventNotHandledErr; | |
275 | ||
276 | switch ( GetEventClass( event ) ) | |
277 | { | |
278 | case kEventClassControl: | |
279 | result = wxMacToolBarToolControlEventHandler( handler, event, data ); | |
280 | break; | |
281 | ||
282 | default: | |
283 | break; | |
284 | } | |
285 | ||
286 | return result; | |
287 | } | |
288 | ||
289 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler ) | |
290 | ||
291 | #if wxMAC_USE_NATIVE_TOOLBAR | |
292 | ||
293 | static const EventTypeSpec toolBarEventList[] = | |
294 | { | |
295 | { kEventClassToolbarItem, kEventToolbarItemPerformAction }, | |
296 | }; | |
297 | ||
298 | static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) | |
299 | { | |
300 | OSStatus result = eventNotHandledErr; | |
301 | ||
302 | switch ( GetEventKind( event ) ) | |
303 | { | |
304 | case kEventToolbarItemPerformAction: | |
305 | { | |
306 | wxToolBarTool* tbartool = (wxToolBarTool*) data; | |
307 | if ( tbartool != NULL ) | |
308 | { | |
309 | wxToolBar *tbar = (wxToolBar*)(tbartool->GetToolBar()); | |
310 | int toolID = tbartool->GetId(); | |
311 | ||
312 | if ( tbartool->CanBeToggled() ) | |
313 | { | |
314 | if ( tbar != NULL ) | |
315 | tbar->ToggleTool(toolID, !tbartool->IsToggled() ); | |
316 | } | |
317 | ||
318 | if ( tbar != NULL ) | |
319 | tbar->OnLeftClick( toolID, tbartool->IsToggled() ); | |
320 | result = noErr; | |
321 | } | |
322 | } | |
323 | break; | |
324 | ||
325 | default: | |
326 | break; | |
327 | } | |
328 | ||
329 | return result; | |
330 | } | |
331 | ||
332 | static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) | |
333 | { | |
334 | OSStatus result = eventNotHandledErr; | |
335 | ||
336 | switch ( GetEventClass( event ) ) | |
337 | { | |
338 | case kEventClassToolbarItem: | |
339 | result = wxMacToolBarCommandEventHandler( handler, event, data ); | |
340 | break; | |
341 | ||
342 | default: | |
343 | break; | |
344 | } | |
345 | ||
346 | return result; | |
347 | } | |
348 | ||
349 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler ) | |
350 | ||
351 | #endif | |
352 | ||
353 | bool wxToolBarTool::DoEnable( bool enable ) | |
354 | { | |
355 | if ( IsControl() ) | |
356 | { | |
357 | GetControl()->Enable( enable ); | |
358 | } | |
359 | else if ( IsButton() ) | |
360 | { | |
361 | #if wxMAC_USE_NATIVE_TOOLBAR | |
362 | if ( m_toolbarItemRef != NULL ) | |
363 | HIToolbarItemSetEnabled( m_toolbarItemRef, enable ); | |
364 | #endif | |
365 | ||
366 | if ( m_controlHandle != NULL ) | |
367 | { | |
368 | #if TARGET_API_MAC_OSX | |
369 | if ( enable ) | |
370 | EnableControl( m_controlHandle ); | |
371 | else | |
372 | DisableControl( m_controlHandle ); | |
373 | #else | |
374 | if ( enable ) | |
375 | ActivateControl( m_controlHandle ); | |
376 | else | |
377 | DeactivateControl( m_controlHandle ); | |
378 | #endif | |
379 | } | |
380 | } | |
381 | ||
382 | return true; | |
383 | } | |
384 | ||
385 | void wxToolBarTool::SetPosition( const wxPoint& position ) | |
386 | { | |
387 | m_x = position.x; | |
388 | m_y = position.y; | |
389 | ||
390 | int x, y; | |
391 | x = y = 0; | |
392 | int mac_x = position.x; | |
393 | int mac_y = position.y; | |
394 | ||
395 | if ( IsButton() ) | |
396 | { | |
397 | Rect contrlRect; | |
398 | GetControlBounds( m_controlHandle, &contrlRect ); | |
399 | int former_mac_x = contrlRect.left; | |
400 | int former_mac_y = contrlRect.top; | |
401 | GetToolBar()->GetToolSize(); | |
402 | ||
403 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) | |
404 | { | |
405 | UMAMoveControl( m_controlHandle, mac_x, mac_y ); | |
406 | } | |
407 | } | |
408 | else if ( IsControl() ) | |
409 | { | |
410 | GetControl()->Move( position ); | |
411 | } | |
412 | else | |
413 | { | |
414 | // separator | |
415 | #ifdef __WXMAC_OSX__ | |
416 | Rect contrlRect; | |
417 | GetControlBounds( m_controlHandle, &contrlRect ); | |
418 | int former_mac_x = contrlRect.left; | |
419 | int former_mac_y = contrlRect.top; | |
420 | ||
421 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) | |
422 | UMAMoveControl( m_controlHandle, mac_x, mac_y ); | |
423 | #endif | |
424 | } | |
425 | } | |
426 | ||
427 | void wxToolBarTool::UpdateToggleImage( bool toggle ) | |
428 | { | |
429 | #if wxMAC_USE_NATIVE_TOOLBAR | |
430 | ||
431 | #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4 | |
432 | #define kHIToolbarItemSelected (1 << 7) | |
433 | #endif | |
434 | ||
435 | // FIXME: this should be a OSX v10.4 runtime check | |
436 | if (m_toolbarItemRef != NULL) | |
437 | { | |
438 | OptionBits addAttrs, removeAttrs; | |
439 | OSStatus result; | |
440 | ||
441 | if (toggle) | |
442 | { | |
443 | addAttrs = kHIToolbarItemSelected; | |
444 | removeAttrs = kHIToolbarItemNoAttributes; | |
445 | } | |
446 | else | |
447 | { | |
448 | addAttrs = kHIToolbarItemNoAttributes; | |
449 | removeAttrs = kHIToolbarItemSelected; | |
450 | } | |
451 | ||
452 | result = HIToolbarItemChangeAttributes( m_toolbarItemRef, addAttrs, removeAttrs ); | |
453 | } | |
454 | #endif | |
455 | ||
456 | #ifdef __WXMAC_OSX__ | |
457 | if ( toggle ) | |
458 | { | |
459 | int w = m_bmpNormal.GetWidth(); | |
460 | int h = m_bmpNormal.GetHeight(); | |
461 | wxBitmap bmp( w, h ); | |
462 | wxMemoryDC dc; | |
463 | ||
464 | dc.SelectObject( bmp ); | |
465 | dc.SetPen( wxNullPen ); | |
466 | dc.SetBackground( *wxWHITE ); | |
467 | dc.DrawRectangle( 0, 0, w, h ); | |
468 | dc.DrawBitmap( m_bmpNormal, 0, 0, true ); | |
469 | dc.SelectObject( wxNullBitmap ); | |
470 | ControlButtonContentInfo info; | |
471 | wxMacCreateBitmapButton( &info, bmp ); | |
472 | SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info ); | |
473 | wxMacReleaseBitmapButton( &info ); | |
474 | } | |
475 | else | |
476 | { | |
477 | ControlButtonContentInfo info; | |
478 | wxMacCreateBitmapButton( &info, m_bmpNormal ); | |
479 | SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info ); | |
480 | wxMacReleaseBitmapButton( &info ); | |
481 | } | |
482 | ||
483 | IconTransformType transform = toggle ? kTransformSelected : kTransformNone; | |
484 | SetControlData( | |
485 | m_controlHandle, 0, kControlIconTransformTag, | |
486 | sizeof(transform), (Ptr)&transform ); | |
487 | HIViewSetNeedsDisplay( m_controlHandle, true ); | |
488 | ||
489 | #else | |
490 | ::SetControl32BitValue( m_controlHandle, toggle ); | |
491 | #endif | |
492 | } | |
493 | ||
494 | wxToolBarTool::wxToolBarTool( | |
495 | wxToolBar *tbar, | |
496 | int id, | |
497 | const wxString& label, | |
498 | const wxBitmap& bmpNormal, | |
499 | const wxBitmap& bmpDisabled, | |
500 | wxItemKind kind, | |
501 | wxObject *clientData, | |
502 | const wxString& shortHelp, | |
503 | const wxString& longHelp ) | |
504 | : | |
505 | wxToolBarToolBase( | |
506 | tbar, id, label, bmpNormal, bmpDisabled, kind, | |
507 | clientData, shortHelp, longHelp ) | |
508 | { | |
509 | Init(); | |
510 | } | |
511 | ||
512 | #pragma mark - | |
513 | #pragma mark Toolbar Implementation | |
514 | ||
515 | wxToolBarToolBase *wxToolBar::CreateTool( | |
516 | int id, | |
517 | const wxString& label, | |
518 | const wxBitmap& bmpNormal, | |
519 | const wxBitmap& bmpDisabled, | |
520 | wxItemKind kind, | |
521 | wxObject *clientData, | |
522 | const wxString& shortHelp, | |
523 | const wxString& longHelp ) | |
524 | { | |
525 | return new wxToolBarTool( | |
526 | this, id, label, bmpNormal, bmpDisabled, kind, | |
527 | clientData, shortHelp, longHelp ); | |
528 | } | |
529 | ||
530 | wxToolBarToolBase * wxToolBar::CreateTool( wxControl *control ) | |
531 | { | |
532 | return new wxToolBarTool( this, control ); | |
533 | } | |
534 | ||
535 | void wxToolBar::Init() | |
536 | { | |
537 | m_maxWidth = -1; | |
538 | m_maxHeight = -1; | |
539 | m_defaultWidth = kwxMacToolBarToolDefaultWidth; | |
540 | m_defaultHeight = kwxMacToolBarToolDefaultHeight; | |
541 | ||
542 | #if wxMAC_USE_NATIVE_TOOLBAR | |
543 | m_macHIToolbarRef = NULL; | |
544 | m_macUsesNativeToolbar = false; | |
545 | #endif | |
546 | } | |
547 | ||
548 | #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" ) | |
549 | ||
550 | const EventTypeSpec kEvents[] = | |
551 | { | |
552 | { kEventClassHIObject, kEventHIObjectConstruct }, | |
553 | { kEventClassHIObject, kEventHIObjectInitialize }, | |
554 | { kEventClassHIObject, kEventHIObjectDestruct }, | |
555 | ||
556 | { kEventClassToolbarItem, kEventToolbarItemCreateCustomView } | |
557 | }; | |
558 | ||
559 | const EventTypeSpec kViewEvents[] = | |
560 | { | |
561 | { kEventClassControl, kEventControlGetSizeConstraints } | |
562 | }; | |
563 | ||
564 | struct ControlToolbarItem | |
565 | { | |
566 | HIToolbarItemRef toolbarItem; | |
567 | HIViewRef viewRef; | |
568 | wxSize lastValidSize ; | |
569 | }; | |
570 | ||
571 | static pascal OSStatus ControlToolbarItemHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData ) | |
572 | { | |
573 | OSStatus result = eventNotHandledErr; | |
574 | ControlToolbarItem* object = (ControlToolbarItem*)inUserData; | |
575 | ||
576 | switch ( GetEventClass( inEvent ) ) | |
577 | { | |
578 | case kEventClassHIObject: | |
579 | switch ( GetEventKind( inEvent ) ) | |
580 | { | |
581 | case kEventHIObjectConstruct: | |
582 | { | |
583 | HIObjectRef toolbarItem; | |
584 | ControlToolbarItem* item; | |
585 | ||
586 | GetEventParameter( inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL, | |
587 | sizeof( HIObjectRef ), NULL, &toolbarItem ); | |
588 | ||
589 | item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ; | |
590 | item->toolbarItem = toolbarItem ; | |
591 | item->viewRef = NULL ; | |
592 | ||
593 | SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item ); | |
594 | ||
595 | result = noErr ; | |
596 | } | |
597 | break; | |
598 | ||
599 | case kEventHIObjectInitialize: | |
600 | result = CallNextEventHandler( inCallRef, inEvent ); | |
601 | if ( result == noErr ) | |
602 | { | |
603 | CFDataRef data; | |
604 | GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL, | |
605 | sizeof( CFTypeRef ), NULL, &data ); | |
606 | ||
607 | HIViewRef viewRef ; | |
608 | ||
609 | wxASSERT_MSG( CFDataGetLength( data ) == sizeof( viewRef ) , wxT("Illegal Data passed") ) ; | |
610 | memcpy( &viewRef , CFDataGetBytePtr( data ) , sizeof( viewRef ) ) ; | |
611 | ||
612 | object->viewRef = (HIViewRef) viewRef ; | |
613 | ||
614 | result = noErr ; | |
615 | } | |
616 | break; | |
617 | ||
618 | case kEventHIObjectDestruct: | |
619 | free( object ) ; | |
620 | result = noErr; | |
621 | break; | |
622 | } | |
623 | break; | |
624 | ||
625 | case kEventClassToolbarItem: | |
626 | switch ( GetEventKind( inEvent ) ) | |
627 | { | |
628 | case kEventToolbarItemCreateCustomView: | |
629 | { | |
630 | HIViewRef viewRef = object->viewRef ; | |
631 | ||
632 | HIViewRemoveFromSuperview( viewRef ) ; | |
633 | HIViewSetVisible(viewRef, true) ; | |
634 | InstallEventHandler( GetControlEventTarget( viewRef ), ControlToolbarItemHandler, | |
635 | GetEventTypeCount( kViewEvents ), kViewEvents, object, NULL ); | |
636 | ||
637 | result = SetEventParameter( inEvent, kEventParamControlRef, typeControlRef, sizeof( HIViewRef ), &viewRef ); | |
638 | } | |
639 | break; | |
640 | } | |
641 | break; | |
642 | ||
643 | case kEventClassControl: | |
644 | switch ( GetEventKind( inEvent ) ) | |
645 | { | |
646 | case kEventControlGetSizeConstraints: | |
647 | { | |
648 | wxWindow* wxwindow = wxFindControlFromMacControl(object->viewRef ) ; | |
649 | if ( wxwindow ) | |
650 | { | |
651 | wxSize sz = wxwindow->GetSize() ; | |
652 | // during toolbar layout the native window sometimes gets negative sizes | |
653 | // so we always keep the last valid size here, to make sure we survive the | |
654 | // shuffle ... | |
655 | if ( sz.x > 0 && sz.y > 0 ) | |
656 | object->lastValidSize = sz ; | |
657 | else | |
658 | sz = object->lastValidSize ; | |
659 | ||
660 | HISize min, max; | |
661 | min.width = max.width = sz.x ; | |
662 | min.height = max.height = sz.y ; | |
663 | ||
664 | result = SetEventParameter( inEvent, kEventParamMinimumSize, typeHISize, | |
665 | sizeof( HISize ), &min ); | |
666 | ||
667 | result = SetEventParameter( inEvent, kEventParamMaximumSize, typeHISize, | |
668 | sizeof( HISize ), &max ); | |
669 | result = noErr ; | |
670 | } | |
671 | } | |
672 | break; | |
673 | } | |
674 | break; | |
675 | } | |
676 | ||
677 | return result; | |
678 | } | |
679 | ||
680 | void RegisterControlToolbarItemClass() | |
681 | { | |
682 | static bool sRegistered; | |
683 | ||
684 | if ( !sRegistered ) | |
685 | { | |
686 | HIObjectRegisterSubclass( kControlToolbarItemClassID, kHIToolbarItemClassID, 0, | |
687 | ControlToolbarItemHandler, GetEventTypeCount( kEvents ), kEvents, 0, NULL ); | |
688 | ||
689 | sRegistered = true; | |
690 | } | |
691 | } | |
692 | ||
693 | HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData) | |
694 | { | |
695 | RegisterControlToolbarItemClass(); | |
696 | ||
697 | OSStatus err; | |
698 | EventRef event; | |
699 | UInt32 options = kHIToolbarItemAllowDuplicates; | |
700 | HIToolbarItemRef result = NULL; | |
701 | ||
702 | err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event ); | |
703 | require_noerr( err, CantCreateEvent ); | |
704 | ||
705 | SetEventParameter( event, kEventParamAttributes, typeUInt32, sizeof( UInt32 ), &options ); | |
706 | SetEventParameter( event, kEventParamToolbarItemIdentifier, typeCFStringRef, sizeof( CFStringRef ), &inIdentifier ); | |
707 | ||
708 | if ( inConfigData ) | |
709 | SetEventParameter( event, kEventParamToolbarItemConfigData, typeCFTypeRef, sizeof( CFTypeRef ), &inConfigData ); | |
710 | ||
711 | err = HIObjectCreate( kControlToolbarItemClassID, event, (HIObjectRef*)&result ); | |
712 | check_noerr( err ); | |
713 | ||
714 | ReleaseEvent( event ); | |
715 | CantCreateEvent : | |
716 | return result ; | |
717 | } | |
718 | ||
719 | static const EventTypeSpec kToolbarEvents[] = | |
720 | { | |
721 | { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers }, | |
722 | { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers }, | |
723 | { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier }, | |
724 | }; | |
725 | ||
726 | static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData ) | |
727 | { | |
728 | OSStatus result = eventNotHandledErr; | |
729 | wxToolBar* toolbar = (wxToolBar*) inUserData ; | |
730 | CFMutableArrayRef array; | |
731 | ||
732 | switch ( GetEventKind( inEvent ) ) | |
733 | { | |
734 | case kEventToolbarGetDefaultIdentifiers: | |
735 | { | |
736 | GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL, | |
737 | sizeof( CFMutableArrayRef ), NULL, &array ); | |
738 | // not implemented yet | |
739 | // GetToolbarDefaultItems( array ); | |
740 | result = noErr; | |
741 | } | |
742 | break; | |
743 | ||
744 | case kEventToolbarGetAllowedIdentifiers: | |
745 | { | |
746 | GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL, | |
747 | sizeof( CFMutableArrayRef ), NULL, &array ); | |
748 | // not implemented yet | |
749 | // GetToolbarAllowedItems( array ); | |
750 | result = noErr; | |
751 | } | |
752 | break; | |
753 | case kEventToolbarCreateItemWithIdentifier: | |
754 | { | |
755 | HIToolbarItemRef item = NULL; | |
756 | CFTypeRef data = NULL; | |
757 | CFStringRef identifier = NULL ; | |
758 | ||
759 | GetEventParameter( inEvent, kEventParamToolbarItemIdentifier, typeCFStringRef, NULL, | |
760 | sizeof( CFStringRef ), NULL, &identifier ); | |
761 | ||
762 | GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL, | |
763 | sizeof( CFTypeRef ), NULL, &data ); | |
764 | ||
765 | if ( CFStringCompare( kControlToolbarItemClassID, identifier, kCFCompareBackwards ) == kCFCompareEqualTo ) | |
766 | { | |
767 | item = CreateControlToolbarItem( kControlToolbarItemClassID, data ); | |
768 | if ( item ) | |
769 | { | |
770 | SetEventParameter( inEvent, kEventParamToolbarItem, typeHIToolbarItemRef, | |
771 | sizeof( HIToolbarItemRef ), &item ); | |
772 | result = noErr; | |
773 | } | |
774 | } | |
775 | ||
776 | } | |
777 | break; | |
778 | } | |
779 | return result ; | |
780 | } | |
781 | ||
782 | // also for the toolbar we have the dual implementation: | |
783 | // only when MacInstallNativeToolbar is called is the native toolbar set as the window toolbar | |
784 | ||
785 | bool wxToolBar::Create( | |
786 | wxWindow *parent, | |
787 | wxWindowID id, | |
788 | const wxPoint& pos, | |
789 | const wxSize& size, | |
790 | long style, | |
791 | const wxString& name ) | |
792 | { | |
793 | if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) ) | |
794 | return false; | |
795 | ||
796 | OSStatus err = noErr; | |
797 | ||
798 | #if wxMAC_USE_NATIVE_TOOLBAR | |
799 | wxString labelStr = wxString::Format( wxT("%xd"), (int)this ); | |
800 | err = HIToolbarCreate( | |
801 | wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0, | |
802 | (HIToolbarRef*) &m_macHIToolbarRef ); | |
803 | ||
804 | if (m_macHIToolbarRef != NULL) | |
805 | { | |
806 | InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler, | |
807 | GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL ); | |
808 | ||
809 | HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault; | |
810 | HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall; | |
811 | ||
812 | if ( style & wxTB_NOICONS ) | |
813 | mode = kHIToolbarDisplayModeLabelOnly; | |
814 | else if ( style & wxTB_TEXT ) | |
815 | mode = kHIToolbarDisplayModeIconAndLabel; | |
816 | else | |
817 | mode = kHIToolbarDisplayModeIconOnly; | |
818 | ||
819 | HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode ); | |
820 | HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize ); | |
821 | } | |
822 | #endif | |
823 | ||
824 | return (err == noErr); | |
825 | } | |
826 | ||
827 | wxToolBar::~wxToolBar() | |
828 | { | |
829 | #if wxMAC_USE_NATIVE_TOOLBAR | |
830 | if (m_macHIToolbarRef != NULL) | |
831 | { | |
832 | // if this is the installed toolbar, then deinstall it | |
833 | if (m_macUsesNativeToolbar) | |
834 | MacInstallNativeToolbar( false ); | |
835 | ||
836 | CFRelease( (HIToolbarRef)m_macHIToolbarRef ); | |
837 | m_macHIToolbarRef = NULL; | |
838 | } | |
839 | #endif | |
840 | } | |
841 | ||
842 | bool wxToolBar::Show( bool show ) | |
843 | { | |
844 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); | |
845 | bool bResult = (tlw != NULL); | |
846 | ||
847 | if (bResult) | |
848 | { | |
849 | #if wxMAC_USE_NATIVE_TOOLBAR | |
850 | bool ownToolbarInstalled = false; | |
851 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
852 | if (ownToolbarInstalled) | |
853 | { | |
854 | bResult = (IsWindowToolbarVisible( tlw ) != show); | |
855 | if ( bResult ) | |
856 | ShowHideWindowToolbar( tlw, show, false ); | |
857 | } | |
858 | else | |
859 | bResult = wxToolBarBase::Show( show ); | |
860 | #else | |
861 | ||
862 | bResult = wxToolBarBase::Show( show ); | |
863 | #endif | |
864 | } | |
865 | ||
866 | return bResult; | |
867 | } | |
868 | ||
869 | bool wxToolBar::IsShown() const | |
870 | { | |
871 | bool bResult; | |
872 | ||
873 | #if wxMAC_USE_NATIVE_TOOLBAR | |
874 | bool ownToolbarInstalled; | |
875 | ||
876 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
877 | if (ownToolbarInstalled) | |
878 | { | |
879 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); | |
880 | bResult = IsWindowToolbarVisible( tlw ); | |
881 | } | |
882 | else | |
883 | bResult = wxToolBarBase::IsShown(); | |
884 | #else | |
885 | ||
886 | bResult = wxToolBarBase::IsShown(); | |
887 | #endif | |
888 | ||
889 | return bResult; | |
890 | } | |
891 | ||
892 | void wxToolBar::DoGetSize( int *width, int *height ) const | |
893 | { | |
894 | #if wxMAC_USE_NATIVE_TOOLBAR | |
895 | Rect boundsR; | |
896 | bool ownToolbarInstalled; | |
897 | ||
898 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
899 | if ( ownToolbarInstalled ) | |
900 | { | |
901 | // TODO: is this really a control ? | |
902 | GetControlBounds( (ControlRef) m_macHIToolbarRef, &boundsR ); | |
903 | if ( width != NULL ) | |
904 | *width = boundsR.right - boundsR.left; | |
905 | if ( height != NULL ) | |
906 | *height = boundsR.bottom - boundsR.top; | |
907 | } | |
908 | else | |
909 | wxToolBarBase::DoGetSize( width, height ); | |
910 | ||
911 | #else | |
912 | wxToolBarBase::DoGetSize( width, height ); | |
913 | #endif | |
914 | } | |
915 | ||
916 | wxSize wxToolBar::DoGetBestSize() const | |
917 | { | |
918 | int width, height; | |
919 | ||
920 | DoGetSize( &width, &height ); | |
921 | ||
922 | return wxSize( width, height ); | |
923 | } | |
924 | ||
925 | void wxToolBar::SetWindowStyleFlag( long style ) | |
926 | { | |
927 | wxToolBarBase::SetWindowStyleFlag( style ); | |
928 | ||
929 | #if wxMAC_USE_NATIVE_TOOLBAR | |
930 | if (m_macHIToolbarRef != NULL) | |
931 | { | |
932 | HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault; | |
933 | ||
934 | if ( style & wxTB_NOICONS ) | |
935 | mode = kHIToolbarDisplayModeLabelOnly; | |
936 | else if ( style & wxTB_TEXT ) | |
937 | mode = kHIToolbarDisplayModeIconAndLabel; | |
938 | else | |
939 | mode = kHIToolbarDisplayModeIconOnly; | |
940 | ||
941 | HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode ); | |
942 | } | |
943 | #endif | |
944 | } | |
945 | ||
946 | #if wxMAC_USE_NATIVE_TOOLBAR | |
947 | bool wxToolBar::MacWantsNativeToolbar() | |
948 | { | |
949 | return m_macUsesNativeToolbar; | |
950 | } | |
951 | ||
952 | bool wxToolBar::MacTopLevelHasNativeToolbar(bool *ownToolbarInstalled) const | |
953 | { | |
954 | bool bResultV = false; | |
955 | ||
956 | if (ownToolbarInstalled != NULL) | |
957 | *ownToolbarInstalled = false; | |
958 | ||
959 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); | |
960 | if (tlw != NULL) | |
961 | { | |
962 | HIToolbarRef curToolbarRef = NULL; | |
963 | OSStatus err = GetWindowToolbar( tlw, &curToolbarRef ); | |
964 | bResultV = ((err == noErr) && (curToolbarRef != NULL)); | |
965 | if (bResultV && (ownToolbarInstalled != NULL)) | |
966 | *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef); | |
967 | } | |
968 | ||
969 | return bResultV; | |
970 | } | |
971 | ||
972 | bool wxToolBar::MacInstallNativeToolbar(bool usesNative) | |
973 | { | |
974 | bool bResult = false; | |
975 | ||
976 | if (usesNative && (m_macHIToolbarRef == NULL)) | |
977 | return bResult; | |
978 | ||
979 | if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0)) | |
980 | return bResult; | |
981 | ||
982 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); | |
983 | if (tlw == NULL) | |
984 | return bResult; | |
985 | ||
986 | // check the existing toolbar | |
987 | HIToolbarRef curToolbarRef = NULL; | |
988 | OSStatus err = GetWindowToolbar( tlw, &curToolbarRef ); | |
989 | if (err != noErr) | |
990 | curToolbarRef = NULL; | |
991 | ||
992 | m_macUsesNativeToolbar = usesNative; | |
993 | ||
994 | if (m_macUsesNativeToolbar) | |
995 | { | |
996 | // only install toolbar if there isn't one installed already | |
997 | if (curToolbarRef == NULL) | |
998 | { | |
999 | bResult = true; | |
1000 | ||
1001 | SetWindowToolbar( tlw, (HIToolbarRef) m_macHIToolbarRef ); | |
1002 | ShowHideWindowToolbar( tlw, true, false ); | |
1003 | ChangeWindowAttributes( tlw, kWindowToolbarButtonAttribute, 0 ); | |
1004 | SetAutomaticControlDragTrackingEnabledForWindow( tlw, true ); | |
1005 | ||
1006 | Rect r = { 0, 0, 0, 0 }; | |
1007 | m_peer->SetRect( &r ); | |
1008 | SetSize( wxSIZE_AUTO_WIDTH, 0 ); | |
1009 | m_peer->SetVisibility( false, true ); | |
1010 | wxToolBarBase::Show( false ); | |
1011 | } | |
1012 | } | |
1013 | else | |
1014 | { | |
1015 | // only deinstall toolbar if this is the installed one | |
1016 | if (m_macHIToolbarRef == curToolbarRef) | |
1017 | { | |
1018 | bResult = true; | |
1019 | ||
1020 | ShowHideWindowToolbar( tlw, false, false ); | |
1021 | ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute ); | |
1022 | SetWindowToolbar( tlw, NULL ); | |
1023 | ||
1024 | m_peer->SetVisibility( true, true ); | |
1025 | } | |
1026 | } | |
1027 | ||
1028 | if (bResult) | |
1029 | InvalidateBestSize(); | |
1030 | ||
1031 | // wxLogDebug( wxT(" --> [%lx] - result [%s]"), (long)this, bResult ? wxT("T") : wxT("F") ); | |
1032 | return bResult; | |
1033 | } | |
1034 | #endif | |
1035 | ||
1036 | bool wxToolBar::Realize() | |
1037 | { | |
1038 | if (m_tools.GetCount() == 0) | |
1039 | return false; | |
1040 | ||
1041 | int maxWidth = 0; | |
1042 | int maxHeight = 0; | |
1043 | ||
1044 | int maxToolWidth = 0; | |
1045 | int maxToolHeight = 0; | |
1046 | ||
1047 | int x = m_xMargin + kwxMacToolBarLeftMargin; | |
1048 | int y = m_yMargin + kwxMacToolBarTopMargin; | |
1049 | ||
1050 | int tw, th; | |
1051 | GetSize( &tw, &th ); | |
1052 | ||
1053 | // find the maximum tool width and height | |
1054 | wxToolBarTool *tool; | |
1055 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
1056 | while ( node != NULL ) | |
1057 | { | |
1058 | tool = (wxToolBarTool *) node->GetData(); | |
1059 | if ( tool != NULL ) | |
1060 | { | |
1061 | wxSize sz = tool->GetSize(); | |
1062 | ||
1063 | if ( sz.x > maxToolWidth ) | |
1064 | maxToolWidth = sz.x; | |
1065 | if ( sz.y > maxToolHeight ) | |
1066 | maxToolHeight = sz.y; | |
1067 | } | |
1068 | ||
1069 | node = node->GetNext(); | |
1070 | } | |
1071 | ||
1072 | bool lastIsRadio = false; | |
1073 | bool curIsRadio = false; | |
1074 | bool setChoiceInGroup = false; | |
1075 | ||
1076 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1077 | CFIndex currentPosition = 0; | |
1078 | bool insertAll = false; | |
1079 | #endif | |
1080 | ||
1081 | node = m_tools.GetFirst(); | |
1082 | while ( node != NULL ) | |
1083 | { | |
1084 | tool = (wxToolBarTool*) node->GetData(); | |
1085 | if ( tool == NULL ) | |
1086 | { | |
1087 | node = node->GetNext(); | |
1088 | continue; | |
1089 | } | |
1090 | ||
1091 | // set tool position: | |
1092 | // for the moment just perform a single row/column alignment | |
1093 | wxSize cursize = tool->GetSize(); | |
1094 | if ( x + cursize.x > maxWidth ) | |
1095 | maxWidth = x + cursize.x; | |
1096 | if ( y + cursize.y > maxHeight ) | |
1097 | maxHeight = y + cursize.y; | |
1098 | ||
1099 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
1100 | { | |
1101 | int x1 = x + ( maxToolWidth - cursize.x ) / 2; | |
1102 | tool->SetPosition( wxPoint(x1, y) ); | |
1103 | } | |
1104 | else | |
1105 | { | |
1106 | int y1 = y + ( maxToolHeight - cursize.y ) / 2; | |
1107 | tool->SetPosition( wxPoint(x, y1) ); | |
1108 | } | |
1109 | ||
1110 | // update the item positioning state | |
1111 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
1112 | y += cursize.y + kwxMacToolSpacing; | |
1113 | else | |
1114 | x += cursize.x + kwxMacToolSpacing; | |
1115 | ||
1116 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1117 | // install in native HIToolbar | |
1118 | if ( m_macHIToolbarRef != NULL ) | |
1119 | { | |
1120 | HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef(); | |
1121 | if ( hiItemRef != NULL ) | |
1122 | { | |
1123 | if ( insertAll || (tool->GetIndex() != currentPosition) ) | |
1124 | { | |
1125 | OSStatus err = noErr; | |
1126 | if ( !insertAll ) | |
1127 | { | |
1128 | insertAll = true; | |
1129 | ||
1130 | // if this is the first tool that gets newly inserted or repositioned | |
1131 | // first remove all 'old' tools from here to the right, because of this | |
1132 | // all following tools will have to be reinserted (insertAll). i = 100 because there's | |
1133 | // no way to determine how many there are in a toolbar, so just a high number :-( | |
1134 | for ( CFIndex i = 100; i >= currentPosition; --i ) | |
1135 | { | |
1136 | err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i ); | |
1137 | } | |
1138 | ||
1139 | if (err != noErr) | |
1140 | { | |
1141 | wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err ); | |
1142 | wxFAIL_MSG( errMsg.c_str() ); | |
1143 | } | |
1144 | } | |
1145 | ||
1146 | err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition ); | |
1147 | if (err != noErr) | |
1148 | { | |
1149 | wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err ); | |
1150 | wxFAIL_MSG( errMsg.c_str() ); | |
1151 | } | |
1152 | ||
1153 | tool->SetIndex( currentPosition ); | |
1154 | } | |
1155 | ||
1156 | currentPosition++; | |
1157 | } | |
1158 | } | |
1159 | #endif | |
1160 | ||
1161 | // update radio button (and group) state | |
1162 | lastIsRadio = curIsRadio; | |
1163 | curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) ); | |
1164 | ||
1165 | if ( !curIsRadio ) | |
1166 | { | |
1167 | if ( tool->IsToggled() ) | |
1168 | DoToggleTool( tool, true ); | |
1169 | ||
1170 | setChoiceInGroup = false; | |
1171 | } | |
1172 | else | |
1173 | { | |
1174 | if ( !lastIsRadio ) | |
1175 | { | |
1176 | if ( tool->Toggle( true ) ) | |
1177 | { | |
1178 | DoToggleTool( tool, true ); | |
1179 | setChoiceInGroup = true; | |
1180 | } | |
1181 | } | |
1182 | else if ( tool->IsToggled() ) | |
1183 | { | |
1184 | if ( tool->IsToggled() ) | |
1185 | DoToggleTool( tool, true ); | |
1186 | ||
1187 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); | |
1188 | while ( nodePrev != NULL ) | |
1189 | { | |
1190 | wxToolBarToolBase *toggleTool = nodePrev->GetData(); | |
1191 | if ( (toggleTool == NULL) || !toggleTool->IsButton() || (toggleTool->GetKind() != wxITEM_RADIO) ) | |
1192 | break; | |
1193 | ||
1194 | if ( toggleTool->Toggle( false ) ) | |
1195 | DoToggleTool( toggleTool, false ); | |
1196 | ||
1197 | nodePrev = nodePrev->GetPrevious(); | |
1198 | } | |
1199 | } | |
1200 | } | |
1201 | ||
1202 | node = node->GetNext(); | |
1203 | } | |
1204 | ||
1205 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) | |
1206 | { | |
1207 | // if not set yet, only one row | |
1208 | if ( m_maxRows <= 0 ) | |
1209 | SetRows( 1 ); | |
1210 | ||
1211 | m_minWidth = maxWidth; | |
1212 | maxWidth = tw; | |
1213 | maxHeight += m_yMargin + kwxMacToolBarTopMargin; | |
1214 | m_minHeight = m_maxHeight = maxHeight; | |
1215 | } | |
1216 | else | |
1217 | { | |
1218 | // if not set yet, have one column | |
1219 | if ( (GetToolsCount() > 0) && (m_maxRows <= 0) ) | |
1220 | SetRows( GetToolsCount() ); | |
1221 | ||
1222 | m_minHeight = maxHeight; | |
1223 | maxHeight = th; | |
1224 | maxWidth += m_xMargin + kwxMacToolBarLeftMargin; | |
1225 | m_minWidth = m_maxWidth = maxWidth; | |
1226 | } | |
1227 | ||
1228 | #if 0 | |
1229 | // FIXME: should this be OSX-only? | |
1230 | { | |
1231 | bool wantNativeToolbar, ownToolbarInstalled; | |
1232 | ||
1233 | // attempt to install the native toolbar | |
1234 | wantNativeToolbar = ((GetWindowStyleFlag() & wxTB_VERTICAL) == 0); | |
1235 | MacInstallNativeToolbar( wantNativeToolbar ); | |
1236 | (void)MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); | |
1237 | if (!ownToolbarInstalled) | |
1238 | { | |
1239 | SetSize( maxWidth, maxHeight ); | |
1240 | InvalidateBestSize(); | |
1241 | } | |
1242 | } | |
1243 | #else | |
1244 | SetSize( maxWidth, maxHeight ); | |
1245 | InvalidateBestSize(); | |
1246 | #endif | |
1247 | ||
1248 | SetBestFittingSize(); | |
1249 | ||
1250 | return true; | |
1251 | } | |
1252 | ||
1253 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
1254 | { | |
1255 | m_defaultWidth = size.x + kwxMacToolBorder; | |
1256 | m_defaultHeight = size.y + kwxMacToolBorder; | |
1257 | ||
1258 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1259 | if (m_macHIToolbarRef != NULL) | |
1260 | { | |
1261 | int maxs = wxMax( size.x, size.y ); | |
1262 | HIToolbarDisplaySize sizeSpec; | |
1263 | if ( maxs > 32 ) | |
1264 | sizeSpec = kHIToolbarDisplaySizeNormal; | |
1265 | else if ( maxs > 24 ) | |
1266 | sizeSpec = kHIToolbarDisplaySizeDefault; | |
1267 | else | |
1268 | sizeSpec = kHIToolbarDisplaySizeSmall; | |
1269 | ||
1270 | HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec ); | |
1271 | } | |
1272 | #endif | |
1273 | } | |
1274 | ||
1275 | // The button size is bigger than the bitmap size | |
1276 | wxSize wxToolBar::GetToolSize() const | |
1277 | { | |
1278 | return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder); | |
1279 | } | |
1280 | ||
1281 | void wxToolBar::SetRows(int nRows) | |
1282 | { | |
1283 | // avoid resizing the frame uselessly | |
1284 | if ( nRows != m_maxRows ) | |
1285 | m_maxRows = nRows; | |
1286 | } | |
1287 | ||
1288 | void wxToolBar::MacSuperChangedPosition() | |
1289 | { | |
1290 | wxWindow::MacSuperChangedPosition(); | |
1291 | ||
1292 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1293 | if (! m_macUsesNativeToolbar ) | |
1294 | Realize(); | |
1295 | #else | |
1296 | ||
1297 | Realize(); | |
1298 | #endif | |
1299 | } | |
1300 | ||
1301 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const | |
1302 | { | |
1303 | wxToolBarTool *tool; | |
1304 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
1305 | while ( node != NULL ) | |
1306 | { | |
1307 | tool = (wxToolBarTool *)node->GetData(); | |
1308 | if (tool != NULL) | |
1309 | { | |
1310 | wxRect2DInt r( tool->GetPosition(), tool->GetSize() ); | |
1311 | if ( r.Contains( wxPoint( x, y ) ) ) | |
1312 | return tool; | |
1313 | } | |
1314 | ||
1315 | node = node->GetNext(); | |
1316 | } | |
1317 | ||
1318 | return (wxToolBarToolBase*)NULL; | |
1319 | } | |
1320 | ||
1321 | wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) | |
1322 | { | |
1323 | wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y ); | |
1324 | if ( tool != NULL ) | |
1325 | return tool->GetShortHelp(); | |
1326 | ||
1327 | return wxEmptyString; | |
1328 | } | |
1329 | ||
1330 | void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) | |
1331 | { | |
1332 | if ( t != NULL ) | |
1333 | ((wxToolBarTool*)t)->DoEnable( enable ); | |
1334 | } | |
1335 | ||
1336 | void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) | |
1337 | { | |
1338 | wxToolBarTool *tool = (wxToolBarTool *)t; | |
1339 | if ( ( tool != NULL ) && tool->IsButton() ) | |
1340 | tool->UpdateToggleImage( toggle ); | |
1341 | } | |
1342 | ||
1343 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) | |
1344 | { | |
1345 | wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase ); | |
1346 | if (tool == NULL) | |
1347 | return false; | |
1348 | ||
1349 | WindowRef window = (WindowRef) MacGetTopLevelWindowRef(); | |
1350 | wxSize toolSize = GetToolSize(); | |
1351 | Rect toolrect = { 0, 0, toolSize.y, toolSize.x }; | |
1352 | ControlRef controlHandle = NULL; | |
1353 | OSStatus err = 0; | |
1354 | ||
1355 | switch (tool->GetStyle()) | |
1356 | { | |
1357 | case wxTOOL_STYLE_SEPARATOR: | |
1358 | { | |
1359 | wxASSERT( tool->GetControlHandle() == NULL ); | |
1360 | toolSize.x /= 4; | |
1361 | toolSize.y /= 4; | |
1362 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
1363 | toolrect.bottom = toolSize.y; | |
1364 | else | |
1365 | toolrect.right = toolSize.x; | |
1366 | ||
1367 | #ifdef __WXMAC_OSX__ | |
1368 | // in flat style we need a visual separator | |
1369 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1370 | HIToolbarItemRef item; | |
1371 | err = HIToolbarItemCreate( | |
1372 | kHIToolbarSeparatorIdentifier, | |
1373 | kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates, | |
1374 | &item ); | |
1375 | if (err == noErr) | |
1376 | tool->SetToolbarItemRef( item ); | |
1377 | #endif | |
1378 | ||
1379 | CreateSeparatorControl( window, &toolrect, &controlHandle ); | |
1380 | tool->SetControlHandle( controlHandle ); | |
1381 | #endif | |
1382 | } | |
1383 | break; | |
1384 | ||
1385 | case wxTOOL_STYLE_BUTTON: | |
1386 | { | |
1387 | wxASSERT( tool->GetControlHandle() == NULL ); | |
1388 | ControlButtonContentInfo info; | |
1389 | wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef ); | |
1390 | ||
1391 | if ( UMAGetSystemVersion() >= 0x1000) | |
1392 | { | |
1393 | CreateIconControl( window, &toolrect, &info, false, &controlHandle ); | |
1394 | } | |
1395 | else | |
1396 | { | |
1397 | SInt16 behaviour = kControlBehaviorOffsetContents; | |
1398 | if ( tool->CanBeToggled() ) | |
1399 | behaviour |= kControlBehaviorToggles; | |
1400 | err = CreateBevelButtonControl( window, | |
1401 | &toolrect, CFSTR(""), kControlBevelButtonNormalBevel, | |
1402 | behaviour, &info, 0, 0, 0, &controlHandle ); | |
1403 | } | |
1404 | ||
1405 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1406 | HIToolbarItemRef item; | |
1407 | wxString labelStr = wxString::Format(wxT("%xd"), (int)tool); | |
1408 | err = HIToolbarItemCreate( | |
1409 | wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()), | |
1410 | kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item ); | |
1411 | if (err == noErr) | |
1412 | { | |
1413 | InstallEventHandler( | |
1414 | HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(), | |
1415 | GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL ); | |
1416 | HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) ); | |
1417 | HIToolbarItemSetIconRef( item, info.u.iconRef ); | |
1418 | HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction ); | |
1419 | tool->SetToolbarItemRef( item ); | |
1420 | } | |
1421 | #endif | |
1422 | ||
1423 | wxMacReleaseBitmapButton( &info ); | |
1424 | ||
1425 | #if 0 | |
1426 | SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic ); | |
1427 | UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() ); | |
1428 | #endif | |
1429 | ||
1430 | InstallControlEventHandler( | |
1431 | (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(), | |
1432 | GetEventTypeCount(eventList), eventList, tool, NULL ); | |
1433 | ||
1434 | tool->SetControlHandle( controlHandle ); | |
1435 | } | |
1436 | break; | |
1437 | ||
1438 | case wxTOOL_STYLE_CONTROL: | |
1439 | ||
1440 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1441 | { | |
1442 | wxASSERT( tool->GetControl() != NULL ); | |
1443 | HIToolbarItemRef item; | |
1444 | HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ; | |
1445 | CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ; | |
1446 | err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID, | |
1447 | data , &item ) ; | |
1448 | ||
1449 | if (err == noErr) | |
1450 | { | |
1451 | tool->SetToolbarItemRef( item ); | |
1452 | } | |
1453 | CFRelease( data ) ; | |
1454 | } | |
1455 | ||
1456 | #else | |
1457 | // right now there's nothing to do here | |
1458 | #endif | |
1459 | break; | |
1460 | ||
1461 | default: | |
1462 | break; | |
1463 | } | |
1464 | ||
1465 | if ( err == noErr ) | |
1466 | { | |
1467 | if ( controlHandle ) | |
1468 | { | |
1469 | ControlRef container = (ControlRef) GetHandle(); | |
1470 | wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") ); | |
1471 | ||
1472 | UMAShowControl( controlHandle ); | |
1473 | ::EmbedControl( controlHandle, container ); | |
1474 | } | |
1475 | ||
1476 | if ( tool->CanBeToggled() && tool->IsToggled() ) | |
1477 | tool->UpdateToggleImage( true ); | |
1478 | ||
1479 | // nothing special to do here - we relayout in Realize() later | |
1480 | tool->Attach( this ); | |
1481 | InvalidateBestSize(); | |
1482 | } | |
1483 | else | |
1484 | { | |
1485 | wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err ); | |
1486 | wxFAIL_MSG( errMsg.c_str() ); | |
1487 | } | |
1488 | ||
1489 | return (err == noErr); | |
1490 | } | |
1491 | ||
1492 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) | |
1493 | { | |
1494 | wxFAIL_MSG( wxT("not implemented") ); | |
1495 | } | |
1496 | ||
1497 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) | |
1498 | { | |
1499 | wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase ); | |
1500 | wxToolBarToolsList::compatibility_iterator node; | |
1501 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) | |
1502 | { | |
1503 | wxToolBarToolBase *tool2 = node->GetData(); | |
1504 | if ( tool2 == tool ) | |
1505 | { | |
1506 | // let node point to the next node in the list | |
1507 | node = node->GetNext(); | |
1508 | ||
1509 | break; | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | wxSize sz = ((wxToolBarTool*)tool)->GetSize(); | |
1514 | ||
1515 | tool->Detach(); | |
1516 | ||
1517 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1518 | CFIndex removeIndex = tool->GetIndex(); | |
1519 | #endif | |
1520 | ||
1521 | switch ( tool->GetStyle() ) | |
1522 | { | |
1523 | case wxTOOL_STYLE_CONTROL: | |
1524 | { | |
1525 | tool->GetControl()->Destroy(); | |
1526 | tool->ClearControl(); | |
1527 | } | |
1528 | break; | |
1529 | ||
1530 | case wxTOOL_STYLE_BUTTON: | |
1531 | case wxTOOL_STYLE_SEPARATOR: | |
1532 | if ( tool->GetControlHandle() ) | |
1533 | { | |
1534 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1535 | if ( removeIndex != -1 && m_macHIToolbarRef ) | |
1536 | { | |
1537 | HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex ); | |
1538 | tool->SetIndex( -1 ); | |
1539 | } | |
1540 | #endif | |
1541 | ||
1542 | tool->ClearControl(); | |
1543 | } | |
1544 | break; | |
1545 | ||
1546 | default: | |
1547 | break; | |
1548 | } | |
1549 | ||
1550 | // and finally reposition all the controls after this one | |
1551 | ||
1552 | for ( /* node -> first after deleted */; node; node = node->GetNext() ) | |
1553 | { | |
1554 | wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); | |
1555 | wxPoint pt = tool2->GetPosition(); | |
1556 | ||
1557 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
1558 | pt.y -= sz.y; | |
1559 | else | |
1560 | pt.x -= sz.x; | |
1561 | ||
1562 | tool2->SetPosition( pt ); | |
1563 | ||
1564 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1565 | if ( removeIndex != -1 && tool2->GetIndex() > removeIndex ) | |
1566 | tool2->SetIndex( tool2->GetIndex() - 1 ); | |
1567 | #endif | |
1568 | } | |
1569 | ||
1570 | InvalidateBestSize(); | |
1571 | ||
1572 | return true; | |
1573 | } | |
1574 | ||
1575 | void wxToolBar::OnPaint(wxPaintEvent& event) | |
1576 | { | |
1577 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1578 | if ( m_macUsesNativeToolbar ) | |
1579 | { | |
1580 | event.Skip(true); | |
1581 | return; | |
1582 | } | |
1583 | #endif | |
1584 | ||
1585 | wxPaintDC dc(this); | |
1586 | ||
1587 | int w, h; | |
1588 | GetSize( &w, &h ); | |
1589 | ||
1590 | bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance(); | |
1591 | bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030); | |
1592 | ||
1593 | #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 | |
1594 | if ( !drawMetalTheme && minimumUmaAvailable ) | |
1595 | { | |
1596 | HIThemePlacardDrawInfo info; | |
1597 | memset( &info, 0, sizeof(info) ); | |
1598 | info.version = 0; | |
1599 | info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive; | |
1600 | ||
1601 | CGContextRef cgContext = (CGContextRef) MacGetCGContextRef(); | |
1602 | HIRect rect = CGRectMake( 0, 0, w, h ); | |
1603 | HIThemeDrawPlacard( &rect, &info, cgContext, kHIThemeOrientationNormal ); | |
1604 | } | |
1605 | else | |
1606 | { | |
1607 | // leave the background as it is (striped or metal) | |
1608 | } | |
1609 | ||
1610 | #else | |
1611 | ||
1612 | const bool drawBorder = true; | |
1613 | ||
1614 | if (drawBorder) | |
1615 | { | |
1616 | wxMacPortSetter helper( &dc ); | |
1617 | ||
1618 | if ( !drawMetalTheme || !minimumUmaAvailable ) | |
1619 | { | |
1620 | Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0), | |
1621 | dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) }; | |
1622 | ||
1623 | #if 0 | |
1624 | if ( toolbarrect.left < 0 ) | |
1625 | toolbarrect.left = 0; | |
1626 | if ( toolbarrect.top < 0 ) | |
1627 | toolbarrect.top = 0; | |
1628 | #endif | |
1629 | ||
1630 | UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive ); | |
1631 | } | |
1632 | else | |
1633 | { | |
1634 | #if TARGET_API_MAC_OSX | |
1635 | HIRect hiToolbarrect = CGRectMake( | |
1636 | dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0), | |
1637 | dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) ); | |
1638 | CGContextRef cgContext; | |
1639 | Rect bounds; | |
1640 | ||
1641 | GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds ); | |
1642 | QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
1643 | ||
1644 | CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top ); | |
1645 | CGContextScaleCTM( cgContext, 1, -1 ); | |
1646 | ||
1647 | HIThemeBackgroundDrawInfo drawInfo; | |
1648 | drawInfo.version = 0; | |
1649 | drawInfo.state = kThemeStateActive; | |
1650 | drawInfo.kind = kThemeBackgroundMetal; | |
1651 | HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal ); | |
1652 | ||
1653 | QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
1654 | #endif | |
1655 | } | |
1656 | } | |
1657 | #endif | |
1658 | ||
1659 | event.Skip(); | |
1660 | } | |
1661 | ||
1662 | #endif // wxUSE_TOOLBAR |