1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
24 #define XtDisplay XTDISPLAY
27 #include "wx/toolbar.h"
33 #include "wx/settings.h"
37 #pragma message disable nosimpint
40 #include <Xm/PushBG.h>
43 #include <Xm/ToggleB.h>
44 #include <Xm/ToggleBG.h>
47 #pragma message enable nosimpint
50 #include "wx/motif/private.h"
51 #include "wx/motif/bmpmotif.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 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
) )
209 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
211 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
213 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
214 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
217 XmNresizePolicy
, XmRESIZE_NONE
,
220 Widget toolbar = XtVaCreateManagedWidget("toolbar",
221 xmFormWidgetClass, (Widget) m_clientWidget,
222 XmNtraversalOn, False,
223 XmNhorizontalSpacing, 0,
224 XmNverticalSpacing, 0,
232 m_mainWidget
= (WXWidget
) toolbar
;
239 if( rPos
.x
== -1 ) rPos
.x
= 0;
240 if( rPos
.y
== -1 ) rPos
.y
= 0;
241 if( rSize
.x
== -1 && GetParent() )
242 rSize
.x
= GetParent()->GetSize().x
;
244 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
245 rPos
.x
, rPos
.y
, rSize
.x
, rSize
.y
);
247 ChangeBackgroundColour();
252 wxToolBar::~wxToolBar()
254 delete wxTheToolBarTimer
;
255 wxTheToolBarTimer
= NULL
;
258 bool wxToolBar::Realize()
260 if ( m_tools
.GetCount() == 0 )
266 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
269 const int separatorSize
= GetToolSeparation(); // 8;
270 wxSize margins
= GetToolMargins();
271 int packing
= GetToolPacking();
272 int marginX
= margins
.x
;
273 int marginY
= margins
.y
;
275 int currentX
= marginX
;
276 int currentY
= marginY
;
278 int buttonHeight
= 0, buttonWidth
= 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() )
384 WXPixel backgroundPixel
;
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
407 WXPixel backgroundPixel
;
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);
475 node
= node
->GetNext();
479 isVertical
? buttonWidth
+ 2 * marginX
: -1,
480 isVertical
? -1 : buttonHeight
+ 2*marginY
);
485 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
486 wxCoord
WXUNUSED(y
)) const
488 wxFAIL_MSG( _T("TODO") );
490 return (wxToolBarToolBase
*)NULL
;
493 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
500 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
504 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
505 const int separatorSize
= GetToolSeparation(); // 8;
506 int packing
= GetToolPacking();
509 for( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
510 node
; node
= node
->GetNext() )
512 wxToolBarTool
*t
= (wxToolBarTool
*)node
->GetData();
516 switch ( t
->GetStyle() )
518 case wxTOOL_STYLE_CONTROL
:
520 wxSize size
= t
->GetControl()->GetSize();
521 offset
= isVertical
? size
.y
: size
.x
;
525 case wxTOOL_STYLE_SEPARATOR
:
526 offset
= isVertical
? 0 : separatorSize
;
528 case wxTOOL_STYLE_BUTTON
:
530 Widget w
= t
->GetButtonWidget();
531 Dimension width
, height
;
538 offset
= isVertical
? height
: width
;
546 switch ( t
->GetStyle() )
548 case wxTOOL_STYLE_CONTROL
:
550 wxPoint location
= t
->GetControl()->GetPosition();
553 location
.y
-= offset
;
555 location
.x
-= offset
;
557 t
->GetControl()->Move( location
);
560 case wxTOOL_STYLE_SEPARATOR
:
562 case wxTOOL_STYLE_BUTTON
:
565 XtVaGetValues( t
->GetButtonWidget(),
571 y
= (Dimension
)(y
- offset
);
573 x
= (Dimension
)(x
- offset
);
575 XtVaSetValues( t
->GetButtonWidget(),
588 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
590 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
591 if (tool
->GetButtonWidget()){
592 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
593 } else if (wxTOOL_STYLE_CONTROL
== tool
->GetStyle()){
594 // Controls (such as wxChoice) do not have button widgets
595 tool
->GetControl()->Enable(enable
);
599 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
601 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
603 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
606 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
607 bool WXUNUSED(toggle
))
612 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
614 int old_width
, old_height
;
615 GetSize(&old_width
, &old_height
);
617 // Correct width and height if needed.
618 if ( width
== -1 || height
== -1 )
620 wxSize defaultSize
= GetSize();
623 width
= defaultSize
.x
;
625 height
= defaultSize
.y
;
628 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
630 // We must refresh the frame size when the toolbar changes size
631 // otherwise the toolbar can be shown incorrectly
632 if ( old_width
!= width
|| old_height
!= height
)
634 // But before we send the size event check it
635 // we have a frame that is not being deleted.
636 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
637 if ( frame
&& !frame
->IsBeingDeleted() )
639 frame
->SendSizeEvent();
644 // ----------------------------------------------------------------------------
646 // ----------------------------------------------------------------------------
648 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
650 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
653 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
654 if ( tool
->GetButtonWidget() == w
)
659 node
= node
->GetNext();
662 return (wxToolBarToolBase
*)NULL
;
665 static void wxToolButtonCallback(Widget w
,
666 XtPointer clientData
,
667 XtPointer
WXUNUSED(ptr
))
669 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
670 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
674 if ( tool
->CanBeToggled() )
677 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
685 static void wxToolButtonPopupCallback(Widget w
,
686 XtPointer client_data
,
688 Boolean
*WXUNUSED(continue_to_dispatch
))
690 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
691 static const int delayMilli
= 800;
693 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
694 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
699 wxString tooltip
= tool
->GetShortHelp();
703 if (!wxTheToolBarTimer
)
704 wxTheToolBarTimer
= new wxToolBarTimer
;
706 wxToolBarTimer::buttonWidget
= w
;
707 wxToolBarTimer::helpString
= tooltip
;
709 /************************************************************/
710 /* Popup help label */
711 /************************************************************/
712 if (event
->type
== EnterNotify
)
714 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
716 XtDestroyWidget (wxToolBarTimer::help_popup
);
717 XtPopdown (wxToolBarTimer::help_popup
);
719 wxToolBarTimer::help_popup
= (Widget
) 0;
722 wxTheToolBarTimer
->Start(delayMilli
, true);
725 /************************************************************/
726 /* Popdown help label */
727 /************************************************************/
728 else if (event
->type
== LeaveNotify
)
730 if (wxTheToolBarTimer
)
731 wxTheToolBarTimer
->Stop();
732 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
734 XtDestroyWidget (wxToolBarTimer::help_popup
);
735 XtPopdown (wxToolBarTimer::help_popup
);
737 wxToolBarTimer::help_popup
= (Widget
) 0;
741 void wxToolBarTimer::Notify()
745 /************************************************************/
746 /* Create shell without window decorations */
747 /************************************************************/
748 help_popup
= XtVaCreatePopupShell ("shell",
749 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
752 /************************************************************/
753 /* Get absolute position on display of toolbar button */
754 /************************************************************/
755 XtTranslateCoords (buttonWidget
,
760 // Move the tooltip more or less above the button
761 int yOffset
= 20; // TODO: What should be really?
762 y
= (Position
)(y
- yOffset
);
763 if (y
< yOffset
) y
= 0;
765 /************************************************************/
766 /* Set the position of the help popup */
767 /************************************************************/
768 XtVaSetValues (help_popup
,
773 /************************************************************/
774 /* Create help label */
775 /************************************************************/
776 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
777 XtVaCreateManagedWidget ("help_label",
778 xmLabelWidgetClass
, help_popup
,
779 XmNlabelString
, text
,
781 XmNforeground
, XtRString
, "black",
784 XmNbackground
, XtRString
, "LightGoldenrod",
785 strlen("LightGoldenrod")+1,
789 /************************************************************/
790 /* Popup help label */
791 /************************************************************/
792 XtPopup (help_popup
, XtGrabNone
);