1 /////////////////////////////////////////////////////////////////////////////
2 // Name: motif/toolbar.cpp
4 // Author: Julian Smart
5 // Modified by: 13.12.99 by VZ during toolbar classes reorganization
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "toolbar.h"
25 #define XtDisplay XTDISPLAY
28 #include "wx/settings.h"
31 #include "wx/toolbar.h"
35 #pragma message disable nosimpint
38 #include <Xm/PushBG.h>
41 #include <Xm/ToggleB.h>
42 #include <Xm/ToggleBG.h>
45 #pragma message enable nosimpint
48 #include "wx/motif/private.h"
49 #include "wx/motif/bmpmotif.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 #if !USE_SHARED_LIBRARY
56 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 static void wxToolButtonCallback (Widget w
, XtPointer clientData
,
65 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
66 XEvent
*event
, Boolean
*continue_to_dispatch
);
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 class wxToolBarTimer
: public wxTimer
75 virtual void Notify();
77 static Widget help_popup
;
78 static Widget buttonWidget
;
79 static wxString helpString
;
82 class wxToolBarTool
: public wxToolBarToolBase
85 wxToolBarTool(wxToolBar
*tbar
,
87 const wxString
& label
,
88 const wxBitmap
& bmpNormal
,
89 const wxBitmap
& bmpToggled
,
92 const wxString
& shortHelp
,
93 const wxString
& longHelp
)
94 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpToggled
, kind
,
95 clientData
, shortHelp
, longHelp
)
100 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
101 : wxToolBarToolBase(tbar
, control
)
106 virtual ~wxToolBarTool();
109 void SetWidget(Widget widget
) { m_widget
= widget
; }
110 Widget
GetButtonWidget() const { return m_widget
; }
112 Pixmap
GetArmPixmap()
114 m_bitmapCache
.SetBitmap( GetNormalBitmap() );
115 return (Pixmap
)m_bitmapCache
.GetArmPixmap( (WXWidget
)m_widget
);
118 Pixmap
GetInsensPixmap()
120 m_bitmapCache
.SetBitmap( GetNormalBitmap() );
121 return (Pixmap
)m_bitmapCache
.GetInsensPixmap( (WXWidget
)m_widget
);
127 wxBitmapCache m_bitmapCache
;
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 static wxToolBarTimer
* wxTheToolBarTimer
= (wxToolBarTimer
*) NULL
;
136 Widget
wxToolBarTimer::help_popup
= (Widget
) 0;
137 Widget
wxToolBarTimer::buttonWidget
= (Widget
) 0;
138 wxString
wxToolBarTimer::helpString
;
140 // ============================================================================
142 // ============================================================================
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
149 const wxString
& label
,
150 const wxBitmap
& bmpNormal
,
151 const wxBitmap
& bmpToggled
,
153 wxObject
*clientData
,
154 const wxString
& shortHelp
,
155 const wxString
& longHelp
)
157 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpToggled
, kind
,
158 clientData
, shortHelp
, longHelp
);
162 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
164 return new wxToolBarTool(this, control
);
167 void wxToolBarTool::Init()
169 m_widget
= (Widget
)0;
172 wxToolBarTool::~wxToolBarTool()
175 XtDestroyWidget(m_widget
);
178 // ----------------------------------------------------------------------------
179 // wxToolBar construction
180 // ----------------------------------------------------------------------------
182 void wxToolBar::Init()
187 m_defaultHeight
= 22;
189 m_toolSeparation
= 8;
196 bool wxToolBar::Create(wxWindow
*parent
,
201 const wxString
& name
)
203 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
204 wxDefaultValidator
, name
) )
207 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
209 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
211 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
212 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
215 XmNresizePolicy
, XmRESIZE_NONE
,
218 Widget toolbar = XtVaCreateManagedWidget("toolbar",
219 xmFormWidgetClass, (Widget) m_clientWidget,
220 XmNtraversalOn, False,
221 XmNhorizontalSpacing, 0,
222 XmNverticalSpacing, 0,
230 m_mainWidget
= (WXWidget
) toolbar
;
237 if( rPos
.x
== -1 ) rPos
.x
= 0;
238 if( rPos
.y
== -1 ) rPos
.y
= 0;
239 if( rSize
.x
== -1 && GetParent() )
240 rSize
.x
= GetParent()->GetSize().x
;
242 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
243 rPos
.x
, rPos
.y
, rSize
.x
, rSize
.y
);
245 ChangeBackgroundColour();
250 wxToolBar::~wxToolBar()
252 delete wxTheToolBarTimer
;
253 wxTheToolBarTimer
= NULL
;
256 bool wxToolBar::Realize()
258 if ( m_tools
.GetCount() == 0 )
264 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
267 const int separatorSize
= GetToolSeparation(); // 8;
268 wxSize margins
= GetToolMargins();
269 int packing
= GetToolPacking();
270 int marginX
= margins
.x
;
271 int marginY
= margins
.y
;
273 int currentX
= marginX
;
274 int currentY
= marginY
;
276 int buttonHeight
= 0, buttonWidth
= 0;
278 int currentSpacing
= 0;
281 Pixmap pixmap
, insensPixmap
;
282 wxBitmap bmp
, insensBmp
;
284 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
287 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
289 switch ( tool
->GetStyle() )
291 case wxTOOL_STYLE_CONTROL
:
293 wxControl
* control
= tool
->GetControl();
294 wxSize sz
= control
->GetSize();
295 wxPoint pos
= control
->GetPosition();
296 // Allow a control to specify a y[x]-offset by setting
297 // its initial position, but still don't allow it to
298 // position itself above the top[left] margin.
299 int controlY
= (pos
.y
> 0) ? pos
.y
: currentY
;
300 int controlX
= (pos
.x
> 0) ? pos
.x
: currentX
;
301 control
->Move( isVertical
? controlX
: currentX
,
302 isVertical
? currentY
: controlY
);
304 currentY
+= sz
.y
+ packing
;
306 currentX
+= sz
.x
+ packing
;
310 case wxTOOL_STYLE_SEPARATOR
:
311 // skip separators for vertical toolbars
314 currentX
+= separatorSize
;
318 case wxTOOL_STYLE_BUTTON
:
321 if ( tool
->CanBeToggled() && !tool
->GetButtonWidget() )
323 button
= XtVaCreateWidget("toggleButton",
324 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
325 XmNx
, currentX
, XmNy
, currentY
,
326 XmNindicatorOn
, False
,
327 XmNshadowThickness
, 2,
332 XmNmultiClick
, XmMULTICLICK_KEEP
,
333 XmNlabelType
, XmPIXMAP
,
335 XtAddCallback ((Widget
) button
,
336 XmNvalueChangedCallback
,
337 (XtCallbackProc
) wxToolButtonCallback
,
340 XtVaSetValues ((Widget
) button
,
342 m_backgroundColour
.AllocColour
343 (XtDisplay((Widget
) button
)),
346 else if( !tool
->GetButtonWidget() )
348 button
= XtVaCreateWidget("button",
349 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
350 XmNx
, currentX
, XmNy
, currentY
,
351 XmNpushButtonEnabled
, True
,
352 XmNmultiClick
, XmMULTICLICK_KEEP
,
353 XmNlabelType
, XmPIXMAP
,
355 XtAddCallback (button
,
357 (XtCallbackProc
) wxToolButtonCallback
,
361 if( !tool
->GetButtonWidget() )
363 wxDoChangeBackgroundColour((WXWidget
) button
,
364 m_backgroundColour
, TRUE
);
366 tool
->SetWidget(button
);
370 button
= (Widget
)tool
->GetButtonWidget();
371 XtVaSetValues( button
,
372 XmNx
, currentX
, XmNy
, currentY
,
376 // For each button, if there is a mask, we must create
377 // a new wxBitmap that has the correct background colour
378 // for the button. Otherwise the background will just be
379 // e.g. black if a transparent XPM has been loaded.
380 bmp
= tool
->GetNormalBitmap();
381 insensBmp
= tool
->GetDisabledBitmap();
382 if ( bmp
.GetMask() || insensBmp
.GetMask() )
385 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
389 col
.SetPixel(backgroundPixel
);
391 if( bmp
.Ok() && bmp
.GetMask() )
393 bmp
= wxCreateMaskedBitmap(bmp
, col
);
394 tool
->SetNormalBitmap(bmp
);
397 if( insensBmp
.Ok() && insensBmp
.GetMask() )
399 insensBmp
= wxCreateMaskedBitmap(insensBmp
, col
);
400 tool
->SetDisabledBitmap(insensBmp
);
404 // Create a selected/toggled bitmap. If there isn't a 2nd
405 // bitmap, we need to create it (with a darker, selected
408 if ( tool
->CanBeToggled() )
409 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
412 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
415 col
.SetPixel(backgroundPixel
);
417 pixmap
= (Pixmap
) bmp
.GetDrawable();
419 wxBitmap tmp
= tool
->GetDisabledBitmap();
421 insensPixmap
= tmp
.Ok() ?
422 (Pixmap
)tmp
.GetDrawable() :
423 tool
->GetInsensPixmap();
426 if (tool
->CanBeToggled())
429 Pixmap pixmap2
= tool
->GetArmPixmap();
430 Pixmap insensPixmap2
= tool
->GetInsensPixmap();
432 XtVaSetValues (button
,
433 XmNfillOnSelect
, True
,
434 XmNlabelPixmap
, pixmap
,
435 XmNselectPixmap
, pixmap2
,
436 XmNlabelInsensitivePixmap
, insensPixmap
,
437 XmNselectInsensitivePixmap
, insensPixmap2
,
438 XmNlabelType
, XmPIXMAP
,
443 Pixmap pixmap2
= tool
->GetArmPixmap();
446 XtVaSetValues(button
,
447 XmNlabelPixmap
, pixmap
,
448 XmNlabelInsensitivePixmap
, insensPixmap
,
449 XmNarmPixmap
, pixmap2
,
453 XtManageChild(button
);
456 Dimension width
, height
;
457 XtVaGetValues(button
,
462 currentY
+= height
+ packing
;
464 currentX
+= width
+ packing
;
465 buttonHeight
= wxMax(buttonHeight
, height
);
466 buttonWidth
= wxMax(buttonWidth
, width
);
469 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
470 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
476 node
= node
->GetNext();
480 isVertical
? buttonWidth
+ 2 * marginX
: -1,
481 isVertical
? -1 : buttonHeight
+ 2*marginY
);
486 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
487 wxCoord
WXUNUSED(y
)) const
489 wxFAIL_MSG( _T("TODO") );
491 return (wxToolBarToolBase
*)NULL
;
494 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
501 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
505 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
506 const int separatorSize
= GetToolSeparation(); // 8;
507 int packing
= GetToolPacking();
510 for( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
511 node
; node
= node
->GetNext() )
513 wxToolBarTool
*t
= (wxToolBarTool
*)node
->GetData();
517 switch ( t
->GetStyle() )
519 case wxTOOL_STYLE_CONTROL
:
521 wxSize size
= t
->GetControl()->GetSize();
522 offset
= isVertical
? size
.y
: size
.x
;
526 case wxTOOL_STYLE_SEPARATOR
:
527 offset
= isVertical
? 0 : separatorSize
;
529 case wxTOOL_STYLE_BUTTON
:
531 Widget w
= t
->GetButtonWidget();
532 Dimension width
, height
;
539 offset
= isVertical
? height
: width
;
547 switch ( t
->GetStyle() )
549 case wxTOOL_STYLE_CONTROL
:
551 wxPoint pos
= t
->GetControl()->GetPosition();
558 t
->GetControl()->Move( pos
);
561 case wxTOOL_STYLE_SEPARATOR
:
563 case wxTOOL_STYLE_BUTTON
:
566 XtVaGetValues( t
->GetButtonWidget(),
576 XtVaSetValues( t
->GetButtonWidget(),
589 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
591 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
593 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
596 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
598 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
600 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
603 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
604 bool WXUNUSED(toggle
))
609 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
611 int old_width
, old_height
;
612 GetSize(&old_width
, &old_height
);
614 // Correct width and height if needed.
615 if ( width
== -1 || height
== -1 )
617 wxSize defaultSize
= GetSize();
620 width
= defaultSize
.x
;
622 height
= defaultSize
.y
;
625 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
627 // We must refresh the frame size when the toolbar changes size
628 // otherwise the toolbar can be shown incorrectly
629 if ( old_width
!= width
|| old_height
!= height
)
631 // But before we send the size event check it
632 // we have a frame that is not being deleted.
633 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
634 if ( frame
&& !frame
->IsBeingDeleted() )
636 frame
->SendSizeEvent();
641 // ----------------------------------------------------------------------------
643 // ----------------------------------------------------------------------------
645 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
647 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
650 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
651 if ( tool
->GetButtonWidget() == w
)
656 node
= node
->GetNext();
659 return (wxToolBarToolBase
*)NULL
;
662 static void wxToolButtonCallback(Widget w
,
663 XtPointer clientData
,
664 XtPointer
WXUNUSED(ptr
))
666 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
667 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
671 if ( tool
->CanBeToggled() )
674 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
682 static void wxToolButtonPopupCallback(Widget w
,
683 XtPointer client_data
,
685 Boolean
*WXUNUSED(continue_to_dispatch
))
687 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
688 static const int delayMilli
= 800;
690 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
691 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
696 wxString tooltip
= tool
->GetShortHelp();
700 if (!wxTheToolBarTimer
)
701 wxTheToolBarTimer
= new wxToolBarTimer
;
703 wxToolBarTimer::buttonWidget
= w
;
704 wxToolBarTimer::helpString
= tooltip
;
706 /************************************************************/
707 /* Popup help label */
708 /************************************************************/
709 if (event
->type
== EnterNotify
)
711 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
713 XtDestroyWidget (wxToolBarTimer::help_popup
);
714 XtPopdown (wxToolBarTimer::help_popup
);
716 wxToolBarTimer::help_popup
= (Widget
) 0;
719 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
722 /************************************************************/
723 /* Popdown help label */
724 /************************************************************/
725 else if (event
->type
== LeaveNotify
)
727 if (wxTheToolBarTimer
)
728 wxTheToolBarTimer
->Stop();
729 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
731 XtDestroyWidget (wxToolBarTimer::help_popup
);
732 XtPopdown (wxToolBarTimer::help_popup
);
734 wxToolBarTimer::help_popup
= (Widget
) 0;
738 void wxToolBarTimer::Notify()
742 /************************************************************/
743 /* Create shell without window decorations */
744 /************************************************************/
745 help_popup
= XtVaCreatePopupShell ("shell",
746 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
749 /************************************************************/
750 /* Get absolute position on display of toolbar button */
751 /************************************************************/
752 XtTranslateCoords (buttonWidget
,
757 // Move the tooltip more or less above the button
758 int yOffset
= 20; // TODO: What should be really?
760 if (y
< yOffset
) y
= 0;
762 /************************************************************/
763 /* Set the position of the help popup */
764 /************************************************************/
765 XtVaSetValues (help_popup
,
770 /************************************************************/
771 /* Create help label */
772 /************************************************************/
773 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
774 XtVaCreateManagedWidget ("help_label",
775 xmLabelWidgetClass
, help_popup
,
776 XmNlabelString
, text
,
778 XmNforeground
, XtRString
, "black",
781 XmNbackground
, XtRString
, "LightGoldenrod",
782 strlen("LightGoldenrod")+1,
786 /************************************************************/
787 /* Popup help label */
788 /************************************************************/
789 XtPopup (help_popup
, XtGrabNone
);