]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: toolbar.cpp | |
3 | // Purpose: wxToolBar | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 SC |
5 | // Modified by: |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
84969af7 | 8 | // Copyright: (c) Stefan Csomor |
65571936 | 9 | // Licence: The wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3d1a4878 | 12 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
e9576ca5 SC |
13 | #pragma implementation "toolbar.h" |
14 | #endif | |
15 | ||
3d1a4878 | 16 | #include "wx/wxprec.h" |
519cb848 SC |
17 | |
18 | #if wxUSE_TOOLBAR | |
19 | ||
3d1a4878 | 20 | #include "wx/wx.h" |
e9576ca5 | 21 | #include "wx/toolbar.h" |
2f1ae414 SC |
22 | #include "wx/notebook.h" |
23 | #include "wx/tabctrl.h" | |
72055702 | 24 | #include "wx/bitmap.h" |
e9576ca5 | 25 | |
2eb10e2a | 26 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) |
e9576ca5 SC |
27 | |
28 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) | |
e044f600 | 29 | EVT_PAINT( wxToolBar::OnPaint ) |
e9576ca5 | 30 | END_EVENT_TABLE() |
e9576ca5 | 31 | |
d497dca4 | 32 | #include "wx/mac/uma.h" |
bfe9ffbc | 33 | #include "wx/geometry.h" |
ee799df7 SC |
34 | |
35 | #ifdef __WXMAC_OSX__ | |
17dfb611 SC |
36 | const short kwxMacToolBarToolDefaultWidth = 16 ; |
37 | const short kwxMacToolBarToolDefaultHeight = 16 ; | |
ee799df7 SC |
38 | const short kwxMacToolBarTopMargin = 4 ; |
39 | const short kwxMacToolBarLeftMargin = 4 ; | |
40 | const short kwxMacToolBorder = 0 ; | |
abbcdf3f | 41 | const short kwxMacToolSpacing = 6 ; |
ee799df7 SC |
42 | #else |
43 | const short kwxMacToolBarToolDefaultWidth = 24 ; | |
44 | const short kwxMacToolBarToolDefaultHeight = 22 ; | |
45 | const short kwxMacToolBarTopMargin = 2 ; | |
46 | const short kwxMacToolBarLeftMargin = 2 ; | |
47 | const short kwxMacToolBorder = 4 ; | |
abbcdf3f | 48 | const short kwxMacToolSpacing = 0 ; |
ee799df7 SC |
49 | #endif |
50 | ||
37e2cb08 SC |
51 | // ---------------------------------------------------------------------------- |
52 | // private classes | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | class wxToolBarTool : public wxToolBarToolBase | |
56 | { | |
57 | public: | |
58 | wxToolBarTool(wxToolBar *tbar, | |
59 | int id, | |
27e2d606 GD |
60 | const wxString& label, |
61 | const wxBitmap& bmpNormal, | |
62 | const wxBitmap& bmpDisabled, | |
63 | wxItemKind kind, | |
37e2cb08 | 64 | wxObject *clientData, |
27e2d606 | 65 | const wxString& shortHelp, |
bfe9ffbc SC |
66 | const wxString& longHelp) ; |
67 | ||
37e2cb08 SC |
68 | wxToolBarTool(wxToolBar *tbar, wxControl *control) |
69 | : wxToolBarToolBase(tbar, control) | |
70 | { | |
bfe9ffbc | 71 | Init() ; |
37e2cb08 | 72 | } |
bfe9ffbc SC |
73 | |
74 | ~wxToolBarTool() | |
75 | { | |
76 | if ( m_controlHandle ) | |
77 | DisposeControl( m_controlHandle ) ; | |
78 | } | |
79 | ||
facd6764 SC |
80 | WXWidget GetControlHandle() { return (WXWidget) m_controlHandle ; } |
81 | void SetControlHandle( ControlRef handle ) { m_controlHandle = handle ; } | |
37e2cb08 | 82 | |
bfe9ffbc SC |
83 | void SetSize(const wxSize& size) ; |
84 | void SetPosition( const wxPoint& position ) ; | |
a2fe01c9 | 85 | |
be5fe3aa SC |
86 | void ClearControl() { m_control = NULL ; } |
87 | ||
bfe9ffbc SC |
88 | wxSize GetSize() const |
89 | { | |
90 | if ( IsControl() ) | |
91 | { | |
92 | return GetControl()->GetSize() ; | |
93 | } | |
94 | else if ( IsButton() ) | |
95 | { | |
96 | return GetToolBar()->GetToolSize() ; | |
97 | } | |
98 | else | |
99 | { | |
abbcdf3f | 100 | // separator size |
bfe9ffbc | 101 | wxSize sz = GetToolBar()->GetToolSize() ; |
5a904a32 SC |
102 | if ( GetToolBar()->GetWindowStyleFlag() & wxTB_VERTICAL ) |
103 | sz.y /= 4 ; | |
104 | else | |
105 | sz.x /= 4 ; | |
bfe9ffbc SC |
106 | return sz ; |
107 | } | |
108 | } | |
109 | wxPoint GetPosition() const | |
110 | { | |
111 | return wxPoint(m_x, m_y); | |
112 | } | |
a2fe01c9 | 113 | bool DoEnable( bool enable ) ; |
73fd9428 SC |
114 | |
115 | void UpdateToggleImage( bool toggle ) ; | |
bfe9ffbc SC |
116 | private : |
117 | void Init() | |
118 | { | |
119 | m_controlHandle = NULL ; | |
120 | } | |
facd6764 | 121 | ControlRef m_controlHandle ; |
37e2cb08 | 122 | |
bfe9ffbc SC |
123 | wxCoord m_x; |
124 | wxCoord m_y; | |
37e2cb08 SC |
125 | }; |
126 | ||
facd6764 SC |
127 | static const EventTypeSpec eventList[] = |
128 | { | |
129 | { kEventClassControl , kEventControlHit } , | |
73fd9428 SC |
130 | #ifdef __WXMAC_OSX__ |
131 | { kEventClassControl , kEventControlHitTest } , | |
132 | #endif | |
facd6764 SC |
133 | } ; |
134 | ||
135 | static pascal OSStatus wxMacToolBarToolControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
136 | { | |
137 | OSStatus result = eventNotHandledErr ; | |
138 | ||
139 | wxMacCarbonEvent cEvent( event ) ; | |
140 | ||
141 | ControlRef controlRef ; | |
142 | ||
143 | cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ; | |
144 | ||
145 | switch( GetEventKind( event ) ) | |
146 | { | |
147 | case kEventControlHit : | |
148 | { | |
149 | wxToolBarTool* tbartool = (wxToolBarTool*)data ; | |
150 | if ( tbartool->CanBeToggled() ) | |
151 | { | |
64f553a4 SC |
152 | #ifdef __WXMAC_OSX__ |
153 | ((wxToolBar*)tbartool->GetToolBar())->ToggleTool(tbartool->GetId(), !tbartool->IsToggled() ); | |
154 | #else | |
214b9484 | 155 | ((wxToolBar*)tbartool->GetToolBar())->ToggleTool(tbartool->GetId(), GetControl32BitValue((ControlRef)tbartool->GetControlHandle())); |
64f553a4 | 156 | #endif |
facd6764 SC |
157 | } |
158 | ((wxToolBar*)tbartool->GetToolBar())->OnLeftClick( tbartool->GetId() , tbartool -> IsToggled() ) ; | |
facd6764 SC |
159 | result = noErr; |
160 | } | |
161 | break ; | |
73fd9428 SC |
162 | #ifdef __WXMAC_OSX__ |
163 | case kEventControlHitTest : | |
164 | { | |
165 | HIPoint pt = cEvent.GetParameter<HIPoint>(kEventParamMouseLocation) ; | |
166 | HIRect rect ; | |
167 | HIViewGetBounds( controlRef , &rect ) ; | |
168 | ||
169 | ControlPartCode pc = kControlNoPart ; | |
170 | if ( CGRectContainsPoint( rect , pt ) ) | |
d60f41f2 | 171 | pc = kControlIconPart ; |
73fd9428 SC |
172 | cEvent.SetParameter( kEventParamControlPart , typeControlPartCode, pc ) ; |
173 | result = noErr ; | |
174 | } | |
175 | break ; | |
176 | #endif | |
facd6764 SC |
177 | default : |
178 | break ; | |
179 | } | |
180 | return result ; | |
181 | } | |
182 | ||
0aeeec99 | 183 | static pascal OSStatus wxMacToolBarToolEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) |
facd6764 SC |
184 | { |
185 | OSStatus result = eventNotHandledErr ; | |
186 | ||
187 | switch ( GetEventClass( event ) ) | |
188 | { | |
189 | case kEventClassControl : | |
190 | result = wxMacToolBarToolControlEventHandler( handler, event, data ) ; | |
191 | break ; | |
192 | default : | |
193 | break ; | |
194 | } | |
195 | return result ; | |
196 | } | |
197 | ||
198 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacToolBarToolEventHandler ) | |
199 | ||
37e2cb08 SC |
200 | // ============================================================================ |
201 | // implementation | |
202 | // ============================================================================ | |
203 | ||
204 | // ---------------------------------------------------------------------------- | |
205 | // wxToolBarTool | |
206 | // ---------------------------------------------------------------------------- | |
207 | ||
a2fe01c9 SC |
208 | bool wxToolBarTool::DoEnable(bool enable) |
209 | { | |
210 | if ( IsControl() ) | |
211 | { | |
212 | GetControl()->Enable( enable ) ; | |
213 | } | |
214 | else if ( IsButton() ) | |
215 | { | |
216 | #if TARGET_API_MAC_OSX | |
217 | if ( enable ) | |
218 | EnableControl( m_controlHandle ) ; | |
219 | else | |
220 | DisableControl( m_controlHandle ) ; | |
221 | #else | |
222 | if ( enable ) | |
223 | ActivateControl( m_controlHandle ) ; | |
224 | else | |
225 | DeactivateControl( m_controlHandle ) ; | |
226 | #endif | |
227 | } | |
228 | return true ; | |
229 | } | |
bfe9ffbc SC |
230 | void wxToolBarTool::SetSize(const wxSize& size) |
231 | { | |
232 | if ( IsControl() ) | |
233 | { | |
234 | GetControl()->SetSize( size ) ; | |
235 | } | |
236 | } | |
237 | ||
238 | void wxToolBarTool::SetPosition(const wxPoint& position) | |
239 | { | |
240 | m_x = position.x; | |
241 | m_y = position.y; | |
242 | ||
789ae0cf SC |
243 | int x , y ; |
244 | x = y = 0 ; | |
245 | int mac_x = position.x ; | |
246 | int mac_y = position.y ; | |
247 | ||
248 | if ( ! GetToolBar()->MacGetTopLevelWindow()->MacUsesCompositing() ) | |
bfe9ffbc | 249 | { |
bfe9ffbc | 250 | GetToolBar()->MacWindowToRootWindow( &x , &y ) ; |
facd6764 SC |
251 | mac_x += x; |
252 | mac_y += y; | |
789ae0cf SC |
253 | } |
254 | ||
255 | if ( IsButton() ) | |
256 | { | |
bfe9ffbc SC |
257 | Rect contrlRect ; |
258 | GetControlBounds( m_controlHandle , &contrlRect ) ; | |
259 | int former_mac_x = contrlRect.left ; | |
260 | int former_mac_y = contrlRect.top ; | |
80e3f464 | 261 | GetToolBar()->GetToolSize() ; |
bfe9ffbc SC |
262 | |
263 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) | |
264 | { | |
bfe9ffbc | 265 | UMAMoveControl( m_controlHandle , mac_x , mac_y ) ; |
bfe9ffbc SC |
266 | } |
267 | } | |
268 | else if ( IsControl() ) | |
269 | { | |
270 | GetControl()->Move( position ) ; | |
271 | } | |
abbcdf3f SC |
272 | else |
273 | { | |
274 | // separator | |
275 | #ifdef __WXMAC_OSX__ | |
abbcdf3f SC |
276 | Rect contrlRect ; |
277 | GetControlBounds( m_controlHandle , &contrlRect ) ; | |
278 | int former_mac_x = contrlRect.left ; | |
279 | int former_mac_y = contrlRect.top ; | |
280 | ||
281 | if ( mac_x != former_mac_x || mac_y != former_mac_y ) | |
282 | { | |
283 | UMAMoveControl( m_controlHandle , mac_x , mac_y ) ; | |
284 | } | |
285 | #endif | |
286 | } | |
bfe9ffbc SC |
287 | } |
288 | ||
73fd9428 SC |
289 | void wxToolBarTool::UpdateToggleImage( bool toggle ) |
290 | { | |
291 | #ifdef __WXMAC_OSX__ | |
292 | if ( toggle ) | |
293 | { | |
294 | int w = m_bmpNormal.GetWidth() ; | |
295 | int h = m_bmpNormal.GetHeight() ; | |
296 | wxBitmap bmp( w , h ) ; | |
297 | wxMemoryDC dc ; | |
298 | dc.SelectObject( bmp ) ; | |
299 | dc.SetPen( wxNullPen ) ; | |
300 | dc.SetBackground( *wxWHITE ) ; | |
301 | dc.DrawRectangle( 0 , 0 , w , h ) ; | |
302 | dc.DrawBitmap( m_bmpNormal , 0 , 0 , true) ; | |
303 | dc.SelectObject( wxNullBitmap ) ; | |
304 | ControlButtonContentInfo info ; | |
305 | wxMacCreateBitmapButton( &info , bmp ) ; | |
306 | SetControlData( m_controlHandle , 0, kControlIconContentTag, sizeof( info ), | |
307 | (Ptr)&info ); | |
308 | wxMacReleaseBitmapButton( &info ) ; | |
309 | } | |
310 | else | |
311 | { | |
312 | ControlButtonContentInfo info ; | |
313 | wxMacCreateBitmapButton( &info , m_bmpNormal ) ; | |
314 | SetControlData( m_controlHandle , 0, kControlIconContentTag, sizeof( info ), | |
315 | (Ptr)&info ); | |
316 | wxMacReleaseBitmapButton( &info ) ; | |
317 | } | |
318 | ||
319 | IconTransformType transform = toggle ? kTransformSelected : kTransformNone ; | |
320 | SetControlData( m_controlHandle, 0, kControlIconTransformTag, sizeof( transform ), | |
321 | (Ptr)&transform ); | |
322 | HIViewSetNeedsDisplay( m_controlHandle , true ) ; | |
323 | ||
324 | #else | |
325 | ::SetControl32BitValue( m_controlHandle , toggle ) ; | |
326 | #endif | |
327 | } | |
328 | ||
bfe9ffbc SC |
329 | wxToolBarTool::wxToolBarTool(wxToolBar *tbar, |
330 | int id, | |
331 | const wxString& label, | |
332 | const wxBitmap& bmpNormal, | |
333 | const wxBitmap& bmpDisabled, | |
334 | wxItemKind kind, | |
335 | wxObject *clientData, | |
336 | const wxString& shortHelp, | |
337 | const wxString& longHelp) | |
338 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind, | |
339 | clientData, shortHelp, longHelp) | |
340 | { | |
f4e0be4f | 341 | Init(); |
bfe9ffbc SC |
342 | } |
343 | ||
2f1ae414 | 344 | |
37e2cb08 | 345 | wxToolBarToolBase *wxToolBar::CreateTool(int id, |
27e2d606 GD |
346 | const wxString& label, |
347 | const wxBitmap& bmpNormal, | |
348 | const wxBitmap& bmpDisabled, | |
349 | wxItemKind kind, | |
37e2cb08 | 350 | wxObject *clientData, |
27e2d606 GD |
351 | const wxString& shortHelp, |
352 | const wxString& longHelp) | |
37e2cb08 | 353 | { |
27e2d606 GD |
354 | return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind, |
355 | clientData, shortHelp, longHelp); | |
37e2cb08 SC |
356 | } |
357 | ||
358 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
359 | { | |
360 | return new wxToolBarTool(this, control); | |
361 | } | |
362 | ||
37e2cb08 | 363 | void wxToolBar::Init() |
e9576ca5 | 364 | { |
e40298d5 JS |
365 | m_maxWidth = -1; |
366 | m_maxHeight = -1; | |
367 | m_defaultWidth = kwxMacToolBarToolDefaultWidth; | |
368 | m_defaultHeight = kwxMacToolBarToolDefaultHeight; | |
e9576ca5 SC |
369 | } |
370 | ||
371 | bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, | |
372 | long style, const wxString& name) | |
e40298d5 | 373 | { |
ee799df7 | 374 | |
facd6764 SC |
375 | if ( !wxToolBarBase::Create( parent , id , pos , size , style ) ) |
376 | return FALSE ; | |
e40298d5 JS |
377 | |
378 | return TRUE; | |
e9576ca5 SC |
379 | } |
380 | ||
381 | wxToolBar::~wxToolBar() | |
bfe9ffbc | 382 | { |
7810c95b SC |
383 | // we must refresh the frame size when the toolbar is deleted but the frame |
384 | // is not - otherwise toolbar leaves a hole in the place it used to occupy | |
e9576ca5 SC |
385 | } |
386 | ||
37e2cb08 | 387 | bool wxToolBar::Realize() |
e9576ca5 | 388 | { |
eb22f2a6 | 389 | if (m_tools.GetCount() == 0) |
0b7a8cd3 GD |
390 | return FALSE; |
391 | ||
bfe9ffbc SC |
392 | int x = m_xMargin + kwxMacToolBarLeftMargin ; |
393 | int y = m_yMargin + kwxMacToolBarTopMargin ; | |
0b7a8cd3 | 394 | |
7c551d95 SC |
395 | int tw, th; |
396 | GetSize(& tw, & th); | |
895f5af7 SC |
397 | |
398 | int maxWidth = 0 ; | |
399 | int maxHeight = 0 ; | |
400 | ||
bfe9ffbc SC |
401 | int maxToolWidth = 0; |
402 | int maxToolHeight = 0; | |
403 | ||
404 | // Find the maximum tool width and height | |
affd2611 | 405 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
bfe9ffbc SC |
406 | while ( node ) |
407 | { | |
408 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
409 | wxSize sz = tool->GetSize() ; | |
410 | ||
411 | if ( sz.x > maxToolWidth ) | |
412 | maxToolWidth = sz.x ; | |
413 | if (sz.y> maxToolHeight) | |
414 | maxToolHeight = sz.y; | |
415 | ||
416 | node = node->GetNext(); | |
417 | } | |
418 | ||
214b9484 | 419 | bool lastWasRadio = FALSE; |
bfe9ffbc | 420 | node = m_tools.GetFirst(); |
0b7a8cd3 | 421 | while (node) |
7810c95b | 422 | { |
eb22f2a6 | 423 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); |
bfe9ffbc | 424 | wxSize cursize = tool->GetSize() ; |
0b7a8cd3 | 425 | |
214b9484 RD |
426 | bool isRadio = FALSE; |
427 | ||
428 | if ( tool->IsButton() && tool->GetKind() == wxITEM_RADIO ) | |
429 | { | |
430 | if ( !lastWasRadio ) | |
431 | { | |
432 | if (tool->Toggle(true)) | |
433 | { | |
434 | DoToggleTool(tool, true); | |
435 | } | |
436 | } | |
8f2ee25d SC |
437 | else if (tool->IsToggled()) |
438 | { | |
439 | wxToolBarToolsList::compatibility_iterator nodePrev = node->GetPrevious(); | |
440 | while ( nodePrev ) | |
441 | { | |
442 | wxToolBarToolBase *tool = nodePrev->GetData(); | |
443 | if ( !tool->IsButton() || (tool->GetKind() != wxITEM_RADIO) ) | |
444 | break; | |
445 | if ( tool->Toggle(false) ) | |
446 | { | |
447 | DoToggleTool(tool, false); | |
448 | } | |
449 | nodePrev = nodePrev->GetPrevious(); | |
450 | } | |
451 | } | |
214b9484 RD |
452 | isRadio = TRUE; |
453 | } | |
454 | else | |
455 | { | |
456 | isRadio = FALSE; | |
457 | } | |
458 | lastWasRadio = isRadio; | |
459 | ||
bfe9ffbc SC |
460 | // for the moment we just do a single row/column alignement |
461 | if ( x + cursize.x > maxWidth ) | |
462 | maxWidth = x + cursize.x ; | |
463 | if ( y + cursize.y > maxHeight ) | |
464 | maxHeight = y + cursize.y ; | |
0b7a8cd3 | 465 | |
73fd9428 SC |
466 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
467 | { | |
468 | int x1 = x + (maxToolWidth - cursize.x)/2 ; | |
469 | tool->SetPosition( wxPoint( x1 , y ) ) ; | |
470 | } | |
471 | else | |
472 | { | |
473 | int y1 = y + (maxToolHeight - cursize.y)/2 ; | |
474 | tool->SetPosition( wxPoint( x , y1 ) ) ; | |
475 | } | |
bfe9ffbc SC |
476 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
477 | { | |
478 | y += cursize.y ; | |
abbcdf3f | 479 | y += kwxMacToolSpacing ; |
0b7a8cd3 GD |
480 | } |
481 | else | |
482 | { | |
bfe9ffbc | 483 | x += cursize.x ; |
abbcdf3f | 484 | x += kwxMacToolSpacing ; |
0b7a8cd3 | 485 | } |
bfe9ffbc | 486 | |
eb22f2a6 | 487 | node = node->GetNext(); |
7810c95b | 488 | } |
0b7a8cd3 GD |
489 | |
490 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) | |
7810c95b | 491 | { |
0b7a8cd3 GD |
492 | if ( m_maxRows == 0 ) |
493 | { | |
494 | // if not set yet, only one row | |
495 | SetRows(1); | |
496 | } | |
90d3f91a | 497 | m_minWidth = maxWidth; |
0b7a8cd3 | 498 | maxWidth = tw ; |
0b7a8cd3 | 499 | maxHeight += m_yMargin + kwxMacToolBarTopMargin; |
90d3f91a | 500 | m_minHeight = m_maxHeight = maxHeight ; |
7810c95b | 501 | } |
0b7a8cd3 GD |
502 | else |
503 | { | |
bfe9ffbc | 504 | if ( GetToolsCount() > 0 && m_maxRows == 0 ) |
0b7a8cd3 GD |
505 | { |
506 | // if not set yet, have one column | |
bfe9ffbc | 507 | SetRows(GetToolsCount()); |
0b7a8cd3 | 508 | } |
90d3f91a | 509 | m_minHeight = maxHeight; |
0b7a8cd3 | 510 | maxHeight = th ; |
0b7a8cd3 | 511 | maxWidth += m_xMargin + kwxMacToolBarLeftMargin; |
90d3f91a | 512 | m_minWidth = m_maxWidth = maxWidth ; |
0b7a8cd3 GD |
513 | } |
514 | ||
facd6764 | 515 | SetSize( maxWidth, maxHeight ); |
9f884528 | 516 | InvalidateBestSize(); |
0b7a8cd3 GD |
517 | |
518 | return TRUE; | |
e9576ca5 SC |
519 | } |
520 | ||
521 | void wxToolBar::SetToolBitmapSize(const wxSize& size) | |
522 | { | |
ee799df7 | 523 | m_defaultWidth = size.x+kwxMacToolBorder; m_defaultHeight = size.y+kwxMacToolBorder; |
e9576ca5 SC |
524 | } |
525 | ||
e9576ca5 SC |
526 | // The button size is bigger than the bitmap size |
527 | wxSize wxToolBar::GetToolSize() const | |
528 | { | |
ee799df7 | 529 | return wxSize(m_defaultWidth + kwxMacToolBorder, m_defaultHeight + kwxMacToolBorder); |
e9576ca5 SC |
530 | } |
531 | ||
37e2cb08 | 532 | void wxToolBar::SetRows(int nRows) |
e9576ca5 | 533 | { |
37e2cb08 | 534 | if ( nRows == m_maxRows ) |
e9576ca5 | 535 | { |
37e2cb08 SC |
536 | // avoid resizing the frame uselessly |
537 | return; | |
e9576ca5 | 538 | } |
37e2cb08 SC |
539 | |
540 | m_maxRows = nRows; | |
e9576ca5 SC |
541 | } |
542 | ||
c257d44d SC |
543 | void wxToolBar::MacSuperChangedPosition() |
544 | { | |
c257d44d | 545 | wxWindow::MacSuperChangedPosition() ; |
bfe9ffbc | 546 | Realize() ; |
c257d44d SC |
547 | } |
548 | ||
37e2cb08 | 549 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const |
e9576ca5 | 550 | { |
affd2611 | 551 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); |
bfe9ffbc | 552 | while (node) |
e044f600 | 553 | { |
bfe9ffbc SC |
554 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ; |
555 | wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ; | |
556 | if ( r.Contains( wxPoint( x , y ) ) ) | |
e044f600 | 557 | { |
bfe9ffbc | 558 | return tool; |
e044f600 | 559 | } |
bfe9ffbc SC |
560 | |
561 | node = node->GetNext(); | |
e044f600 | 562 | } |
37e2cb08 SC |
563 | |
564 | return (wxToolBarToolBase *)NULL; | |
e9576ca5 SC |
565 | } |
566 | ||
2f1ae414 SC |
567 | wxString wxToolBar::MacGetToolTipString( wxPoint &pt ) |
568 | { | |
e044f600 RR |
569 | wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ; |
570 | if ( tool ) | |
571 | { | |
572 | return tool->GetShortHelp() ; | |
573 | } | |
427ff662 | 574 | return wxEmptyString ; |
2f1ae414 SC |
575 | } |
576 | ||
37e2cb08 | 577 | void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable) |
e9576ca5 | 578 | { |
a2fe01c9 | 579 | ((wxToolBarTool*)t)->DoEnable( enable ) ; |
e9576ca5 SC |
580 | } |
581 | ||
37e2cb08 | 582 | void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle) |
e9576ca5 | 583 | { |
e044f600 | 584 | wxToolBarTool *tool = (wxToolBarTool *)t; |
bfe9ffbc SC |
585 | if ( tool->IsButton() ) |
586 | { | |
73fd9428 | 587 | tool->UpdateToggleImage( toggle ) ; |
bfe9ffbc | 588 | } |
37e2cb08 | 589 | } |
7c551d95 | 590 | |
37e2cb08 | 591 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), |
be5fe3aa | 592 | wxToolBarToolBase *toolBase) |
37e2cb08 | 593 | { |
be5fe3aa SC |
594 | wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolBase ) ; |
595 | ||
596 | WindowRef window = (WindowRef) MacGetTopLevelWindowRef() ; | |
597 | wxSize toolSize = GetToolSize() ; | |
598 | Rect toolrect = { 0, 0 , toolSize.y , toolSize.x } ; | |
599 | ControlRef controlHandle = NULL ; | |
600 | ||
601 | switch( tool->GetStyle() ) | |
602 | { | |
603 | case wxTOOL_STYLE_SEPARATOR : | |
604 | { | |
605 | wxASSERT( tool->GetControlHandle() == NULL ) ; | |
606 | toolSize.x /= 4 ; | |
607 | toolSize.y /= 4 ; | |
608 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
609 | { | |
610 | toolrect.bottom = toolSize.y ; | |
611 | } | |
612 | else | |
613 | { | |
614 | toolrect.right = toolSize.x ; | |
615 | } | |
616 | #ifdef __WXMAC_OSX__ | |
617 | // in flat style we need a visual separator | |
618 | CreateSeparatorControl( window , &toolrect , &controlHandle ) ; | |
619 | tool->SetControlHandle( controlHandle ) ; | |
620 | #endif | |
621 | } | |
622 | break ; | |
623 | case wxTOOL_STYLE_BUTTON : | |
624 | { | |
625 | wxASSERT( tool->GetControlHandle() == NULL ) ; | |
626 | ControlButtonContentInfo info ; | |
627 | wxMacCreateBitmapButton( &info , tool->GetNormalBitmap() , kControlContentIconRef ) ; | |
628 | ||
629 | #ifdef __WXMAC_OSX__ | |
630 | CreateIconControl( window , &toolrect , &info , false , &controlHandle ) ; | |
631 | #else | |
632 | SInt16 behaviour = kControlBehaviorOffsetContents ; | |
61c747cd | 633 | if ( tool->CanBeToggled() ) |
be5fe3aa SC |
634 | behaviour += kControlBehaviorToggles ; |
635 | CreateBevelButtonControl( window , &toolrect , CFSTR("") , kControlBevelButtonNormalBevel , behaviour , &info , | |
636 | 0 , 0 , 0 , &controlHandle ) ; | |
637 | #endif | |
638 | ||
639 | wxMacReleaseBitmapButton( &info ) ; | |
640 | /* | |
641 | SetBevelButtonTextPlacement( m_controlHandle , kControlBevelButtonPlaceBelowGraphic ) ; | |
642 | UMASetControlTitle( m_controlHandle , label , wxFont::GetDefaultEncoding() ) ; | |
643 | */ | |
644 | ||
645 | InstallControlEventHandler( (ControlRef) controlHandle, GetwxMacToolBarToolEventHandlerUPP(), | |
646 | GetEventTypeCount(eventList), eventList, tool,NULL); | |
647 | ||
648 | tool->SetControlHandle( controlHandle ) ; | |
649 | } | |
650 | break ; | |
651 | case wxTOOL_STYLE_CONTROL : | |
652 | wxASSERT( tool->GetControl() != NULL ) ; | |
653 | // right now there's nothing to do here | |
654 | break ; | |
655 | } | |
656 | ||
657 | if ( controlHandle ) | |
658 | { | |
659 | ControlRef container = (ControlRef) GetHandle() ; | |
660 | wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ; | |
661 | ||
662 | UMAShowControl( controlHandle ) ; | |
663 | ::EmbedControl( controlHandle , container ) ; | |
664 | } | |
665 | ||
666 | if ( tool->CanBeToggled() && tool->IsToggled() ) | |
667 | { | |
668 | tool->UpdateToggleImage( true ) ; | |
669 | } | |
670 | ||
bfe9ffbc | 671 | // nothing special to do here - we relayout in Realize() later |
37e2cb08 | 672 | tool->Attach(this); |
9f884528 | 673 | InvalidateBestSize(); |
7c551d95 | 674 | |
37e2cb08 SC |
675 | return TRUE; |
676 | } | |
e9576ca5 | 677 | |
5115c51a | 678 | void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle)) |
37e2cb08 | 679 | { |
5115c51a | 680 | wxFAIL_MSG( _T("not implemented") ); |
e9576ca5 SC |
681 | } |
682 | ||
be5fe3aa | 683 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *toolbase) |
37e2cb08 | 684 | { |
be5fe3aa | 685 | wxToolBarTool* tool = wx_static_cast( wxToolBarTool* , toolbase ) ; |
affd2611 | 686 | wxToolBarToolsList::compatibility_iterator node; |
bfe9ffbc SC |
687 | for ( node = m_tools.GetFirst(); node; node = node->GetNext() ) |
688 | { | |
689 | wxToolBarToolBase *tool2 = node->GetData(); | |
690 | if ( tool2 == tool ) | |
691 | { | |
692 | // let node point to the next node in the list | |
693 | node = node->GetNext(); | |
694 | ||
695 | break; | |
696 | } | |
697 | } | |
698 | ||
699 | wxSize sz = ((wxToolBarTool*)tool)->GetSize() ; | |
700 | ||
701 | tool->Detach(); | |
702 | ||
488b2c29 SC |
703 | switch ( tool->GetStyle() ) |
704 | { | |
705 | case wxTOOL_STYLE_CONTROL: | |
be5fe3aa SC |
706 | { |
707 | tool->GetControl()->Destroy(); | |
708 | tool->ClearControl() ; | |
709 | } | |
488b2c29 SC |
710 | break; |
711 | ||
712 | case wxTOOL_STYLE_BUTTON: | |
713 | case wxTOOL_STYLE_SEPARATOR: | |
be5fe3aa | 714 | if ( tool->GetControlHandle() ) |
488b2c29 | 715 | { |
be5fe3aa SC |
716 | DisposeControl( (ControlRef) tool->GetControlHandle() ) ; |
717 | tool->SetControlHandle( (ControlRef) NULL ) ; | |
488b2c29 SC |
718 | } |
719 | break; | |
720 | } | |
721 | ||
bfe9ffbc SC |
722 | // and finally reposition all the controls after this one |
723 | ||
724 | for ( /* node -> first after deleted */ ; node; node = node->GetNext() ) | |
725 | { | |
726 | wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData(); | |
727 | wxPoint pt = tool2->GetPosition() ; | |
728 | ||
729 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) | |
730 | { | |
731 | pt.y -= sz.y ; | |
732 | } | |
733 | else | |
734 | { | |
735 | pt.x -= sz.x ; | |
736 | } | |
737 | tool2->SetPosition( pt ) ; | |
738 | } | |
739 | ||
9f884528 | 740 | InvalidateBestSize(); |
5115c51a | 741 | return TRUE ; |
37e2cb08 | 742 | } |
2f1ae414 SC |
743 | |
744 | void wxToolBar::OnPaint(wxPaintEvent& event) | |
745 | { | |
e40298d5 | 746 | wxPaintDC dc(this) ; |
ddc548ec | 747 | |
facd6764 SC |
748 | int w, h ; |
749 | GetSize( &w , &h ) ; | |
ddc548ec SC |
750 | #if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3 |
751 | if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() ) | |
752 | { | |
753 | if ( UMAGetSystemVersion() >= 0x1030 ) | |
754 | { | |
755 | HIThemePlacardDrawInfo info ; | |
756 | memset( &info, 0 , sizeof( info ) ) ; | |
757 | info.version = 0 ; | |
758 | info.state = IsEnabled() ? kThemeStateActive : kThemeStateInactive ; | |
759 | ||
760 | CGContextRef cgContext = (CGContextRef) MacGetCGContextRef() ; | |
761 | HIRect rect = CGRectMake( 0 , 0 , w , h ) ; | |
762 | HIThemeDrawPlacard( &rect , & info , cgContext, kHIThemeOrientationNormal) ; | |
763 | } | |
764 | } | |
765 | else | |
766 | { | |
767 | // leave the background as it is (striped or metal) | |
768 | } | |
769 | #else | |
770 | wxMacPortSetter helper(&dc) ; | |
e40298d5 | 771 | |
1fd1922a | 772 | Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) , |
facd6764 SC |
773 | dc.YLOG2DEVMAC(h) , dc.XLOG2DEVMAC(w) } ; |
774 | /* | |
775 | if( toolbarrect.left < 0 ) | |
776 | toolbarrect.left = 0 ; | |
777 | if ( toolbarrect.top < 0 ) | |
778 | toolbarrect.top = 0 ; | |
779 | */ | |
01ffa8f7 SC |
780 | if ( !MacGetTopLevelWindow()->MacGetMetalAppearance() ) |
781 | { | |
782 | UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ; | |
783 | } | |
784 | else | |
785 | { | |
786 | #if TARGET_API_MAC_OSX | |
787 | #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 | |
20b69855 SC |
788 | if ( UMAGetSystemVersion() >= 0x1030 ) |
789 | { | |
790 | HIRect hiToolbarrect = CGRectMake( dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) , | |
791 | dc.YLOG2DEVREL(h) , dc.XLOG2DEVREL(w) ); | |
792 | CGContextRef cgContext ; | |
793 | Rect bounds ; | |
794 | GetPortBounds( (CGrafPtr) dc.m_macPort , &bounds ) ; | |
795 | QDBeginCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ; | |
796 | CGContextTranslateCTM( cgContext , 0 , bounds.bottom - bounds.top ) ; | |
797 | CGContextScaleCTM( cgContext , 1 , -1 ) ; | |
785f5eaa | 798 | |
20b69855 SC |
799 | { |
800 | HIThemeBackgroundDrawInfo drawInfo ; | |
801 | drawInfo.version = 0 ; | |
802 | drawInfo.state = kThemeStateActive ; | |
803 | drawInfo.kind = kThemeBackgroundMetal ; | |
804 | HIThemeApplyBackground( &hiToolbarrect, &drawInfo , cgContext,kHIThemeOrientationNormal) ; | |
805 | } | |
806 | QDEndCGContext( (CGrafPtr) dc.m_macPort , &cgContext ) ; | |
807 | } | |
808 | else | |
809 | #endif | |
01ffa8f7 | 810 | { |
20b69855 | 811 | UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ; |
01ffa8f7 | 812 | } |
01ffa8f7 | 813 | #endif |
01ffa8f7 SC |
814 | } |
815 | #endif | |
20b69855 | 816 | |
facd6764 | 817 | event.Skip() ; |
2f1ae414 | 818 | } |
895f5af7 | 819 | |
519cb848 SC |
820 | #endif // wxUSE_TOOLBAR |
821 |