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 // ----------------------------------------------------------------------------
21 #pragma implementation "toolbar.h"
25 #define XtDisplay XTDISPLAY
31 #include "wx/toolbar.h"
34 #pragma message disable nosimpint
37 #include <Xm/PushBG.h>
40 #include <Xm/ToggleB.h>
41 #include <Xm/ToggleBG.h>
44 #pragma message enable nosimpint
47 #include "wx/motif/private.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 static void wxToolButtonCallback (Widget w
, XtPointer clientData
,
63 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
64 XEvent
*event
, Boolean
*continue_to_dispatch
);
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 class wxToolBarTimer
: public wxTimer
73 virtual void Notify();
75 static Widget help_popup
;
76 static Widget buttonWidget
;
77 static wxString helpString
;
80 class wxToolBarTool
: public wxToolBarToolBase
83 wxToolBarTool(wxToolBar
*tbar
,
85 const wxString
& label
,
86 const wxBitmap
& bmpNormal
,
87 const wxBitmap
& bmpToggled
,
90 const wxString
& shortHelp
,
91 const wxString
& longHelp
)
92 : wxToolBarToolBase(tbar
, id
, label
, bmpNormal
, bmpToggled
, kind
,
93 clientData
, shortHelp
, longHelp
)
98 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
99 : wxToolBarToolBase(tbar
, control
)
104 virtual ~wxToolBarTool();
107 void SetWidget(Widget widget
) { m_widget
= widget
; }
108 Widget
GetButtonWidget() const { return m_widget
; }
110 void SetPixmap(Pixmap pixmap
) { m_pixmap
= pixmap
; }
111 Pixmap
GetPixmap() const { return m_pixmap
; }
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 static wxToolBarTimer
* wxTheToolBarTimer
= (wxToolBarTimer
*) NULL
;
126 Widget
wxToolBarTimer::help_popup
= (Widget
) 0;
127 Widget
wxToolBarTimer::buttonWidget
= (Widget
) 0;
128 wxString
wxToolBarTimer::helpString
;
130 // ============================================================================
132 // ============================================================================
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 wxToolBarToolBase
*wxToolBar::CreateTool(int id
,
139 const wxString
& label
,
140 const wxBitmap
& bmpNormal
,
141 const wxBitmap
& bmpToggled
,
143 wxObject
*clientData
,
144 const wxString
& shortHelp
,
145 const wxString
& longHelp
)
147 return new wxToolBarTool(this, id
, label
, bmpNormal
, bmpToggled
, kind
,
148 clientData
, shortHelp
, longHelp
);
152 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
154 return new wxToolBarTool(this, control
);
157 void wxToolBarTool::Init()
159 m_widget
= (Widget
)0;
160 m_pixmap
= (Pixmap
)0;
163 wxToolBarTool::~wxToolBarTool()
166 XtDestroyWidget(m_widget
);
168 XmDestroyPixmap(DefaultScreenOfDisplay((Display
*)wxGetDisplay()),
172 // ----------------------------------------------------------------------------
173 // wxToolBar construction
174 // ----------------------------------------------------------------------------
176 void wxToolBar::Init()
181 m_defaultHeight
= 22;
183 m_toolSeparation
= 8;
190 bool wxToolBar::Create(wxWindow
*parent
,
195 const wxString
& name
)
202 m_backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
203 m_foregroundColour
= parent
->GetForegroundColour();
204 m_windowStyle
= style
;
208 if (parent
) parent
->AddChild(this);
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
;
233 m_font
= parent
->GetFont();
236 SetCanAddEventHandler(TRUE
);
237 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
239 ChangeBackgroundColour();
244 wxToolBar::~wxToolBar()
246 delete wxTheToolBarTimer
;
247 wxTheToolBarTimer
= NULL
;
250 bool wxToolBar::Realize()
252 if ( m_tools
.GetCount() == 0 )
259 const int separatorSize
= GetToolSeparation(); // 8;
260 wxSize margins
= GetToolMargins();
261 int packing
= GetToolPacking();
262 int marginX
= margins
.x
;
263 int marginY
= margins
.y
;
265 int currentX
= marginX
;
266 int currentY
= marginY
;
268 int buttonHeight
= 0;
270 int currentSpacing
= 0;
273 Pixmap pixmap
, insensPixmap
;
276 wxToolBarToolsList::Node
*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-offset by setting its initial position,
289 // but still don't allow it to position itself above the top margin.
290 int controlY
= (pos
.y
> 0) ? currentY
+ pos
.y
: currentY
;
291 control
->Move(currentX
, controlY
);
292 currentX
+= sz
.x
+ packing
;
296 case wxTOOL_STYLE_SEPARATOR
:
297 currentX
+= separatorSize
;
300 case wxTOOL_STYLE_BUTTON
:
303 if ( tool
->CanBeToggled() )
305 button
= XtVaCreateWidget("toggleButton",
306 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
307 XmNx
, currentX
, XmNy
, currentY
,
308 XmNindicatorOn
, False
,
309 XmNshadowThickness
, 2,
314 XmNmultiClick
, XmMULTICLICK_KEEP
,
315 XmNlabelType
, XmPIXMAP
,
317 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
320 XtVaSetValues ((Widget
) button
,
321 XmNselectColor
, m_backgroundColour
.AllocColour(XtDisplay((Widget
) button
)),
326 button
= XtVaCreateWidget("button",
327 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
328 XmNx
, currentX
, XmNy
, currentY
,
329 XmNpushButtonEnabled
, True
,
330 XmNmultiClick
, XmMULTICLICK_KEEP
,
331 XmNlabelType
, XmPIXMAP
,
333 XtAddCallback (button
,
334 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
338 DoChangeBackgroundColour((WXWidget
) button
, m_backgroundColour
, TRUE
);
340 tool
->SetWidget(button
);
342 // For each button, if there is a mask, we must create
343 // a new wxBitmap that has the correct background colour
344 // for the button. Otherwise the background will just be
345 // e.g. black if a transparent XPM has been loaded.
346 bmp
= tool
->GetNormalBitmap();
350 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
354 col
.SetPixel(backgroundPixel
);
356 bmp
= wxCreateMaskedBitmap(bmp
, col
);
358 tool
->SetNormalBitmap(bmp
);
361 // Create a selected/toggled bitmap. If there isn't a 2nd
362 // bitmap, we need to create it (with a darker, selected
365 if ( tool
->CanBeToggled() )
366 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
369 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
373 col
.SetPixel(backgroundPixel
);
375 // FIXME: we use disabled bitmap as the bitmap for the toggled
376 // state, we probably need a GetToggledBitmap() instead
377 wxBitmap bmpToggled
= tool
->GetDisabledBitmap();
378 if ( bmpToggled
.Ok() && bmpToggled
.GetMask())
381 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmpToggled
, col
);
382 tool
->SetDisabledBitmap(newBitmap
);
386 // Use unselected bitmap
389 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
390 tool
->SetDisabledBitmap(newBitmap
);
393 tool
->SetDisabledBitmap(bmp
);
396 // FIXME: and here we should use GetDisabledBitmap()...
397 pixmap
= (Pixmap
) bmp
.GetPixmap();
398 insensPixmap
= (Pixmap
) bmp
.GetInsensPixmap();
400 if (tool
->CanBeToggled())
403 Pixmap pixmap2
= (Pixmap
) 0;
404 Pixmap insensPixmap2
= (Pixmap
) 0;
406 // If there's a bitmap for the toggled state, use it,
407 // otherwise generate one.
410 if ( bmpToggled
.Ok() )
412 pixmap2
= (Pixmap
) bmpToggled
.GetPixmap();
413 insensPixmap2
= (Pixmap
) bmpToggled
.GetInsensPixmap();
417 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
418 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
421 tool
->SetPixmap(pixmap2
);
423 XtVaSetValues (button
,
424 XmNfillOnSelect
, True
,
425 XmNlabelPixmap
, pixmap
,
426 XmNselectPixmap
, pixmap2
,
427 XmNlabelInsensitivePixmap
, insensPixmap
,
428 XmNselectInsensitivePixmap
, insensPixmap2
,
429 XmNlabelType
, XmPIXMAP
,
434 Pixmap pixmap2
= (Pixmap
) 0;
436 // If there's a bitmap for the armed state, use it,
437 // otherwise generate one.
438 if ( bmpToggled
.Ok() )
440 pixmap2
= (Pixmap
) bmpToggled
.GetPixmap();
444 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
448 tool
->SetPixmap(pixmap2
);
451 XtVaSetValues(button
,
452 XmNlabelPixmap
, pixmap
,
453 XmNlabelInsensitivePixmap
, insensPixmap
,
454 XmNarmPixmap
, pixmap2
,
458 XtManageChild(button
);
461 Dimension width
, height
;
462 XtVaGetValues(button
,
466 currentX
+= width
+ packing
;
467 buttonHeight
= wxMax(buttonHeight
, height
);
470 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
471 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
477 node
= node
->GetNext();
480 SetSize(-1, -1, currentX
, 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
)
507 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
509 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
511 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
514 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
516 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
518 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
521 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
522 bool WXUNUSED(toggle
))
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
531 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
533 wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
536 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
537 if ( tool
->GetButtonWidget() == w
)
542 node
= node
->GetNext();
545 return (wxToolBarToolBase
*)NULL
;
548 static void wxToolButtonCallback(Widget w
,
549 XtPointer clientData
,
550 XtPointer
WXUNUSED(ptr
))
552 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
553 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
557 if ( tool
->CanBeToggled() )
560 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
568 static void wxToolButtonPopupCallback(Widget w
,
569 XtPointer client_data
,
571 Boolean
*WXUNUSED(continue_to_dispatch
))
573 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
574 static const int delayMilli
= 800;
576 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
577 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
582 wxString tooltip
= tool
->GetShortHelp();
586 if (!wxTheToolBarTimer
)
587 wxTheToolBarTimer
= new wxToolBarTimer
;
589 wxToolBarTimer::buttonWidget
= w
;
590 wxToolBarTimer::helpString
= tooltip
;
592 /************************************************************/
593 /* Popup help label */
594 /************************************************************/
595 if (event
->type
== EnterNotify
)
597 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
599 XtDestroyWidget (wxToolBarTimer::help_popup
);
600 XtPopdown (wxToolBarTimer::help_popup
);
602 wxToolBarTimer::help_popup
= (Widget
) 0;
605 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
608 /************************************************************/
609 /* Popdown help label */
610 /************************************************************/
611 else if (event
->type
== LeaveNotify
)
613 if (wxTheToolBarTimer
)
614 wxTheToolBarTimer
->Stop();
615 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
617 XtDestroyWidget (wxToolBarTimer::help_popup
);
618 XtPopdown (wxToolBarTimer::help_popup
);
620 wxToolBarTimer::help_popup
= (Widget
) 0;
624 void wxToolBarTimer::Notify()
628 /************************************************************/
629 /* Create shell without window decorations */
630 /************************************************************/
631 help_popup
= XtVaCreatePopupShell ("shell",
632 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
635 /************************************************************/
636 /* Get absolute position on display of toolbar button */
637 /************************************************************/
638 XtTranslateCoords (buttonWidget
,
643 // Move the tooltip more or less above the button
644 int yOffset
= 20; // TODO: What should be really?
646 if (y
< yOffset
) y
= 0;
648 /************************************************************/
649 /* Set the position of the help popup */
650 /************************************************************/
651 XtVaSetValues (help_popup
,
656 /************************************************************/
657 /* Create help label */
658 /************************************************************/
659 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
660 XtVaCreateManagedWidget ("help_label",
661 xmLabelWidgetClass
, help_popup
,
662 XmNlabelString
, text
,
664 XmNforeground
, XtRString
, "black",
667 XmNbackground
, XtRString
, "LightGoldenrod",
668 strlen("LightGoldenrod")+1,
672 /************************************************************/
673 /* Popup help label */
674 /************************************************************/
675 XtPopup (help_popup
, XtGrabNone
);