]>
Commit | Line | Data |
---|---|---|
e9576ca5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
3b6a1179 | 2 | // Name: src/mac/carbon/toolbar.cpp |
e9576ca5 | 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 | ||
e56d2520 | 16 | #include "wx/toolbar.h" |
e9576ca5 | 17 | |
4e3e485b WS |
18 | #ifndef WX_PRECOMP |
19 | #include "wx/wx.h" | |
20 | #endif | |
21 | ||
d497dca4 | 22 | #include "wx/mac/uma.h" |
bfe9ffbc | 23 | #include "wx/geometry.h" |
ee799df7 | 24 | |
991f71dc | 25 | |
ee799df7 | 26 | #ifdef __WXMAC_OSX__ |
3b6a1179 DS |
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; | |
ee799df7 | 33 | #else |
3b6a1179 DS |
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; | |
ee799df7 SC |
40 | #endif |
41 | ||
991f71dc DS |
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 | ||
e56d2520 SC |
50 | #pragma mark - |
51 | #pragma mark Tool Implementation | |
52 | ||
53 | ||
37e2cb08 SC |
54 | // ---------------------------------------------------------------------------- |
55 | // private classes | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
e56d2520 SC |
58 | // We have a dual implementation for each tool, ControlRef and HIToolbarItemRef |
59 | ||
37e2cb08 SC |
60 | class wxToolBarTool : public wxToolBarToolBase |
61 | { | |
62 | public: | |
3025abca DS |
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 ); | |
f3a65c3e | 73 | |
37e2cb08 SC |
74 | wxToolBarTool(wxToolBar *tbar, wxControl *control) |
75 | : wxToolBarToolBase(tbar, control) | |
76 | { | |
3b6a1179 | 77 | Init(); |
e56d2520 | 78 | if (control != NULL) |
3b6a1179 | 79 | SetControlHandle( (ControlRef) control->GetHandle() ); |
37e2cb08 | 80 | } |
f3a65c3e | 81 | |
d3c7fc99 | 82 | virtual ~wxToolBarTool() |
bfe9ffbc | 83 | { |
3b6a1179 | 84 | ClearControl(); |
991f71dc | 85 | |
e56d2520 SC |
86 | #if wxMAC_USE_NATIVE_TOOLBAR |
87 | if ( m_toolbarItemRef ) | |
3b6a1179 | 88 | CFRelease( m_toolbarItemRef ); |
e56d2520 | 89 | #endif |
bfe9ffbc | 90 | } |
f3a65c3e VZ |
91 | |
92 | WXWidget GetControlHandle() | |
93 | { | |
3b6a1179 | 94 | return (WXWidget) m_controlHandle; |
e56d2520 | 95 | } |
f3a65c3e VZ |
96 | |
97 | void SetControlHandle( ControlRef handle ) | |
98 | { | |
3b6a1179 | 99 | m_controlHandle = handle; |
e56d2520 | 100 | } |
37e2cb08 | 101 | |
3b6a1179 | 102 | void SetPosition( const wxPoint& position ); |
f3a65c3e VZ |
103 | |
104 | void ClearControl() | |
105 | { | |
3b6a1179 | 106 | m_control = NULL; |
8c07d8b3 SC |
107 | if ( m_controlHandle ) |
108 | { | |
9d5ccdd3 SC |
109 | if ( !IsControl() ) |
110 | DisposeControl( m_controlHandle ); | |
111 | else | |
112 | { | |
113 | // the embedded control is not under the responsibility of the tool | |
114 | } | |
8c07d8b3 SC |
115 | m_controlHandle = NULL ; |
116 | } | |
991f71dc | 117 | |
e56d2520 | 118 | #if wxMAC_USE_NATIVE_TOOLBAR |
3b6a1179 | 119 | m_toolbarItemRef = NULL; |
f3a65c3e | 120 | #endif |
e56d2520 | 121 | } |
f3a65c3e | 122 | |
bfe9ffbc SC |
123 | wxSize GetSize() const |
124 | { | |
3025abca DS |
125 | wxSize curSize; |
126 | ||
bfe9ffbc SC |
127 | if ( IsControl() ) |
128 | { | |
3025abca | 129 | curSize = GetControl()->GetSize(); |
bfe9ffbc SC |
130 | } |
131 | else if ( IsButton() ) | |
132 | { | |
3025abca | 133 | curSize = GetToolBar()->GetToolSize(); |
bfe9ffbc SC |
134 | } |
135 | else | |
136 | { | |
abbcdf3f | 137 | // separator size |
3025abca | 138 | curSize = GetToolBar()->GetToolSize(); |
5a904a32 | 139 | if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL ) |
3025abca | 140 | curSize.y /= 4; |
5a904a32 | 141 | else |
3025abca | 142 | curSize.x /= 4; |
bfe9ffbc | 143 | } |
3025abca DS |
144 | |
145 | return curSize; | |
bfe9ffbc | 146 | } |
991f71dc | 147 | |
bfe9ffbc SC |
148 | wxPoint GetPosition() const |
149 | { | |
3b6a1179 | 150 | return wxPoint( m_x, m_y ); |
f3a65c3e | 151 | } |
991f71dc | 152 | |
3b6a1179 | 153 | bool DoEnable( bool enable ); |
f3a65c3e | 154 | |
3b6a1179 | 155 | void UpdateToggleImage( bool toggle ); |
f3a65c3e VZ |
156 | |
157 | #if wxMAC_USE_NATIVE_TOOLBAR | |
158 | void SetToolbarItemRef( HIToolbarItemRef ref ) | |
159 | { | |
e56d2520 | 160 | if ( m_controlHandle ) |
3b6a1179 | 161 | HideControl( m_controlHandle ); |
e56d2520 | 162 | if ( m_toolbarItemRef ) |
3b6a1179 | 163 | CFRelease( m_toolbarItemRef ); |
991f71dc | 164 | |
3b6a1179 | 165 | m_toolbarItemRef = ref; |
e56d2520 SC |
166 | if ( m_toolbarItemRef ) |
167 | { | |
168 | HIToolbarItemSetHelpText( | |
991f71dc | 169 | m_toolbarItemRef, |
3b6a1179 DS |
170 | wxMacCFStringHolder( GetShortHelp(), GetToolBar()->GetFont().GetEncoding() ), |
171 | wxMacCFStringHolder( GetLongHelp(), GetToolBar()->GetFont().GetEncoding() ) ); | |
e56d2520 SC |
172 | } |
173 | } | |
991f71dc | 174 | |
f3a65c3e VZ |
175 | HIToolbarItemRef GetToolbarItemRef() const |
176 | { | |
3b6a1179 | 177 | return m_toolbarItemRef; |
e56d2520 | 178 | } |
df7998fc VZ |
179 | |
180 | void SetIndex( CFIndex idx ) | |
2c1dbc95 | 181 | { |
3b6a1179 | 182 | m_index = idx; |
2c1dbc95 SC |
183 | } |
184 | ||
df7998fc | 185 | CFIndex GetIndex() const |
2c1dbc95 | 186 | { |
3b6a1179 | 187 | return m_index; |
2c1dbc95 | 188 | } |
e56d2520 | 189 | #endif |
5d0bf05a | 190 | |
3b6a1179 | 191 | private: |
f3a65c3e | 192 | void Init() |
bfe9ffbc | 193 | { |
3b6a1179 | 194 | m_controlHandle = NULL; |
991f71dc | 195 | |
e56d2520 | 196 | #if wxMAC_USE_NATIVE_TOOLBAR |
3b6a1179 DS |
197 | m_toolbarItemRef = NULL; |
198 | m_index = -1; | |
e56d2520 | 199 | #endif |
bfe9ffbc | 200 | } |
991f71dc | 201 | |
3b6a1179 | 202 | ControlRef m_controlHandle; |
991f71dc DS |
203 | wxCoord m_x; |
204 | wxCoord m_y; | |
205 | ||
e56d2520 | 206 | #if wxMAC_USE_NATIVE_TOOLBAR |
3b6a1179 | 207 | HIToolbarItemRef m_toolbarItemRef; |
2c1dbc95 | 208 | // position in its toolbar, -1 means not inserted |
3b6a1179 | 209 | CFIndex m_index; |
e56d2520 | 210 | #endif |
37e2cb08 SC |
211 | }; |
212 | ||
facd6764 SC |
213 | static const EventTypeSpec eventList[] = |
214 | { | |
3b6a1179 | 215 | { kEventClassControl, kEventControlHit }, |
73fd9428 | 216 | #ifdef __WXMAC_OSX__ |
3b6a1179 | 217 | { kEventClassControl, kEventControlHitTest }, |
73fd9428 | 218 | #endif |
3b6a1179 | 219 | }; |
facd6764 | 220 | |
3b6a1179 | 221 | static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) |
facd6764 | 222 | { |
3b6a1179 DS |
223 | OSStatus result = eventNotHandledErr; |
224 | ControlRef controlRef; | |
225 | wxMacCarbonEvent cEvent( event ); | |
facd6764 | 226 | |
3b6a1179 | 227 | cEvent.GetParameter( kEventParamDirectObject, &controlRef ); |
facd6764 | 228 | |
991f71dc | 229 | switch ( GetEventKind( event ) ) |
facd6764 | 230 | { |
3b6a1179 | 231 | case kEventControlHit: |
facd6764 | 232 | { |
3b6a1179 DS |
233 | wxToolBarTool *tbartool = (wxToolBarTool*)data; |
234 | wxToolBar *tbar = tbartool != NULL ? (wxToolBar*) (tbartool->GetToolBar()) : NULL; | |
991f71dc | 235 | if ((tbartool != NULL) && tbartool->CanBeToggled()) |
facd6764 | 236 | { |
e56d2520 | 237 | bool shouldToggle; |
991f71dc | 238 | |
64f553a4 | 239 | #ifdef __WXMAC_OSX__ |
e56d2520 | 240 | shouldToggle = !tbartool->IsToggled(); |
64f553a4 | 241 | #else |
3025abca | 242 | shouldToggle = (GetControl32BitValue( (ControlRef)(tbartool->GetControlHandle()) ) != 0); |
64f553a4 | 243 | #endif |
991f71dc | 244 | |
e56d2520 | 245 | tbar->ToggleTool( tbartool->GetId(), shouldToggle ); |
facd6764 | 246 | } |
991f71dc | 247 | |
e56d2520 SC |
248 | if (tbartool != NULL) |
249 | tbar->OnLeftClick( tbartool->GetId(), tbartool->IsToggled() ); | |
f3a65c3e | 250 | result = noErr; |
facd6764 | 251 | } |
3b6a1179 | 252 | break; |
5d0bf05a | 253 | |
73fd9428 | 254 | #ifdef __WXMAC_OSX__ |
3b6a1179 | 255 | case kEventControlHitTest: |
73fd9428 | 256 | { |
3b6a1179 DS |
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; | |
73fd9428 | 266 | } |
3b6a1179 | 267 | break; |
73fd9428 | 268 | #endif |
5d0bf05a | 269 | |
3b6a1179 DS |
270 | default: |
271 | break; | |
facd6764 | 272 | } |
991f71dc | 273 | |
3b6a1179 | 274 | return result; |
facd6764 SC |
275 | } |
276 | ||
3b6a1179 | 277 | static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) |
facd6764 | 278 | { |
3b6a1179 | 279 | OSStatus result = eventNotHandledErr; |
facd6764 SC |
280 | |
281 | switch ( GetEventClass( event ) ) | |
282 | { | |
3b6a1179 DS |
283 | case kEventClassControl: |
284 | result = wxMacToolBarToolControlEventHandler( handler, event, data ); | |
285 | break; | |
5d0bf05a | 286 | |
3b6a1179 DS |
287 | default: |
288 | break; | |
facd6764 | 289 | } |
991f71dc | 290 | |
3b6a1179 | 291 | return result; |
facd6764 SC |
292 | } |
293 | ||
294 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler ) | |
295 | ||
e56d2520 SC |
296 | #if wxMAC_USE_NATIVE_TOOLBAR |
297 | ||
e56d2520 SC |
298 | static const EventTypeSpec toolBarEventList[] = |
299 | { | |
3b6a1179 DS |
300 | { kEventClassToolbarItem, kEventToolbarItemPerformAction }, |
301 | }; | |
e56d2520 | 302 | |
3b6a1179 | 303 | static pascal OSStatus wxMacToolBarCommandEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) |
e56d2520 | 304 | { |
3b6a1179 | 305 | OSStatus result = eventNotHandledErr; |
f3a65c3e | 306 | |
991f71dc | 307 | switch ( GetEventKind( event ) ) |
e56d2520 | 308 | { |
3b6a1179 | 309 | case kEventToolbarItemPerformAction: |
e56d2520 | 310 | { |
3b6a1179 | 311 | wxToolBarTool* tbartool = (wxToolBarTool*) data; |
e56d2520 SC |
312 | if ( tbartool != NULL ) |
313 | { | |
991f71dc DS |
314 | wxToolBar *tbar = (wxToolBar*)(tbartool->GetToolBar()); |
315 | int toolID = tbartool->GetId(); | |
316 | ||
e56d2520 SC |
317 | if ( tbartool->CanBeToggled() ) |
318 | { | |
3025abca | 319 | if ( tbar != NULL ) |
2c1dbc95 | 320 | tbar->ToggleTool(toolID, !tbartool->IsToggled() ); |
e56d2520 | 321 | } |
991f71dc | 322 | |
3025abca | 323 | if ( tbar != NULL ) |
3b6a1179 | 324 | tbar->OnLeftClick( toolID, tbartool->IsToggled() ); |
f3a65c3e | 325 | result = noErr; |
e56d2520 SC |
326 | } |
327 | } | |
3b6a1179 | 328 | break; |
5d0bf05a | 329 | |
3b6a1179 DS |
330 | default: |
331 | break; | |
e56d2520 | 332 | } |
991f71dc | 333 | |
3b6a1179 | 334 | return result; |
e56d2520 SC |
335 | } |
336 | ||
337 | static pascal OSStatus wxMacToolBarEventHandler( EventHandlerCallRef handler, EventRef event, void *data ) | |
338 | { | |
3b6a1179 | 339 | OSStatus result = eventNotHandledErr; |
991f71dc DS |
340 | |
341 | switch ( GetEventClass( event ) ) | |
e56d2520 | 342 | { |
3b6a1179 DS |
343 | case kEventClassToolbarItem: |
344 | result = wxMacToolBarCommandEventHandler( handler, event, data ); | |
345 | break; | |
5d0bf05a | 346 | |
3b6a1179 DS |
347 | default: |
348 | break; | |
e56d2520 | 349 | } |
991f71dc | 350 | |
3b6a1179 | 351 | return result; |
e56d2520 SC |
352 | } |
353 | ||
354 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarEventHandler ) | |
355 | ||
356 | #endif | |
357 | ||
3b6a1179 | 358 | bool wxToolBarTool::DoEnable( bool enable ) |
a2fe01c9 SC |
359 | { |
360 | if ( IsControl() ) | |
361 | { | |
3b6a1179 | 362 | GetControl()->Enable( enable ); |
a2fe01c9 SC |
363 | } |
364 | else if ( IsButton() ) | |
365 | { | |
f3a65c3e | 366 | #if wxMAC_USE_NATIVE_TOOLBAR |
3025abca | 367 | if ( m_toolbarItemRef != NULL ) |
3b6a1179 | 368 | HIToolbarItemSetEnabled( m_toolbarItemRef, enable ); |
e56d2520 SC |
369 | #endif |
370 | ||
3025abca | 371 | if ( m_controlHandle != NULL ) |
e56d2520 | 372 | { |
a2fe01c9 | 373 | #if TARGET_API_MAC_OSX |
e56d2520 | 374 | if ( enable ) |
3b6a1179 | 375 | EnableControl( m_controlHandle ); |
e56d2520 | 376 | else |
3b6a1179 | 377 | DisableControl( m_controlHandle ); |
a2fe01c9 | 378 | #else |
e56d2520 | 379 | if ( enable ) |
3b6a1179 | 380 | ActivateControl( m_controlHandle ); |
e56d2520 | 381 | else |
3b6a1179 | 382 | DeactivateControl( m_controlHandle ); |
a2fe01c9 | 383 | #endif |
e56d2520 | 384 | } |
a2fe01c9 | 385 | } |
991f71dc | 386 | |
3b6a1179 | 387 | return true; |
a2fe01c9 | 388 | } |
bfe9ffbc | 389 | |
3b6a1179 | 390 | void wxToolBarTool::SetPosition( const wxPoint& position ) |
bfe9ffbc SC |
391 | { |
392 | m_x = position.x; | |
393 | m_y = position.y; | |
394 | ||
3b6a1179 DS |
395 | int x, y; |
396 | x = y = 0; | |
397 | int mac_x = position.x; | |
398 | int mac_y = position.y; | |
789ae0cf | 399 | |
789ae0cf SC |
400 | if ( IsButton() ) |
401 | { | |
3b6a1179 DS |
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(); | |
f3a65c3e | 407 | |
bfe9ffbc SC |
408 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) |
409 | { | |
3b6a1179 | 410 | UMAMoveControl( m_controlHandle, mac_x, mac_y ); |
bfe9ffbc SC |
411 | } |
412 | } | |
413 | else if ( IsControl() ) | |
414 | { | |
9d5ccdd3 SC |
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 | } | |
bfe9ffbc | 422 | } |
abbcdf3f SC |
423 | else |
424 | { | |
f3a65c3e | 425 | // separator |
abbcdf3f | 426 | #ifdef __WXMAC_OSX__ |
3b6a1179 DS |
427 | Rect contrlRect; |
428 | GetControlBounds( m_controlHandle, &contrlRect ); | |
429 | int former_mac_x = contrlRect.left; | |
430 | int former_mac_y = contrlRect.top; | |
f3a65c3e | 431 | |
abbcdf3f | 432 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) |
3b6a1179 | 433 | UMAMoveControl( m_controlHandle, mac_x, mac_y ); |
abbcdf3f SC |
434 | #endif |
435 | } | |
bfe9ffbc SC |
436 | } |
437 | ||
f3a65c3e | 438 | void wxToolBarTool::UpdateToggleImage( bool toggle ) |
73fd9428 SC |
439 | { |
440 | #ifdef __WXMAC_OSX__ | |
441 | if ( toggle ) | |
442 | { | |
3b6a1179 DS |
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 ); | |
ce9da810 RD |
449 | dc.SetPen( wxPen(*wxBLACK) ); |
450 | dc.SetBrush( wxBrush( *wxLIGHT_GREY )); | |
3b6a1179 DS |
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 ); | |
991f71dc | 456 | SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info ); |
ce9da810 RD |
457 | #if wxMAC_USE_NATIVE_TOOLBAR |
458 | if (m_toolbarItemRef != NULL) | |
459 | { | |
460 | HIToolbarItemSetIconRef( m_toolbarItemRef, info.u.iconRef ); | |
461 | } | |
462 | #endif | |
3b6a1179 | 463 | wxMacReleaseBitmapButton( &info ); |
73fd9428 SC |
464 | } |
465 | else | |
466 | { | |
3b6a1179 DS |
467 | ControlButtonContentInfo info; |
468 | wxMacCreateBitmapButton( &info, m_bmpNormal ); | |
3025abca | 469 | SetControlData( m_controlHandle, 0, kControlIconContentTag, sizeof(info), (Ptr)&info ); |
ce9da810 RD |
470 | #if wxMAC_USE_NATIVE_TOOLBAR |
471 | if (m_toolbarItemRef != NULL) | |
472 | { | |
473 | HIToolbarItemSetIconRef( m_toolbarItemRef, info.u.iconRef ); | |
474 | } | |
475 | #endif | |
3b6a1179 | 476 | wxMacReleaseBitmapButton( &info ); |
73fd9428 SC |
477 | } |
478 | ||
3b6a1179 DS |
479 | IconTransformType transform = toggle ? kTransformSelected : kTransformNone; |
480 | SetControlData( | |
481 | m_controlHandle, 0, kControlIconTransformTag, | |
3025abca | 482 | sizeof(transform), (Ptr)&transform ); |
3b6a1179 | 483 | HIViewSetNeedsDisplay( m_controlHandle, true ); |
73fd9428 SC |
484 | |
485 | #else | |
3b6a1179 | 486 | ::SetControl32BitValue( m_controlHandle, toggle ); |
73fd9428 SC |
487 | #endif |
488 | } | |
489 | ||
3b6a1179 DS |
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 ) | |
bfe9ffbc | 504 | { |
f4e0be4f | 505 | Init(); |
bfe9ffbc SC |
506 | } |
507 | ||
e56d2520 SC |
508 | #pragma mark - |
509 | #pragma mark Toolbar Implementation | |
2f1ae414 | 510 | |
3b6a1179 DS |
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 ) | |
37e2cb08 | 520 | { |
3b6a1179 DS |
521 | return new wxToolBarTool( |
522 | this, id, label, bmpNormal, bmpDisabled, kind, | |
523 | clientData, shortHelp, longHelp ); | |
37e2cb08 SC |
524 | } |
525 | ||
3b6a1179 | 526 | wxToolBarToolBase * wxToolBar::CreateTool( wxControl *control ) |
37e2cb08 | 527 | { |
3025abca | 528 | return new wxToolBarTool( this, control ); |
37e2cb08 SC |
529 | } |
530 | ||
37e2cb08 | 531 | void wxToolBar::Init() |
e9576ca5 | 532 | { |
e40298d5 JS |
533 | m_maxWidth = -1; |
534 | m_maxHeight = -1; | |
535 | m_defaultWidth = kwxMacToolBarToolDefaultWidth; | |
536 | m_defaultHeight = kwxMacToolBarToolDefaultHeight; | |
991f71dc | 537 | |
e56d2520 | 538 | #if wxMAC_USE_NATIVE_TOOLBAR |
3b6a1179 DS |
539 | m_macHIToolbarRef = NULL; |
540 | m_macUsesNativeToolbar = false; | |
e56d2520 | 541 | #endif |
e9576ca5 SC |
542 | } |
543 | ||
0ac091ae | 544 | #define kControlToolbarItemClassID CFSTR( "org.wxwidgets.controltoolbaritem" ) |
6d4835dc SC |
545 | |
546 | const EventTypeSpec kEvents[] = | |
547 | { | |
0ac091ae RD |
548 | { kEventClassHIObject, kEventHIObjectConstruct }, |
549 | { kEventClassHIObject, kEventHIObjectInitialize }, | |
550 | { kEventClassHIObject, kEventHIObjectDestruct }, | |
551 | ||
552 | { kEventClassToolbarItem, kEventToolbarItemCreateCustomView } | |
6d4835dc SC |
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 | { | |
0ac091ae RD |
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, | |
6d4835dc | 583 | sizeof( HIObjectRef ), NULL, &toolbarItem ); |
0ac091ae | 584 | |
6d4835dc SC |
585 | item = (ControlToolbarItem*) malloc(sizeof(ControlToolbarItem)) ; |
586 | item->toolbarItem = toolbarItem ; | |
587 | item->viewRef = NULL ; | |
588 | ||
0ac091ae | 589 | SetEventParameter( inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof( void * ), &item ); |
6d4835dc SC |
590 | |
591 | result = noErr ; | |
0ac091ae RD |
592 | } |
593 | break; | |
6d4835dc SC |
594 | |
595 | case kEventHIObjectInitialize: | |
596 | result = CallNextEventHandler( inCallRef, inEvent ); | |
0ac091ae | 597 | if ( result == noErr ) |
6d4835dc SC |
598 | { |
599 | CFDataRef data; | |
600 | GetEventParameter( inEvent, kEventParamToolbarItemConfigData, typeCFTypeRef, NULL, | |
601 | sizeof( CFTypeRef ), NULL, &data ); | |
0ac091ae | 602 | |
6d4835dc SC |
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 | ||
0ac091ae RD |
610 | result = noErr ; |
611 | } | |
6d4835dc SC |
612 | break; |
613 | ||
0ac091ae | 614 | case kEventHIObjectDestruct: |
6d4835dc | 615 | free( object ) ; |
0ac091ae RD |
616 | result = noErr; |
617 | break; | |
618 | } | |
619 | break; | |
620 | ||
621 | case kEventClassToolbarItem: | |
622 | switch ( GetEventKind( inEvent ) ) | |
623 | { | |
624 | case kEventToolbarItemCreateCustomView: | |
625 | { | |
6d4835dc SC |
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 ); | |
0ac091ae RD |
634 | } |
635 | break; | |
636 | } | |
637 | break; | |
638 | ||
639 | case kEventClassControl: | |
640 | switch ( GetEventKind( inEvent ) ) | |
641 | { | |
642 | case kEventControlGetSizeConstraints: | |
643 | { | |
6d4835dc SC |
644 | wxWindow* wxwindow = wxFindControlFromMacControl(object->viewRef ) ; |
645 | if ( wxwindow ) | |
646 | { | |
647 | wxSize sz = wxwindow->GetSize() ; | |
da14a87d SC |
648 | sz.x -= wxwindow->MacGetLeftBorderSize() + wxwindow->MacGetRightBorderSize(); |
649 | sz.y -= wxwindow->MacGetTopBorderSize() + wxwindow->MacGetBottomBorderSize(); | |
6d4835dc SC |
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 | } | |
0ac091ae RD |
669 | } |
670 | break; | |
671 | } | |
672 | break; | |
673 | } | |
674 | ||
675 | return result; | |
6d4835dc SC |
676 | } |
677 | ||
678 | void RegisterControlToolbarItemClass() | |
679 | { | |
0ac091ae RD |
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 | } | |
6d4835dc SC |
689 | } |
690 | ||
691 | HIToolbarItemRef CreateControlToolbarItem(CFStringRef inIdentifier, CFTypeRef inConfigData) | |
692 | { | |
0ac091ae RD |
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 ); | |
6d4835dc | 711 | |
0ac091ae RD |
712 | ReleaseEvent( event ); |
713 | CantCreateEvent : | |
714 | return result ; | |
6d4835dc SC |
715 | } |
716 | ||
717 | static const EventTypeSpec kToolbarEvents[] = | |
718 | { | |
0ac091ae RD |
719 | { kEventClassToolbar, kEventToolbarGetDefaultIdentifiers }, |
720 | { kEventClassToolbar, kEventToolbarGetAllowedIdentifiers }, | |
721 | { kEventClassToolbar, kEventToolbarCreateItemWithIdentifier }, | |
6d4835dc SC |
722 | }; |
723 | ||
724 | static OSStatus ToolbarDelegateHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData ) | |
725 | { | |
0ac091ae | 726 | OSStatus result = eventNotHandledErr; |
9d5ccdd3 SC |
727 | // Not yet needed |
728 | // wxToolBar* toolbar = (wxToolBar*) inUserData ; | |
0ac091ae | 729 | CFMutableArrayRef array; |
6d4835dc | 730 | |
0ac091ae RD |
731 | switch ( GetEventKind( inEvent ) ) |
732 | { | |
733 | case kEventToolbarGetDefaultIdentifiers: | |
6d4835dc SC |
734 | { |
735 | GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL, | |
0ac091ae | 736 | sizeof( CFMutableArrayRef ), NULL, &array ); |
6d4835dc SC |
737 | // not implemented yet |
738 | // GetToolbarDefaultItems( array ); | |
739 | result = noErr; | |
740 | } | |
0ac091ae RD |
741 | break; |
742 | ||
743 | case kEventToolbarGetAllowedIdentifiers: | |
6d4835dc SC |
744 | { |
745 | GetEventParameter( inEvent, kEventParamMutableArray, typeCFMutableArrayRef, NULL, | |
0ac091ae | 746 | sizeof( CFMutableArrayRef ), NULL, &array ); |
6d4835dc SC |
747 | // not implemented yet |
748 | // GetToolbarAllowedItems( array ); | |
749 | result = noErr; | |
750 | } | |
0ac091ae RD |
751 | break; |
752 | case kEventToolbarCreateItemWithIdentifier: | |
753 | { | |
754 | HIToolbarItemRef item = NULL; | |
755 | CFTypeRef data = NULL; | |
6d4835dc | 756 | CFStringRef identifier = NULL ; |
0ac091ae RD |
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 | ||
6d4835dc SC |
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 | } | |
0ac091ae | 773 | } |
6d4835dc | 774 | |
0ac091ae RD |
775 | } |
776 | break; | |
6d4835dc SC |
777 | } |
778 | return result ; | |
779 | } | |
780 | ||
e56d2520 SC |
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 | |
6d4835dc | 783 | |
3b6a1179 DS |
784 | bool wxToolBar::Create( |
785 | wxWindow *parent, | |
991f71dc DS |
786 | wxWindowID id, |
787 | const wxPoint& pos, | |
788 | const wxSize& size, | |
789 | long style, | |
3b6a1179 | 790 | const wxString& name ) |
5d0bf05a | 791 | { |
3b6a1179 DS |
792 | if ( !wxToolBarBase::Create( parent, id, pos, size, style, wxDefaultValidator, name ) ) |
793 | return false; | |
e56d2520 | 794 | |
991f71dc | 795 | OSStatus err = noErr; |
e56d2520 SC |
796 | |
797 | #if wxMAC_USE_NATIVE_TOOLBAR | |
3b6a1179 DS |
798 | wxString labelStr = wxString::Format( wxT("%xd"), (int)this ); |
799 | err = HIToolbarCreate( | |
800 | wxMacCFStringHolder( labelStr, wxFont::GetDefaultEncoding() ), 0, | |
801 | (HIToolbarRef*) &m_macHIToolbarRef ); | |
e56d2520 SC |
802 | |
803 | if (m_macHIToolbarRef != NULL) | |
f3a65c3e | 804 | { |
0ac091ae RD |
805 | InstallEventHandler( HIObjectGetEventTarget((HIToolbarRef)m_macHIToolbarRef ), ToolbarDelegateHandler, |
806 | GetEventTypeCount( kToolbarEvents ), kToolbarEvents, this, NULL ); | |
6d4835dc | 807 | |
3b6a1179 DS |
808 | HIToolbarDisplayMode mode = kHIToolbarDisplayModeDefault; |
809 | HIToolbarDisplaySize displaySize = kHIToolbarDisplaySizeSmall; | |
e56d2520 SC |
810 | |
811 | if ( style & wxTB_NOICONS ) | |
3b6a1179 | 812 | mode = kHIToolbarDisplayModeLabelOnly; |
e56d2520 | 813 | else if ( style & wxTB_TEXT ) |
3b6a1179 | 814 | mode = kHIToolbarDisplayModeIconAndLabel; |
e56d2520 | 815 | else |
3b6a1179 | 816 | mode = kHIToolbarDisplayModeIconOnly; |
e56d2520 | 817 | |
3b6a1179 DS |
818 | HIToolbarSetDisplayMode( (HIToolbarRef) m_macHIToolbarRef, mode ); |
819 | HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, displaySize ); | |
e56d2520 SC |
820 | } |
821 | #endif | |
822 | ||
991f71dc | 823 | return (err == noErr); |
e9576ca5 SC |
824 | } |
825 | ||
826 | wxToolBar::~wxToolBar() | |
f3a65c3e | 827 | { |
e56d2520 | 828 | #if wxMAC_USE_NATIVE_TOOLBAR |
3025abca | 829 | if (m_macHIToolbarRef != NULL) |
e56d2520 SC |
830 | { |
831 | // if this is the installed toolbar, then deinstall it | |
832 | if (m_macUsesNativeToolbar) | |
833 | MacInstallNativeToolbar( false ); | |
834 | ||
3025abca | 835 | CFRelease( (HIToolbarRef)m_macHIToolbarRef ); |
e56d2520 SC |
836 | m_macHIToolbarRef = NULL; |
837 | } | |
838 | #endif | |
839 | } | |
840 | ||
e56d2520 SC |
841 | bool wxToolBar::Show( bool show ) |
842 | { | |
e56d2520 | 843 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); |
3025abca | 844 | bool bResult = (tlw != NULL); |
f3a65c3e | 845 | |
e56d2520 SC |
846 | if (bResult) |
847 | { | |
848 | #if wxMAC_USE_NATIVE_TOOLBAR | |
f3a65c3e | 849 | bool ownToolbarInstalled = false; |
e56d2520 SC |
850 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); |
851 | if (ownToolbarInstalled) | |
852 | { | |
3025abca | 853 | bResult = (IsWindowToolbarVisible( tlw ) != show); |
a749a99f SC |
854 | if ( bResult ) |
855 | ShowHideWindowToolbar( tlw, show, false ); | |
e56d2520 SC |
856 | } |
857 | else | |
e56d2520 | 858 | bResult = wxToolBarBase::Show( show ); |
3025abca DS |
859 | #else |
860 | ||
861 | bResult = wxToolBarBase::Show( show ); | |
862 | #endif | |
e56d2520 | 863 | } |
f3a65c3e | 864 | |
e56d2520 SC |
865 | return bResult; |
866 | } | |
867 | ||
868 | bool wxToolBar::IsShown() const | |
869 | { | |
870 | bool bResult; | |
871 | ||
872 | #if wxMAC_USE_NATIVE_TOOLBAR | |
3b6a1179 | 873 | bool ownToolbarInstalled; |
3025abca | 874 | |
e56d2520 SC |
875 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); |
876 | if (ownToolbarInstalled) | |
a749a99f SC |
877 | { |
878 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); | |
3b6a1179 | 879 | bResult = IsWindowToolbarVisible( tlw ); |
a749a99f | 880 | } |
e56d2520 | 881 | else |
e56d2520 | 882 | bResult = wxToolBarBase::IsShown(); |
3025abca DS |
883 | #else |
884 | ||
885 | bResult = wxToolBarBase::IsShown(); | |
886 | #endif | |
f3a65c3e | 887 | |
e56d2520 SC |
888 | return bResult; |
889 | } | |
890 | ||
e56d2520 SC |
891 | void wxToolBar::DoGetSize( int *width, int *height ) const |
892 | { | |
893 | #if wxMAC_USE_NATIVE_TOOLBAR | |
894 | Rect boundsR; | |
895 | bool ownToolbarInstalled; | |
f3a65c3e | 896 | |
e56d2520 SC |
897 | MacTopLevelHasNativeToolbar( &ownToolbarInstalled ); |
898 | if ( ownToolbarInstalled ) | |
899 | { | |
991f71dc | 900 | // TODO: is this really a control ? |
e56d2520 SC |
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 | |
e56d2520 | 908 | wxToolBarBase::DoGetSize( width, height ); |
3025abca DS |
909 | |
910 | #else | |
911 | wxToolBarBase::DoGetSize( width, height ); | |
912 | #endif | |
e9576ca5 SC |
913 | } |
914 | ||
b13095df SC |
915 | wxSize wxToolBar::DoGetBestSize() const |
916 | { | |
3b6a1179 | 917 | int width, height; |
991f71dc | 918 | |
3b6a1179 | 919 | DoGetSize( &width, &height ); |
991f71dc | 920 | |
3b6a1179 | 921 | return wxSize( width, height ); |
b13095df SC |
922 | } |
923 | ||
f3a65c3e | 924 | void wxToolBar::SetWindowStyleFlag( long style ) |
e56d2520 SC |
925 | { |
926 | wxToolBarBase::SetWindowStyleFlag( style ); | |
991f71dc | 927 | |
e56d2520 SC |
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; | |
f3a65c3e | 939 | |
e56d2520 SC |
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 ); | |
3025abca | 963 | bResultV = ((err == noErr) && (curToolbarRef != NULL)); |
e56d2520 SC |
964 | if (bResultV && (ownToolbarInstalled != NULL)) |
965 | *ownToolbarInstalled = (curToolbarRef == m_macHIToolbarRef); | |
966 | } | |
967 | ||
968 | return bResultV; | |
969 | } | |
970 | ||
f3a65c3e | 971 | bool wxToolBar::MacInstallNativeToolbar(bool usesNative) |
e56d2520 | 972 | { |
df7998fc | 973 | bool bResult = false; |
e56d2520 | 974 | |
e56d2520 SC |
975 | if (usesNative && (m_macHIToolbarRef == NULL)) |
976 | return bResult; | |
f3a65c3e | 977 | |
e56d2520 SC |
978 | if (usesNative && ((GetWindowStyleFlag() & wxTB_VERTICAL) != 0)) |
979 | return bResult; | |
f3a65c3e | 980 | |
3025abca DS |
981 | WindowRef tlw = MAC_WXHWND(MacGetTopLevelWindowRef()); |
982 | if (tlw == NULL) | |
983 | return bResult; | |
984 | ||
e56d2520 SC |
985 | // check the existing toolbar |
986 | HIToolbarRef curToolbarRef = NULL; | |
987 | OSStatus err = GetWindowToolbar( tlw, &curToolbarRef ); | |
991f71dc | 988 | if (err != noErr) |
e56d2520 SC |
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 ); | |
f3a65c3e | 1004 | |
3b6a1179 | 1005 | Rect r = { 0, 0, 0, 0 }; |
e56d2520 | 1006 | m_peer->SetRect( &r ); |
e56d2520 | 1007 | SetSize( wxSIZE_AUTO_WIDTH, 0 ); |
e56d2520 SC |
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 ); | |
3b6a1179 | 1020 | ChangeWindowAttributes( tlw, 0, kWindowToolbarButtonAttribute ); |
e56d2520 | 1021 | SetWindowToolbar( tlw, NULL ); |
f3a65c3e | 1022 | |
e56d2520 | 1023 | m_peer->SetVisibility( true, true ); |
e56d2520 SC |
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 | ||
37e2cb08 | 1035 | bool wxToolBar::Realize() |
e9576ca5 | 1036 | { |
eb22f2a6 | 1037 | if (m_tools.GetCount() == 0) |
3803c372 | 1038 | return false; |
0b7a8cd3 | 1039 | |
e56d2520 SC |
1040 | int maxWidth = 0; |
1041 | int maxHeight = 0; | |
f3a65c3e | 1042 | |
bfe9ffbc SC |
1043 | int maxToolWidth = 0; |
1044 | int maxToolHeight = 0; | |
f3a65c3e | 1045 | |
991f71dc DS |
1046 | int x = m_xMargin + kwxMacToolBarLeftMargin; |
1047 | int y = m_yMargin + kwxMacToolBarTopMargin; | |
1048 | ||
1049 | int tw, th; | |
1050 | GetSize( &tw, &th ); | |
1051 | ||
e56d2520 | 1052 | // find the maximum tool width and height |
3025abca DS |
1053 | wxToolBarTool *tool; |
1054 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
e56d2520 | 1055 | while ( node != NULL ) |
bfe9ffbc | 1056 | { |
3025abca | 1057 | tool = (wxToolBarTool *) node->GetData(); |
e56d2520 SC |
1058 | if ( tool != NULL ) |
1059 | { | |
1060 | wxSize sz = tool->GetSize(); | |
f3a65c3e | 1061 | |
e56d2520 SC |
1062 | if ( sz.x > maxToolWidth ) |
1063 | maxToolWidth = sz.x; | |
1064 | if ( sz.y > maxToolHeight ) | |
1065 | maxToolHeight = sz.y; | |
1066 | } | |
f3a65c3e | 1067 | |
bfe9ffbc SC |
1068 | node = node->GetNext(); |
1069 | } | |
f3a65c3e | 1070 | |
991f71dc DS |
1071 | bool lastIsRadio = false; |
1072 | bool curIsRadio = false; | |
1073 | bool setChoiceInGroup = false; | |
df7998fc VZ |
1074 | |
1075 | #if wxMAC_USE_NATIVE_TOOLBAR | |
3b6a1179 DS |
1076 | CFIndex currentPosition = 0; |
1077 | bool insertAll = false; | |
991f71dc | 1078 | #endif |
df7998fc | 1079 | |
991f71dc | 1080 | node = m_tools.GetFirst(); |
e56d2520 | 1081 | while ( node != NULL ) |
7810c95b | 1082 | { |
3025abca | 1083 | tool = (wxToolBarTool*) node->GetData(); |
e56d2520 | 1084 | if ( tool == NULL ) |
214b9484 | 1085 | { |
e56d2520 SC |
1086 | node = node->GetNext(); |
1087 | continue; | |
214b9484 | 1088 | } |
f3a65c3e | 1089 | |
991f71dc | 1090 | // set tool position: |
e56d2520 SC |
1091 | // for the moment just perform a single row/column alignment |
1092 | wxSize cursize = tool->GetSize(); | |
bfe9ffbc | 1093 | if ( x + cursize.x > maxWidth ) |
e56d2520 | 1094 | maxWidth = x + cursize.x; |
bfe9ffbc | 1095 | if ( y + cursize.y > maxHeight ) |
e56d2520 | 1096 | maxHeight = y + cursize.y; |
f3a65c3e | 1097 | |
73fd9428 SC |
1098 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
1099 | { | |
e56d2520 SC |
1100 | int x1 = x + ( maxToolWidth - cursize.x ) / 2; |
1101 | tool->SetPosition( wxPoint(x1, y) ); | |
73fd9428 SC |
1102 | } |
1103 | else | |
1104 | { | |
e56d2520 SC |
1105 | int y1 = y + ( maxToolHeight - cursize.y ) / 2; |
1106 | tool->SetPosition( wxPoint(x, y1) ); | |
1107 | } | |
f3a65c3e | 1108 | |
e56d2520 | 1109 | // update the item positioning state |
bfe9ffbc | 1110 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
e56d2520 SC |
1111 | y += cursize.y + kwxMacToolSpacing; |
1112 | else | |
1113 | x += cursize.x + kwxMacToolSpacing; | |
f3a65c3e | 1114 | |
e56d2520 SC |
1115 | #if wxMAC_USE_NATIVE_TOOLBAR |
1116 | // install in native HIToolbar | |
1117 | if ( m_macHIToolbarRef != NULL ) | |
bfe9ffbc | 1118 | { |
e56d2520 SC |
1119 | HIToolbarItemRef hiItemRef = tool->GetToolbarItemRef(); |
1120 | if ( hiItemRef != NULL ) | |
1121 | { | |
991f71dc | 1122 | if ( insertAll || (tool->GetIndex() != currentPosition) ) |
e56d2520 | 1123 | { |
dcae64c2 | 1124 | OSStatus err = noErr; |
8138cfec SC |
1125 | if ( !insertAll ) |
1126 | { | |
dcae64c2 DS |
1127 | insertAll = true; |
1128 | ||
8138cfec SC |
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 :-( | |
3b6a1179 | 1133 | for ( CFIndex i = 100; i >= currentPosition; --i ) |
8138cfec | 1134 | { |
dcae64c2 | 1135 | err = HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, i ); |
8138cfec | 1136 | } |
991f71dc | 1137 | |
dcae64c2 DS |
1138 | if (err != noErr) |
1139 | { | |
1140 | wxString errMsg = wxString::Format( wxT("HIToolbarRemoveItemAtIndex failed [%ld]"), (long)err ); | |
4362c705 | 1141 | wxFAIL_MSG( errMsg.c_str() ); |
dcae64c2 DS |
1142 | } |
1143 | } | |
991f71dc | 1144 | |
dcae64c2 DS |
1145 | err = HIToolbarInsertItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, hiItemRef, currentPosition ); |
1146 | if (err != noErr) | |
1147 | { | |
1148 | wxString errMsg = wxString::Format( wxT("HIToolbarInsertItemAtIndex failed [%ld]"), (long)err ); | |
4362c705 | 1149 | wxFAIL_MSG( errMsg.c_str() ); |
dcae64c2 | 1150 | } |
3b6a1179 | 1151 | |
dcae64c2 | 1152 | tool->SetIndex( currentPosition ); |
e56d2520 | 1153 | } |
991f71dc | 1154 | |
dcae64c2 | 1155 | currentPosition++; |
e56d2520 SC |
1156 | } |
1157 | } | |
dcae64c2 | 1158 | #endif |
f3a65c3e | 1159 | |
e56d2520 SC |
1160 | // update radio button (and group) state |
1161 | lastIsRadio = curIsRadio; | |
1162 | curIsRadio = ( tool->IsButton() && (tool->GetKind() == wxITEM_RADIO) ); | |
f3a65c3e | 1163 | |
e56d2520 SC |
1164 | if ( !curIsRadio ) |
1165 | { | |
1166 | if ( tool->IsToggled() ) | |
1167 | DoToggleTool( tool, true ); | |
f3a65c3e | 1168 | |
e56d2520 | 1169 | setChoiceInGroup = false; |
0b7a8cd3 GD |
1170 | } |
1171 | else | |
1172 | { | |
e56d2520 SC |
1173 | if ( !lastIsRadio ) |
1174 | { | |
dcae64c2 | 1175 | if ( tool->Toggle( true ) ) |
e56d2520 SC |
1176 | { |
1177 | DoToggleTool( tool, true ); | |
1178 | setChoiceInGroup = true; | |
1179 | } | |
1180 | } | |
1181 | else if ( tool->IsToggled() ) | |
1182 | { | |
1183 | if ( tool->IsToggled() ) | |
1184 | DoToggleTool( tool, true ); | |
f3a65c3e | 1185 | |
e56d2520 SC |
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; | |
f3a65c3e | 1192 | |
dcae64c2 | 1193 | if ( toggleTool->Toggle( false ) ) |
e56d2520 | 1194 | DoToggleTool( toggleTool, false ); |
f3a65c3e | 1195 | |
e56d2520 SC |
1196 | nodePrev = nodePrev->GetPrevious(); |
1197 | } | |
1198 | } | |
0b7a8cd3 | 1199 | } |
f3a65c3e | 1200 | |
eb22f2a6 | 1201 | node = node->GetNext(); |
7810c95b | 1202 | } |
f3a65c3e | 1203 | |
0b7a8cd3 | 1204 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) |
7810c95b | 1205 | { |
e56d2520 SC |
1206 | // if not set yet, only one row |
1207 | if ( m_maxRows <= 0 ) | |
1208 | SetRows( 1 ); | |
f3a65c3e | 1209 | |
90d3f91a | 1210 | m_minWidth = maxWidth; |
e56d2520 | 1211 | maxWidth = tw; |
0b7a8cd3 | 1212 | maxHeight += m_yMargin + kwxMacToolBarTopMargin; |
e56d2520 | 1213 | m_minHeight = m_maxHeight = maxHeight; |
7810c95b | 1214 | } |
0b7a8cd3 GD |
1215 | else |
1216 | { | |
e56d2520 SC |
1217 | // if not set yet, have one column |
1218 | if ( (GetToolsCount() > 0) && (m_maxRows <= 0) ) | |
1219 | SetRows( GetToolsCount() ); | |
f3a65c3e | 1220 | |
90d3f91a | 1221 | m_minHeight = maxHeight; |
e56d2520 | 1222 | maxHeight = th; |
0b7a8cd3 | 1223 | maxWidth += m_xMargin + kwxMacToolBarLeftMargin; |
e56d2520 | 1224 | m_minWidth = m_maxWidth = maxWidth; |
0b7a8cd3 | 1225 | } |
e56d2520 | 1226 | |
f3a65c3e | 1227 | #if 0 |
e56d2520 SC |
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 | } | |
f3a65c3e | 1242 | #else |
facd6764 | 1243 | SetSize( maxWidth, maxHeight ); |
9f884528 | 1244 | InvalidateBestSize(); |
e56d2520 | 1245 | #endif |
2c1dbc95 SC |
1246 | |
1247 | SetBestFittingSize(); | |
1248 | ||
3803c372 | 1249 | return true; |
e9576ca5 SC |
1250 | } |
1251 | ||
1252 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
1253 | { | |
e56d2520 SC |
1254 | m_defaultWidth = size.x + kwxMacToolBorder; |
1255 | m_defaultHeight = size.y + kwxMacToolBorder; | |
f3a65c3e | 1256 | |
e56d2520 SC |
1257 | #if wxMAC_USE_NATIVE_TOOLBAR |
1258 | if (m_macHIToolbarRef != NULL) | |
1259 | { | |
1260 | int maxs = wxMax( size.x, size.y ); | |
3b6a1179 | 1261 | HIToolbarDisplaySize sizeSpec; |
328f4fee | 1262 | if ( maxs > 32 ) |
3b6a1179 | 1263 | sizeSpec = kHIToolbarDisplaySizeNormal; |
00a7bd85 | 1264 | else if ( maxs > 24 ) |
3b6a1179 | 1265 | sizeSpec = kHIToolbarDisplaySizeDefault; |
328f4fee | 1266 | else |
3b6a1179 | 1267 | sizeSpec = kHIToolbarDisplaySizeSmall; |
f3a65c3e | 1268 | |
e56d2520 SC |
1269 | HIToolbarSetDisplaySize( (HIToolbarRef) m_macHIToolbarRef, sizeSpec ); |
1270 | } | |
1271 | #endif | |
e9576ca5 SC |
1272 | } |
1273 | ||
e9576ca5 SC |
1274 | // The button size is bigger than the bitmap size |
1275 | wxSize wxToolBar::GetToolSize() const | |
1276 | { | |
ee799df7 | 1277 | return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder); |
e9576ca5 SC |
1278 | } |
1279 | ||
37e2cb08 | 1280 | void wxToolBar::SetRows(int nRows) |
e9576ca5 | 1281 | { |
e56d2520 SC |
1282 | // avoid resizing the frame uselessly |
1283 | if ( nRows != m_maxRows ) | |
e56d2520 | 1284 | m_maxRows = nRows; |
e9576ca5 SC |
1285 | } |
1286 | ||
f3a65c3e | 1287 | void wxToolBar::MacSuperChangedPosition() |
c257d44d | 1288 | { |
e56d2520 | 1289 | wxWindow::MacSuperChangedPosition(); |
991f71dc | 1290 | |
e56d2520 SC |
1291 | #if wxMAC_USE_NATIVE_TOOLBAR |
1292 | if (! m_macUsesNativeToolbar ) | |
e56d2520 | 1293 | Realize(); |
991f71dc | 1294 | #else |
3025abca | 1295 | |
991f71dc DS |
1296 | Realize(); |
1297 | #endif | |
c257d44d SC |
1298 | } |
1299 | ||
37e2cb08 | 1300 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
e9576ca5 | 1301 | { |
3025abca | 1302 | wxToolBarTool *tool; |
affd2611 | 1303 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
e56d2520 | 1304 | while ( node != NULL ) |
e044f600 | 1305 | { |
3025abca | 1306 | tool = (wxToolBarTool *)node->GetData(); |
e56d2520 | 1307 | if (tool != NULL) |
e044f600 | 1308 | { |
e56d2520 SC |
1309 | wxRect2DInt r( tool->GetPosition(), tool->GetSize() ); |
1310 | if ( r.Contains( wxPoint( x, y ) ) ) | |
1311 | return tool; | |
e044f600 | 1312 | } |
bfe9ffbc SC |
1313 | |
1314 | node = node->GetNext(); | |
e044f600 | 1315 | } |
37e2cb08 | 1316 | |
3025abca | 1317 | return (wxToolBarToolBase*)NULL; |
e9576ca5 SC |
1318 | } |
1319 | ||
2f1ae414 SC |
1320 | wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) |
1321 | { | |
3b6a1179 | 1322 | wxToolBarToolBase *tool = FindToolForPosition( pt.x, pt.y ); |
e56d2520 | 1323 | if ( tool != NULL ) |
3b6a1179 | 1324 | return tool->GetShortHelp(); |
e56d2520 | 1325 | |
3b6a1179 | 1326 | return wxEmptyString; |
2f1ae414 SC |
1327 | } |
1328 | ||
37e2cb08 | 1329 | void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) |
e9576ca5 | 1330 | { |
e56d2520 | 1331 | if ( t != NULL ) |
3b6a1179 | 1332 | ((wxToolBarTool*)t)->DoEnable( enable ); |
e9576ca5 SC |
1333 | } |
1334 | ||
37e2cb08 | 1335 | void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) |
e9576ca5 | 1336 | { |
e044f600 | 1337 | wxToolBarTool *tool = (wxToolBarTool *)t; |
e56d2520 SC |
1338 | if ( ( tool != NULL ) && tool->IsButton() ) |
1339 | tool->UpdateToggleImage( toggle ); | |
37e2cb08 | 1340 | } |
7c551d95 | 1341 | |
3b6a1179 | 1342 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolBase) |
37e2cb08 | 1343 | { |
3b6a1179 | 1344 | wxToolBarTool *tool = wx_static_cast( wxToolBarTool*, toolBase ); |
e56d2520 SC |
1345 | if (tool == NULL) |
1346 | return false; | |
1347 | ||
1348 | WindowRef window = (WindowRef) MacGetTopLevelWindowRef(); | |
1349 | wxSize toolSize = GetToolSize(); | |
3b6a1179 | 1350 | Rect toolrect = { 0, 0, toolSize.y, toolSize.x }; |
e56d2520 SC |
1351 | ControlRef controlHandle = NULL; |
1352 | OSStatus err = 0; | |
be5fe3aa | 1353 | |
e56d2520 | 1354 | switch (tool->GetStyle()) |
be5fe3aa | 1355 | { |
3b6a1179 | 1356 | case wxTOOL_STYLE_SEPARATOR: |
be5fe3aa | 1357 | { |
e56d2520 SC |
1358 | wxASSERT( tool->GetControlHandle() == NULL ); |
1359 | toolSize.x /= 4; | |
1360 | toolSize.y /= 4; | |
be5fe3aa | 1361 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
e56d2520 | 1362 | toolrect.bottom = toolSize.y; |
be5fe3aa | 1363 | else |
e56d2520 SC |
1364 | toolrect.right = toolSize.x; |
1365 | ||
991f71dc | 1366 | #ifdef __WXMAC_OSX__ |
be5fe3aa | 1367 | // in flat style we need a visual separator |
991f71dc | 1368 | #if wxMAC_USE_NATIVE_TOOLBAR |
e56d2520 | 1369 | HIToolbarItemRef item; |
991f71dc DS |
1370 | err = HIToolbarItemCreate( |
1371 | kHIToolbarSeparatorIdentifier, | |
1372 | kHIToolbarItemCantBeRemoved | kHIToolbarItemIsSeparator | kHIToolbarItemAllowDuplicates, | |
1373 | &item ); | |
e56d2520 SC |
1374 | if (err == noErr) |
1375 | tool->SetToolbarItemRef( item ); | |
991f71dc DS |
1376 | #endif |
1377 | ||
e56d2520 SC |
1378 | CreateSeparatorControl( window, &toolrect, &controlHandle ); |
1379 | tool->SetControlHandle( controlHandle ); | |
991f71dc | 1380 | #endif |
be5fe3aa | 1381 | } |
e56d2520 SC |
1382 | break; |
1383 | ||
3b6a1179 | 1384 | case wxTOOL_STYLE_BUTTON: |
be5fe3aa | 1385 | { |
3b6a1179 DS |
1386 | wxASSERT( tool->GetControlHandle() == NULL ); |
1387 | ControlButtonContentInfo info; | |
1388 | wxMacCreateBitmapButton( &info, tool->GetNormalBitmap(), kControlContentIconRef ); | |
f3a65c3e | 1389 | |
df7998fc | 1390 | if ( UMAGetSystemVersion() >= 0x1000) |
3b6a1179 DS |
1391 | { |
1392 | CreateIconControl( window, &toolrect, &info, false, &controlHandle ); | |
1393 | } | |
df7998fc VZ |
1394 | else |
1395 | { | |
3b6a1179 | 1396 | SInt16 behaviour = kControlBehaviorOffsetContents; |
df7998fc | 1397 | if ( tool->CanBeToggled() ) |
3b6a1179 DS |
1398 | behaviour |= kControlBehaviorToggles; |
1399 | err = CreateBevelButtonControl( window, | |
1400 | &toolrect, CFSTR(""), kControlBevelButtonNormalBevel, | |
1401 | behaviour, &info, 0, 0, 0, &controlHandle ); | |
df7998fc | 1402 | } |
e56d2520 SC |
1403 | |
1404 | #if wxMAC_USE_NATIVE_TOOLBAR | |
3b6a1179 | 1405 | HIToolbarItemRef item; |
30962327 | 1406 | wxString labelStr = wxString::Format(wxT("%xd"), (int)tool); |
e56d2520 SC |
1407 | err = HIToolbarItemCreate( |
1408 | wxMacCFStringHolder(labelStr, wxFont::GetDefaultEncoding()), | |
1409 | kHIToolbarItemCantBeRemoved | kHIToolbarItemAnchoredLeft | kHIToolbarItemAllowDuplicates, &item ); | |
1410 | if (err == noErr) | |
1411 | { | |
3025abca DS |
1412 | InstallEventHandler( |
1413 | HIObjectGetEventTarget(item), GetwxMacToolBarEventHandlerUPP(), | |
1414 | GetEventTypeCount(toolBarEventList), toolBarEventList, tool, NULL ); | |
e56d2520 SC |
1415 | HIToolbarItemSetLabel( item, wxMacCFStringHolder(tool->GetLabel(), m_font.GetEncoding()) ); |
1416 | HIToolbarItemSetIconRef( item, info.u.iconRef ); | |
2c1dbc95 | 1417 | HIToolbarItemSetCommandID( item, kHIToolbarCommandPressAction ); |
e56d2520 SC |
1418 | tool->SetToolbarItemRef( item ); |
1419 | } | |
991f71dc | 1420 | #endif |
e56d2520 | 1421 | |
3b6a1179 | 1422 | wxMacReleaseBitmapButton( &info ); |
3025abca | 1423 | |
991f71dc | 1424 | #if 0 |
3b6a1179 DS |
1425 | SetBevelButtonTextPlacement( m_controlHandle, kControlBevelButtonPlaceBelowGraphic ); |
1426 | UMASetControlTitle( m_controlHandle, label, wxFont::GetDefaultEncoding() ); | |
991f71dc | 1427 | #endif |
f3a65c3e | 1428 | |
3b6a1179 DS |
1429 | InstallControlEventHandler( |
1430 | (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(), | |
e56d2520 | 1431 | GetEventTypeCount(eventList), eventList, tool, NULL ); |
be5fe3aa | 1432 | |
e56d2520 | 1433 | tool->SetControlHandle( controlHandle ); |
be5fe3aa | 1434 | } |
e56d2520 SC |
1435 | break; |
1436 | ||
3b6a1179 | 1437 | case wxTOOL_STYLE_CONTROL: |
3b6a1179 | 1438 | |
6d4835dc | 1439 | #if wxMAC_USE_NATIVE_TOOLBAR |
e56d2520 | 1440 | { |
0ac091ae | 1441 | wxASSERT( tool->GetControl() != NULL ); |
e56d2520 | 1442 | HIToolbarItemRef item; |
6d4835dc | 1443 | HIViewRef viewRef = (HIViewRef) tool->GetControl()->GetHandle() ; |
9d5ccdd3 SC |
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 ) ; | |
6d4835dc SC |
1447 | CFDataRef data = CFDataCreate( kCFAllocatorDefault , (UInt8*) &viewRef , sizeof(viewRef) ) ; |
1448 | err = HIToolbarCreateItemWithIdentifier((HIToolbarRef) m_macHIToolbarRef,kControlToolbarItemClassID, | |
1449 | data , &item ) ; | |
1450 | ||
1451 | if (err == noErr) | |
e56d2520 | 1452 | { |
e56d2520 | 1453 | tool->SetToolbarItemRef( item ); |
e56d2520 | 1454 | } |
6d4835dc SC |
1455 | CFRelease( data ) ; |
1456 | } | |
f3a65c3e | 1457 | |
e56d2520 | 1458 | #else |
6d4835dc | 1459 | // right now there's nothing to do here |
e56d2520 SC |
1460 | #endif |
1461 | break; | |
1462 | ||
3b6a1179 | 1463 | default: |
e56d2520 | 1464 | break; |
be5fe3aa | 1465 | } |
f3a65c3e | 1466 | |
991f71dc | 1467 | if ( err == noErr ) |
be5fe3aa | 1468 | { |
e56d2520 SC |
1469 | if ( controlHandle ) |
1470 | { | |
1471 | ControlRef container = (ControlRef) GetHandle(); | |
3b6a1179 | 1472 | wxASSERT_MSG( container != NULL, wxT("No valid Mac container control") ); |
be5fe3aa | 1473 | |
e56d2520 SC |
1474 | UMAShowControl( controlHandle ); |
1475 | ::EmbedControl( controlHandle, container ); | |
1476 | } | |
1477 | ||
1478 | if ( tool->CanBeToggled() && tool->IsToggled() ) | |
1479 | tool->UpdateToggleImage( true ); | |
be5fe3aa | 1480 | |
e56d2520 | 1481 | // nothing special to do here - we relayout in Realize() later |
3b6a1179 | 1482 | tool->Attach( this ); |
e56d2520 SC |
1483 | InvalidateBestSize(); |
1484 | } | |
1485 | else | |
be5fe3aa | 1486 | { |
3b6a1179 | 1487 | wxString errMsg = wxString::Format( wxT("wxToolBar::DoInsertTool - failure [%ld]"), (long)err ); |
4362c705 | 1488 | wxFAIL_MSG( errMsg.c_str() ); |
be5fe3aa | 1489 | } |
f3a65c3e | 1490 | |
991f71dc | 1491 | return (err == noErr); |
37e2cb08 | 1492 | } |
e9576ca5 | 1493 | |
5115c51a | 1494 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) |
37e2cb08 | 1495 | { |
3b6a1179 | 1496 | wxFAIL_MSG( wxT("not implemented") ); |
e9576ca5 SC |
1497 | } |
1498 | ||
be5fe3aa | 1499 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) |
37e2cb08 | 1500 | { |
3b6a1179 | 1501 | wxToolBarTool* tool = wx_static_cast( wxToolBarTool*, toolbase ); |
affd2611 | 1502 | wxToolBarToolsList::compatibility_iterator node; |
bfe9ffbc SC |
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 | ||
3b6a1179 | 1515 | wxSize sz = ((wxToolBarTool*)tool)->GetSize(); |
bfe9ffbc SC |
1516 | |
1517 | tool->Detach(); | |
df7998fc VZ |
1518 | |
1519 | #if wxMAC_USE_NATIVE_TOOLBAR | |
1520 | CFIndex removeIndex = tool->GetIndex(); | |
dcae64c2 | 1521 | #endif |
bfe9ffbc | 1522 | |
488b2c29 SC |
1523 | switch ( tool->GetStyle() ) |
1524 | { | |
1525 | case wxTOOL_STYLE_CONTROL: | |
be5fe3aa SC |
1526 | { |
1527 | tool->GetControl()->Destroy(); | |
3b6a1179 | 1528 | tool->ClearControl(); |
be5fe3aa | 1529 | } |
488b2c29 SC |
1530 | break; |
1531 | ||
1532 | case wxTOOL_STYLE_BUTTON: | |
1533 | case wxTOOL_STYLE_SEPARATOR: | |
be5fe3aa | 1534 | if ( tool->GetControlHandle() ) |
488b2c29 | 1535 | { |
e56d2520 | 1536 | #if wxMAC_USE_NATIVE_TOOLBAR |
df7998fc | 1537 | if ( removeIndex != -1 && m_macHIToolbarRef ) |
2c1dbc95 | 1538 | { |
3b6a1179 DS |
1539 | HIToolbarRemoveItemAtIndex( (HIToolbarRef) m_macHIToolbarRef, removeIndex ); |
1540 | tool->SetIndex( -1 ); | |
2c1dbc95 | 1541 | } |
991f71dc DS |
1542 | #endif |
1543 | ||
3b6a1179 | 1544 | tool->ClearControl(); |
488b2c29 SC |
1545 | } |
1546 | break; | |
e56d2520 SC |
1547 | |
1548 | default: | |
1549 | break; | |
488b2c29 SC |
1550 | } |
1551 | ||
bfe9ffbc | 1552 | // and finally reposition all the controls after this one |
f3a65c3e | 1553 | |
3b6a1179 | 1554 | for ( /* node -> first after deleted */; node; node = node->GetNext() ) |
bfe9ffbc SC |
1555 | { |
1556 | wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); | |
3b6a1179 | 1557 | wxPoint pt = tool2->GetPosition(); |
bfe9ffbc SC |
1558 | |
1559 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
3b6a1179 | 1560 | pt.y -= sz.y; |
bfe9ffbc | 1561 | else |
3b6a1179 | 1562 | pt.x -= sz.x; |
e56d2520 | 1563 | |
3b6a1179 | 1564 | tool2->SetPosition( pt ); |
df7998fc | 1565 | |
2c1dbc95 SC |
1566 | #if wxMAC_USE_NATIVE_TOOLBAR |
1567 | if ( removeIndex != -1 && tool2->GetIndex() > removeIndex ) | |
3b6a1179 | 1568 | tool2->SetIndex( tool2->GetIndex() - 1 ); |
2c1dbc95 | 1569 | #endif |
bfe9ffbc | 1570 | } |
f3a65c3e | 1571 | |
9f884528 | 1572 | InvalidateBestSize(); |
991f71dc | 1573 | |
3b6a1179 | 1574 | return true; |
37e2cb08 | 1575 | } |
2f1ae414 SC |
1576 | |
1577 | void wxToolBar::OnPaint(wxPaintEvent& event) | |
1578 | { | |
e56d2520 SC |
1579 | #if wxMAC_USE_NATIVE_TOOLBAR |
1580 | if ( m_macUsesNativeToolbar ) | |
1581 | { | |
dcae64c2 DS |
1582 | event.Skip(true); |
1583 | return; | |
e56d2520 SC |
1584 | } |
1585 | #endif | |
f3a65c3e | 1586 | |
3b6a1179 | 1587 | wxPaintDC dc(this); |
ddc548ec | 1588 | |
3b6a1179 DS |
1589 | int w, h; |
1590 | GetSize( &w, &h ); | |
991f71dc | 1591 | |
dcae64c2 DS |
1592 | bool drawMetalTheme = MacGetTopLevelWindow()->MacGetMetalAppearance(); |
1593 | bool minimumUmaAvailable = (UMAGetSystemVersion() >= 0x1030); | |
1594 | ||
ddc548ec | 1595 | #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 |
dcae64c2 | 1596 | if ( !drawMetalTheme && minimumUmaAvailable ) |
ddc548ec | 1597 | { |
dcae64c2 DS |
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 ); | |
ddc548ec SC |
1606 | } |
1607 | else | |
1608 | { | |
1609 | // leave the background as it is (striped or metal) | |
1610 | } | |
991f71dc | 1611 | |
ddc548ec | 1612 | #else |
dcae64c2 DS |
1613 | |
1614 | const bool drawBorder = true; | |
1615 | ||
1616 | if (drawBorder) | |
01ffa8f7 | 1617 | { |
3b6a1179 | 1618 | wxMacPortSetter helper( &dc ); |
dcae64c2 DS |
1619 | |
1620 | if ( !drawMetalTheme || !minimumUmaAvailable ) | |
20b69855 | 1621 | { |
3b6a1179 DS |
1622 | Rect toolbarrect = { dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0), |
1623 | dc.YLOG2DEVMAC(h), dc.XLOG2DEVMAC(w) }; | |
dcae64c2 DS |
1624 | |
1625 | #if 0 | |
1626 | if ( toolbarrect.left < 0 ) | |
3b6a1179 | 1627 | toolbarrect.left = 0; |
dcae64c2 | 1628 | if ( toolbarrect.top < 0 ) |
3b6a1179 | 1629 | toolbarrect.top = 0; |
dcae64c2 DS |
1630 | #endif |
1631 | ||
1632 | UMADrawThemePlacard( &toolbarrect, IsEnabled() ? kThemeStateActive : kThemeStateInactive ); | |
1633 | } | |
1634 | else | |
1635 | { | |
1636 | #if TARGET_API_MAC_OSX | |
991f71dc | 1637 | HIRect hiToolbarrect = CGRectMake( |
3b6a1179 DS |
1638 | dc.YLOG2DEVMAC(0), dc.XLOG2DEVMAC(0), |
1639 | dc.YLOG2DEVREL(h), dc.XLOG2DEVREL(w) ); | |
1640 | CGContextRef cgContext; | |
1641 | Rect bounds; | |
dcae64c2 | 1642 | |
3b6a1179 DS |
1643 | GetPortBounds( (CGrafPtr) dc.m_macPort, &bounds ); |
1644 | QDBeginCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); | |
dcae64c2 | 1645 | |
3b6a1179 DS |
1646 | CGContextTranslateCTM( cgContext, 0, bounds.bottom - bounds.top ); |
1647 | CGContextScaleCTM( cgContext, 1, -1 ); | |
785f5eaa | 1648 | |
3b6a1179 DS |
1649 | HIThemeBackgroundDrawInfo drawInfo; |
1650 | drawInfo.version = 0; | |
1651 | drawInfo.state = kThemeStateActive; | |
1652 | drawInfo.kind = kThemeBackgroundMetal; | |
dcae64c2 | 1653 | HIThemeApplyBackground( &hiToolbarrect, &drawInfo, cgContext, kHIThemeOrientationNormal ); |
e56d2520 | 1654 | |
3b6a1179 | 1655 | QDEndCGContext( (CGrafPtr) dc.m_macPort, &cgContext ); |
20b69855 | 1656 | #endif |
01ffa8f7 | 1657 | } |
01ffa8f7 SC |
1658 | } |
1659 | #endif | |
20b69855 | 1660 | |
dcae64c2 | 1661 | event.Skip(); |
2f1ae414 | 1662 | } |
895f5af7 | 1663 | |
519cb848 | 1664 | #endif // wxUSE_TOOLBAR |