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
, const wxString
& label
)
101 : wxToolBarToolBase(tbar
, control
, label
)
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
);
163 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
165 return new wxToolBarTool(this, control
, label
);
168 void wxToolBarTool::Init()
170 m_widget
= (Widget
)0;
173 wxToolBarTool::~wxToolBarTool()
176 XtDestroyWidget(m_widget
);
179 // ----------------------------------------------------------------------------
180 // wxToolBar construction
181 // ----------------------------------------------------------------------------
183 void wxToolBar::Init()
188 m_defaultHeight
= 22;
190 m_toolSeparation
= 8;
197 bool wxToolBar::Create(wxWindow
*parent
,
202 const wxString
& name
)
204 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
205 wxDefaultValidator
, name
) )
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
;
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
;
243 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
244 rPos
.x
, rPos
.y
, rSize
.x
, rSize
.y
);
249 wxToolBar::~wxToolBar()
251 delete wxTheToolBarTimer
;
252 wxTheToolBarTimer
= NULL
;
255 bool wxToolBar::Realize()
257 if ( m_tools
.GetCount() == 0 )
263 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
266 const int separatorSize
= GetToolSeparation(); // 8;
267 wxSize margins
= GetToolMargins();
268 int packing
= GetToolPacking();
269 int marginX
= margins
.x
;
270 int marginY
= margins
.y
;
272 int currentX
= marginX
;
273 int currentY
= marginY
;
275 int buttonHeight
= 0, buttonWidth
= 0;
278 Pixmap pixmap
, insensPixmap
;
279 wxBitmap bmp
, insensBmp
;
281 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
284 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
286 switch ( tool
->GetStyle() )
288 case wxTOOL_STYLE_CONTROL
:
290 wxControl
* control
= tool
->GetControl();
291 wxSize sz
= control
->GetSize();
292 wxPoint pos
= control
->GetPosition();
293 // Allow a control to specify a y[x]-offset by setting
294 // its initial position, but still don't allow it to
295 // position itself above the top[left] margin.
296 int controlY
= (pos
.y
> 0) ? pos
.y
: currentY
;
297 int controlX
= (pos
.x
> 0) ? pos
.x
: currentX
;
298 control
->Move( isVertical
? controlX
: currentX
,
299 isVertical
? currentY
: controlY
);
301 currentY
+= sz
.y
+ packing
;
303 currentX
+= sz
.x
+ packing
;
307 case wxTOOL_STYLE_SEPARATOR
:
308 // skip separators for vertical toolbars
311 currentX
+= separatorSize
;
315 case wxTOOL_STYLE_BUTTON
:
318 if ( tool
->CanBeToggled() && !tool
->GetButtonWidget() )
320 button
= XtVaCreateWidget("toggleButton",
321 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
322 XmNx
, currentX
, XmNy
, currentY
,
323 XmNindicatorOn
, False
,
324 XmNshadowThickness
, 2,
329 XmNmultiClick
, XmMULTICLICK_KEEP
,
330 XmNlabelType
, XmPIXMAP
,
332 XtAddCallback ((Widget
) button
,
333 XmNvalueChangedCallback
,
334 (XtCallbackProc
) wxToolButtonCallback
,
337 XtVaSetValues ((Widget
) button
,
339 m_backgroundColour
.AllocColour
340 (XtDisplay((Widget
) button
)),
343 else if( !tool
->GetButtonWidget() )
345 button
= XtVaCreateWidget("button",
346 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
347 XmNx
, currentX
, XmNy
, currentY
,
348 XmNpushButtonEnabled
, True
,
349 XmNmultiClick
, XmMULTICLICK_KEEP
,
350 XmNlabelType
, XmPIXMAP
,
352 XtAddCallback (button
,
354 (XtCallbackProc
) wxToolButtonCallback
,
358 if( !tool
->GetButtonWidget() )
360 wxDoChangeBackgroundColour((WXWidget
) button
,
361 m_backgroundColour
, true);
363 tool
->SetWidget(button
);
367 button
= (Widget
)tool
->GetButtonWidget();
368 XtVaSetValues( button
,
369 XmNx
, currentX
, XmNy
, currentY
,
373 // For each button, if there is a mask, we must create
374 // a new wxBitmap that has the correct background colour
375 // for the button. Otherwise the background will just be
376 // e.g. black if a transparent XPM has been loaded.
377 bmp
= tool
->GetNormalBitmap();
378 insensBmp
= tool
->GetDisabledBitmap();
379 if ( bmp
.GetMask() || insensBmp
.GetMask() )
381 WXPixel backgroundPixel
;
382 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
386 col
.SetPixel(backgroundPixel
);
388 if( bmp
.Ok() && bmp
.GetMask() )
390 bmp
= wxCreateMaskedBitmap(bmp
, col
);
391 tool
->SetNormalBitmap(bmp
);
394 if( insensBmp
.Ok() && insensBmp
.GetMask() )
396 insensBmp
= wxCreateMaskedBitmap(insensBmp
, col
);
397 tool
->SetDisabledBitmap(insensBmp
);
401 // Create a selected/toggled bitmap. If there isn't a 2nd
402 // bitmap, we need to create it (with a darker, selected
404 WXPixel backgroundPixel
;
405 if ( tool
->CanBeToggled() )
406 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
409 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
412 col
.SetPixel(backgroundPixel
);
414 pixmap
= (Pixmap
) bmp
.GetDrawable();
416 wxBitmap tmp
= tool
->GetDisabledBitmap();
418 insensPixmap
= tmp
.Ok() ?
419 (Pixmap
)tmp
.GetDrawable() :
420 tool
->GetInsensPixmap();
423 if (tool
->CanBeToggled())
426 Pixmap pixmap2
= tool
->GetArmPixmap();
427 Pixmap insensPixmap2
= tool
->GetInsensPixmap();
429 XtVaSetValues (button
,
430 XmNfillOnSelect
, True
,
431 XmNlabelPixmap
, pixmap
,
432 XmNselectPixmap
, pixmap2
,
433 XmNlabelInsensitivePixmap
, insensPixmap
,
434 XmNselectInsensitivePixmap
, insensPixmap2
,
435 XmNlabelType
, XmPIXMAP
,
440 Pixmap pixmap2
= tool
->GetArmPixmap();
443 XtVaSetValues(button
,
444 XmNlabelPixmap
, pixmap
,
445 XmNlabelInsensitivePixmap
, insensPixmap
,
446 XmNarmPixmap
, pixmap2
,
450 XtManageChild(button
);
453 Dimension width
, height
;
454 XtVaGetValues(button
,
459 currentY
+= height
+ packing
;
461 currentX
+= width
+ packing
;
462 buttonHeight
= wxMax(buttonHeight
, height
);
463 buttonWidth
= wxMax(buttonWidth
, width
);
466 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
467 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
472 node
= node
->GetNext();
476 isVertical
? buttonWidth
+ 2 * marginX
: -1,
477 isVertical
? -1 : buttonHeight
+ 2*marginY
);
482 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
483 wxCoord
WXUNUSED(y
)) const
485 wxFAIL_MSG( _T("TODO") );
487 return (wxToolBarToolBase
*)NULL
;
490 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
497 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
501 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
502 const int separatorSize
= GetToolSeparation(); // 8;
503 int packing
= GetToolPacking();
506 for( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
507 node
; node
= node
->GetNext() )
509 wxToolBarTool
*t
= (wxToolBarTool
*)node
->GetData();
513 switch ( t
->GetStyle() )
515 case wxTOOL_STYLE_CONTROL
:
517 wxSize size
= t
->GetControl()->GetSize();
518 offset
= isVertical
? size
.y
: size
.x
;
522 case wxTOOL_STYLE_SEPARATOR
:
523 offset
= isVertical
? 0 : separatorSize
;
525 case wxTOOL_STYLE_BUTTON
:
527 Widget w
= t
->GetButtonWidget();
528 Dimension width
, height
;
535 offset
= isVertical
? height
: width
;
543 switch ( t
->GetStyle() )
545 case wxTOOL_STYLE_CONTROL
:
547 wxPoint location
= t
->GetControl()->GetPosition();
550 location
.y
-= offset
;
552 location
.x
-= offset
;
554 t
->GetControl()->Move( location
);
557 case wxTOOL_STYLE_SEPARATOR
:
559 case wxTOOL_STYLE_BUTTON
:
562 XtVaGetValues( t
->GetButtonWidget(),
568 y
= (Dimension
)(y
- offset
);
570 x
= (Dimension
)(x
- offset
);
572 XtVaSetValues( t
->GetButtonWidget(),
585 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
587 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
588 if (tool
->GetButtonWidget()){
589 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
590 } else if (wxTOOL_STYLE_CONTROL
== tool
->GetStyle()){
591 // Controls (such as wxChoice) do not have button widgets
592 tool
->GetControl()->Enable(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?
759 y
= (Position
)(y
- yOffset
);
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
);