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;
177 bool wxToolBar::Create(wxWindow
*parent
,
182 const wxString
& name
)
189 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
190 m_foregroundColour
= parent
->GetForegroundColour();
191 m_windowStyle
= style
;
195 if (parent
) parent
->AddChild(this);
197 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
199 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
200 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
203 XmNresizePolicy
, XmRESIZE_NONE
,
206 Widget toolbar = XtVaCreateManagedWidget("toolbar",
207 xmFormWidgetClass, (Widget) m_clientWidget,
208 XmNtraversalOn, False,
209 XmNhorizontalSpacing, 0,
210 XmNverticalSpacing, 0,
218 m_mainWidget
= (WXWidget
) toolbar
;
220 m_font
= parent
->GetFont();
223 SetCanAddEventHandler(TRUE
);
224 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
226 ChangeBackgroundColour();
231 wxToolBar::~wxToolBar()
233 delete wxTheToolBarTimer
;
234 wxTheToolBarTimer
= NULL
;
237 bool wxToolBar::Realize()
239 if ( m_tools
.GetCount() == 0 )
246 const int separatorSize
= GetToolSeparation(); // 8;
247 wxSize margins
= GetToolMargins();
248 int marginX
= margins
.x
;
249 int marginY
= margins
.y
;
251 int currentX
= marginX
;
252 int currentY
= marginY
;
254 int buttonHeight
= 0;
256 int currentSpacing
= 0;
259 Pixmap pixmap
, insensPixmap
;
262 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
265 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
267 switch ( tool
->GetStyle() )
269 case wxTOOL_STYLE_CONTROL
:
270 wxFAIL_MSG( _T("not implemented") );
273 case wxTOOL_STYLE_SEPARATOR
:
274 currentX
+= separatorSize
;
277 case wxTOOL_STYLE_BUTTON
:
280 if ( tool
->CanBeToggled() )
282 button
= XtVaCreateWidget("toggleButton",
283 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
284 XmNx
, currentX
, XmNy
, currentY
,
285 // XmNpushButtonEnabled, True,
286 XmNmultiClick
, XmMULTICLICK_KEEP
,
287 XmNlabelType
, XmPIXMAP
,
289 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
292 XtVaSetValues ((Widget
) button
,
293 XmNselectColor
, m_backgroundColour
.AllocColour(XtDisplay((Widget
) button
)),
298 button
= XtVaCreateWidget("button",
299 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
300 XmNx
, currentX
, XmNy
, currentY
,
301 XmNpushButtonEnabled
, True
,
302 XmNmultiClick
, XmMULTICLICK_KEEP
,
303 XmNlabelType
, XmPIXMAP
,
305 XtAddCallback (button
,
306 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
310 DoChangeBackgroundColour((WXWidget
) button
, m_backgroundColour
, TRUE
);
312 tool
->SetWidget(button
);
314 // For each button, if there is a mask, we must create
315 // a new wxBitmap that has the correct background colour
316 // for the button. Otherwise the background will just be
317 // e.g. black if a transparent XPM has been loaded.
318 bmp
= tool
->GetBitmap1();
322 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
326 col
.SetPixel(backgroundPixel
);
328 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
330 tool
->SetBitmap1(newBitmap
);
333 // Create a selected/toggled bitmap. If there isn't a 2nd
334 // bitmap, we need to create it (with a darker, selected
337 if ( tool
->CanBeToggled() )
338 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
341 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
345 col
.SetPixel(backgroundPixel
);
347 if (tool
->GetBitmap2().Ok() && tool
->GetBitmap2().GetMask())
350 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->GetBitmap2(), col
);
351 tool
->SetBitmap2(newBitmap
);
355 // Use unselected bitmap
358 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
359 tool
->SetBitmap2(newBitmap
);
362 tool
->SetBitmap2(bmp
);
365 pixmap
= (Pixmap
) bmp
.GetPixmap();
366 insensPixmap
= (Pixmap
) bmp
.GetInsensPixmap();
368 if (tool
->CanBeToggled())
371 Pixmap pixmap2
= (Pixmap
) 0;
372 Pixmap insensPixmap2
= (Pixmap
) 0;
374 // If there's a bitmap for the toggled state, use it,
375 // otherwise generate one.
376 if (tool
->GetBitmap2().Ok())
378 wxBitmap bmp2
= tool
->GetBitmap2();
379 pixmap2
= (Pixmap
) bmp2
.GetPixmap();
380 insensPixmap2
= (Pixmap
) bmp2
.GetInsensPixmap();
384 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
385 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
388 tool
->SetPixmap(pixmap2
);
390 XtVaSetValues (button
,
391 XmNindicatorOn
, False
,
392 XmNshadowThickness
, 2,
393 // XmNborderWidth, 0,
397 XmNfillOnSelect
, True
,
398 XmNlabelPixmap
, pixmap
,
399 XmNselectPixmap
, pixmap2
,
400 XmNlabelInsensitivePixmap
, insensPixmap
,
401 XmNselectInsensitivePixmap
, insensPixmap2
,
402 XmNlabelType
, XmPIXMAP
,
407 Pixmap pixmap2
= (Pixmap
) 0;
409 // If there's a bitmap for the armed state, use it,
410 // otherwise generate one.
411 if (tool
->GetBitmap2().Ok())
413 pixmap2
= (Pixmap
) tool
->GetBitmap2().GetPixmap();
417 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
421 tool
->SetPixmap(pixmap2
);
424 XtVaSetValues(button
,
425 XmNlabelPixmap
, pixmap
,
426 XmNlabelInsensitivePixmap
, insensPixmap
,
427 XmNarmPixmap
, pixmap2
,
431 XtManageChild(button
);
434 Dimension width
, height
;
435 XtVaGetValues(button
,
439 currentX
+= width
+ marginX
;
440 buttonHeight
= wxMax(buttonHeight
, height
);
443 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
444 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
450 node
= node
->GetNext();
453 SetSize(-1, -1, currentX
, buttonHeight
+ 2*marginY
);
458 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
459 wxCoord
WXUNUSED(y
)) const
461 wxFAIL_MSG( _T("TODO") );
463 return (wxToolBarToolBase
*)NULL
;
466 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
473 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
480 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
482 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
484 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
487 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
489 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
491 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
494 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
495 bool WXUNUSED(toggle
))
500 // ----------------------------------------------------------------------------
502 // ----------------------------------------------------------------------------
504 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
506 wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
509 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
510 if ( tool
->GetButtonWidget() == w
)
515 node
= node
->GetNext();
518 return (wxToolBarToolBase
*)NULL
;
521 static void wxToolButtonCallback(Widget w
,
522 XtPointer clientData
,
523 XtPointer
WXUNUSED(ptr
))
525 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
526 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
530 if ( tool
->CanBeToggled() )
533 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
541 static void wxToolButtonPopupCallback(Widget w
,
542 XtPointer client_data
,
544 Boolean
*WXUNUSED(continue_to_dispatch
))
546 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
547 static const int delayMilli
= 800;
549 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
550 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
555 wxString tooltip
= tool
->GetShortHelp();
559 if (!wxTheToolBarTimer
)
560 wxTheToolBarTimer
= new wxToolBarTimer
;
562 wxToolBarTimer::buttonWidget
= w
;
563 wxToolBarTimer::helpString
= tooltip
;
565 /************************************************************/
566 /* Popup help label */
567 /************************************************************/
568 if (event
->type
== EnterNotify
)
570 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
572 XtDestroyWidget (wxToolBarTimer::help_popup
);
573 XtPopdown (wxToolBarTimer::help_popup
);
575 wxToolBarTimer::help_popup
= (Widget
) 0;
578 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
581 /************************************************************/
582 /* Popdown help label */
583 /************************************************************/
584 else if (event
->type
== LeaveNotify
)
586 if (wxTheToolBarTimer
)
587 wxTheToolBarTimer
->Stop();
588 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
590 XtDestroyWidget (wxToolBarTimer::help_popup
);
591 XtPopdown (wxToolBarTimer::help_popup
);
593 wxToolBarTimer::help_popup
= (Widget
) 0;
597 void wxToolBarTimer::Notify()
601 /************************************************************/
602 /* Create shell without window decorations */
603 /************************************************************/
604 help_popup
= XtVaCreatePopupShell ("shell",
605 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
608 /************************************************************/
609 /* Get absolute position on display of toolbar button */
610 /************************************************************/
611 XtTranslateCoords (buttonWidget
,
616 // Move the tooltip more or less above the button
617 int yOffset
= 20; // TODO: What should be really?
619 if (y
< yOffset
) y
= 0;
621 /************************************************************/
622 /* Set the position of the help popup */
623 /************************************************************/
624 XtVaSetValues (help_popup
,
629 /************************************************************/
630 /* Create help label */
631 /************************************************************/
632 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
633 XtVaCreateManagedWidget ("help_label",
634 xmLabelWidgetClass
, help_popup
,
635 XmNlabelString
, text
,
637 XmNforeground
, XtRString
, "black",
640 XmNbackground
, XtRString
, "LightGoldenrod",
641 strlen("LightGoldenrod")+1,
645 /************************************************************/
646 /* Popup help label */
647 /************************************************************/
648 XtPopup (help_popup
, XtGrabNone
);