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 XmNindicatorOn
, False
,
286 XmNshadowThickness
, 2,
291 XmNmultiClick
, XmMULTICLICK_KEEP
,
292 XmNlabelType
, XmPIXMAP
,
294 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
297 XtVaSetValues ((Widget
) button
,
298 XmNselectColor
, m_backgroundColour
.AllocColour(XtDisplay((Widget
) button
)),
303 button
= XtVaCreateWidget("button",
304 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
305 XmNx
, currentX
, XmNy
, currentY
,
306 XmNpushButtonEnabled
, True
,
307 XmNmultiClick
, XmMULTICLICK_KEEP
,
308 XmNlabelType
, XmPIXMAP
,
310 XtAddCallback (button
,
311 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
315 DoChangeBackgroundColour((WXWidget
) button
, m_backgroundColour
, TRUE
);
317 tool
->SetWidget(button
);
319 // For each button, if there is a mask, we must create
320 // a new wxBitmap that has the correct background colour
321 // for the button. Otherwise the background will just be
322 // e.g. black if a transparent XPM has been loaded.
323 bmp
= tool
->GetBitmap1();
327 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
331 col
.SetPixel(backgroundPixel
);
333 bmp
= wxCreateMaskedBitmap(bmp
, col
);
335 tool
->SetBitmap1(bmp
);
338 // Create a selected/toggled bitmap. If there isn't a 2nd
339 // bitmap, we need to create it (with a darker, selected
342 if ( tool
->CanBeToggled() )
343 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
346 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
350 col
.SetPixel(backgroundPixel
);
352 if (tool
->GetBitmap2().Ok() && tool
->GetBitmap2().GetMask())
355 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->GetBitmap2(), col
);
356 tool
->SetBitmap2(newBitmap
);
360 // Use unselected bitmap
363 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
364 tool
->SetBitmap2(newBitmap
);
367 tool
->SetBitmap2(bmp
);
370 pixmap
= (Pixmap
) bmp
.GetPixmap();
371 insensPixmap
= (Pixmap
) bmp
.GetInsensPixmap();
373 if (tool
->CanBeToggled())
376 Pixmap pixmap2
= (Pixmap
) 0;
377 Pixmap insensPixmap2
= (Pixmap
) 0;
379 // If there's a bitmap for the toggled state, use it,
380 // otherwise generate one.
381 if (tool
->GetBitmap2().Ok())
383 wxBitmap bmp2
= tool
->GetBitmap2();
384 pixmap2
= (Pixmap
) bmp2
.GetPixmap();
385 insensPixmap2
= (Pixmap
) bmp2
.GetInsensPixmap();
389 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
390 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
393 tool
->SetPixmap(pixmap2
);
395 XtVaSetValues (button
,
396 XmNfillOnSelect
, True
,
397 XmNlabelPixmap
, pixmap
,
398 XmNselectPixmap
, pixmap2
,
399 XmNlabelInsensitivePixmap
, insensPixmap
,
400 XmNselectInsensitivePixmap
, insensPixmap2
,
401 XmNlabelType
, XmPIXMAP
,
406 Pixmap pixmap2
= (Pixmap
) 0;
408 // If there's a bitmap for the armed state, use it,
409 // otherwise generate one.
410 if (tool
->GetBitmap2().Ok())
412 pixmap2
= (Pixmap
) tool
->GetBitmap2().GetPixmap();
416 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
420 tool
->SetPixmap(pixmap2
);
423 XtVaSetValues(button
,
424 XmNlabelPixmap
, pixmap
,
425 XmNlabelInsensitivePixmap
, insensPixmap
,
426 XmNarmPixmap
, pixmap2
,
430 XtManageChild(button
);
433 Dimension width
, height
;
434 XtVaGetValues(button
,
438 currentX
+= width
+ marginX
;
439 buttonHeight
= wxMax(buttonHeight
, height
);
442 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
443 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
449 node
= node
->GetNext();
452 SetSize(-1, -1, currentX
, buttonHeight
+ 2*marginY
);
457 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
458 wxCoord
WXUNUSED(y
)) const
460 wxFAIL_MSG( _T("TODO") );
462 return (wxToolBarToolBase
*)NULL
;
465 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
472 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
479 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
481 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
483 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
486 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
488 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
490 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
493 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
494 bool WXUNUSED(toggle
))
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
503 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
505 wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
508 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
509 if ( tool
->GetButtonWidget() == w
)
514 node
= node
->GetNext();
517 return (wxToolBarToolBase
*)NULL
;
520 static void wxToolButtonCallback(Widget w
,
521 XtPointer clientData
,
522 XtPointer
WXUNUSED(ptr
))
524 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
525 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
529 if ( tool
->CanBeToggled() )
532 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
540 static void wxToolButtonPopupCallback(Widget w
,
541 XtPointer client_data
,
543 Boolean
*WXUNUSED(continue_to_dispatch
))
545 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
546 static const int delayMilli
= 800;
548 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
549 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
554 wxString tooltip
= tool
->GetShortHelp();
558 if (!wxTheToolBarTimer
)
559 wxTheToolBarTimer
= new wxToolBarTimer
;
561 wxToolBarTimer::buttonWidget
= w
;
562 wxToolBarTimer::helpString
= tooltip
;
564 /************************************************************/
565 /* Popup help label */
566 /************************************************************/
567 if (event
->type
== EnterNotify
)
569 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
571 XtDestroyWidget (wxToolBarTimer::help_popup
);
572 XtPopdown (wxToolBarTimer::help_popup
);
574 wxToolBarTimer::help_popup
= (Widget
) 0;
577 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
580 /************************************************************/
581 /* Popdown help label */
582 /************************************************************/
583 else if (event
->type
== LeaveNotify
)
585 if (wxTheToolBarTimer
)
586 wxTheToolBarTimer
->Stop();
587 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
589 XtDestroyWidget (wxToolBarTimer::help_popup
);
590 XtPopdown (wxToolBarTimer::help_popup
);
592 wxToolBarTimer::help_popup
= (Widget
) 0;
596 void wxToolBarTimer::Notify()
600 /************************************************************/
601 /* Create shell without window decorations */
602 /************************************************************/
603 help_popup
= XtVaCreatePopupShell ("shell",
604 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
607 /************************************************************/
608 /* Get absolute position on display of toolbar button */
609 /************************************************************/
610 XtTranslateCoords (buttonWidget
,
615 // Move the tooltip more or less above the button
616 int yOffset
= 20; // TODO: What should be really?
618 if (y
< yOffset
) y
= 0;
620 /************************************************************/
621 /* Set the position of the help popup */
622 /************************************************************/
623 XtVaSetValues (help_popup
,
628 /************************************************************/
629 /* Create help label */
630 /************************************************************/
631 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
632 XtVaCreateManagedWidget ("help_label",
633 xmLabelWidgetClass
, help_popup
,
634 XmNlabelString
, text
,
636 XmNforeground
, XtRString
, "black",
639 XmNbackground
, XtRString
, "LightGoldenrod",
640 strlen("LightGoldenrod")+1,
644 /************************************************************/
645 /* Popup help label */
646 /************************************************************/
647 XtPopup (help_popup
, XtGrabNone
);