1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "toolbar.h"
19 #include "wx/toolbar.h"
22 #include <Xm/PushBG.h>
25 #include <Xm/ToggleB.h>
26 #include <Xm/ToggleBG.h>
29 #include "wx/motif/private.h"
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxToolBar
, wxToolBarBase
)
34 BEGIN_EVENT_TABLE(wxToolBar
, wxToolBarBase
)
38 static void wxToolButtonCallback (Widget w
, XtPointer clientData
,
40 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
41 XEvent
*event
, Boolean
*continue_to_dispatch
);
43 class wxToolBarTimer
: public wxTimer
47 virtual void Notify();
49 static Widget help_popup
;
50 static Widget buttonWidget
;
51 static wxString helpString
;
54 static wxToolBarTimer
* wxTheToolBarTimer
= (wxToolBarTimer
*) NULL
;
56 Widget
wxToolBarTimer::help_popup
= (Widget
) 0;
57 Widget
wxToolBarTimer::buttonWidget
= (Widget
) 0;
58 wxString
wxToolBarTimer::helpString
= "";
60 wxToolBar::wxToolBar():
61 m_widgets(wxKEY_INTEGER
)
69 bool wxToolBar::Create(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
,
70 long style
, const wxString
& name
)
78 m_backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
79 m_foregroundColour
= parent
->GetForegroundColour();
80 m_windowStyle
= style
;
84 if (parent
) parent
->AddChild(this);
86 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
88 Widget toolbar
= XtVaCreateManagedWidget("toolbar",
89 xmBulletinBoardWidgetClass
, (Widget
) parentWidget
,
92 XmNresizePolicy
, XmRESIZE_NONE
,
95 Widget toolbar = XtVaCreateManagedWidget("toolbar",
96 xmFormWidgetClass, (Widget) m_clientWidget,
97 XmNtraversalOn, False,
98 XmNhorizontalSpacing, 0,
99 XmNverticalSpacing, 0,
107 m_mainWidget
= (WXWidget
) toolbar
;
109 m_font
= parent
->GetFont();
112 SetCanAddEventHandler(TRUE
);
113 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
115 ChangeBackgroundColour();
120 wxToolBar::~wxToolBar()
122 delete wxTheToolBarTimer
;
123 wxTheToolBarTimer
= NULL
;
128 bool wxToolBar::CreateTools()
130 if (m_tools
.Number() == 0)
134 const int separatorSize
= GetToolSeparation(); // 8;
135 wxSize margins
= GetToolMargins();
136 int marginX
= margins
.x
;
137 int marginY
= margins
.y
;
139 int currentX
= marginX
;
140 int currentY
= marginY
;
142 int buttonHeight
= 0;
144 int currentSpacing
= 0;
147 Widget prevButton
= (Widget
) 0;
148 wxNode
* node
= m_tools
.First();
151 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
153 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
154 currentX
+= separatorSize
;
155 else if (tool
->m_bitmap1
.Ok())
157 Widget button
= (Widget
) 0;
159 if (tool
->m_isToggle
)
161 button
= XtVaCreateWidget("toggleButton",
162 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
163 XmNx
, currentX
, XmNy
, currentY
,
164 // XmNpushButtonEnabled, True,
165 XmNmultiClick
, XmMULTICLICK_KEEP
,
166 XmNlabelType
, XmPIXMAP
,
168 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
171 XtVaSetValues ((Widget
) button
,
172 XmNselectColor
, m_backgroundColour
.AllocColour(XtDisplay((Widget
) button
)),
177 button
= XtVaCreateWidget("button",
178 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
179 XmNx
, currentX
, XmNy
, currentY
,
180 XmNpushButtonEnabled
, True
,
181 XmNmultiClick
, XmMULTICLICK_KEEP
,
182 XmNlabelType
, XmPIXMAP
,
184 XtAddCallback (button
,
185 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
189 DoChangeBackgroundColour((WXWidget
) button
, m_backgroundColour
, TRUE
);
191 // For each button, if there is a mask, we must create
192 // a new wxBitmap that has the correct background colour
193 // for the button. Otherwise the background will just be
194 // e.g. black if a transparent XPM has been loaded.
195 wxBitmap originalBitmap
= tool
->m_bitmap1
;
197 if (tool
->m_bitmap1
.GetMask())
200 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
205 col
.SetPixel(backgroundPixel
);
207 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->m_bitmap1
, col
);
209 tool
->m_bitmap1
= newBitmap
;
212 // Create a selected/toggled bitmap. If there isn't a m_bitmap2,
213 // we need to create it (with a darker, selected background)
215 if (tool
->m_isToggle
)
216 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
219 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
223 col
.SetPixel(backgroundPixel
);
225 if (tool
->m_bitmap2
.Ok() && tool
->m_bitmap2
.GetMask())
228 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->m_bitmap2
, col
);
229 tool
->m_bitmap2
= newBitmap
;
233 // Use unselected bitmap
234 if (originalBitmap
.GetMask())
236 wxBitmap newBitmap
= wxCreateMaskedBitmap(originalBitmap
, col
);
237 tool
->m_bitmap2
= newBitmap
;
240 tool
->m_bitmap2
= tool
->m_bitmap1
;
243 Pixmap pixmap
= (Pixmap
) tool
->m_bitmap1
.GetPixmap();
244 Pixmap insensPixmap
= (Pixmap
) tool
->m_bitmap1
.GetInsensPixmap();
246 if (tool
->m_isToggle
)
249 Pixmap pixmap2
= (Pixmap
) 0;
250 Pixmap insensPixmap2
= (Pixmap
) 0;
252 // If there's a bitmap for the toggled state, use it,
253 // otherwise generate one.
254 if (tool
->m_bitmap2
.Ok())
256 pixmap2
= (Pixmap
) tool
->m_bitmap2
.GetPixmap();
257 insensPixmap2
= (Pixmap
) tool
->m_bitmap2
.GetInsensPixmap();
261 pixmap2
= (Pixmap
) tool
->m_bitmap1
.GetArmPixmap(button
);
262 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
263 m_pixmaps
.Append((wxObject
*) insensPixmap2
); // Store for later deletion
265 XtVaSetValues (button
,
266 XmNindicatorOn
, False
,
267 XmNshadowThickness
, 2,
268 // XmNborderWidth, 0,
272 XmNfillOnSelect
, True
,
273 XmNlabelPixmap
, pixmap
,
274 XmNselectPixmap
, pixmap2
,
275 XmNlabelInsensitivePixmap
, insensPixmap
,
276 XmNselectInsensitivePixmap
, insensPixmap2
,
277 XmNlabelType
, XmPIXMAP
,
282 Pixmap pixmap2
= (Pixmap
) 0;
284 // If there's a bitmap for the armed state, use it,
285 // otherwise generate one.
286 if (tool
->m_bitmap2
.Ok())
288 pixmap2
= (Pixmap
) tool
->m_bitmap2
.GetPixmap();
292 pixmap2
= (Pixmap
) tool
->m_bitmap1
.GetArmPixmap(button
);
296 XtVaSetValues(button
,
297 XmNlabelPixmap
, pixmap
,
298 XmNlabelInsensitivePixmap
, insensPixmap
,
299 XmNarmPixmap
, pixmap2
,
302 XtManageChild(button
);
304 Dimension width
, height
;
305 XtVaGetValues(button
, XmNwidth
, & width
, XmNheight
, & height
,
307 currentX
+= width
+ marginX
;
308 buttonHeight
= wxMax(buttonHeight
, height
);
310 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
311 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
312 m_widgets
.Append(tool
->m_index
, (wxObject
*) button
);
320 SetSize(-1, -1, currentX
, buttonHeight
+ 2*marginY
);
325 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
327 // TODO not necessary?
328 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
331 wxSize
wxToolBar::GetMaxSize() const
339 // The button size is bigger than the bitmap size
340 wxSize
wxToolBar::GetToolSize() const
342 // TODO not necessary?
343 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
346 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
348 wxNode
*node
= m_tools
.Find((long)toolIndex
);
351 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
352 tool
->m_enabled
= enable
;
354 WXWidget widget
= FindWidgetForIndex(tool
->m_index
);
355 if (widget
== (WXWidget
) 0)
358 XtSetSensitive((Widget
) widget
, (Boolean
) enable
);
362 void wxToolBar::ToggleTool(int toolIndex
, bool toggle
)
364 wxNode
*node
= m_tools
.Find((long)toolIndex
);
367 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
368 if (tool
->m_isToggle
)
370 tool
->m_toggleState
= toggle
;
372 WXWidget widget
= FindWidgetForIndex(tool
->m_index
);
373 if (widget
== (WXWidget
) 0)
376 XmToggleButtonSetState((Widget
) widget
, (Boolean
) toggle
, False
);
381 void wxToolBar::ClearTools()
383 wxNode
* node
= m_widgets
.First();
386 Widget button
= (Widget
) node
->Data();
387 XtDestroyWidget(button
);
393 wxToolBarBase::ClearTools();
396 void wxToolBar::DestroyPixmaps()
398 wxNode
* node
= m_pixmaps
.First();
401 Pixmap pixmap
= (Pixmap
) node
->Data();
402 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()), pixmap
);
408 // If pushedBitmap is NULL, a reversed version of bitmap is
409 // created and used as the pushed/toggled image.
410 // If toggle is TRUE, the button toggles between the two states.
412 wxToolBarTool
*wxToolBar::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
413 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
415 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, wxNullBitmap
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
416 tool
->m_clientData
= clientData
;
421 tool
->m_x
= m_xMargin
;
426 tool
->m_y
= m_yMargin
;
428 wxSize size
= GetToolSize();
429 tool
->SetSize(size
.x
, size
.y
);
431 m_tools
.Append((long)index
, tool
);
435 int wxToolBar::FindIndexForWidget(WXWidget w
)
437 wxNode
* node
= m_widgets
.First();
440 WXWidget widget
= (WXWidget
) node
->Data();
442 return (int) node
->GetKeyInteger();
448 WXWidget
wxToolBar::FindWidgetForIndex(int index
)
450 wxNode
* node
= m_widgets
.Find((long) index
);
454 return (WXWidget
) node
->Data();
457 WXWidget
wxToolBar::GetTopWidget() const
462 WXWidget
wxToolBar::GetClientWidget() const
467 WXWidget
wxToolBar::GetMainWidget() const
473 void wxToolButtonCallback (Widget w
, XtPointer clientData
,
476 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
477 int index
= toolBar
->FindIndexForWidget((WXWidget
) w
);
481 wxNode
*node
= toolBar
->GetTools().Find((long)index
);
484 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
485 if (tool
->m_isToggle
)
486 tool
->m_toggleState
= !tool
->m_toggleState
;
488 (void) toolBar
->OnLeftClick(index
, tool
->m_toggleState
);
494 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
495 XEvent
*event
, Boolean
*continue_to_dispatch
)
497 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
498 int delayMilli
= 800;
499 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
501 int index
= toolBar
->FindIndexForWidget((WXWidget
) w
);
505 wxNode
*node
= toolBar
->GetTools().Find((long)index
);
508 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
509 wxString
str(toolBar
->GetToolShortHelp(index
));
510 if (str
.IsNull() || str
== "")
513 if (!wxTheToolBarTimer
)
514 wxTheToolBarTimer
= new wxToolBarTimer
;
516 wxToolBarTimer::buttonWidget
= w
;
517 wxToolBarTimer::helpString
= str
;
520 /************************************************************/
521 /* Popup help label */
522 /************************************************************/
523 if (event
->type
== EnterNotify
)
525 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
527 XtDestroyWidget (wxToolBarTimer::help_popup
);
528 XtPopdown (wxToolBarTimer::help_popup
);
530 wxToolBarTimer::help_popup
= (Widget
) 0;
533 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
536 /************************************************************/
537 /* Popdown help label */
538 /************************************************************/
539 else if (event
->type
== LeaveNotify
)
541 if (wxTheToolBarTimer
)
542 wxTheToolBarTimer
->Stop();
543 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
545 XtDestroyWidget (wxToolBarTimer::help_popup
);
546 XtPopdown (wxToolBarTimer::help_popup
);
548 wxToolBarTimer::help_popup
= (Widget
) 0;
553 void wxToolBarTimer::Notify()
557 /************************************************************/
558 /* Create shell without window decorations */
559 /************************************************************/
560 help_popup
= XtVaCreatePopupShell ("shell",
561 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
564 /************************************************************/
565 /* Get absolute position on display of toolbar button */
566 /************************************************************/
567 XtTranslateCoords (buttonWidget
,
572 // Move the tooltip more or less above the button
573 int yOffset
= 20; // TODO: What should be really?
575 if (y
< yOffset
) y
= 0;
577 /************************************************************/
578 /* Set the position of the help popup */
579 /************************************************************/
580 XtVaSetValues (help_popup
,
585 /************************************************************/
586 /* Create help label */
587 /************************************************************/
588 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
589 XtVaCreateManagedWidget ("help_label",
590 xmLabelWidgetClass
, help_popup
,
591 XmNlabelString
, text
,
593 XmNforeground
, XtRString
, "black",
596 XmNbackground
, XtRString
, "LightGoldenrod",
597 strlen("LightGoldenrod")+1,
601 /************************************************************/
602 /* Popup help label */
603 /************************************************************/
604 XtPopup (help_popup
, XtGrabNone
);