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 delete wxTheToolBarTimer
;
248 wxTheToolBarTimer
= NULL
;
251 bool wxToolBar::Realize()
253 if ( m_tools
.GetCount() == 0 )
259 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
262 const int separatorSize
= GetToolSeparation(); // 8;
263 wxSize margins
= GetToolMargins();
264 int packing
= GetToolPacking();
265 int marginX
= margins
.x
;
266 int marginY
= margins
.y
;
268 int currentX
= marginX
;
269 int currentY
= marginY
;
271 int buttonHeight
= 0, buttonWidth
= 0;
274 Pixmap pixmap
, insensPixmap
;
275 wxBitmap bmp
, insensBmp
;
277 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
280 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
282 switch ( tool
->GetStyle() )
284 case wxTOOL_STYLE_CONTROL
:
286 wxControl
* control
= tool
->GetControl();
287 wxSize sz
= control
->GetSize();
288 wxPoint pos
= control
->GetPosition();
289 // Allow a control to specify a y[x]-offset by setting
290 // its initial position, but still don't allow it to
291 // position itself above the top[left] margin.
292 int controlY
= (pos
.y
> 0) ? pos
.y
: currentY
;
293 int controlX
= (pos
.x
> 0) ? pos
.x
: currentX
;
294 control
->Move( isVertical
? controlX
: currentX
,
295 isVertical
? currentY
: controlY
);
297 currentY
+= sz
.y
+ packing
;
299 currentX
+= sz
.x
+ packing
;
303 case wxTOOL_STYLE_SEPARATOR
:
304 // skip separators for vertical toolbars
307 currentX
+= separatorSize
;
311 case wxTOOL_STYLE_BUTTON
:
314 if ( tool
->CanBeToggled() && !tool
->GetButtonWidget() )
316 button
= XtVaCreateWidget("toggleButton",
317 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
318 XmNx
, currentX
, XmNy
, currentY
,
319 XmNindicatorOn
, False
,
320 XmNshadowThickness
, 2,
325 XmNmultiClick
, XmMULTICLICK_KEEP
,
326 XmNlabelType
, XmPIXMAP
,
328 XtAddCallback ((Widget
) button
,
329 XmNvalueChangedCallback
,
330 (XtCallbackProc
) wxToolButtonCallback
,
333 XtVaSetValues ((Widget
) button
,
335 m_backgroundColour
.AllocColour
336 (XtDisplay((Widget
) button
)),
339 else if( !tool
->GetButtonWidget() )
341 button
= XtVaCreateWidget("button",
342 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
343 XmNx
, currentX
, XmNy
, currentY
,
344 XmNpushButtonEnabled
, True
,
345 XmNmultiClick
, XmMULTICLICK_KEEP
,
346 XmNlabelType
, XmPIXMAP
,
348 XtAddCallback (button
,
350 (XtCallbackProc
) wxToolButtonCallback
,
354 if( !tool
->GetButtonWidget() )
356 wxDoChangeBackgroundColour((WXWidget
) button
,
357 m_backgroundColour
, true);
359 tool
->SetWidget(button
);
363 button
= (Widget
)tool
->GetButtonWidget();
364 XtVaSetValues( button
,
365 XmNx
, currentX
, XmNy
, currentY
,
369 // For each button, if there is a mask, we must create
370 // a new wxBitmap that has the correct background colour
371 // for the button. Otherwise the background will just be
372 // e.g. black if a transparent XPM has been loaded.
373 bmp
= tool
->GetNormalBitmap();
374 insensBmp
= tool
->GetDisabledBitmap();
375 if ( bmp
.GetMask() || insensBmp
.GetMask() )
377 WXPixel backgroundPixel
;
378 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
382 col
.SetPixel(backgroundPixel
);
384 if( bmp
.Ok() && bmp
.GetMask() )
386 bmp
= wxCreateMaskedBitmap(bmp
, col
);
387 tool
->SetNormalBitmap(bmp
);
390 if( insensBmp
.Ok() && insensBmp
.GetMask() )
392 insensBmp
= wxCreateMaskedBitmap(insensBmp
, col
);
393 tool
->SetDisabledBitmap(insensBmp
);
397 // Create a selected/toggled bitmap. If there isn't a 2nd
398 // bitmap, we need to create it (with a darker, selected
400 WXPixel backgroundPixel
;
401 if ( tool
->CanBeToggled() )
402 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
405 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
408 col
.SetPixel(backgroundPixel
);
410 pixmap
= (Pixmap
) bmp
.GetDrawable();
412 wxBitmap tmp
= tool
->GetDisabledBitmap();
414 insensPixmap
= tmp
.Ok() ?
415 (Pixmap
)tmp
.GetDrawable() :
416 tool
->GetInsensPixmap();
419 if (tool
->CanBeToggled())
422 Pixmap pixmap2
= tool
->GetArmPixmap();
423 Pixmap insensPixmap2
= tool
->GetInsensPixmap();
425 XtVaSetValues (button
,
426 XmNfillOnSelect
, True
,
427 XmNlabelPixmap
, pixmap
,
428 XmNselectPixmap
, pixmap2
,
429 XmNlabelInsensitivePixmap
, insensPixmap
,
430 XmNselectInsensitivePixmap
, insensPixmap2
,
431 XmNlabelType
, XmPIXMAP
,
436 Pixmap pixmap2
= tool
->GetArmPixmap();
439 XtVaSetValues(button
,
440 XmNlabelPixmap
, pixmap
,
441 XmNlabelInsensitivePixmap
, insensPixmap
,
442 XmNarmPixmap
, pixmap2
,
446 XtManageChild(button
);
449 Dimension width
, height
;
450 XtVaGetValues(button
,
455 currentY
+= height
+ packing
;
457 currentX
+= width
+ packing
;
458 buttonHeight
= wxMax(buttonHeight
, height
);
459 buttonWidth
= wxMax(buttonWidth
, width
);
462 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
463 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
468 node
= node
->GetNext();
472 isVertical
? buttonWidth
+ 2 * marginX
: -1,
473 isVertical
? -1 : buttonHeight
+ 2*marginY
);
478 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
479 wxCoord
WXUNUSED(y
)) const
481 wxFAIL_MSG( wxT("TODO") );
486 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
493 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
497 bool isVertical
= GetWindowStyle() & wxTB_VERTICAL
;
498 const int separatorSize
= GetToolSeparation(); // 8;
499 int packing
= GetToolPacking();
502 for( wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
503 node
; node
= node
->GetNext() )
505 wxToolBarTool
*t
= (wxToolBarTool
*)node
->GetData();
509 switch ( t
->GetStyle() )
511 case wxTOOL_STYLE_CONTROL
:
513 wxSize size
= t
->GetControl()->GetSize();
514 offset
= isVertical
? size
.y
: size
.x
;
518 case wxTOOL_STYLE_SEPARATOR
:
519 offset
= isVertical
? 0 : separatorSize
;
521 case wxTOOL_STYLE_BUTTON
:
523 Widget w
= t
->GetButtonWidget();
524 Dimension width
, height
;
531 offset
= isVertical
? height
: width
;
539 switch ( t
->GetStyle() )
541 case wxTOOL_STYLE_CONTROL
:
543 wxPoint location
= t
->GetControl()->GetPosition();
546 location
.y
-= offset
;
548 location
.x
-= offset
;
550 t
->GetControl()->Move( location
);
553 case wxTOOL_STYLE_SEPARATOR
:
555 case wxTOOL_STYLE_BUTTON
:
558 XtVaGetValues( t
->GetButtonWidget(),
564 y
= (Dimension
)(y
- offset
);
566 x
= (Dimension
)(x
- offset
);
568 XtVaSetValues( t
->GetButtonWidget(),
581 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
583 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
584 if (tool
->GetButtonWidget()){
585 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
586 } else if (wxTOOL_STYLE_CONTROL
== tool
->GetStyle()){
587 // Controls (such as wxChoice) do not have button widgets
588 tool
->GetControl()->Enable(enable
);
592 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
594 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
596 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
599 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
600 bool WXUNUSED(toggle
))
605 void wxToolBar::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
607 int old_width
, old_height
;
608 GetSize(&old_width
, &old_height
);
610 // Correct width and height if needed.
611 if ( width
== -1 || height
== -1 )
613 wxSize defaultSize
= GetSize();
616 width
= defaultSize
.x
;
618 height
= defaultSize
.y
;
621 wxToolBarBase::DoSetSize(x
, y
, width
, height
, sizeFlags
);
623 // We must refresh the frame size when the toolbar changes size
624 // otherwise the toolbar can be shown incorrectly
625 if ( old_width
!= width
|| old_height
!= height
)
627 // But before we send the size event check it
628 // we have a frame that is not being deleted.
629 wxFrame
*frame
= wxDynamicCast(GetParent(), wxFrame
);
630 if ( frame
&& !frame
->IsBeingDeleted() )
632 frame
->SendSizeEvent();
637 // ----------------------------------------------------------------------------
639 // ----------------------------------------------------------------------------
641 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
643 wxToolBarToolsList::compatibility_iterator node
= m_tools
.GetFirst();
646 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
647 if ( tool
->GetButtonWidget() == w
)
652 node
= node
->GetNext();
658 static void wxToolButtonCallback(Widget w
,
659 XtPointer clientData
,
660 XtPointer
WXUNUSED(ptr
))
662 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
663 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
667 if ( tool
->CanBeToggled() )
670 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
678 static void wxToolButtonPopupCallback(Widget w
,
679 XtPointer client_data
,
681 Boolean
*WXUNUSED(continue_to_dispatch
))
683 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
684 static const int delayMilli
= 800;
686 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
687 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
692 wxString tooltip
= tool
->GetShortHelp();
696 if (!wxTheToolBarTimer
)
697 wxTheToolBarTimer
= new wxToolBarTimer
;
699 wxToolBarTimer::buttonWidget
= w
;
700 wxToolBarTimer::helpString
= tooltip
;
702 /************************************************************/
703 /* Popup help label */
704 /************************************************************/
705 if (event
->type
== EnterNotify
)
707 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
709 XtDestroyWidget (wxToolBarTimer::help_popup
);
710 XtPopdown (wxToolBarTimer::help_popup
);
712 wxToolBarTimer::help_popup
= (Widget
) 0;
715 wxTheToolBarTimer
->Start(delayMilli
, true);
718 /************************************************************/
719 /* Popdown help label */
720 /************************************************************/
721 else if (event
->type
== LeaveNotify
)
723 if (wxTheToolBarTimer
)
724 wxTheToolBarTimer
->Stop();
725 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
727 XtDestroyWidget (wxToolBarTimer::help_popup
);
728 XtPopdown (wxToolBarTimer::help_popup
);
730 wxToolBarTimer::help_popup
= (Widget
) 0;
734 void wxToolBarTimer::Notify()
738 /************************************************************/
739 /* Create shell without window decorations */
740 /************************************************************/
741 help_popup
= XtVaCreatePopupShell ("shell",
742 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
745 /************************************************************/
746 /* Get absolute position on display of toolbar button */
747 /************************************************************/
748 XtTranslateCoords (buttonWidget
,
753 // Move the tooltip more or less above the button
754 int yOffset
= 20; // TODO: What should be really?
755 y
= (Position
)(y
- yOffset
);
756 if (y
< yOffset
) y
= 0;
758 /************************************************************/
759 /* Set the position of the help popup */
760 /************************************************************/
761 XtVaSetValues (help_popup
,
766 /************************************************************/
767 /* Create help label */
768 /************************************************************/
769 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
770 XtVaCreateManagedWidget ("help_label",
771 xmLabelWidgetClass
, help_popup
,
772 XmNlabelString
, text
,
774 XmNforeground
, XtRString
, "black",
777 XmNbackground
, XtRString
, "LightGoldenrod",
778 strlen("LightGoldenrod")+1,
782 /************************************************************/
783 /* Popup help label */
784 /************************************************************/
785 XtPopup (help_popup
, XtGrabNone
);