1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "toolbar.h"
19 #include "wx/motif/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 xmFormWidgetClass
, parentWidget
,
90 XmNtraversalOn
, False
,
91 XmNhorizontalSpacing
, 0,
92 XmNverticalSpacing
, 0,
99 m_mainWidget
= (WXWidget
) toolbar
;
101 m_windowFont
= parent
->GetFont();
104 SetCanAddEventHandler(TRUE
);
105 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
107 ChangeBackgroundColour();
112 wxToolBar::~wxToolBar()
114 delete wxTheToolBarTimer
;
115 wxTheToolBarTimer
= NULL
;
120 bool wxToolBar::CreateTools()
122 if (m_tools
.Number() == 0)
126 const int separatorSize
= GetToolSeparation(); // 8;
128 int currentSpacing
= 0;
131 Widget prevButton
= (Widget
) 0;
132 wxNode
* node
= m_tools
.First();
135 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
137 if (tool
->m_toolStyle
== wxTOOL_STYLE_SEPARATOR
)
138 currentSpacing
= separatorSize
;
139 else if (tool
->m_bitmap1
.Ok())
141 Widget button
= (Widget
) 0;
143 if (tool
->m_isToggle
)
145 button
= XtVaCreateManagedWidget("toggleButton",
146 xmToggleButtonWidgetClass
, (Widget
) m_mainWidget
,
147 XmNleftAttachment
, (prevButton
== (Widget
) 0) ? XmATTACH_FORM
: XmATTACH_WIDGET
,
148 XmNleftWidget
, (prevButton
== (Widget
) 0) ? NULL
: prevButton
,
149 XmNleftOffset
, currentSpacing
,
150 XmNtopAttachment
, XmATTACH_FORM
,
151 // XmNpushButtonEnabled, True,
152 XmNmultiClick
, XmMULTICLICK_KEEP
,
153 XmNlabelType
, XmPIXMAP
,
155 XtAddCallback ((Widget
) button
, XmNvalueChangedCallback
, (XtCallbackProc
) wxToolButtonCallback
,
160 button
= XtVaCreateManagedWidget("button",
161 xmPushButtonWidgetClass
, (Widget
) m_mainWidget
,
162 XmNleftAttachment
, (prevButton
== (Widget
) 0) ? XmATTACH_FORM
: XmATTACH_WIDGET
,
163 XmNleftWidget
, (prevButton
== (Widget
) 0) ? NULL
: prevButton
,
164 XmNleftOffset
, currentSpacing
,
165 XmNtopAttachment
, XmATTACH_FORM
,
166 XmNpushButtonEnabled
, True
,
167 XmNmultiClick
, XmMULTICLICK_KEEP
,
168 XmNlabelType
, XmPIXMAP
,
170 XtAddCallback (button
,
171 XmNactivateCallback
, (XtCallbackProc
) wxToolButtonCallback
,
175 // For each button, if there is a mask, we must create
176 // a new wxBitmap that has the correct background colour
177 // for the button. Otherwise the background will just be
178 // e.g. black if a transparent XPM has been loaded.
179 wxBitmap originalBitmap
= tool
->m_bitmap1
;
181 if (tool
->m_bitmap1
.GetMask())
184 XtVaGetValues(button
, XmNbackground
, &backgroundPixel
,
189 col
.SetPixel(backgroundPixel
);
191 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->m_bitmap1
, col
);
193 tool
->m_bitmap1
= newBitmap
;
196 // Create a selected/toggled bitmap. If there isn't a m_bitmap2,
197 // we need to create it (with a darker, selected background)
199 if (tool
->m_isToggle
)
200 XtVaGetValues(button
, XmNselectColor
, &backgroundPixel
,
203 XtVaGetValues(button
, XmNarmColor
, &backgroundPixel
,
207 col
.SetPixel(backgroundPixel
);
209 if (tool
->m_bitmap2
.Ok() && tool
->m_bitmap2
.GetMask())
212 wxBitmap newBitmap
= wxCreateMaskedBitmap(tool
->m_bitmap2
, col
);
213 tool
->m_bitmap2
= newBitmap
;
217 // Use unselected bitmap
218 if (originalBitmap
.GetMask())
220 wxBitmap newBitmap
= wxCreateMaskedBitmap(originalBitmap
, col
);
221 tool
->m_bitmap2
= newBitmap
;
224 tool
->m_bitmap2
= tool
->m_bitmap1
;
227 Pixmap pixmap
= (Pixmap
) tool
->m_bitmap1
.GetPixmap();
228 Pixmap insensPixmap
= (Pixmap
) tool
->m_bitmap1
.GetInsensPixmap();
230 if (tool
->m_isToggle
)
233 Pixmap pixmap2
= (Pixmap
) 0;
234 Pixmap insensPixmap2
= (Pixmap
) 0;
236 // If there's a bitmap for the toggled state, use it,
237 // otherwise generate one.
238 if (tool
->m_bitmap2
.Ok())
240 pixmap2
= (Pixmap
) tool
->m_bitmap2
.GetPixmap();
241 insensPixmap2
= (Pixmap
) tool
->m_bitmap2
.GetInsensPixmap();
245 pixmap2
= (Pixmap
) tool
->m_bitmap1
.GetArmPixmap(button
);
246 insensPixmap2
= XCreateInsensitivePixmap((Display
*) wxGetDisplay(), pixmap2
);
247 m_pixmaps
.Append((wxObject
*) insensPixmap2
); // Store for later deletion
249 XtVaSetValues (button
,
250 XmNindicatorOn
, False
,
251 XmNshadowThickness
, 2,
252 // XmNborderWidth, 0,
256 XmNfillOnSelect
, True
,
257 XmNlabelPixmap
, pixmap
,
258 XmNselectPixmap
, pixmap2
,
259 XmNlabelInsensitivePixmap
, insensPixmap
,
260 XmNselectInsensitivePixmap
, insensPixmap2
,
261 XmNlabelType
, XmPIXMAP
,
266 Pixmap pixmap2
= (Pixmap
) 0;
268 // If there's a bitmap for the armed state, use it,
269 // otherwise generate one.
270 if (tool
->m_bitmap2
.Ok())
272 pixmap2
= (Pixmap
) tool
->m_bitmap2
.GetPixmap();
276 pixmap2
= (Pixmap
) tool
->m_bitmap1
.GetArmPixmap(button
);
280 XtVaSetValues(button
,
281 XmNlabelPixmap
, pixmap
,
282 XmNlabelInsensitivePixmap
, insensPixmap
,
283 XmNarmPixmap
, pixmap2
,
287 XtAddEventHandler (button
, EnterWindowMask
| LeaveWindowMask
,
288 False
, wxToolButtonPopupCallback
, (XtPointer
) this);
289 m_widgets
.Append(tool
->m_index
, (wxObject
*) button
);
300 void wxToolBar::SetToolBitmapSize(const wxSize
& size
)
302 // TODO not necessary?
303 m_defaultWidth
= size
.x
; m_defaultHeight
= size
.y
;
306 wxSize
wxToolBar::GetMaxSize() const
314 // The button size is bigger than the bitmap size
315 wxSize
wxToolBar::GetToolSize() const
317 // TODO not necessary?
318 return wxSize(m_defaultWidth
+ 8, m_defaultHeight
+ 7);
321 void wxToolBar::EnableTool(int toolIndex
, bool enable
)
323 wxNode
*node
= m_tools
.Find((long)toolIndex
);
326 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
327 tool
->m_enabled
= enable
;
329 WXWidget widget
= FindWidgetForIndex(tool
->m_index
);
330 if (widget
== (WXWidget
) 0)
333 XtSetSensitive((Widget
) widget
, (Boolean
) enable
);
337 void wxToolBar::ToggleTool(int toolIndex
, bool toggle
)
339 wxNode
*node
= m_tools
.Find((long)toolIndex
);
342 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
343 if (tool
->m_isToggle
)
345 tool
->m_toggleState
= toggle
;
347 WXWidget widget
= FindWidgetForIndex(tool
->m_index
);
348 if (widget
== (WXWidget
) 0)
351 XmToggleButtonSetState((Widget
) widget
, (Boolean
) toggle
, False
);
356 void wxToolBar::ClearTools()
358 wxNode
* node
= m_widgets
.First();
361 Widget button
= (Widget
) node
->Data();
362 XtDestroyWidget(button
);
368 wxToolBarBase::ClearTools();
371 void wxToolBar::DestroyPixmaps()
373 wxNode
* node
= m_pixmaps
.First();
376 Pixmap pixmap
= (Pixmap
) node
->Data();
377 XmDestroyPixmap (DefaultScreenOfDisplay ((Display
*) GetXDisplay()), pixmap
);
383 // If pushedBitmap is NULL, a reversed version of bitmap is
384 // created and used as the pushed/toggled image.
385 // If toggle is TRUE, the button toggles between the two states.
387 wxToolBarTool
*wxToolBar::AddTool(int index
, const wxBitmap
& bitmap
, const wxBitmap
& pushedBitmap
,
388 bool toggle
, long xPos
, long yPos
, wxObject
*clientData
, const wxString
& helpString1
, const wxString
& helpString2
)
390 wxToolBarTool
*tool
= new wxToolBarTool(index
, bitmap
, (wxBitmap
*)NULL
, toggle
, xPos
, yPos
, helpString1
, helpString2
);
391 tool
->m_clientData
= clientData
;
396 tool
->m_x
= m_xMargin
;
401 tool
->m_y
= m_yMargin
;
403 tool
->SetSize(GetDefaultButtonWidth(), GetDefaultButtonHeight());
405 m_tools
.Append((long)index
, tool
);
409 int wxToolBar::FindIndexForWidget(WXWidget w
)
411 wxNode
* node
= m_widgets
.First();
414 WXWidget widget
= (WXWidget
) node
->Data();
416 return (int) node
->key
.integer
;
422 WXWidget
wxToolBar::FindWidgetForIndex(int index
)
424 wxNode
* node
= m_widgets
.Find((long) index
);
428 return (WXWidget
) node
->Data();
431 void wxToolButtonCallback (Widget w
, XtPointer clientData
,
434 wxToolBar
*toolBar
= (wxToolBar
*) clientData
;
435 int index
= toolBar
->FindIndexForWidget((WXWidget
) w
);
439 wxNode
*node
= toolBar
->GetTools().Find((long)index
);
442 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
443 if (tool
->m_isToggle
)
444 tool
->m_toggleState
= toolBar
->GetToolState(index
);
446 (void) toolBar
->OnLeftClick(index
, tool
->m_toggleState
);
452 static void wxToolButtonPopupCallback (Widget w
, XtPointer client_data
,
453 XEvent
*event
, Boolean
*continue_to_dispatch
)
455 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
456 int delayMilli
= 800;
457 wxToolBar
* toolBar
= (wxToolBar
*) client_data
;
459 int index
= toolBar
->FindIndexForWidget((WXWidget
) w
);
463 wxNode
*node
= toolBar
->GetTools().Find((long)index
);
466 wxToolBarTool
*tool
= (wxToolBarTool
*)node
->Data();
467 wxString
str(toolBar
->GetToolShortHelp(index
));
468 if (str
.IsNull() || str
== "")
471 if (!wxTheToolBarTimer
)
472 wxTheToolBarTimer
= new wxToolBarTimer
;
474 wxToolBarTimer::buttonWidget
= w
;
475 wxToolBarTimer::helpString
= str
;
478 /************************************************************/
479 /* Popup help label */
480 /************************************************************/
481 if (event
->type
== EnterNotify
)
483 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
485 XtDestroyWidget (wxToolBarTimer::help_popup
);
486 XtPopdown (wxToolBarTimer::help_popup
);
488 wxToolBarTimer::help_popup
= (Widget
) 0;
491 wxTheToolBarTimer
->Start(delayMilli
, TRUE
);
494 /************************************************************/
495 /* Popdown help label */
496 /************************************************************/
497 else if (event
->type
== LeaveNotify
)
499 if (wxTheToolBarTimer
)
500 wxTheToolBarTimer
->Stop();
501 if (wxToolBarTimer::help_popup
!= (Widget
) 0)
503 XtDestroyWidget (wxToolBarTimer::help_popup
);
504 XtPopdown (wxToolBarTimer::help_popup
);
506 wxToolBarTimer::help_popup
= (Widget
) 0;
511 void wxToolBarTimer::Notify()
515 /************************************************************/
516 /* Create shell without window decorations */
517 /************************************************************/
518 help_popup
= XtVaCreatePopupShell ("shell",
519 overrideShellWidgetClass
, (Widget
) wxTheApp
->GetTopLevelWidget(),
522 /************************************************************/
523 /* Get absolute position on display of toolbar button */
524 /************************************************************/
525 XtTranslateCoords (buttonWidget
,
530 // Move the tooltip more or less above the button
531 int yOffset
= 20; // TODO: What should be really?
533 if (y
< yOffset
) y
= 0;
535 /************************************************************/
536 /* Set the position of the help popup */
537 /************************************************************/
538 XtVaSetValues (help_popup
,
543 /************************************************************/
544 /* Create help label */
545 /************************************************************/
546 XmString text
= XmStringCreateSimple ((char*) (const char*) helpString
);
547 XtVaCreateManagedWidget ("help_label",
548 xmLabelWidgetClass
, help_popup
,
549 XmNlabelString
, text
,
551 XmNforeground
, XtRString
, "black",
554 XmNbackground
, XtRString
, "LightGoldenrod",
555 strlen("LightGoldenrod")+1,
559 /************************************************************/
560 /* Popup help label */
561 /************************************************************/
562 XtPopup (help_popup
, XtGrabNone
);