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