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