]> git.saurik.com Git - wxWidgets.git/blob - src/motif/toolbar.cpp
added a WXDLLEXPORT
[wxWidgets.git] / src / motif / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: toolbar.cpp
3 // Purpose: wxToolBar
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "toolbar.h"
14 #endif
15
16 #include "wx/wx.h"
17 #include "wx/app.h"
18 #include "wx/timer.h"
19 #include "wx/toolbar.h"
20
21 #include <Xm/Xm.h>
22 #include <Xm/PushBG.h>
23 #include <Xm/PushB.h>
24 #include <Xm/Label.h>
25 #include <Xm/ToggleB.h>
26 #include <Xm/ToggleBG.h>
27 #include <Xm/Form.h>
28
29 #include "wx/motif/private.h"
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
33
34 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
35 END_EVENT_TABLE()
36 #endif
37
38 static void wxToolButtonCallback (Widget w, XtPointer clientData,
39 XtPointer ptr);
40 static void wxToolButtonPopupCallback (Widget w, XtPointer client_data,
41 XEvent *event, Boolean *continue_to_dispatch);
42
43 class wxToolBarTimer: public wxTimer
44 {
45 public:
46 wxToolBarTimer() { }
47 virtual void Notify();
48
49 static Widget help_popup;
50 static Widget buttonWidget;
51 static wxString helpString;
52 };
53
54 static wxToolBarTimer* wxTheToolBarTimer = (wxToolBarTimer*) NULL;
55
56 Widget wxToolBarTimer::help_popup = (Widget) 0;
57 Widget wxToolBarTimer::buttonWidget = (Widget) 0;
58 wxString wxToolBarTimer::helpString = "";
59
60 wxToolBar::wxToolBar():
61 m_widgets(wxKEY_INTEGER)
62 {
63 m_maxWidth = -1;
64 m_maxHeight = -1;
65 m_defaultWidth = 24;
66 m_defaultHeight = 22;
67 }
68
69 bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size,
70 long style, const wxString& name)
71 {
72 m_windowId = id;
73 m_maxWidth = -1;
74 m_maxHeight = -1;
75
76 m_defaultWidth = 24;
77 m_defaultHeight = 22;
78 SetName(name);
79 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
80 m_foregroundColour = parent->GetForegroundColour();
81 m_windowStyle = style;
82
83 SetParent(parent);
84
85 if (parent) parent->AddChild(this);
86
87 Widget parentWidget = (Widget) parent->GetClientWidget();
88
89 Widget toolbar = XtVaCreateManagedWidget("toolbar",
90 xmBulletinBoardWidgetClass, (Widget) parentWidget,
91 XmNmarginWidth, 0,
92 XmNmarginHeight, 0,
93 XmNresizePolicy, XmRESIZE_NONE,
94 NULL);
95 /*
96 Widget toolbar = XtVaCreateManagedWidget("toolbar",
97 xmFormWidgetClass, (Widget) m_clientWidget,
98 XmNtraversalOn, False,
99 XmNhorizontalSpacing, 0,
100 XmNverticalSpacing, 0,
101 XmNleftOffset, 0,
102 XmNrightOffset, 0,
103 XmNmarginWidth, 0,
104 XmNmarginHeight, 0,
105 NULL);
106 */
107
108 m_mainWidget = (WXWidget) toolbar;
109
110 m_font = parent->GetFont();
111 ChangeFont(FALSE);
112
113 SetCanAddEventHandler(TRUE);
114 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
115
116 ChangeBackgroundColour();
117
118 return TRUE;
119 }
120
121 wxToolBar::~wxToolBar()
122 {
123 delete wxTheToolBarTimer;
124 wxTheToolBarTimer = NULL;
125 ClearTools();
126 DestroyPixmaps();
127 }
128
129 bool wxToolBar::CreateTools()
130 {
131 if (m_tools.Number() == 0)
132 return FALSE;
133
134 // Separator spacing
135 const int separatorSize = GetToolSeparation(); // 8;
136 wxSize margins = GetToolMargins();
137 int marginX = margins.x;
138 int marginY = margins.y;
139
140 int currentX = marginX;
141 int currentY = marginY;
142
143 int buttonHeight = 0;
144
145 int currentSpacing = 0;
146
147 m_widgets.Clear();
148 Widget prevButton = (Widget) 0;
149 wxNode* node = m_tools.First();
150 while (node)
151 {
152 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
153
154 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
155 currentX += separatorSize;
156 else if (tool->m_bitmap1.Ok())
157 {
158 Widget button = (Widget) 0;
159
160 if (tool->m_isToggle)
161 {
162 button = XtVaCreateWidget("toggleButton",
163 xmToggleButtonWidgetClass, (Widget) m_mainWidget,
164 XmNx, currentX, XmNy, currentY,
165 // XmNpushButtonEnabled, True,
166 XmNmultiClick, XmMULTICLICK_KEEP,
167 XmNlabelType, XmPIXMAP,
168 NULL);
169 XtAddCallback ((Widget) button, XmNvalueChangedCallback, (XtCallbackProc) wxToolButtonCallback,
170 (XtPointer) this);
171
172 XtVaSetValues ((Widget) button,
173 XmNselectColor, m_backgroundColour.AllocColour(XtDisplay((Widget) button)),
174 NULL);
175 }
176 else
177 {
178 button = XtVaCreateWidget("button",
179 xmPushButtonWidgetClass, (Widget) m_mainWidget,
180 XmNx, currentX, XmNy, currentY,
181 XmNpushButtonEnabled, True,
182 XmNmultiClick, XmMULTICLICK_KEEP,
183 XmNlabelType, XmPIXMAP,
184 NULL);
185 XtAddCallback (button,
186 XmNactivateCallback, (XtCallbackProc) wxToolButtonCallback,
187 (XtPointer) this);
188 }
189
190 DoChangeBackgroundColour((WXWidget) button, m_backgroundColour, TRUE);
191
192 // For each button, if there is a mask, we must create
193 // a new wxBitmap that has the correct background colour
194 // for the button. Otherwise the background will just be
195 // e.g. black if a transparent XPM has been loaded.
196 wxBitmap originalBitmap = tool->m_bitmap1;
197
198 if (tool->m_bitmap1.GetMask())
199 {
200 int backgroundPixel;
201 XtVaGetValues(button, XmNbackground, &backgroundPixel,
202 NULL);
203
204
205 wxColour col;
206 col.SetPixel(backgroundPixel);
207
208 wxBitmap newBitmap = wxCreateMaskedBitmap(tool->m_bitmap1, col);
209
210 tool->m_bitmap1 = newBitmap;
211 }
212
213 // Create a selected/toggled bitmap. If there isn't a m_bitmap2,
214 // we need to create it (with a darker, selected background)
215 int backgroundPixel;
216 if (tool->m_isToggle)
217 XtVaGetValues(button, XmNselectColor, &backgroundPixel,
218 NULL);
219 else
220 XtVaGetValues(button, XmNarmColor, &backgroundPixel,
221 NULL);
222
223 wxColour col;
224 col.SetPixel(backgroundPixel);
225
226 if (tool->m_bitmap2.Ok() && tool->m_bitmap2.GetMask())
227 {
228 // Use what's there
229 wxBitmap newBitmap = wxCreateMaskedBitmap(tool->m_bitmap2, col);
230 tool->m_bitmap2 = newBitmap;
231 }
232 else
233 {
234 // Use unselected bitmap
235 if (originalBitmap.GetMask())
236 {
237 wxBitmap newBitmap = wxCreateMaskedBitmap(originalBitmap, col);
238 tool->m_bitmap2 = newBitmap;
239 }
240 else
241 tool->m_bitmap2 = tool->m_bitmap1;
242 }
243
244 Pixmap pixmap = (Pixmap) tool->m_bitmap1.GetPixmap();
245 Pixmap insensPixmap = (Pixmap) tool->m_bitmap1.GetInsensPixmap();
246
247 if (tool->m_isToggle)
248 {
249 // Toggle button
250 Pixmap pixmap2 = (Pixmap) 0;
251 Pixmap insensPixmap2 = (Pixmap) 0;
252
253 // If there's a bitmap for the toggled state, use it,
254 // otherwise generate one.
255 if (tool->m_bitmap2.Ok())
256 {
257 pixmap2 = (Pixmap) tool->m_bitmap2.GetPixmap();
258 insensPixmap2 = (Pixmap) tool->m_bitmap2.GetInsensPixmap();
259 }
260 else
261 {
262 pixmap2 = (Pixmap) tool->m_bitmap1.GetArmPixmap(button);
263 insensPixmap2 = XCreateInsensitivePixmap((Display*) wxGetDisplay(), pixmap2);
264 m_pixmaps.Append((wxObject*) insensPixmap2); // Store for later deletion
265 }
266 XtVaSetValues (button,
267 XmNindicatorOn, False,
268 XmNshadowThickness, 2,
269 // XmNborderWidth, 0,
270 // XmNspacing, 0,
271 XmNmarginWidth, 0,
272 XmNmarginHeight, 0,
273 XmNfillOnSelect, True,
274 XmNlabelPixmap, pixmap,
275 XmNselectPixmap, pixmap2,
276 XmNlabelInsensitivePixmap, insensPixmap,
277 XmNselectInsensitivePixmap, insensPixmap2,
278 XmNlabelType, XmPIXMAP,
279 NULL);
280 }
281 else
282 {
283 Pixmap pixmap2 = (Pixmap) 0;
284
285 // If there's a bitmap for the armed state, use it,
286 // otherwise generate one.
287 if (tool->m_bitmap2.Ok())
288 {
289 pixmap2 = (Pixmap) tool->m_bitmap2.GetPixmap();
290 }
291 else
292 {
293 pixmap2 = (Pixmap) tool->m_bitmap1.GetArmPixmap(button);
294
295 }
296 // Normal button
297 XtVaSetValues(button,
298 XmNlabelPixmap, pixmap,
299 XmNlabelInsensitivePixmap, insensPixmap,
300 XmNarmPixmap, pixmap2,
301 NULL);
302 }
303 XtManageChild(button);
304
305 Dimension width, height;
306 XtVaGetValues(button, XmNwidth, & width, XmNheight, & height,
307 NULL);
308 currentX += width + marginX;
309 buttonHeight = wxMax(buttonHeight, height);
310
311 XtAddEventHandler (button, EnterWindowMask | LeaveWindowMask,
312 False, wxToolButtonPopupCallback, (XtPointer) this);
313 m_widgets.Append(tool->m_index, (wxObject*) button);
314
315 prevButton = button;
316 currentSpacing = 0;
317 }
318 node = node->Next();
319 }
320
321 SetSize(-1, -1, currentX, buttonHeight + 2*marginY);
322
323 return TRUE;
324 }
325
326 void wxToolBar::SetToolBitmapSize(const wxSize& size)
327 {
328 // TODO not necessary?
329 m_defaultWidth = size.x; m_defaultHeight = size.y;
330 }
331
332 wxSize wxToolBar::GetMaxSize() const
333 {
334 int w, h;
335 GetSize(& w, & h);
336
337 return wxSize(w, h);
338 }
339
340 // The button size is bigger than the bitmap size
341 wxSize wxToolBar::GetToolSize() const
342 {
343 // TODO not necessary?
344 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
345 }
346
347 void wxToolBar::EnableTool(int toolIndex, bool enable)
348 {
349 wxNode *node = m_tools.Find((long)toolIndex);
350 if (node)
351 {
352 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
353 tool->m_enabled = enable;
354
355 WXWidget widget = FindWidgetForIndex(tool->m_index);
356 if (widget == (WXWidget) 0)
357 return;
358
359 XtSetSensitive((Widget) widget, (Boolean) enable);
360 }
361 }
362
363 void wxToolBar::ToggleTool(int toolIndex, bool toggle)
364 {
365 wxNode *node = m_tools.Find((long)toolIndex);
366 if (node)
367 {
368 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
369 if (tool->m_isToggle)
370 {
371 tool->m_toggleState = toggle;
372
373 WXWidget widget = FindWidgetForIndex(tool->m_index);
374 if (widget == (WXWidget) 0)
375 return;
376
377 XmToggleButtonSetState((Widget) widget, (Boolean) toggle, False);
378 }
379 }
380 }
381
382 void wxToolBar::ClearTools()
383 {
384 wxNode* node = m_widgets.First();
385 while (node)
386 {
387 Widget button = (Widget) node->Data();
388 XtDestroyWidget(button);
389 node = node->Next();
390 }
391 m_widgets.Clear();
392 DestroyPixmaps();
393
394 wxToolBarBase::ClearTools();
395 }
396
397 void wxToolBar::DestroyPixmaps()
398 {
399 wxNode* node = m_pixmaps.First();
400 while (node)
401 {
402 Pixmap pixmap = (Pixmap) node->Data();
403 XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()), pixmap);
404 node = node->Next();
405 }
406 m_pixmaps.Clear();
407 }
408
409 // If pushedBitmap is NULL, a reversed version of bitmap is
410 // created and used as the pushed/toggled image.
411 // If toggle is TRUE, the button toggles between the two states.
412
413 wxToolBarTool *wxToolBar::AddTool(int index, const wxBitmap& bitmap, const wxBitmap& WXUNUSED(pushedBitmap),
414 bool toggle, long xPos, long yPos, wxObject *clientData, const wxString& helpString1, const wxString& helpString2)
415 {
416 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap, toggle, xPos, yPos, helpString1, helpString2);
417 tool->m_clientData = clientData;
418
419 if (xPos > -1)
420 tool->m_x = xPos;
421 else
422 tool->m_x = m_xMargin;
423
424 if (yPos > -1)
425 tool->m_y = yPos;
426 else
427 tool->m_y = m_yMargin;
428
429 wxSize size = GetToolSize();
430 tool->SetSize(size.x, size.y);
431
432 m_tools.Append((long)index, tool);
433 return tool;
434 }
435
436 int wxToolBar::FindIndexForWidget(WXWidget w)
437 {
438 wxNode* node = m_widgets.First();
439 while (node)
440 {
441 WXWidget widget = (WXWidget) node->Data();
442 if (widget == w)
443 return (int) node->GetKeyInteger();
444 node = node->Next();
445 }
446 return -1;
447 }
448
449 WXWidget wxToolBar::FindWidgetForIndex(int index)
450 {
451 wxNode* node = m_widgets.Find((long) index);
452 if (!node)
453 return (WXWidget) 0;
454 else
455 return (WXWidget) node->Data();
456 }
457
458 WXWidget wxToolBar::GetTopWidget() const
459 {
460 return m_mainWidget;
461 }
462
463 WXWidget wxToolBar::GetClientWidget() const
464 {
465 return m_mainWidget;
466 }
467
468 WXWidget wxToolBar::GetMainWidget() const
469 {
470 return m_mainWidget;
471 }
472
473
474 void wxToolButtonCallback (Widget w, XtPointer clientData,
475 XtPointer WXUNUSED(ptr))
476 {
477 wxToolBar *toolBar = (wxToolBar *) clientData;
478 int index = toolBar->FindIndexForWidget((WXWidget) w);
479
480 if (index != -1)
481 {
482 wxNode *node = toolBar->GetTools().Find((long)index);
483 if (!node)
484 return;
485 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
486 if (tool->m_isToggle)
487 tool->m_toggleState = !tool->m_toggleState;
488
489 (void) toolBar->OnLeftClick(index, tool->m_toggleState);
490 }
491
492 }
493
494
495 static void wxToolButtonPopupCallback (Widget w, XtPointer client_data,
496 XEvent *event, Boolean *WXUNUSED(continue_to_dispatch))
497 {
498 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
499 int delayMilli = 800;
500 wxToolBar* toolBar = (wxToolBar*) client_data;
501
502 int index = toolBar->FindIndexForWidget((WXWidget) w);
503
504 if (index != -1)
505 {
506 wxNode *node = toolBar->GetTools().Find((long)index);
507 if (!node)
508 return;
509 wxString str(toolBar->GetToolShortHelp(index));
510 if (str.IsNull() || str == "")
511 return;
512
513 if (!wxTheToolBarTimer)
514 wxTheToolBarTimer = new wxToolBarTimer;
515
516 wxToolBarTimer::buttonWidget = w;
517 wxToolBarTimer::helpString = str;
518
519
520 /************************************************************/
521 /* Popup help label */
522 /************************************************************/
523 if (event->type == EnterNotify)
524 {
525 if (wxToolBarTimer::help_popup != (Widget) 0)
526 {
527 XtDestroyWidget (wxToolBarTimer::help_popup);
528 XtPopdown (wxToolBarTimer::help_popup);
529 }
530 wxToolBarTimer::help_popup = (Widget) 0;
531
532 // One shot
533 wxTheToolBarTimer->Start(delayMilli, TRUE);
534
535 }
536 /************************************************************/
537 /* Popdown help label */
538 /************************************************************/
539 else if (event->type == LeaveNotify)
540 {
541 if (wxTheToolBarTimer)
542 wxTheToolBarTimer->Stop();
543 if (wxToolBarTimer::help_popup != (Widget) 0)
544 {
545 XtDestroyWidget (wxToolBarTimer::help_popup);
546 XtPopdown (wxToolBarTimer::help_popup);
547 }
548 wxToolBarTimer::help_popup = (Widget) 0;
549 }
550 }
551 }
552
553 void wxToolBarTimer::Notify()
554 {
555 Position x, y;
556
557 /************************************************************/
558 /* Create shell without window decorations */
559 /************************************************************/
560 help_popup = XtVaCreatePopupShell ("shell",
561 overrideShellWidgetClass, (Widget) wxTheApp->GetTopLevelWidget(),
562 NULL);
563
564 /************************************************************/
565 /* Get absolute position on display of toolbar button */
566 /************************************************************/
567 XtTranslateCoords (buttonWidget,
568 (Position) 0,
569 (Position) 0,
570 &x, &y);
571
572 // Move the tooltip more or less above the button
573 int yOffset = 20; // TODO: What should be really?
574 y -= yOffset;
575 if (y < yOffset) y = 0;
576
577 /************************************************************/
578 /* Set the position of the help popup */
579 /************************************************************/
580 XtVaSetValues (help_popup,
581 XmNx, (Position) x,
582 XmNy, (Position) y,
583 NULL);
584
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,
592 XtVaTypedArg,
593 XmNforeground, XtRString, "black",
594 strlen("black")+1,
595 XtVaTypedArg,
596 XmNbackground, XtRString, "LightGoldenrod",
597 strlen("LightGoldenrod")+1,
598 NULL);
599 XmStringFree (text);
600
601 /************************************************************/
602 /* Popup help label */
603 /************************************************************/
604 XtPopup (help_popup, XtGrabNone);
605 }
606