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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "toolbar.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
28 #define XtDisplay XTDISPLAY
31 #include "wx/settings.h"
34 #include "wx/toolbar.h"
38 #pragma message disable nosimpint
41 #include <Xm/PushBG.h>
44 #include <Xm/ToggleB.h>
45 #include <Xm/ToggleBG.h>
48 #pragma message enable nosimpint
51 #include "wx/motif/private.h"
52 #include "wx/motif/bmpmotif.h"
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 static void wxToolButtonCallback (Widget w
, XtPointer clientData
,
66 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
67 XEvent
*event
, Boolean
*continue_to_dispatch
);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 class wxToolBarTimer
: public wxTimer
76 virtual void Notify();
78 static Widget help_popup
;
79 static Widget buttonWidget
;
80 static wxString helpString
;
83 class wxToolBarTool
: public wxToolBarToolBase
86 wxToolBarTool(wxToolBar
*tbar
,
88 const wxString
& label
,
89 const wxBitmap
& bmpNormal
,
90 const wxBitmap
& bmpToggled
,
93 const wxString
& shortHelp
,
94 const wxString
& longHelp
)
95 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpToggled
, kind
,
96 clientData
, shortHelp
, longHelp
)
101 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
102 : wxToolBarToolBase(tbar
, control
)
107 virtual ~wxToolBarTool();
110 void SetWidget(Widget widget
) { m_widget
= widget
; }
111 Widget
GetButtonWidget() const { return m_widget
; }
113 Pixmap
GetArmPixmap()
115 m_bitmapCache
.SetBitmap( GetNormalBitmap() );
116 return (Pixmap
)m_bitmapCache
.GetArmPixmap( (WXWidget
)m_widget
);
119 Pixmap
GetInsensPixmap()
121 m_bitmapCache
.SetBitmap( GetNormalBitmap() );
122 return (Pixmap
)m_bitmapCache
.GetInsensPixmap( (WXWidget
)m_widget
);
128 wxBitmapCache m_bitmapCache
;
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 static wxToolBarTimer
* wxTheToolBarTimer
= (wxToolBarTimer
*) NULL
;
137 Widget
wxToolBarTimer::help_popup
= (Widget
) 0;
138 Widget
wxToolBarTimer::buttonWidget
= (Widget
) 0;
139 wxString
wxToolBarTimer::helpString
;
141 // ============================================================================
143 // ============================================================================
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
150 const wxString
& label
,
151 const wxBitmap
& bmpNormal
,
152 const wxBitmap
& bmpToggled
,
154 wxObject
*clientData
,
155 const wxString
& shortHelp
,
156 const wxString
& longHelp
)
158 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpToggled
, kind
,
159 clientData
, shortHelp
, longHelp
);
163 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
165 return new wxToolBarTool(this, control
);
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
) )
208 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
210 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
212 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
213 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
216 XmNresizePolicy
, XmRESIZE_NONE
,
219 Widget toolbar = XtVaCreateManagedWidget("toolbar",
220 xmFormWidgetClass, (Widget) m_clientWidget,
221 XmNtraversalOn, False,
222 XmNhorizontalSpacing, 0,
223 XmNverticalSpacing, 0,
231 m_mainWidget
= (WXWidget
) toolbar
;
238 if( rPos
.x
== -1 ) rPos
.x
= 0;
239 if( rPos
.y
== -1 ) rPos
.y
= 0;
240 if( rSize
.x
== -1 && GetParent() )
241 rSize
.x
= GetParent()->GetSize().x
;
243 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
244 rPos
.x
, rPos
.y
, rSize
.x
, rSize
.y
);
246 ChangeBackgroundColour();
251 wxToolBar::~wxToolBar()
253 delete wxTheToolBarTimer
;
254 wxTheToolBarTimer
= NULL
;
257 bool wxToolBar::Realize()
259 if ( m_tools
.GetCount() == 0 )
265 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
268 const int separatorSize
= GetToolSeparation(); // 8;
269 wxSize margins
= GetToolMargins();
270 int packing
= GetToolPacking();
271 int marginX
= margins
.x
;
272 int marginY
= margins
.y
;
274 int currentX
= marginX
;
275 int currentY
= marginY
;
277 int buttonHeight
= 0, buttonWidth
= 0;
279 int currentSpacing
= 0;
282 Pixmap pixmap
, insensPixmap
;
283 wxBitmap bmp
, insensBmp
;
285 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
288 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
290 switch ( tool
->GetStyle() )
292 case wxTOOL_STYLE_CONTROL
:
294 wxControl
* control
= tool
->GetControl();
295 wxSize sz
= control
->GetSize();
296 wxPoint pos
= control
->GetPosition();
297 // Allow a control to specify a y[x]-offset by setting
298 // its initial position, but still don't allow it to
299 // position itself above the top[left] margin.
300 int controlY
= (pos
.y
> 0) ? pos
.y
: currentY
;
301 int controlX
= (pos
.x
> 0) ? pos
.x
: currentX
;
302 control
->Move( isVertical
? controlX
: currentX
,
303 isVertical
? currentY
: controlY
);
305 currentY
+= sz
.y
+ packing
;
307 currentX
+= sz
.x
+ packing
;
311 case wxTOOL_STYLE_SEPARATOR
:
312 // skip separators for vertical toolbars
315 currentX
+= separatorSize
;
319 case wxTOOL_STYLE_BUTTON
:
322 if ( tool
->CanBeToggled() && !tool
->GetButtonWidget() )
324 button
= XtVaCreateWidget("toggleButton",
325 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
326 XmNx
, currentX
, XmNy
, currentY
,
327 XmNindicatorOn
, False
,
328 XmNshadowThickness
, 2,
333 XmNmultiClick
, XmMULTICLICK_KEEP
,
334 XmNlabelType
, XmPIXMAP
,
336 XtAddCallback ((Widget
) button
,
337 XmNvalueChangedCallback
,
338 (XtCallbackProc
) wxToolButtonCallback
,
341 XtVaSetValues ((Widget
) button
,
343 m_backgroundColour
.AllocColour
344 (XtDisplay((Widget
) button
)),
347 else if( !tool
->GetButtonWidget() )
349 button
= XtVaCreateWidget("button",
350 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
351 XmNx
, currentX
, XmNy
, currentY
,
352 XmNpushButtonEnabled
, True
,
353 XmNmultiClick
, XmMULTICLICK_KEEP
,
354 XmNlabelType
, XmPIXMAP
,
356 XtAddCallback (button
,
358 (XtCallbackProc
) wxToolButtonCallback
,
362 if( !tool
->GetButtonWidget() )
364 wxDoChangeBackgroundColour((WXWidget
) button
,
365 m_backgroundColour
, true);
367 tool
->SetWidget(button
);
371 button
= (Widget
)tool
->GetButtonWidget();
372 XtVaSetValues( button
,
373 XmNx
, currentX
, XmNy
, currentY
,
377 // For each button, if there is a mask, we must create
378 // a new wxBitmap that has the correct background colour
379 // for the button. Otherwise the background will just be
380 // e.g. black if a transparent XPM has been loaded.
381 bmp
= tool
->GetNormalBitmap();
382 insensBmp
= tool
->GetDisabledBitmap();
383 if ( bmp
.GetMask() || insensBmp
.GetMask() )
386 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
390 col
.SetPixel(backgroundPixel
);
392 if( bmp
.Ok() && bmp
.GetMask() )
394 bmp
= wxCreateMaskedBitmap(bmp
, col
);
395 tool
->SetNormalBitmap(bmp
);
398 if( insensBmp
.Ok() && insensBmp
.GetMask() )
400 insensBmp
= wxCreateMaskedBitmap(insensBmp
, col
);
401 tool
->SetDisabledBitmap(insensBmp
);
405 // Create a selected/toggled bitmap. If there isn't a 2nd
406 // bitmap, we need to create it (with a darker, selected
409 if ( tool
->CanBeToggled() )
410 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
413 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
416 col
.SetPixel(backgroundPixel
);
418 pixmap
= (Pixmap
) bmp
.GetDrawable();
420 wxBitmap tmp
= tool
->GetDisabledBitmap();
422 insensPixmap
= tmp
.Ok() ?
423 (Pixmap
)tmp
.GetDrawable() :
424 tool
->GetInsensPixmap();
427 if (tool
->CanBeToggled())
430 Pixmap pixmap2
= tool
->GetArmPixmap();
431 Pixmap insensPixmap2
= tool
->GetInsensPixmap();
433 XtVaSetValues (button
,
434 XmNfillOnSelect
, True
,
435 XmNlabelPixmap
, pixmap
,
436 XmNselectPixmap
, pixmap2
,
437 XmNlabelInsensitivePixmap
, insensPixmap
,
438 XmNselectInsensitivePixmap
, insensPixmap2
,
439 XmNlabelType
, XmPIXMAP
,
444 Pixmap pixmap2
= tool
->GetArmPixmap();
447 XtVaSetValues(button
,
448 XmNlabelPixmap
, pixmap
,
449 XmNlabelInsensitivePixmap
, insensPixmap
,
450 XmNarmPixmap
, pixmap2
,
454 XtManageChild(button
);
457 Dimension width
, height
;
458 XtVaGetValues(button
,
463 currentY
+= height
+ packing
;
465 currentX
+= width
+ packing
;
466 buttonHeight
= wxMax(buttonHeight
, height
);
467 buttonWidth
= wxMax(buttonWidth
, width
);
470 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
471 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
477 node
= node
->GetNext();
481 isVertical
? buttonWidth
+ 2 * marginX
: -1,
482 isVertical
? -1 : buttonHeight
+ 2*marginY
);
487 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
488 wxCoord
WXUNUSED(y
)) const
490 wxFAIL_MSG( _T("TODO") );
492 return (wxToolBarToolBase
*)NULL
;
495 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
502 bool wxToolBar::DoDeleteTool(size_t pos
, wxToolBarToolBase
*tool
)
506 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
507 const int separatorSize
= GetToolSeparation(); // 8;
508 int packing
= GetToolPacking();
511 for( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
512 node
; node
= node
->GetNext() )
514 wxToolBarTool
*t
= (wxToolBarTool
*)node
->GetData();
518 switch ( t
->GetStyle() )
520 case wxTOOL_STYLE_CONTROL
:
522 wxSize size
= t
->GetControl()->GetSize();
523 offset
= isVertical
? size
.y
: size
.x
;
527 case wxTOOL_STYLE_SEPARATOR
:
528 offset
= isVertical
? 0 : separatorSize
;
530 case wxTOOL_STYLE_BUTTON
:
532 Widget w
= t
->GetButtonWidget();
533 Dimension width
, height
;
540 offset
= isVertical
? height
: width
;
548 switch ( t
->GetStyle() )
550 case wxTOOL_STYLE_CONTROL
:
552 wxPoint pos
= t
->GetControl()->GetPosition();
559 t
->GetControl()->Move( pos
);
562 case wxTOOL_STYLE_SEPARATOR
:
564 case wxTOOL_STYLE_BUTTON
:
567 XtVaGetValues( t
->GetButtonWidget(),
577 XtVaSetValues( t
->GetButtonWidget(),
590 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
592 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
593 if (tool
->GetButtonWidget()){
594 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
595 } else if (wxTOOL_STYLE_CONTROL
== tool
->GetStyle()){
596 // Controls (such as wxChoice) do not have button widgets
597 tool
->GetControl()->Enable(enable
);
601 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
603 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
605 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
608 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
609 bool WXUNUSED(toggle
))
614 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
616 int old_width
, old_height
;
617 GetSize(&old_width
, &old_height
);
619 // Correct width and height if needed.
620 if ( width
== -1 || height
== -1 )
622 wxSize defaultSize
= GetSize();
625 width
= defaultSize
.x
;
627 height
= defaultSize
.y
;
630 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
632 // We must refresh the frame size when the toolbar changes size
633 // otherwise the toolbar can be shown incorrectly
634 if ( old_width
!= width
|| old_height
!= height
)
636 // But before we send the size event check it
637 // we have a frame that is not being deleted.
638 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
639 if ( frame
&& !frame
->IsBeingDeleted() )
641 frame
->SendSizeEvent();
646 // ----------------------------------------------------------------------------
648 // ----------------------------------------------------------------------------
650 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
652 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
655 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
656 if ( tool
->GetButtonWidget() == w
)
661 node
= node
->GetNext();
664 return (wxToolBarToolBase
*)NULL
;
667 static void wxToolButtonCallback(Widget w
,
668 XtPointer clientData
,
669 XtPointer
WXUNUSED(ptr
))
671 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
672 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
676 if ( tool
->CanBeToggled() )
679 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
687 static void wxToolButtonPopupCallback(Widget w
,
688 XtPointer client_data
,
690 Boolean
*WXUNUSED(continue_to_dispatch
))
692 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
693 static const int delayMilli
= 800;
695 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
696 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
701 wxString tooltip
= tool
->GetShortHelp();
705 if (!wxTheToolBarTimer
)
706 wxTheToolBarTimer
= new wxToolBarTimer
;
708 wxToolBarTimer::buttonWidget
= w
;
709 wxToolBarTimer::helpString
= tooltip
;
711 /************************************************************/
712 /* Popup help label */
713 /************************************************************/
714 if (event
->type
== EnterNotify
)
716 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
718 XtDestroyWidget (wxToolBarTimer::help_popup
);
719 XtPopdown (wxToolBarTimer::help_popup
);
721 wxToolBarTimer::help_popup
= (Widget
) 0;
724 wxTheToolBarTimer
->Start(delayMilli
, true);
727 /************************************************************/
728 /* Popdown help label */
729 /************************************************************/
730 else if (event
->type
== LeaveNotify
)
732 if (wxTheToolBarTimer
)
733 wxTheToolBarTimer
->Stop();
734 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
736 XtDestroyWidget (wxToolBarTimer::help_popup
);
737 XtPopdown (wxToolBarTimer::help_popup
);
739 wxToolBarTimer::help_popup
= (Widget
) 0;
743 void wxToolBarTimer::Notify()
747 /************************************************************/
748 /* Create shell without window decorations */
749 /************************************************************/
750 help_popup
= XtVaCreatePopupShell ("shell",
751 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
754 /************************************************************/
755 /* Get absolute position on display of toolbar button */
756 /************************************************************/
757 XtTranslateCoords (buttonWidget
,
762 // Move the tooltip more or less above the button
763 int yOffset
= 20; // TODO: What should be really?
765 if (y
< yOffset
) y
= 0;
767 /************************************************************/
768 /* Set the position of the help popup */
769 /************************************************************/
770 XtVaSetValues (help_popup
,
775 /************************************************************/
776 /* Create help label */
777 /************************************************************/
778 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
779 XtVaCreateManagedWidget ("help_label",
780 xmLabelWidgetClass
, help_popup
,
781 XmNlabelString
, text
,
783 XmNforeground
, XtRString
, "black",
786 XmNbackground
, XtRString
, "LightGoldenrod",
787 strlen("LightGoldenrod")+1,
791 /************************************************************/
792 /* Popup help label */
793 /************************************************************/
794 XtPopup (help_popup
, XtGrabNone
);