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"
23 #include "wx/toolbar.h"
29 #include "wx/settings.h"
33 #pragma message disable nosimpint
36 #include <Xm/PushBG.h>
39 #include <Xm/ToggleB.h>
40 #include <Xm/ToggleBG.h>
43 #pragma message enable nosimpint
46 #include "wx/motif/private.h"
47 #include "wx/motif/bmpmotif.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 static void wxToolButtonCallback (Widget w
, XtPointer clientData
,
61 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
62 XEvent
*event
, Boolean
*continue_to_dispatch
);
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 class wxToolBarTimer
: public wxTimer
71 virtual void Notify();
73 static Widget help_popup
;
74 static Widget buttonWidget
;
75 static wxString helpString
;
78 class wxToolBarTool
: public wxToolBarToolBase
81 wxToolBarTool(wxToolBar
*tbar
,
83 const wxString
& label
,
84 const wxBitmap
& bmpNormal
,
85 const wxBitmap
& bmpToggled
,
88 const wxString
& shortHelp
,
89 const wxString
& longHelp
)
90 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpToggled
, kind
,
91 clientData
, shortHelp
, longHelp
)
96 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
, const wxString
& label
)
97 : wxToolBarToolBase(tbar
, control
, label
)
102 virtual ~wxToolBarTool();
105 void SetWidget(Widget widget
) { m_widget
= widget
; }
106 Widget
GetButtonWidget() const { return m_widget
; }
108 Pixmap
GetArmPixmap()
110 m_bitmapCache
.SetBitmap( GetNormalBitmap() );
111 return (Pixmap
)m_bitmapCache
.GetArmPixmap( (WXWidget
)m_widget
);
114 Pixmap
GetInsensPixmap()
116 m_bitmapCache
.SetBitmap( GetNormalBitmap() );
117 return (Pixmap
)m_bitmapCache
.GetInsensPixmap( (WXWidget
)m_widget
);
123 wxBitmapCache m_bitmapCache
;
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 static wxToolBarTimer
* wxTheToolBarTimer
= NULL
;
132 Widget
wxToolBarTimer::help_popup
= (Widget
) 0;
133 Widget
wxToolBarTimer::buttonWidget
= (Widget
) 0;
134 wxString
wxToolBarTimer::helpString
;
136 // ============================================================================
138 // ============================================================================
140 // ----------------------------------------------------------------------------
142 // ----------------------------------------------------------------------------
144 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
145 const wxString
& label
,
146 const wxBitmap
& bmpNormal
,
147 const wxBitmap
& bmpToggled
,
149 wxObject
*clientData
,
150 const wxString
& shortHelp
,
151 const wxString
& longHelp
)
153 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpToggled
, kind
,
154 clientData
, shortHelp
, longHelp
);
159 wxToolBar::CreateTool(wxControl
*control
, const wxString
& label
)
161 return new wxToolBarTool(this, control
, label
);
164 void wxToolBarTool::Init()
166 m_widget
= (Widget
)0;
169 wxToolBarTool::~wxToolBarTool()
172 XtDestroyWidget(m_widget
);
175 // ----------------------------------------------------------------------------
176 // wxToolBar construction
177 // ----------------------------------------------------------------------------
179 void wxToolBar::Init()
184 m_defaultHeight
= 22;
186 m_toolSeparation
= 8;
193 bool wxToolBar::Create(wxWindow
*parent
,
198 const wxString
& name
)
200 if( !wxControl::CreateControl( parent
, id
, pos
, size
, style
,
201 wxDefaultValidator
, name
) )
207 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
209 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
210 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
213 XmNresizePolicy
, XmRESIZE_NONE
,
216 Widget toolbar = XtVaCreateManagedWidget("toolbar",
217 xmFormWidgetClass, (Widget) m_clientWidget,
218 XmNtraversalOn, False,
219 XmNhorizontalSpacing, 0,
220 XmNverticalSpacing, 0,
228 m_mainWidget
= (WXWidget
) toolbar
;
233 if( rPos
.x
== -1 ) rPos
.x
= 0;
234 if( rPos
.y
== -1 ) rPos
.y
= 0;
235 if( rSize
.x
== -1 && GetParent() )
236 rSize
.x
= GetParent()->GetSize().x
;
239 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
240 rPos
.x
, rPos
.y
, rSize
.x
, rSize
.y
);
245 wxToolBar::~wxToolBar()
247 wxDELETE(wxTheToolBarTimer
);
250 bool wxToolBar::Realize()
252 if ( m_tools
.GetCount() == 0 )
258 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
261 const int separatorSize
= GetToolSeparation(); // 8;
262 wxSize margins
= GetToolMargins();
263 int packing
= GetToolPacking();
264 int marginX
= margins
.x
;
265 int marginY
= margins
.y
;
267 int currentX
= marginX
;
268 int currentY
= marginY
;
270 int buttonHeight
= 0, buttonWidth
= 0;
273 Pixmap pixmap
, insensPixmap
;
274 wxBitmap bmp
, insensBmp
;
276 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
279 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
281 switch ( tool
->GetStyle() )
283 case wxTOOL_STYLE_CONTROL
:
285 wxControl
* control
= tool
->GetControl();
286 wxSize sz
= control
->GetSize();
287 wxPoint pos
= control
->GetPosition();
288 // Allow a control to specify a y[x]-offset by setting
289 // its initial position, but still don't allow it to
290 // position itself above the top[left] margin.
291 int controlY
= (pos
.y
> 0) ? pos
.y
: currentY
;
292 int controlX
= (pos
.x
> 0) ? pos
.x
: currentX
;
293 control
->Move( isVertical
? controlX
: currentX
,
294 isVertical
? currentY
: controlY
);
296 currentY
+= sz
.y
+ packing
;
298 currentX
+= sz
.x
+ packing
;
302 case wxTOOL_STYLE_SEPARATOR
:
303 // skip separators for vertical toolbars
306 currentX
+= separatorSize
;
310 case wxTOOL_STYLE_BUTTON
:
313 if ( tool
->CanBeToggled() && !tool
->GetButtonWidget() )
315 button
= XtVaCreateWidget("toggleButton",
316 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
317 XmNx
, currentX
, XmNy
, currentY
,
318 XmNindicatorOn
, False
,
319 XmNshadowThickness
, 2,
324 XmNmultiClick
, XmMULTICLICK_KEEP
,
325 XmNlabelType
, XmPIXMAP
,
327 XtAddCallback ((Widget
) button
,
328 XmNvalueChangedCallback
,
329 (XtCallbackProc
) wxToolButtonCallback
,
332 XtVaSetValues ((Widget
) button
,
334 m_backgroundColour
.AllocColour
335 (XtDisplay((Widget
) button
)),
338 else if( !tool
->GetButtonWidget() )
340 button
= XtVaCreateWidget("button",
341 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
342 XmNx
, currentX
, XmNy
, currentY
,
343 XmNpushButtonEnabled
, True
,
344 XmNmultiClick
, XmMULTICLICK_KEEP
,
345 XmNlabelType
, XmPIXMAP
,
347 XtAddCallback (button
,
349 (XtCallbackProc
) wxToolButtonCallback
,
353 if( !tool
->GetButtonWidget() )
355 wxDoChangeBackgroundColour((WXWidget
) button
,
356 m_backgroundColour
, true);
358 tool
->SetWidget(button
);
362 button
= (Widget
)tool
->GetButtonWidget();
363 XtVaSetValues( button
,
364 XmNx
, currentX
, XmNy
, currentY
,
368 // For each button, if there is a mask, we must create
369 // a new wxBitmap that has the correct background colour
370 // for the button. Otherwise the background will just be
371 // e.g. black if a transparent XPM has been loaded.
372 bmp
= tool
->GetNormalBitmap();
373 insensBmp
= tool
->GetDisabledBitmap();
374 if ( bmp
.GetMask() || insensBmp
.GetMask() )
376 WXPixel backgroundPixel
;
377 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
381 col
.SetPixel(backgroundPixel
);
383 if( bmp
.Ok() && bmp
.GetMask() )
385 bmp
= wxCreateMaskedBitmap(bmp
, col
);
386 tool
->SetNormalBitmap(bmp
);
389 if( insensBmp
.Ok() && insensBmp
.GetMask() )
391 insensBmp
= wxCreateMaskedBitmap(insensBmp
, col
);
392 tool
->SetDisabledBitmap(insensBmp
);
396 // Create a selected/toggled bitmap. If there isn't a 2nd
397 // bitmap, we need to create it (with a darker, selected
399 WXPixel backgroundPixel
;
400 if ( tool
->CanBeToggled() )
401 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
404 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
407 col
.SetPixel(backgroundPixel
);
409 pixmap
= (Pixmap
) bmp
.GetDrawable();
411 wxBitmap tmp
= tool
->GetDisabledBitmap();
413 insensPixmap
= tmp
.Ok() ?
414 (Pixmap
)tmp
.GetDrawable() :
415 tool
->GetInsensPixmap();
418 if (tool
->CanBeToggled())
421 Pixmap pixmap2
= tool
->GetArmPixmap();
422 Pixmap insensPixmap2
= tool
->GetInsensPixmap();
424 XtVaSetValues (button
,
425 XmNfillOnSelect
, True
,
426 XmNlabelPixmap
, pixmap
,
427 XmNselectPixmap
, pixmap2
,
428 XmNlabelInsensitivePixmap
, insensPixmap
,
429 XmNselectInsensitivePixmap
, insensPixmap2
,
430 XmNlabelType
, XmPIXMAP
,
435 Pixmap pixmap2
= tool
->GetArmPixmap();
438 XtVaSetValues(button
,
439 XmNlabelPixmap
, pixmap
,
440 XmNlabelInsensitivePixmap
, insensPixmap
,
441 XmNarmPixmap
, pixmap2
,
445 XtManageChild(button
);
448 Dimension width
, height
;
449 XtVaGetValues(button
,
454 currentY
+= height
+ packing
;
456 currentX
+= width
+ packing
;
457 buttonHeight
= wxMax(buttonHeight
, height
);
458 buttonWidth
= wxMax(buttonWidth
, width
);
461 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
462 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
467 node
= node
->GetNext();
471 isVertical
? buttonWidth
+ 2 * marginX
: -1,
472 isVertical
? -1 : buttonHeight
+ 2*marginY
);
477 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
478 wxCoord
WXUNUSED(y
)) const
480 wxFAIL_MSG( wxT("TODO") );
485 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
492 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
496 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
497 const int separatorSize
= GetToolSeparation(); // 8;
498 int packing
= GetToolPacking();
501 for( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
502 node
; node
= node
->GetNext() )
504 wxToolBarTool
*t
= (wxToolBarTool
*)node
->GetData();
508 switch ( t
->GetStyle() )
510 case wxTOOL_STYLE_CONTROL
:
512 wxSize size
= t
->GetControl()->GetSize();
513 offset
= isVertical
? size
.y
: size
.x
;
517 case wxTOOL_STYLE_SEPARATOR
:
518 offset
= isVertical
? 0 : separatorSize
;
520 case wxTOOL_STYLE_BUTTON
:
522 Widget w
= t
->GetButtonWidget();
523 Dimension width
, height
;
530 offset
= isVertical
? height
: width
;
538 switch ( t
->GetStyle() )
540 case wxTOOL_STYLE_CONTROL
:
542 wxPoint location
= t
->GetControl()->GetPosition();
545 location
.y
-= offset
;
547 location
.x
-= offset
;
549 t
->GetControl()->Move( location
);
552 case wxTOOL_STYLE_SEPARATOR
:
554 case wxTOOL_STYLE_BUTTON
:
557 XtVaGetValues( t
->GetButtonWidget(),
563 y
= (Dimension
)(y
- offset
);
565 x
= (Dimension
)(x
- offset
);
567 XtVaSetValues( t
->GetButtonWidget(),
580 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
582 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
583 if (tool
->GetButtonWidget()){
584 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
585 } else if (wxTOOL_STYLE_CONTROL
== tool
->GetStyle()){
586 // Controls (such as wxChoice) do not have button widgets
587 tool
->GetControl()->Enable(enable
);
591 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
593 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
595 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
598 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
599 bool WXUNUSED(toggle
))
604 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
606 int old_width
, old_height
;
607 GetSize(&old_width
, &old_height
);
609 // Correct width and height if needed.
610 if ( width
== -1 || height
== -1 )
612 wxSize defaultSize
= GetSize();
615 width
= defaultSize
.x
;
617 height
= defaultSize
.y
;
620 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
622 // We must refresh the frame size when the toolbar changes size
623 // otherwise the toolbar can be shown incorrectly
624 if ( old_width
!= width
|| old_height
!= height
)
626 // But before we send the size event check it
627 // we have a frame that is not being deleted.
628 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
629 if ( frame
&& !frame
->IsBeingDeleted() )
631 frame
->SendSizeEvent();
636 // ----------------------------------------------------------------------------
638 // ----------------------------------------------------------------------------
640 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
642 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
645 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
646 if ( tool
->GetButtonWidget() == w
)
651 node
= node
->GetNext();
657 static void wxToolButtonCallback(Widget w
,
658 XtPointer clientData
,
659 XtPointer
WXUNUSED(ptr
))
661 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
662 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
666 if ( tool
->CanBeToggled() )
669 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
677 static void wxToolButtonPopupCallback(Widget w
,
678 XtPointer client_data
,
680 Boolean
*WXUNUSED(continue_to_dispatch
))
682 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
683 static const int delayMilli
= 800;
685 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
686 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
691 wxString tooltip
= tool
->GetShortHelp();
695 if (!wxTheToolBarTimer
)
696 wxTheToolBarTimer
= new wxToolBarTimer
;
698 wxToolBarTimer::buttonWidget
= w
;
699 wxToolBarTimer::helpString
= tooltip
;
701 /************************************************************/
702 /* Popup help label */
703 /************************************************************/
704 if (event
->type
== EnterNotify
)
706 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
708 XtDestroyWidget (wxToolBarTimer::help_popup
);
709 XtPopdown (wxToolBarTimer::help_popup
);
711 wxToolBarTimer::help_popup
= (Widget
) 0;
714 wxTheToolBarTimer
->Start(delayMilli
, true);
717 /************************************************************/
718 /* Popdown help label */
719 /************************************************************/
720 else if (event
->type
== LeaveNotify
)
722 if (wxTheToolBarTimer
)
723 wxTheToolBarTimer
->Stop();
724 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
726 XtDestroyWidget (wxToolBarTimer::help_popup
);
727 XtPopdown (wxToolBarTimer::help_popup
);
729 wxToolBarTimer::help_popup
= (Widget
) 0;
733 void wxToolBarTimer::Notify()
737 /************************************************************/
738 /* Create shell without window decorations */
739 /************************************************************/
740 help_popup
= XtVaCreatePopupShell ("shell",
741 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
744 /************************************************************/
745 /* Get absolute position on display of toolbar button */
746 /************************************************************/
747 XtTranslateCoords (buttonWidget
,
752 // Move the tooltip more or less above the button
753 int yOffset
= 20; // TODO: What should be really?
754 y
= (Position
)(y
- yOffset
);
755 if (y
< yOffset
) y
= 0;
757 /************************************************************/
758 /* Set the position of the help popup */
759 /************************************************************/
760 XtVaSetValues (help_popup
,
765 /************************************************************/
766 /* Create help label */
767 /************************************************************/
768 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
769 XtVaCreateManagedWidget ("help_label",
770 xmLabelWidgetClass
, help_popup
,
771 XmNlabelString
, text
,
773 XmNforeground
, XtRString
, "black",
776 XmNbackground
, XtRString
, "LightGoldenrod",
777 strlen("LightGoldenrod")+1,
781 /************************************************************/
782 /* Popup help label */
783 /************************************************************/
784 XtPopup (help_popup
, XtGrabNone
);