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"
27 #include "wx/toolbar.h"
30 #pragma message disable nosimpint
33 #include <Xm/PushBG.h>
36 #include <Xm/ToggleB.h>
37 #include <Xm/ToggleBG.h>
40 #pragma message enable nosimpint
43 #include "wx/motif/private.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxControl
)
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 static void wxToolButtonCallback (Widget w
, XtPointer clientData
,
59 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
60 XEvent
*event
, Boolean
*continue_to_dispatch
);
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class wxToolBarTimer
: public wxTimer
69 virtual void Notify();
71 static Widget help_popup
;
72 static Widget buttonWidget
;
73 static wxString helpString
;
76 class wxToolBarTool
: public wxToolBarToolBase
79 wxToolBarTool(wxToolBar
*tbar
,
81 const wxBitmap
& bitmap1
,
82 const wxBitmap
& bitmap2
,
85 const wxString
& shortHelpString
,
86 const wxString
& longHelpString
)
87 : wxToolBarToolBase(tbar
, id
, bitmap1
, bitmap2
, toggle
,
88 clientData
, shortHelpString
, longHelpString
)
93 wxToolBarTool(wxToolBar
*tbar
, wxControl
*control
)
94 : wxToolBarToolBase(tbar
, control
)
99 virtual ~wxToolBarTool();
102 void SetWidget(Widget widget
) { m_widget
= widget
; }
103 Widget
GetButtonWidget() const { return m_widget
; }
105 void SetPixmap(Pixmap pixmap
) { m_pixmap
= pixmap
; }
106 Pixmap
GetPixmap() const { return m_pixmap
; }
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 static wxToolBarTimer
* wxTheToolBarTimer
= (wxToolBarTimer
*) NULL
;
121 Widget
wxToolBarTimer::help_popup
= (Widget
) 0;
122 Widget
wxToolBarTimer::buttonWidget
= (Widget
) 0;
123 wxString
wxToolBarTimer::helpString
;
125 // ============================================================================
127 // ============================================================================
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
133 wxToolBarToolBase
*wxToolBarToolBase::New(wxToolBar
*tbar
,
135 const wxBitmap
& bitmap1
,
136 const wxBitmap
& bitmap2
,
138 wxObject
*clientData
,
139 const wxString
& shortHelpString
,
140 const wxString
& longHelpString
)
142 return new wxToolBarTool(tbar
, id
, bitmap1
, bitmap2
, toggle
,
143 clientData
, shortHelpString
, longHelpString
);
146 wxToolBarToolBase
*wxToolBarToolBase::New(wxToolBar
*tbar
, wxControl
*control
)
148 return new wxToolBarTool(tbar
, control
);
151 void wxToolBarTool::Init()
153 m_widget
= (Widget
)0;
154 m_pixmap
= (Pixmap
)0;
157 wxToolBarTool::~wxToolBarTool()
159 XtDestroyWidget(m_widget
);
160 XmDestroyPixmap(DefaultScreenOfDisplay((Display
*)wxGetDisplay()), m_pixmap
);
163 // ----------------------------------------------------------------------------
164 // wxToolBar construction
165 // ----------------------------------------------------------------------------
167 void wxToolBar::Init()
172 m_defaultHeight
= 22;
175 bool wxToolBar::Create(wxWindow
*parent
,
180 const wxString
& name
)
187 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
188 m_foregroundColour
= parent
->GetForegroundColour();
189 m_windowStyle
= style
;
193 if (parent
) parent
->AddChild(this);
195 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
197 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
198 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
201 XmNresizePolicy
, XmRESIZE_NONE
,
204 Widget toolbar = XtVaCreateManagedWidget("toolbar",
205 xmFormWidgetClass, (Widget) m_clientWidget,
206 XmNtraversalOn, False,
207 XmNhorizontalSpacing, 0,
208 XmNverticalSpacing, 0,
216 m_mainWidget
= (WXWidget
) toolbar
;
218 m_font
= parent
->GetFont();
221 SetCanAddEventHandler(TRUE
);
222 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
224 ChangeBackgroundColour();
229 wxToolBar::~wxToolBar()
231 delete wxTheToolBarTimer
;
232 wxTheToolBarTimer
= NULL
;
235 bool wxToolBar::Realize()
237 if ( m_tools
.GetCount() == 0 )
244 const int separatorSize
= GetToolSeparation(); // 8;
245 wxSize margins
= GetToolMargins();
246 int marginX
= margins
.x
;
247 int marginY
= margins
.y
;
249 int currentX
= marginX
;
250 int currentY
= marginY
;
252 int buttonHeight
= 0;
254 int currentSpacing
= 0;
257 Pixmap pixmap
, insensPixmap
;
260 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
263 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
265 switch ( tool
->GetStyle() )
267 case wxTOOL_STYLE_CONTROL
:
268 wxFAIL_MSG( _T("not implemented") );
271 case wxTOOL_STYLE_SEPARATOR
:
272 currentX
+= separatorSize
;
275 case wxTOOL_STYLE_BUTTON
:
278 if ( tool
->CanBeToggled() )
280 button
= XtVaCreateWidget("toggleButton",
281 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
282 XmNx
, currentX
, XmNy
, currentY
,
283 // XmNpushButtonEnabled, True,
284 XmNmultiClick
, XmMULTICLICK_KEEP
,
285 XmNlabelType
, XmPIXMAP
,
287 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
290 XtVaSetValues ((Widget
) button
,
291 XmNselectColor
, m_backgroundColour
.AllocColour(XtDisplay((Widget
) button
)),
296 button
= XtVaCreateWidget("button",
297 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
298 XmNx
, currentX
, XmNy
, currentY
,
299 XmNpushButtonEnabled
, True
,
300 XmNmultiClick
, XmMULTICLICK_KEEP
,
301 XmNlabelType
, XmPIXMAP
,
303 XtAddCallback (button
,
304 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
308 DoChangeBackgroundColour((WXWidget
) button
, m_backgroundColour
, TRUE
);
310 tool
->SetWidget(button
);
312 // For each button, if there is a mask, we must create
313 // a new wxBitmap that has the correct background colour
314 // for the button. Otherwise the background will just be
315 // e.g. black if a transparent XPM has been loaded.
316 bmp
= tool
->GetBitmap1();
320 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
324 col
.SetPixel(backgroundPixel
);
326 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
328 tool
->SetBitmap1(newBitmap
);
331 // Create a selected/toggled bitmap. If there isn't a 2nd
332 // bitmap, we need to create it (with a darker, selected
335 if ( tool
->CanBeToggled() )
336 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
339 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
343 col
.SetPixel(backgroundPixel
);
345 if (tool
->GetBitmap2().Ok() && tool
->GetBitmap2().GetMask())
348 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->GetBitmap2(), col
);
349 tool
->SetBitmap2(newBitmap
);
353 // Use unselected bitmap
356 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
357 tool
->SetBitmap2(newBitmap
);
360 tool
->SetBitmap2(bmp
);
363 pixmap
= (Pixmap
) bmp
.GetPixmap();
364 insensPixmap
= (Pixmap
) bmp
.GetInsensPixmap();
366 if (tool
->CanBeToggled())
369 Pixmap pixmap2
= (Pixmap
) 0;
370 Pixmap insensPixmap2
= (Pixmap
) 0;
372 // If there's a bitmap for the toggled state, use it,
373 // otherwise generate one.
374 if (tool
->GetBitmap2().Ok())
376 wxBitmap bmp2
= tool
->GetBitmap2();
377 pixmap2
= (Pixmap
) bmp2
.GetPixmap();
378 insensPixmap2
= (Pixmap
) bmp2
.GetInsensPixmap();
382 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
383 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
386 XtVaSetValues (button
,
387 XmNindicatorOn
, False
,
388 XmNshadowThickness
, 2,
389 // XmNborderWidth, 0,
393 XmNfillOnSelect
, True
,
394 XmNlabelPixmap
, pixmap
,
395 XmNselectPixmap
, pixmap2
,
396 XmNlabelInsensitivePixmap
, insensPixmap
,
397 XmNselectInsensitivePixmap
, insensPixmap2
,
398 XmNlabelType
, XmPIXMAP
,
403 Pixmap pixmap2
= (Pixmap
) 0;
405 // If there's a bitmap for the armed state, use it,
406 // otherwise generate one.
407 if (tool
->GetBitmap2().Ok())
409 pixmap2
= (Pixmap
) tool
->GetBitmap2().GetPixmap();
413 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
417 XtVaSetValues(button
,
418 XmNlabelPixmap
, pixmap
,
419 XmNlabelInsensitivePixmap
, insensPixmap
,
420 XmNarmPixmap
, pixmap2
,
423 XtManageChild(button
);
426 Dimension width
, height
;
427 XtVaGetValues(button
,
431 currentX
+= width
+ marginX
;
432 buttonHeight
= wxMax(buttonHeight
, height
);
435 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
436 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
442 node
= node
->GetNext();
445 SetSize(-1, -1, currentX
, buttonHeight
+ 2*marginY
);
450 wxToolBarTool
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
451 wxCoord
WXUNUSED(y
)) const
453 wxFAIL_MSG( _T("TODO") );
455 return (wxToolBarTool
*)NULL
;
458 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarTool
*tool
)
465 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarTool
*tool
)
472 void wxToolBar::DoEnableTool(wxToolBarTool
*tool
, bool enable
)
474 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
477 void wxToolBar::DoToggleTool(wxToolBarTool
*tool
, bool toggle
)
479 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
482 void wxToolBar::DoSetToggle(wxToolBarTool
*tool
, bool toggle
)
484 wxFAIL_MSG( _T("TODO") );
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 wxToolBarTool
*wxToolBar::FindToolByWidget(WXWidget w
) const
493 wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
496 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
497 if ( tool
->GetButtonWidget() == w
)
502 node
= node
->GetNext();
505 return (wxToolBarTool
*)NULL
;
508 static void wxToolButtonCallback(Widget w
,
509 XtPointer clientData
,
510 XtPointer
WXUNUSED(ptr
))
512 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
513 wxToolBarTool
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
517 if ( tool
->CanBeToggled() )
520 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
528 static void wxToolButtonPopupCallback(Widget w
,
529 XtPointer client_data
,
531 Boolean
*WXUNUSED(continue_to_dispatch
))
533 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
534 static const int delayMilli
= 800;
536 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
537 wxToolBarTool
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
542 wxString tooltip
= tool
->GetShortHelp();
546 if (!wxTheToolBarTimer
)
547 wxTheToolBarTimer
= new wxToolBarTimer
;
549 wxToolBarTimer::buttonWidget
= w
;
550 wxToolBarTimer::helpString
= tooltip
;
552 /************************************************************/
553 /* Popup help label */
554 /************************************************************/
555 if (event
->type
== EnterNotify
)
557 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
559 XtDestroyWidget (wxToolBarTimer::help_popup
);
560 XtPopdown (wxToolBarTimer::help_popup
);
562 wxToolBarTimer::help_popup
= (Widget
) 0;
565 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
568 /************************************************************/
569 /* Popdown help label */
570 /************************************************************/
571 else if (event
->type
== LeaveNotify
)
573 if (wxTheToolBarTimer
)
574 wxTheToolBarTimer
->Stop();
575 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
577 XtDestroyWidget (wxToolBarTimer::help_popup
);
578 XtPopdown (wxToolBarTimer::help_popup
);
580 wxToolBarTimer::help_popup
= (Widget
) 0;
584 void wxToolBarTimer::Notify()
588 /************************************************************/
589 /* Create shell without window decorations */
590 /************************************************************/
591 help_popup
= XtVaCreatePopupShell ("shell",
592 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
595 /************************************************************/
596 /* Get absolute position on display of toolbar button */
597 /************************************************************/
598 XtTranslateCoords (buttonWidget
,
603 // Move the tooltip more or less above the button
604 int yOffset
= 20; // TODO: What should be really?
606 if (y
< yOffset
) y
= 0;
608 /************************************************************/
609 /* Set the position of the help popup */
610 /************************************************************/
611 XtVaSetValues (help_popup
,
616 /************************************************************/
617 /* Create help label */
618 /************************************************************/
619 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
620 XtVaCreateManagedWidget ("help_label",
621 xmLabelWidgetClass
, help_popup
,
622 XmNlabelString
, text
,
624 XmNforeground
, XtRString
, "black",
627 XmNbackground
, XtRString
, "LightGoldenrod",
628 strlen("LightGoldenrod")+1,
632 /************************************************************/
633 /* Popup help label */
634 /************************************************************/
635 XtPopup (help_popup
, XtGrabNone
);