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 packing
= GetToolPacking();
255 int marginX
= margins
.x
;
256 int marginY
= margins
.y
;
258 int currentX
= marginX
;
259 int currentY
= marginY
;
261 int buttonHeight
= 0;
263 int currentSpacing
= 0;
266 Pixmap pixmap
, insensPixmap
;
269 wxToolBarToolsList::Node
*node
= m_tools
.GetFirst();
272 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
274 switch ( tool
->GetStyle() )
276 case wxTOOL_STYLE_CONTROL
:
278 wxControl
* control
= tool
->GetControl();
279 wxSize sz
= control
->GetSize();
280 wxPoint pos
= control
->GetPosition();
281 control
->Move(currentX
, pos
.y
);
282 currentX
+= sz
.x
+ packing
;
286 case wxTOOL_STYLE_SEPARATOR
:
287 currentX
+= separatorSize
;
290 case wxTOOL_STYLE_BUTTON
:
293 if ( tool
->CanBeToggled() )
295 button
= XtVaCreateWidget("toggleButton",
296 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
297 XmNx
, currentX
, XmNy
, currentY
,
298 XmNindicatorOn
, False
,
299 XmNshadowThickness
, 2,
304 XmNmultiClick
, XmMULTICLICK_KEEP
,
305 XmNlabelType
, XmPIXMAP
,
307 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
310 XtVaSetValues ((Widget
) button
,
311 XmNselectColor
, m_backgroundColour
.AllocColour(XtDisplay((Widget
) button
)),
316 button
= XtVaCreateWidget("button",
317 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
318 XmNx
, currentX
, XmNy
, currentY
,
319 XmNpushButtonEnabled
, True
,
320 XmNmultiClick
, XmMULTICLICK_KEEP
,
321 XmNlabelType
, XmPIXMAP
,
323 XtAddCallback (button
,
324 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
328 DoChangeBackgroundColour((WXWidget
) button
, m_backgroundColour
, TRUE
);
330 tool
->SetWidget(button
);
332 // For each button, if there is a mask, we must create
333 // a new wxBitmap that has the correct background colour
334 // for the button. Otherwise the background will just be
335 // e.g. black if a transparent XPM has been loaded.
336 bmp
= tool
->GetBitmap1();
340 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
344 col
.SetPixel(backgroundPixel
);
346 bmp
= wxCreateMaskedBitmap(bmp
, col
);
348 tool
->SetBitmap1(bmp
);
351 // Create a selected/toggled bitmap. If there isn't a 2nd
352 // bitmap, we need to create it (with a darker, selected
355 if ( tool
->CanBeToggled() )
356 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
359 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
363 col
.SetPixel(backgroundPixel
);
365 if (tool
->GetBitmap2().Ok() && tool
->GetBitmap2().GetMask())
368 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->GetBitmap2(), col
);
369 tool
->SetBitmap2(newBitmap
);
373 // Use unselected bitmap
376 wxBitmap newBitmap
= wxCreateMaskedBitmap(bmp
, col
);
377 tool
->SetBitmap2(newBitmap
);
380 tool
->SetBitmap2(bmp
);
383 pixmap
= (Pixmap
) bmp
.GetPixmap();
384 insensPixmap
= (Pixmap
) bmp
.GetInsensPixmap();
386 if (tool
->CanBeToggled())
389 Pixmap pixmap2
= (Pixmap
) 0;
390 Pixmap insensPixmap2
= (Pixmap
) 0;
392 // If there's a bitmap for the toggled state, use it,
393 // otherwise generate one.
394 if (tool
->GetBitmap2().Ok())
396 wxBitmap bmp2
= tool
->GetBitmap2();
397 pixmap2
= (Pixmap
) bmp2
.GetPixmap();
398 insensPixmap2
= (Pixmap
) bmp2
.GetInsensPixmap();
402 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
403 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
406 tool
->SetPixmap(pixmap2
);
408 XtVaSetValues (button
,
409 XmNfillOnSelect
, True
,
410 XmNlabelPixmap
, pixmap
,
411 XmNselectPixmap
, pixmap2
,
412 XmNlabelInsensitivePixmap
, insensPixmap
,
413 XmNselectInsensitivePixmap
, insensPixmap2
,
414 XmNlabelType
, XmPIXMAP
,
419 Pixmap pixmap2
= (Pixmap
) 0;
421 // If there's a bitmap for the armed state, use it,
422 // otherwise generate one.
423 if (tool
->GetBitmap2().Ok())
425 pixmap2
= (Pixmap
) tool
->GetBitmap2().GetPixmap();
429 pixmap2
= (Pixmap
) bmp
.GetArmPixmap(button
);
433 tool
->SetPixmap(pixmap2
);
436 XtVaSetValues(button
,
437 XmNlabelPixmap
, pixmap
,
438 XmNlabelInsensitivePixmap
, insensPixmap
,
439 XmNarmPixmap
, pixmap2
,
443 XtManageChild(button
);
446 Dimension width
, height
;
447 XtVaGetValues(button
,
451 currentX
+= width
+ packing
;
452 buttonHeight
= wxMax(buttonHeight
, height
);
455 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
456 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
462 node
= node
->GetNext();
465 SetSize(-1, -1, currentX
, buttonHeight
+ 2*marginY
);
470 wxToolBarToolBase
*wxToolBar::FindToolForPosition(wxCoord
WXUNUSED(x
),
471 wxCoord
WXUNUSED(y
)) const
473 wxFAIL_MSG( _T("TODO") );
475 return (wxToolBarToolBase
*)NULL
;
478 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
485 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos
), wxToolBarToolBase
*tool
)
492 void wxToolBar::DoEnableTool(wxToolBarToolBase
*toolBase
, bool enable
)
494 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
496 XtSetSensitive(tool
->GetButtonWidget(), (Boolean
) enable
);
499 void wxToolBar::DoToggleTool(wxToolBarToolBase
*toolBase
, bool toggle
)
501 wxToolBarTool
*tool
= (wxToolBarTool
*)toolBase
;
503 XmToggleButtonSetState(tool
->GetButtonWidget(), (Boolean
) toggle
, False
);
506 void wxToolBar::DoSetToggle(wxToolBarToolBase
* WXUNUSED(tool
),
507 bool WXUNUSED(toggle
))
512 // ----------------------------------------------------------------------------
514 // ----------------------------------------------------------------------------
516 wxToolBarToolBase
*wxToolBar::FindToolByWidget(WXWidget w
) const
518 wxToolBarToolsList::Node
* node
= m_tools
.GetFirst();
521 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->GetData();
522 if ( tool
->GetButtonWidget() == w
)
527 node
= node
->GetNext();
530 return (wxToolBarToolBase
*)NULL
;
533 static void wxToolButtonCallback(Widget w
,
534 XtPointer clientData
,
535 XtPointer
WXUNUSED(ptr
))
537 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
538 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
542 if ( tool
->CanBeToggled() )
545 if ( !toolBar
->OnLeftClick(tool
->GetId(), tool
->IsToggled()) )
553 static void wxToolButtonPopupCallback(Widget w
,
554 XtPointer client_data
,
556 Boolean
*WXUNUSED(continue_to_dispatch
))
558 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
559 static const int delayMilli
= 800;
561 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
562 wxToolBarToolBase
*tool
= toolBar
->FindToolByWidget((WXWidget
) w
);
567 wxString tooltip
= tool
->GetShortHelp();
571 if (!wxTheToolBarTimer
)
572 wxTheToolBarTimer
= new wxToolBarTimer
;
574 wxToolBarTimer::buttonWidget
= w
;
575 wxToolBarTimer::helpString
= tooltip
;
577 /************************************************************/
578 /* Popup help label */
579 /************************************************************/
580 if (event
->type
== EnterNotify
)
582 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
584 XtDestroyWidget (wxToolBarTimer::help_popup
);
585 XtPopdown (wxToolBarTimer::help_popup
);
587 wxToolBarTimer::help_popup
= (Widget
) 0;
590 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
593 /************************************************************/
594 /* Popdown help label */
595 /************************************************************/
596 else if (event
->type
== LeaveNotify
)
598 if (wxTheToolBarTimer
)
599 wxTheToolBarTimer
->Stop();
600 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
602 XtDestroyWidget (wxToolBarTimer::help_popup
);
603 XtPopdown (wxToolBarTimer::help_popup
);
605 wxToolBarTimer::help_popup
= (Widget
) 0;
609 void wxToolBarTimer::Notify()
613 /************************************************************/
614 /* Create shell without window decorations */
615 /************************************************************/
616 help_popup
= XtVaCreatePopupShell ("shell",
617 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
620 /************************************************************/
621 /* Get absolute position on display of toolbar button */
622 /************************************************************/
623 XtTranslateCoords (buttonWidget
,
628 // Move the tooltip more or less above the button
629 int yOffset
= 20; // TODO: What should be really?
631 if (y
< yOffset
) y
= 0;
633 /************************************************************/
634 /* Set the position of the help popup */
635 /************************************************************/
636 XtVaSetValues (help_popup
,
641 /************************************************************/
642 /* Create help label */
643 /************************************************************/
644 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
645 XtVaCreateManagedWidget ("help_label",
646 xmLabelWidgetClass
, help_popup
,
647 XmNlabelString
, text
,
649 XmNforeground
, XtRString
, "black",
652 XmNbackground
, XtRString
, "LightGoldenrod",
653 strlen("LightGoldenrod")+1,
657 /************************************************************/
658 /* Popup help label */
659 /************************************************************/
660 XtPopup (help_popup
, XtGrabNone
);