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