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
*wxToolBar::CreateTool(int id
,
134 const wxBitmap
& bitmap1
,
135 const wxBitmap
& bitmap2
,
137 wxObject
*clientData
,
138 const wxString
& shortHelpString
,
139 const wxString
& longHelpString
)
141 return new wxToolBarTool(this, id
, bitmap1
, bitmap2
, toggle
,
142 clientData
, shortHelpString
, longHelpString
);
145 wxToolBarToolBase
*wxToolBar::CreateTool(wxControl
*control
)
147 return new wxToolBarTool(this, control
);
150 void wxToolBarTool::Init()
152 m_widget
= (Widget
)0;
153 m_pixmap
= (Pixmap
)0;
156 wxToolBarTool::~wxToolBarTool()
159 XtDestroyWidget(m_widget
);
161 XmDestroyPixmap(DefaultScreenOfDisplay((Display
*)wxGetDisplay()),
165 // ----------------------------------------------------------------------------
166 // wxToolBar construction
167 // ----------------------------------------------------------------------------
169 void wxToolBar::Init()
174 m_defaultHeight
= 22;
176 m_toolSeparation
= 8;
183 bool wxToolBar::Create(wxWindow
*parent
,
188 const wxString
& name
)
195 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
196 m_foregroundColour
= parent
->GetForegroundColour();
197 m_windowStyle
= style
;
201 if (parent
) parent
->AddChild(this);
203 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
205 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
206 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
209 XmNresizePolicy
, XmRESIZE_NONE
,
212 Widget toolbar = XtVaCreateManagedWidget("toolbar",
213 xmFormWidgetClass, (Widget) m_clientWidget,
214 XmNtraversalOn, False,
215 XmNhorizontalSpacing, 0,
216 XmNverticalSpacing, 0,
224 m_mainWidget
= (WXWidget
) toolbar
;
226 m_font
= parent
->GetFont();
229 SetCanAddEventHandler(TRUE
);
230 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
232 ChangeBackgroundColour();
237 wxToolBar::~wxToolBar()
239 delete wxTheToolBarTimer
;
240 wxTheToolBarTimer
= NULL
;
243 bool wxToolBar::Realize()
245 if ( m_tools
.GetCount() == 0 )
252 const int separatorSize
= GetToolSeparation(); // 8;
253 wxSize margins
= GetToolMargins();
254 int marginX
= margins
.x
;
255 int marginY
= margins
.y
;
257 int currentX
= marginX
;
258 int currentY
= marginY
;
260 int buttonHeight
= 0;
262 int currentSpacing
= 0;
265 Pixmap pixmap
, insensPixmap
;
268 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
271 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
273 switch ( tool
->GetStyle() )
275 case wxTOOL_STYLE_CONTROL
:
276 wxFAIL_MSG( _T("not implemented") );
279 case wxTOOL_STYLE_SEPARATOR
:
280 currentX
+= separatorSize
;
283 case wxTOOL_STYLE_BUTTON
:
286 if ( tool
->CanBeToggled() )
288 button
= XtVaCreateWidget("toggleButton",
289 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
290 XmNx
, currentX
, XmNy
, currentY
,
291 XmNindicatorOn
, False
,
292 XmNshadowThickness
, 2,
297 XmNmultiClick
, XmMULTICLICK_KEEP
,
298 XmNlabelType
, XmPIXMAP
,
300 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
303 XtVaSetValues ((Widget
) button
,
304 XmNselectColor
, m_backgroundColour
.AllocColour(XtDisplay((Widget
) button
)),
309 button
= XtVaCreateWidget("button",
310 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
311 XmNx
, currentX
, XmNy
, currentY
,
312 XmNpushButtonEnabled
, True
,
313 XmNmultiClick
, XmMULTICLICK_KEEP
,
314 XmNlabelType
, XmPIXMAP
,
316 XtAddCallback (button
,
317 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
321 DoChangeBackgroundColour((WXWidget
) button
, m_backgroundColour
, TRUE
);
323 tool
->SetWidget(button
);
325 // For each button, if there is a mask, we must create
326 // a new wxBitmap that has the correct background colour
327 // for the button. Otherwise the background will just be
328 // e.g. black if a transparent XPM has been loaded.
329 bmp
= tool
->GetBitmap1();
333 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
337 col
.SetPixel(backgroundPixel
);
339 bmp
= wxCreateMaskedBitmap(bmp
, col
);
341 tool
->SetBitmap1(bmp
);
344 // Create a selected/toggled bitmap. If there isn't a 2nd
345 // bitmap, we need to create it (with a darker, selected
348 if ( tool
->CanBeToggled() )
349 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
352 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
356 col
.SetPixel(backgroundPixel
);
358 if (tool
->GetBitmap2().Ok() && tool
->GetBitmap2().GetMask())
361 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->GetBitmap2(), col
);
362 tool
->SetBitmap2(newBitmap
);
366 // Use unselected bitmap
369 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
370 tool
->SetBitmap2(newBitmap
);
373 tool
->SetBitmap2(bmp
);
376 pixmap
= (Pixmap
) bmp
.GetPixmap();
377 insensPixmap
= (Pixmap
) bmp
.GetInsensPixmap();
379 if (tool
->CanBeToggled())
382 Pixmap pixmap2
= (Pixmap
) 0;
383 Pixmap insensPixmap2
= (Pixmap
) 0;
385 // If there's a bitmap for the toggled state, use it,
386 // otherwise generate one.
387 if (tool
->GetBitmap2().Ok())
389 wxBitmap bmp2
= tool
->GetBitmap2();
390 pixmap2
= (Pixmap
) bmp2
.GetPixmap();
391 insensPixmap2
= (Pixmap
) bmp2
.GetInsensPixmap();
395 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
396 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
399 tool
->SetPixmap(pixmap2
);
401 XtVaSetValues (button
,
402 XmNfillOnSelect
, True
,
403 XmNlabelPixmap
, pixmap
,
404 XmNselectPixmap
, pixmap2
,
405 XmNlabelInsensitivePixmap
, insensPixmap
,
406 XmNselectInsensitivePixmap
, insensPixmap2
,
407 XmNlabelType
, XmPIXMAP
,
412 Pixmap pixmap2
= (Pixmap
) 0;
414 // If there's a bitmap for the armed state, use it,
415 // otherwise generate one.
416 if (tool
->GetBitmap2().Ok())
418 pixmap2
= (Pixmap
) tool
->GetBitmap2().GetPixmap();
422 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
426 tool
->SetPixmap(pixmap2
);
429 XtVaSetValues(button
,
430 XmNlabelPixmap
, pixmap
,
431 XmNlabelInsensitivePixmap
, insensPixmap
,
432 XmNarmPixmap
, pixmap2
,
436 XtManageChild(button
);
439 Dimension width
, height
;
440 XtVaGetValues(button
,
444 currentX
+= width
+ marginX
;
445 buttonHeight
= wxMax(buttonHeight
, height
);
448 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
449 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
455 node
= node
->GetNext();
458 SetSize(-1, -1, currentX
, buttonHeight
+ 2*marginY
);
463 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
464 wxCoord
WXUNUSED(y
)) const
466 wxFAIL_MSG( _T("TODO") );
468 return (wxToolBarToolBase
*)NULL
;
471 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
478 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
485 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
487 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
489 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
492 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
494 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
496 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
499 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
500 bool WXUNUSED(toggle
))
505 // ----------------------------------------------------------------------------
507 // ----------------------------------------------------------------------------
509 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
511 wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
514 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
515 if ( tool
->GetButtonWidget() == w
)
520 node
= node
->GetNext();
523 return (wxToolBarToolBase
*)NULL
;
526 static void wxToolButtonCallback(Widget w
,
527 XtPointer clientData
,
528 XtPointer
WXUNUSED(ptr
))
530 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
531 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
535 if ( tool
->CanBeToggled() )
538 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
546 static void wxToolButtonPopupCallback(Widget w
,
547 XtPointer client_data
,
549 Boolean
*WXUNUSED(continue_to_dispatch
))
551 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
552 static const int delayMilli
= 800;
554 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
555 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
560 wxString tooltip
= tool
->GetShortHelp();
564 if (!wxTheToolBarTimer
)
565 wxTheToolBarTimer
= new wxToolBarTimer
;
567 wxToolBarTimer::buttonWidget
= w
;
568 wxToolBarTimer::helpString
= tooltip
;
570 /************************************************************/
571 /* Popup help label */
572 /************************************************************/
573 if (event
->type
== EnterNotify
)
575 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
577 XtDestroyWidget (wxToolBarTimer::help_popup
);
578 XtPopdown (wxToolBarTimer::help_popup
);
580 wxToolBarTimer::help_popup
= (Widget
) 0;
583 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
586 /************************************************************/
587 /* Popdown help label */
588 /************************************************************/
589 else if (event
->type
== LeaveNotify
)
591 if (wxTheToolBarTimer
)
592 wxTheToolBarTimer
->Stop();
593 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
595 XtDestroyWidget (wxToolBarTimer::help_popup
);
596 XtPopdown (wxToolBarTimer::help_popup
);
598 wxToolBarTimer::help_popup
= (Widget
) 0;
602 void wxToolBarTimer::Notify()
606 /************************************************************/
607 /* Create shell without window decorations */
608 /************************************************************/
609 help_popup
= XtVaCreatePopupShell ("shell",
610 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
613 /************************************************************/
614 /* Get absolute position on display of toolbar button */
615 /************************************************************/
616 XtTranslateCoords (buttonWidget
,
621 // Move the tooltip more or less above the button
622 int yOffset
= 20; // TODO: What should be really?
624 if (y
< yOffset
) y
= 0;
626 /************************************************************/
627 /* Set the position of the help popup */
628 /************************************************************/
629 XtVaSetValues (help_popup
,
634 /************************************************************/
635 /* Create help label */
636 /************************************************************/
637 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
638 XtVaCreateManagedWidget ("help_label",
639 xmLabelWidgetClass
, help_popup
,
640 XmNlabelString
, text
,
642 XmNforeground
, XtRString
, "black",
645 XmNbackground
, XtRString
, "LightGoldenrod",
646 strlen("LightGoldenrod")+1,
650 /************************************************************/
651 /* Popup help label */
652 /************************************************************/
653 XtPopup (help_popup
, XtGrabNone
);