]> git.saurik.com Git - wxWidgets.git/blob - src/motif/toolbar.cpp
call to pthread_attr_setscope() added (disabled for now) and several other small...
[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_maxWidth = -1;
73 m_maxHeight = -1;
74
75 m_defaultWidth = 24;
76 m_defaultHeight = 22;
77 SetName(name);
78 m_backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
79 m_foregroundColour = parent->GetForegroundColour();
80 m_windowStyle = style;
81
82 SetParent(parent);
83
84 if (parent) parent->AddChild(this);
85
86 Widget parentWidget = (Widget) parent->GetClientWidget();
87
88 Widget toolbar = XtVaCreateManagedWidget("toolbar",
89 xmBulletinBoardWidgetClass, (Widget) parentWidget,
90 XmNmarginWidth, 0,
91 XmNmarginHeight, 0,
92 XmNresizePolicy, XmRESIZE_NONE,
93 NULL);
94 /*
95 Widget toolbar = XtVaCreateManagedWidget("toolbar",
96 xmFormWidgetClass, (Widget) m_clientWidget,
97 XmNtraversalOn, False,
98 XmNhorizontalSpacing, 0,
99 XmNverticalSpacing, 0,
100 XmNleftOffset, 0,
101 XmNrightOffset, 0,
102 XmNmarginWidth, 0,
103 XmNmarginHeight, 0,
104 NULL);
105 */
106
107 m_mainWidget = (WXWidget) toolbar;
108
109 m_windowFont = parent->GetFont();
110 ChangeFont(FALSE);
111
112 SetCanAddEventHandler(TRUE);
113 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
114
115 ChangeBackgroundColour();
116
117 return TRUE;
118 }
119
120 wxToolBar::~wxToolBar()
121 {
122 delete wxTheToolBarTimer;
123 wxTheToolBarTimer = NULL;
124 ClearTools();
125 DestroyPixmaps();
126 }
127
128 bool wxToolBar::CreateTools()
129 {
130 if (m_tools.Number() == 0)
131 return FALSE;
132
133 // Separator spacing
134 const int separatorSize = GetToolSeparation(); // 8;
135 wxSize margins = GetToolMargins();
136 int marginX = margins.x;
137 int marginY = margins.y;
138
139 int currentX = marginX;
140 int currentY = marginY;
141
142 int buttonHeight = 0;
143
144 int currentSpacing = 0;
145
146 m_widgets.Clear();
147 Widget prevButton = (Widget) 0;
148 wxNode* node = m_tools.First();
149 while (node)
150 {
151 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
152
153 if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR)
154 currentX += separatorSize;
155 else if (tool->m_bitmap1.Ok())
156 {
157 Widget button = (Widget) 0;
158
159 if (tool->m_isToggle)
160 {
161 button = XtVaCreateWidget("toggleButton",
162 xmToggleButtonWidgetClass, (Widget) m_mainWidget,
163 XmNx, currentX, XmNy, currentY,
164 // XmNpushButtonEnabled, True,
165 XmNmultiClick, XmMULTICLICK_KEEP,
166 XmNlabelType, XmPIXMAP,
167 NULL);
168 XtAddCallback ((Widget) button, XmNvalueChangedCallback, (XtCallbackProc) wxToolButtonCallback,
169 (XtPointer) this);
170
171 XtVaSetValues ((Widget) button,
172 XmNselectColor, m_backgroundColour.AllocColour(XtDisplay((Widget) button)),
173 NULL);
174 }
175 else
176 {
177 button = XtVaCreateWidget("button",
178 xmPushButtonWidgetClass, (Widget) m_mainWidget,
179 XmNx, currentX, XmNy, currentY,
180 XmNpushButtonEnabled, True,
181 XmNmultiClick, XmMULTICLICK_KEEP,
182 XmNlabelType, XmPIXMAP,
183 NULL);
184 XtAddCallback (button,
185 XmNactivateCallback, (XtCallbackProc) wxToolButtonCallback,
186 (XtPointer) this);
187 }
188
189 DoChangeBackgroundColour((WXWidget) button, m_backgroundColour, TRUE);
190
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;
196
197 if (tool->m_bitmap1.GetMask())
198 {
199 int backgroundPixel;
200 XtVaGetValues(button, XmNbackground, &backgroundPixel,
201 NULL);
202
203
204 wxColour col;
205 col.SetPixel(backgroundPixel);
206
207 wxBitmap newBitmap = wxCreateMaskedBitmap(tool->m_bitmap1, col);
208
209 tool->m_bitmap1 = newBitmap;
210 }
211
212 // Create a selected/toggled bitmap. If there isn't a m_bitmap2,
213 // we need to create it (with a darker, selected background)
214 int backgroundPixel;
215 if (tool->m_isToggle)
216 XtVaGetValues(button, XmNselectColor, &backgroundPixel,
217 NULL);
218 else
219 XtVaGetValues(button, XmNarmColor, &backgroundPixel,
220 NULL);
221
222 wxColour col;
223 col.SetPixel(backgroundPixel);
224
225 if (tool->m_bitmap2.Ok() && tool->m_bitmap2.GetMask())
226 {
227 // Use what's there
228 wxBitmap newBitmap = wxCreateMaskedBitmap(tool->m_bitmap2, col);
229 tool->m_bitmap2 = newBitmap;
230 }
231 else
232 {
233 // Use unselected bitmap
234 if (originalBitmap.GetMask())
235 {
236 wxBitmap newBitmap = wxCreateMaskedBitmap(originalBitmap, col);
237 tool->m_bitmap2 = newBitmap;
238 }
239 else
240 tool->m_bitmap2 = tool->m_bitmap1;
241 }
242
243 Pixmap pixmap = (Pixmap) tool->m_bitmap1.GetPixmap();
244 Pixmap insensPixmap = (Pixmap) tool->m_bitmap1.GetInsensPixmap();
245
246 if (tool->m_isToggle)
247 {
248 // Toggle button
249 Pixmap pixmap2 = (Pixmap) 0;
250 Pixmap insensPixmap2 = (Pixmap) 0;
251
252 // If there's a bitmap for the toggled state, use it,
253 // otherwise generate one.
254 if (tool->m_bitmap2.Ok())
255 {
256 pixmap2 = (Pixmap) tool->m_bitmap2.GetPixmap();
257 insensPixmap2 = (Pixmap) tool->m_bitmap2.GetInsensPixmap();
258 }
259 else
260 {
261 pixmap2 = (Pixmap) tool->m_bitmap1.GetArmPixmap(button);
262 insensPixmap2 = XCreateInsensitivePixmap((Display*) wxGetDisplay(), pixmap2);
263 m_pixmaps.Append((wxObject*) insensPixmap2); // Store for later deletion
264 }
265 XtVaSetValues (button,
266 XmNindicatorOn, False,
267 XmNshadowThickness, 2,
268 // XmNborderWidth, 0,
269 // XmNspacing, 0,
270 XmNmarginWidth, 0,
271 XmNmarginHeight, 0,
272 XmNfillOnSelect, True,
273 XmNlabelPixmap, pixmap,
274 XmNselectPixmap, pixmap2,
275 XmNlabelInsensitivePixmap, insensPixmap,
276 XmNselectInsensitivePixmap, insensPixmap2,
277 XmNlabelType, XmPIXMAP,
278 NULL);
279 }
280 else
281 {
282 Pixmap pixmap2 = (Pixmap) 0;
283
284 // If there's a bitmap for the armed state, use it,
285 // otherwise generate one.
286 if (tool->m_bitmap2.Ok())
287 {
288 pixmap2 = (Pixmap) tool->m_bitmap2.GetPixmap();
289 }
290 else
291 {
292 pixmap2 = (Pixmap) tool->m_bitmap1.GetArmPixmap(button);
293
294 }
295 // Normal button
296 XtVaSetValues(button,
297 XmNlabelPixmap, pixmap,
298 XmNlabelInsensitivePixmap, insensPixmap,
299 XmNarmPixmap, pixmap2,
300 NULL);
301 }
302 XtManageChild(button);
303
304 Dimension width, height;
305 XtVaGetValues(button, XmNwidth, & width, XmNheight, & height,
306 NULL);
307 currentX += width + marginX;
308 buttonHeight = wxMax(buttonHeight, height);
309
310 XtAddEventHandler (button, EnterWindowMask | LeaveWindowMask,
311 False, wxToolButtonPopupCallback, (XtPointer) this);
312 m_widgets.Append(tool->m_index, (wxObject*) button);
313
314 prevButton = button;
315 currentSpacing = 0;
316 }
317 node = node->Next();
318 }
319
320 SetSize(-1, -1, currentX, buttonHeight + 2*marginY);
321
322 return TRUE;
323 }
324
325 void wxToolBar::SetToolBitmapSize(const wxSize& size)
326 {
327 // TODO not necessary?
328 m_defaultWidth = size.x; m_defaultHeight = size.y;
329 }
330
331 wxSize wxToolBar::GetMaxSize() const
332 {
333 int w, h;
334 GetSize(& w, & h);
335
336 return wxSize(w, h);
337 }
338
339 // The button size is bigger than the bitmap size
340 wxSize wxToolBar::GetToolSize() const
341 {
342 // TODO not necessary?
343 return wxSize(m_defaultWidth + 8, m_defaultHeight + 7);
344 }
345
346 void wxToolBar::EnableTool(int toolIndex, bool enable)
347 {
348 wxNode *node = m_tools.Find((long)toolIndex);
349 if (node)
350 {
351 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
352 tool->m_enabled = enable;
353
354 WXWidget widget = FindWidgetForIndex(tool->m_index);
355 if (widget == (WXWidget) 0)
356 return;
357
358 XtSetSensitive((Widget) widget, (Boolean) enable);
359 }
360 }
361
362 void wxToolBar::ToggleTool(int toolIndex, bool toggle)
363 {
364 wxNode *node = m_tools.Find((long)toolIndex);
365 if (node)
366 {
367 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
368 if (tool->m_isToggle)
369 {
370 tool->m_toggleState = toggle;
371
372 WXWidget widget = FindWidgetForIndex(tool->m_index);
373 if (widget == (WXWidget) 0)
374 return;
375
376 XmToggleButtonSetState((Widget) widget, (Boolean) toggle, False);
377 }
378 }
379 }
380
381 void wxToolBar::ClearTools()
382 {
383 wxNode* node = m_widgets.First();
384 while (node)
385 {
386 Widget button = (Widget) node->Data();
387 XtDestroyWidget(button);
388 node = node->Next();
389 }
390 m_widgets.Clear();
391 DestroyPixmaps();
392
393 wxToolBarBase::ClearTools();
394 }
395
396 void wxToolBar::DestroyPixmaps()
397 {
398 wxNode* node = m_pixmaps.First();
399 while (node)
400 {
401 Pixmap pixmap = (Pixmap) node->Data();
402 XmDestroyPixmap (DefaultScreenOfDisplay ((Display*) GetXDisplay()), pixmap);
403 node = node->Next();
404 }
405 m_pixmaps.Clear();
406 }
407
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.
411
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)
414 {
415 wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap, toggle, xPos, yPos, helpString1, helpString2);
416 tool->m_clientData = clientData;
417
418 if (xPos > -1)
419 tool->m_x = xPos;
420 else
421 tool->m_x = m_xMargin;
422
423 if (yPos > -1)
424 tool->m_y = yPos;
425 else
426 tool->m_y = m_yMargin;
427
428 wxSize size = GetToolSize();
429 tool->SetSize(size.x, size.y);
430
431 m_tools.Append((long)index, tool);
432 return tool;
433 }
434
435 int wxToolBar::FindIndexForWidget(WXWidget w)
436 {
437 wxNode* node = m_widgets.First();
438 while (node)
439 {
440 WXWidget widget = (WXWidget) node->Data();
441 if (widget == w)
442 return (int) node->GetKeyInteger();
443 node = node->Next();
444 }
445 return -1;
446 }
447
448 WXWidget wxToolBar::FindWidgetForIndex(int index)
449 {
450 wxNode* node = m_widgets.Find((long) index);
451 if (!node)
452 return (WXWidget) 0;
453 else
454 return (WXWidget) node->Data();
455 }
456
457 WXWidget wxToolBar::GetTopWidget() const
458 {
459 return m_mainWidget;
460 }
461
462 WXWidget wxToolBar::GetClientWidget() const
463 {
464 return m_mainWidget;
465 }
466
467 WXWidget wxToolBar::GetMainWidget() const
468 {
469 return m_mainWidget;
470 }
471
472
473 void wxToolButtonCallback (Widget w, XtPointer clientData,
474 XtPointer ptr)
475 {
476 wxToolBar *toolBar = (wxToolBar *) clientData;
477 int index = toolBar->FindIndexForWidget((WXWidget) w);
478
479 if (index != -1)
480 {
481 wxNode *node = toolBar->GetTools().Find((long)index);
482 if (!node)
483 return;
484 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
485 if (tool->m_isToggle)
486 tool->m_toggleState = !tool->m_toggleState;
487
488 (void) toolBar->OnLeftClick(index, tool->m_toggleState);
489 }
490
491 }
492
493
494 static void wxToolButtonPopupCallback (Widget w, XtPointer client_data,
495 XEvent *event, Boolean *continue_to_dispatch)
496 {
497 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
498 int delayMilli = 800;
499 wxToolBar* toolBar = (wxToolBar*) client_data;
500
501 int index = toolBar->FindIndexForWidget((WXWidget) w);
502
503 if (index != -1)
504 {
505 wxNode *node = toolBar->GetTools().Find((long)index);
506 if (!node)
507 return;
508 wxToolBarTool *tool = (wxToolBarTool *)node->Data();
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