]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/toolbar.cpp
* Fix logical error in m_overflow attribute merging
[wxWidgets.git] / src / mac / toolbar.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: toolbar.cpp
3// Purpose: wxToolBar
4// Author: Stefan Csomor
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
9// Licence: The wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "toolbar.h"
14#endif
15
16#include "wx/wx.h"
17
18#if wxUSE_TOOLBAR
19
20#include "wx/toolbar.h"
21#include "wx/notebook.h"
22#include "wx/tabctrl.h"
23#include "wx/bitmap.h"
24
25#if !USE_SHARED_LIBRARY
26IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
27
28BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
29 EVT_MOUSE_EVENTS( wxToolBar::OnMouse )
30 EVT_PAINT( wxToolBar::OnPaint )
31END_EVENT_TABLE()
32#endif
33
34#include "wx/mac/uma.h"
35#include "wx/geometry.h"
36// ----------------------------------------------------------------------------
37// private classes
38// ----------------------------------------------------------------------------
39
40class wxToolBarTool : public wxToolBarToolBase
41{
42public:
43 wxToolBarTool(wxToolBar *tbar,
44 int id,
45 const wxString& label,
46 const wxBitmap& bmpNormal,
47 const wxBitmap& bmpDisabled,
48 wxItemKind kind,
49 wxObject *clientData,
50 const wxString& shortHelp,
51 const wxString& longHelp) ;
52
53 wxToolBarTool(wxToolBar *tbar, wxControl *control)
54 : wxToolBarToolBase(tbar, control)
55 {
56 Init() ;
57 }
58
59 ~wxToolBarTool()
60 {
61 if ( m_controlHandle )
62 DisposeControl( m_controlHandle ) ;
63 }
64
65 ControlHandle GetControlHandle() { return m_controlHandle ; }
66 void SetControlHandle( ControlHandle handle ) { m_controlHandle = handle ; }
67
68 void SetSize(const wxSize& size) ;
69 void SetPosition( const wxPoint& position ) ;
70 wxSize GetSize() const
71 {
72 if ( IsControl() )
73 {
74 return GetControl()->GetSize() ;
75 }
76 else if ( IsButton() )
77 {
78 return GetToolBar()->GetToolSize() ;
79 }
80 else
81 {
82 wxSize sz = GetToolBar()->GetToolSize() ;
83 sz.x /= 4 ;
84 sz.y /= 4 ;
85 return sz ;
86 }
87 }
88 wxPoint GetPosition() const
89 {
90 return wxPoint(m_x, m_y);
91 }
92private :
93 void Init()
94 {
95 m_controlHandle = NULL ;
96 }
97 ControlHandle m_controlHandle ;
98
99 wxCoord m_x;
100 wxCoord m_y;
101};
102
103// ============================================================================
104// implementation
105// ============================================================================
106
107// ----------------------------------------------------------------------------
108// wxToolBarTool
109// ----------------------------------------------------------------------------
110
111void wxToolBarTool::SetSize(const wxSize& size)
112{
113 if ( IsControl() )
114 {
115 GetControl()->SetSize( size ) ;
116 }
117}
118
119void wxToolBarTool::SetPosition(const wxPoint& position)
120{
121 m_x = position.x;
122 m_y = position.y;
123
124 if ( IsButton() )
125 {
126 int x , y ;
127 x = y = 0 ;
128 WindowRef rootwindow = (WindowRef) GetToolBar()->MacGetRootWindow() ;
129 GetToolBar()->MacWindowToRootWindow( &x , &y ) ;
130 int mac_x = x + position.x ;
131 int mac_y = y + position.y ;
132
133
134 Rect contrlRect ;
135 GetControlBounds( m_controlHandle , &contrlRect ) ;
136 int former_mac_x = contrlRect.left ;
137 int former_mac_y = contrlRect.top ;
138 wxSize sz = GetToolBar()->GetToolSize() ;
139
140 if ( mac_x != former_mac_x || mac_y != former_mac_y )
141 {
142 {
143 Rect inval = { former_mac_y , former_mac_x , former_mac_y + sz.y , former_mac_x + sz.x } ;
144 InvalWindowRect( rootwindow , &inval ) ;
145 }
146 UMAMoveControl( m_controlHandle , mac_x , mac_y ) ;
147 {
148 Rect inval = { mac_y , mac_x , mac_y + sz.y , mac_x + sz.x } ;
149 InvalWindowRect( rootwindow , &inval ) ;
150 }
151 }
152 }
153 else if ( IsControl() )
154 {
155 GetControl()->Move( position ) ;
156 }
157}
158
159const short kwxMacToolBarToolDefaultWidth = 24 ;
160const short kwxMacToolBarToolDefaultHeight = 22 ;
161const short kwxMacToolBarTopMargin = 2 ;
162const short kwxMacToolBarLeftMargin = 2 ;
163
164wxToolBarTool::wxToolBarTool(wxToolBar *tbar,
165 int id,
166 const wxString& label,
167 const wxBitmap& bmpNormal,
168 const wxBitmap& bmpDisabled,
169 wxItemKind kind,
170 wxObject *clientData,
171 const wxString& shortHelp,
172 const wxString& longHelp)
173 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpDisabled, kind,
174 clientData, shortHelp, longHelp)
175{
176 Init() ;
177
178 WindowRef window = (WindowRef) tbar->MacGetRootWindow() ;
179 wxSize toolSize = tbar->GetToolSize() ;
180 Rect toolrect = { 0, 0 , toolSize.y , toolSize.x } ;
181
182 ControlButtonContentInfo info ;
183 wxMacCreateBitmapButton( &info , GetNormalBitmap() ) ;
184
185 SInt16 behaviour = kControlBehaviorOffsetContents ;
186 if ( CanBeToggled() )
187 behaviour += kControlBehaviorToggles ;
188
189 if ( info.contentType != kControlNoContent )
190 {
191 m_controlHandle = ::NewControl( window , &toolrect , "\p" , false , 0 ,
192 behaviour + info.contentType , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
193
194 ::SetControlData( m_controlHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ;
195 }
196 else
197 {
198 m_controlHandle = ::NewControl( window , &toolrect , "\p" , false , 0 ,
199 behaviour , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ;
200 }
201 UMAShowControl( m_controlHandle ) ;
202 if ( !IsEnabled() )
203 {
204 UMADeactivateControl( m_controlHandle ) ;
205 }
206 if ( CanBeToggled() && IsToggled() )
207 {
208 ::SetControl32BitValue( m_controlHandle , 1 ) ;
209 }
210 else
211 {
212 ::SetControl32BitValue( m_controlHandle , 0 ) ;
213 }
214
215 ControlHandle container = (ControlHandle) tbar->MacGetContainerForEmbedding() ;
216 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
217 ::EmbedControl( m_controlHandle , container ) ;
218}
219
220
221wxToolBarToolBase *wxToolBar::CreateTool(int id,
222 const wxString& label,
223 const wxBitmap& bmpNormal,
224 const wxBitmap& bmpDisabled,
225 wxItemKind kind,
226 wxObject *clientData,
227 const wxString& shortHelp,
228 const wxString& longHelp)
229{
230 return new wxToolBarTool(this, id, label, bmpNormal, bmpDisabled, kind,
231 clientData, shortHelp, longHelp);
232}
233
234wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
235{
236 return new wxToolBarTool(this, control);
237}
238
239void wxToolBar::Init()
240{
241 m_maxWidth = -1;
242 m_maxHeight = -1;
243 m_defaultWidth = kwxMacToolBarToolDefaultWidth;
244 m_defaultHeight = kwxMacToolBarToolDefaultHeight;
245}
246
247bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
248 long style, const wxString& name)
249{
250 int x = pos.x;
251 int y = pos.y;
252 int width = size.x;
253 int height = size.y;
254
255 if (width <= 0)
256 width = 100;
257 if (height <= 0)
258 height = 30;
259 if (x < 0)
260 x = 0;
261 if (y < 0)
262 y = 0;
263
264 SetName(name);
265
266 m_windowStyle = style;
267 parent->AddChild(this);
268
269 m_backgroundColour = parent->GetBackgroundColour() ;
270 m_foregroundColour = parent->GetForegroundColour() ;
271
272 if (id == -1)
273 m_windowId = NewControlId();
274 else
275 m_windowId = id;
276
277 {
278 m_width = size.x ;
279 m_height = size.y ;
280 int x = pos.x ;
281 int y = pos.y ;
282 AdjustForParentClientOrigin(x, y, wxSIZE_USE_EXISTING);
283 m_x = x ;
284 m_y = y ;
285 }
286
287 return TRUE;
288}
289
290wxToolBar::~wxToolBar()
291{
292 // we must refresh the frame size when the toolbar is deleted but the frame
293 // is not - otherwise toolbar leaves a hole in the place it used to occupy
294}
295
296bool wxToolBar::Realize()
297{
298 if (m_tools.GetCount() == 0)
299 return FALSE;
300
301 int x = m_xMargin + kwxMacToolBarLeftMargin ;
302 int y = m_yMargin + kwxMacToolBarTopMargin ;
303
304 int tw, th;
305 GetSize(& tw, & th);
306
307 int maxWidth = 0 ;
308 int maxHeight = 0 ;
309
310 int maxToolWidth = 0;
311 int maxToolHeight = 0;
312
313 // Find the maximum tool width and height
314 wxToolBarToolsList::Node *node = m_tools.GetFirst();
315 while ( node )
316 {
317 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
318 wxSize sz = tool->GetSize() ;
319
320 if ( sz.x > maxToolWidth )
321 maxToolWidth = sz.x ;
322 if (sz.y> maxToolHeight)
323 maxToolHeight = sz.y;
324
325 node = node->GetNext();
326 }
327
328 int separatorSize = GetToolSize().x / 4 ;
329
330 node = m_tools.GetFirst();
331 while (node)
332 {
333 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
334 wxSize cursize = tool->GetSize() ;
335
336 // for the moment we just do a single row/column alignement
337 if ( x + cursize.x > maxWidth )
338 maxWidth = x + cursize.x ;
339 if ( y + cursize.y > maxHeight )
340 maxHeight = y + cursize.y ;
341
342 tool->SetPosition( wxPoint( x , y ) ) ;
343
344 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
345 {
346 y += cursize.y ;
347 }
348 else
349 {
350 x += cursize.x ;
351 }
352
353 node = node->GetNext();
354 }
355
356 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
357 {
358 if ( m_maxRows == 0 )
359 {
360 // if not set yet, only one row
361 SetRows(1);
362 }
363 maxWidth = tw ;
364 maxHeight += m_yMargin + kwxMacToolBarTopMargin;
365 m_maxHeight = maxHeight ;
366 }
367 else
368 {
369 if ( GetToolsCount() > 0 && m_maxRows == 0 )
370 {
371 // if not set yet, have one column
372 SetRows(GetToolsCount());
373 }
374 maxHeight = th ;
375 maxWidth += m_xMargin + kwxMacToolBarLeftMargin;
376 m_maxWidth = maxWidth ;
377 }
378
379 SetSize(maxWidth, maxHeight);
380
381 return TRUE;
382}
383
384void wxToolBar::SetToolBitmapSize(const wxSize& size)
385{
386 m_defaultWidth = size.x+4; m_defaultHeight = size.y+4;
387}
388
389// The button size is bigger than the bitmap size
390wxSize wxToolBar::GetToolSize() const
391{
392 return wxSize(m_defaultWidth + 4, m_defaultHeight + 4);
393}
394
395void wxToolBar::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
396{
397 wxToolBarToolsList::Node *node;
398 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
399 {
400 wxToolBarTool* tool = (wxToolBarTool*) node->GetData() ;
401 if ( tool->IsButton() )
402 {
403 if( tool->GetControlHandle() == control )
404 {
405 if ( tool->CanBeToggled() )
406 {
407 tool->Toggle( GetControl32BitValue( (ControlHandle) control ) ) ;
408 }
409 OnLeftClick( tool->GetId() , tool -> IsToggled() ) ;
410 break ;
411 }
412 }
413 }
414}
415
416void wxToolBar::SetRows(int nRows)
417{
418 if ( nRows == m_maxRows )
419 {
420 // avoid resizing the frame uselessly
421 return;
422 }
423
424 m_maxRows = nRows;
425}
426
427void wxToolBar::MacSuperChangedPosition()
428{
429 wxWindow::MacSuperChangedPosition() ;
430 Realize() ;
431}
432
433wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord x, wxCoord y) const
434{
435 wxToolBarToolsList::Node *node = m_tools.GetFirst();
436 while (node)
437 {
438 wxToolBarTool *tool = (wxToolBarTool *)node->GetData() ;
439 wxRect2DInt r( tool->GetPosition() , tool->GetSize() ) ;
440 if ( r.Contains( wxPoint( x , y ) ) )
441 {
442 return tool;
443 }
444
445 node = node->GetNext();
446 }
447
448 return (wxToolBarToolBase *)NULL;
449}
450
451wxString wxToolBar::MacGetToolTipString( wxPoint &pt )
452{
453 wxToolBarToolBase* tool = FindToolForPosition( pt.x , pt.y ) ;
454 if ( tool )
455 {
456 return tool->GetShortHelp() ;
457 }
458 return wxEmptyString ;
459}
460
461void wxToolBar::DoEnableTool(wxToolBarToolBase *t, bool enable)
462{
463 if (!IsShown())
464 return ;
465
466 wxToolBarTool *tool = (wxToolBarTool *)t;
467 if ( tool->IsControl() )
468 {
469 tool->GetControl()->Enable( enable ) ;
470 }
471 else if ( tool->IsButton() )
472 {
473 if ( enable )
474 UMAActivateControl( tool->GetControlHandle() ) ;
475 else
476 UMADeactivateControl( tool->GetControlHandle() ) ;
477 }
478}
479
480void wxToolBar::DoToggleTool(wxToolBarToolBase *t, bool toggle)
481{
482 if (!IsShown())
483 return ;
484
485 wxToolBarTool *tool = (wxToolBarTool *)t;
486 if ( tool->IsButton() )
487 {
488 ::SetControl32BitValue( tool->GetControlHandle() , toggle ) ;
489 }
490}
491
492bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos),
493 wxToolBarToolBase *tool)
494{
495 // nothing special to do here - we relayout in Realize() later
496 tool->Attach(this);
497
498 return TRUE;
499}
500
501void wxToolBar::DoSetToggle(wxToolBarToolBase *WXUNUSED(tool), bool WXUNUSED(toggle))
502{
503 wxFAIL_MSG( _T("not implemented") );
504}
505
506bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
507{
508 wxToolBarToolsList::Node *node;
509 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
510 {
511 wxToolBarToolBase *tool2 = node->GetData();
512 if ( tool2 == tool )
513 {
514 // let node point to the next node in the list
515 node = node->GetNext();
516
517 break;
518 }
519 }
520
521 wxSize sz = ((wxToolBarTool*)tool)->GetSize() ;
522
523 tool->Detach();
524
525 // and finally reposition all the controls after this one
526
527 for ( /* node -> first after deleted */ ; node; node = node->GetNext() )
528 {
529 wxToolBarTool *tool2 = (wxToolBarTool*) node->GetData();
530 wxPoint pt = tool2->GetPosition() ;
531
532 if ( GetWindowStyleFlag() & wxTB_VERTICAL )
533 {
534 pt.y -= sz.y ;
535 }
536 else
537 {
538 pt.x -= sz.x ;
539 }
540 tool2->SetPosition( pt ) ;
541 }
542
543 return TRUE ;
544}
545
546void wxToolBar::OnPaint(wxPaintEvent& event)
547{
548 wxPaintDC dc(this) ;
549 wxMacPortSetter helper(&dc) ;
550
551 Rect toolbarrect = { dc.YLOG2DEVMAC(0) , dc.XLOG2DEVMAC(0) ,
552 dc.YLOG2DEVMAC(m_height) , dc.XLOG2DEVMAC(m_width) } ;
553 UMADrawThemePlacard( &toolbarrect , IsEnabled() ? kThemeStateActive : kThemeStateInactive) ;
554 {
555 wxToolBarToolsList::Node *node;
556 for ( node = m_tools.GetFirst(); node; node = node->GetNext() )
557 {
558 wxToolBarTool* tool = (wxToolBarTool*) node->GetData() ;
559 if ( tool->IsButton() )
560 {
561 UMADrawControl( tool->GetControlHandle() ) ;
562 }
563 }
564 }
565}
566
567void wxToolBar::OnMouse( wxMouseEvent &event )
568{
569 if (event.GetEventType() == wxEVT_LEFT_DOWN || event.GetEventType() == wxEVT_LEFT_DCLICK )
570 {
571
572 int x = event.m_x ;
573 int y = event.m_y ;
574
575 MacClientToRootWindow( &x , &y ) ;
576
577 ControlHandle control ;
578 Point localwhere ;
579 SInt16 controlpart ;
580 WindowRef window = (WindowRef) MacGetRootWindow() ;
581
582 localwhere.h = x ;
583 localwhere.v = y ;
584
585 short modifiers = 0;
586
587 if ( !event.m_leftDown && !event.m_rightDown )
588 modifiers |= btnState ;
589
590 if ( event.m_shiftDown )
591 modifiers |= shiftKey ;
592
593 if ( event.m_controlDown )
594 modifiers |= controlKey ;
595
596 if ( event.m_altDown )
597 modifiers |= optionKey ;
598
599 if ( event.m_metaDown )
600 modifiers |= cmdKey ;
601
602 controlpart = ::FindControl( localwhere , window , &control ) ;
603 {
604 if ( control && ::IsControlActive( control ) )
605 {
606 {
607 if ( controlpart == kControlIndicatorPart && !UMAHasAppearance() )
608 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) NULL ) ;
609 else
610 controlpart = ::HandleControlClick( control , localwhere , modifiers , (ControlActionUPP) -1 ) ;
611 wxTheApp->s_lastMouseDown = 0 ;
612 if ( controlpart && ! ( ( UMAHasAppearance() || (controlpart != kControlIndicatorPart) )
613 && (IsKindOf( CLASSINFO( wxScrollBar ) ) ) ) ) // otherwise we will get the event twice
614 {
615 MacHandleControlClick( control , controlpart ) ;
616 }
617 }
618 }
619 }
620 }
621}
622
623#endif // wxUSE_TOOLBAR
624