]> git.saurik.com Git - wxWidgets.git/blob - src/motif/toolbar.cpp
0. wxExecute() with IO redirection now works again (but the real fix is
[wxWidgets.git] / src / motif / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: motif/toolbar.cpp
3 // Purpose: wxToolBar
4 // Author: Julian Smart
5 // Modified by: 13.12.99 by VZ during toolbar classes reorganization
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "toolbar.h"
22 #endif
23
24 #ifdef __VMS
25 #define XtDisplay XTDISPLAY
26 #endif
27
28 #include "wx/wx.h"
29 #include "wx/app.h"
30 #include "wx/timer.h"
31 #include "wx/toolbar.h"
32
33 #ifdef __VMS__
34 #pragma message disable nosimpint
35 #endif
36 #include <Xm/Xm.h>
37 #include <Xm/PushBG.h>
38 #include <Xm/PushB.h>
39 #include <Xm/Label.h>
40 #include <Xm/ToggleB.h>
41 #include <Xm/ToggleBG.h>
42 #include <Xm/Form.h>
43 #ifdef __VMS__
44 #pragma message enable nosimpint
45 #endif
46
47 #include "wx/motif/private.h"
48
49 // ----------------------------------------------------------------------------
50 // wxWin macros
51 // ----------------------------------------------------------------------------
52
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase)
55 #endif
56
57 // ----------------------------------------------------------------------------
58 // private functions
59 // ----------------------------------------------------------------------------
60
61 static void wxToolButtonCallback (Widget w, XtPointer clientData,
62 XtPointer ptr);
63 static void wxToolButtonPopupCallback (Widget w, XtPointer client_data,
64 XEvent *event, Boolean *continue_to_dispatch);
65
66 // ----------------------------------------------------------------------------
67 // private classes
68 // ----------------------------------------------------------------------------
69
70 class wxToolBarTimer : public wxTimer
71 {
72 public:
73 virtual void Notify();
74
75 static Widget help_popup;
76 static Widget buttonWidget;
77 static wxString helpString;
78 };
79
80 class wxToolBarTool : public wxToolBarToolBase
81 {
82 public:
83 wxToolBarTool(wxToolBar *tbar,
84 int id,
85 const wxString& label,
86 const wxBitmap& bmpNormal,
87 const wxBitmap& bmpToggled,
88 wxItemKind kind,
89 wxObject *clientData,
90 const wxString& shortHelp,
91 const wxString& longHelp)
92 : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpToggled, kind,
93 clientData, shortHelp, longHelp)
94 {
95 Init();
96 }
97
98 wxToolBarTool(wxToolBar *tbar, wxControl *control)
99 : wxToolBarToolBase(tbar, control)
100 {
101 Init();
102 }
103
104 virtual ~wxToolBarTool();
105
106 // accessors
107 void SetWidget(Widget widget) { m_widget = widget; }
108 Widget GetButtonWidget() const { return m_widget; }
109
110 void SetPixmap(Pixmap pixmap) { m_pixmap = pixmap; }
111 Pixmap GetPixmap() const { return m_pixmap; }
112
113 protected:
114 void Init();
115
116 Widget m_widget;
117 Pixmap m_pixmap;
118 };
119
120 // ----------------------------------------------------------------------------
121 // globals
122 // ----------------------------------------------------------------------------
123
124 static wxToolBarTimer* wxTheToolBarTimer = (wxToolBarTimer*) NULL;
125
126 Widget wxToolBarTimer::help_popup = (Widget) 0;
127 Widget wxToolBarTimer::buttonWidget = (Widget) 0;
128 wxString wxToolBarTimer::helpString;
129
130 // ============================================================================
131 // implementation
132 // ============================================================================
133
134 // ----------------------------------------------------------------------------
135 // wxToolBarTool
136 // ----------------------------------------------------------------------------
137
138 wxToolBarToolBase *wxToolBar::CreateTool(int id,
139 const wxString& label,
140 const wxBitmap& bmpNormal,
141 const wxBitmap& bmpToggled,
142 wxItemKind kind,
143 wxObject *clientData,
144 const wxString& shortHelp,
145 const wxString& longHelp)
146 {
147 return new wxToolBarTool(this, id, label, bmpNormal, bmpToggled, kind,
148 clientData, shortHelp, longHelp);
149 }
150
151
152 wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control)
153 {
154 return new wxToolBarTool(this, control);
155 }
156
157 void wxToolBarTool::Init()
158 {
159 m_widget = (Widget)0;
160 m_pixmap = (Pixmap)0;
161 }
162
163 wxToolBarTool::~wxToolBarTool()
164 {
165 if ( m_widget )
166 XtDestroyWidget(m_widget);
167 if ( m_pixmap )
168 XmDestroyPixmap(DefaultScreenOfDisplay((Display*)wxGetDisplay()),
169 m_pixmap);
170 }
171
172 // ----------------------------------------------------------------------------
173 // wxToolBar construction
174 // ----------------------------------------------------------------------------
175
176 void wxToolBar::Init()
177 {
178 m_maxWidth = -1;
179 m_maxHeight = -1;
180 m_defaultWidth = 24;
181 m_defaultHeight = 22;
182 m_toolPacking = 2;
183 m_toolSeparation = 8;
184 m_xMargin = 2;
185 m_yMargin = 2;
186 m_maxRows = 100;
187 m_maxCols = 100;
188 }
189
190 bool wxToolBar::Create(wxWindow *parent,
191 wxWindowID id,
192 const wxPoint& pos,
193 const wxSize& size,
194 long style,
195 const wxString& name)
196 {
197 Init();
198
199 m_windowId = id;
200
201 SetName(name);
202 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
203 m_foregroundColour = parent->GetForegroundColour();
204 m_windowStyle = style;
205
206 SetParent(parent);
207
208 if (parent) parent->AddChild(this);
209
210 Widget parentWidget = (Widget) parent->GetClientWidget();
211
212 Widget toolbar = XtVaCreateManagedWidget("toolbar",
213 xmBulletinBoardWidgetClass, (Widget) parentWidget,
214 XmNmarginWidth, 0,
215 XmNmarginHeight, 0,
216 XmNresizePolicy, XmRESIZE_NONE,
217 NULL);
218 /*
219 Widget toolbar = XtVaCreateManagedWidget("toolbar",
220 xmFormWidgetClass, (Widget) m_clientWidget,
221 XmNtraversalOn, False,
222 XmNhorizontalSpacing, 0,
223 XmNverticalSpacing, 0,
224 XmNleftOffset, 0,
225 XmNrightOffset, 0,
226 XmNmarginWidth, 0,
227 XmNmarginHeight, 0,
228 NULL);
229 */
230
231 m_mainWidget = (WXWidget) toolbar;
232
233 m_font = parent->GetFont();
234 ChangeFont(FALSE);
235
236 SetCanAddEventHandler(TRUE);
237 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
238
239 ChangeBackgroundColour();
240
241 return TRUE;
242 }
243
244 wxToolBar::~wxToolBar()
245 {
246 delete wxTheToolBarTimer;
247 wxTheToolBarTimer = NULL;
248 }
249
250 bool wxToolBar::Realize()
251 {
252 if ( m_tools.GetCount() == 0 )
253 {
254 // nothing to do
255 return TRUE;
256 }
257
258 // Separator spacing
259 const int separatorSize = GetToolSeparation(); // 8;
260 wxSize margins = GetToolMargins();
261 int packing = GetToolPacking();
262 int marginX = margins.x;
263 int marginY = margins.y;
264
265 int currentX = marginX;
266 int currentY = marginY;
267
268 int buttonHeight = 0;
269
270 int currentSpacing = 0;
271
272 Widget button;
273 Pixmap pixmap, insensPixmap;
274 wxBitmap bmp;
275
276 wxToolBarToolsList::Node *node = m_tools.GetFirst();
277 while ( node )
278 {
279 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
280
281 switch ( tool->GetStyle() )
282 {
283 case wxTOOL_STYLE_CONTROL:
284 {
285 wxControl* control = tool->GetControl();
286 wxSize sz = control->GetSize();
287 wxPoint pos = control->GetPosition();
288 control->Move(currentX, pos.y);
289 currentX += sz.x + packing;
290
291 break;
292 }
293 case wxTOOL_STYLE_SEPARATOR:
294 currentX += separatorSize;
295 break;
296
297 case wxTOOL_STYLE_BUTTON:
298 button = (Widget) 0;
299
300 if ( tool->CanBeToggled() )
301 {
302 button = XtVaCreateWidget("toggleButton",
303 xmToggleButtonWidgetClass, (Widget) m_mainWidget,
304 XmNx, currentX, XmNy, currentY,
305 XmNindicatorOn, False,
306 XmNshadowThickness, 2,
307 XmNborderWidth, 0,
308 XmNspacing, 0,
309 XmNmarginWidth, 0,
310 XmNmarginHeight, 0,
311 XmNmultiClick, XmMULTICLICK_KEEP,
312 XmNlabelType, XmPIXMAP,
313 NULL);
314 XtAddCallback ((Widget) button, XmNvalueChangedCallback, (XtCallbackProc) wxToolButtonCallback,
315 (XtPointer) this);
316
317 XtVaSetValues ((Widget) button,
318 XmNselectColor, m_backgroundColour.AllocColour(XtDisplay((Widget) button)),
319 NULL);
320 }
321 else
322 {
323 button = XtVaCreateWidget("button",
324 xmPushButtonWidgetClass, (Widget) m_mainWidget,
325 XmNx, currentX, XmNy, currentY,
326 XmNpushButtonEnabled, True,
327 XmNmultiClick, XmMULTICLICK_KEEP,
328 XmNlabelType, XmPIXMAP,
329 NULL);
330 XtAddCallback (button,
331 XmNactivateCallback, (XtCallbackProc) wxToolButtonCallback,
332 (XtPointer) this);
333 }
334
335 DoChangeBackgroundColour((WXWidget) button, m_backgroundColour, TRUE);
336
337 tool->SetWidget(button);
338
339 // For each button, if there is a mask, we must create
340 // a new wxBitmap that has the correct background colour
341 // for the button. Otherwise the background will just be
342 // e.g. black if a transparent XPM has been loaded.
343 bmp = tool->GetNormalBitmap();
344 if ( bmp.GetMask() )
345 {
346 int backgroundPixel;
347 XtVaGetValues(button, XmNbackground, &backgroundPixel,
348 NULL);
349
350 wxColour col;
351 col.SetPixel(backgroundPixel);
352
353 bmp = wxCreateMaskedBitmap(bmp, col);
354
355 tool->SetNormalBitmap(bmp);
356 }
357
358 // Create a selected/toggled bitmap. If there isn't a 2nd
359 // bitmap, we need to create it (with a darker, selected
360 // background)
361 int backgroundPixel;
362 if ( tool->CanBeToggled() )
363 XtVaGetValues(button, XmNselectColor, &backgroundPixel,
364 NULL);
365 else
366 XtVaGetValues(button, XmNarmColor, &backgroundPixel,
367 NULL);
368
369 wxColour col;
370 col.SetPixel(backgroundPixel);
371
372 // FIXME: we use disabled bitmap as the bitmap for the toggled
373 // state, we probably need a GetToggledBitmap() instead
374 wxBitmap bmpToggled = tool->GetDisabledBitmap();
375 if ( bmpToggled.Ok() && bmpToggled.GetMask())
376 {
377 // Use what's there
378 wxBitmap newBitmap = wxCreateMaskedBitmap(bmpToggled, col);
379 tool->SetDisabledBitmap(newBitmap);
380 }
381 else
382 {
383 // Use unselected bitmap
384 if ( bmp.GetMask() )
385 {
386 wxBitmap newBitmap = wxCreateMaskedBitmap(bmp, col);
387 tool->SetDisabledBitmap(newBitmap);
388 }
389 else
390 tool->SetDisabledBitmap(bmp);
391 }
392
393 // FIXME: and here we should use GetDisabledBitmap()...
394 pixmap = (Pixmap) bmp.GetPixmap();
395 insensPixmap = (Pixmap) bmp.GetInsensPixmap();
396
397 if (tool->CanBeToggled())
398 {
399 // Toggle button
400 Pixmap pixmap2 = (Pixmap) 0;
401 Pixmap insensPixmap2 = (Pixmap) 0;
402
403 // If there's a bitmap for the toggled state, use it,
404 // otherwise generate one.
405 //
406 // FIXME: see above
407 if ( bmpToggled.Ok() )
408 {
409 pixmap2 = (Pixmap) bmpToggled.GetPixmap();
410 insensPixmap2 = (Pixmap) bmpToggled.GetInsensPixmap();
411 }
412 else
413 {
414 pixmap2 = (Pixmap) bmp.GetArmPixmap(button);
415 insensPixmap2 = XCreateInsensitivePixmap((Display*) wxGetDisplay(), pixmap2);
416 }
417
418 tool->SetPixmap(pixmap2);
419
420 XtVaSetValues (button,
421 XmNfillOnSelect, True,
422 XmNlabelPixmap, pixmap,
423 XmNselectPixmap, pixmap2,
424 XmNlabelInsensitivePixmap, insensPixmap,
425 XmNselectInsensitivePixmap, insensPixmap2,
426 XmNlabelType, XmPIXMAP,
427 NULL);
428 }
429 else
430 {
431 Pixmap pixmap2 = (Pixmap) 0;
432
433 // If there's a bitmap for the armed state, use it,
434 // otherwise generate one.
435 if ( bmpToggled.Ok() )
436 {
437 pixmap2 = (Pixmap) bmpToggled.GetPixmap();
438 }
439 else
440 {
441 pixmap2 = (Pixmap) bmp.GetArmPixmap(button);
442
443 }
444
445 tool->SetPixmap(pixmap2);
446
447 // Normal button
448 XtVaSetValues(button,
449 XmNlabelPixmap, pixmap,
450 XmNlabelInsensitivePixmap, insensPixmap,
451 XmNarmPixmap, pixmap2,
452 NULL);
453 }
454
455 XtManageChild(button);
456
457 {
458 Dimension width, height;
459 XtVaGetValues(button,
460 XmNwidth, &width,
461 XmNheight, & height,
462 NULL);
463 currentX += width + packing;
464 buttonHeight = wxMax(buttonHeight, height);
465 }
466
467 XtAddEventHandler (button, EnterWindowMask | LeaveWindowMask,
468 False, wxToolButtonPopupCallback, (XtPointer) this);
469
470 currentSpacing = 0;
471 break;
472 }
473
474 node = node->GetNext();
475 }
476
477 SetSize(-1, -1, currentX, buttonHeight + 2*marginY);
478
479 return TRUE;
480 }
481
482 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
483 wxCoord WXUNUSED(y)) const
484 {
485 wxFAIL_MSG( _T("TODO") );
486
487 return (wxToolBarToolBase *)NULL;
488 }
489
490 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
491 {
492 tool->Attach(this);
493
494 return TRUE;
495 }
496
497 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
498 {
499 tool->Detach();
500
501 return TRUE;
502 }
503
504 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
505 {
506 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
507
508 XtSetSensitive(tool->GetButtonWidget(), (Boolean) enable);
509 }
510
511 void wxToolBar::DoToggleTool(wxToolBarToolBase *toolBase, bool toggle)
512 {
513 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
514
515 XmToggleButtonSetState(tool->GetButtonWidget(), (Boolean) toggle, False);
516 }
517
518 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
519 bool WXUNUSED(toggle))
520 {
521 // nothing to do
522 }
523
524 // ----------------------------------------------------------------------------
525 // Motif callbacks
526 // ----------------------------------------------------------------------------
527
528 wxToolBarToolBase *wxToolBar::FindToolByWidget(WXWidget w) const
529 {
530 wxToolBarToolsList::Node* node = m_tools.GetFirst();
531 while ( node )
532 {
533 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
534 if ( tool->GetButtonWidget() == w)
535 {
536 return tool;
537 }
538
539 node = node->GetNext();
540 }
541
542 return (wxToolBarToolBase *)NULL;
543 }
544
545 static void wxToolButtonCallback(Widget w,
546 XtPointer clientData,
547 XtPointer WXUNUSED(ptr))
548 {
549 wxToolBar *toolBar = (wxToolBar *) clientData;
550 wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w);
551 if ( !tool )
552 return;
553
554 if ( tool->CanBeToggled() )
555 tool->Toggle();
556
557 if ( !toolBar->OnLeftClick(tool->GetId(), tool->IsToggled()) )
558 {
559 // revert
560 tool->Toggle();
561 }
562 }
563
564
565 static void wxToolButtonPopupCallback(Widget w,
566 XtPointer client_data,
567 XEvent *event,
568 Boolean *WXUNUSED(continue_to_dispatch))
569 {
570 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
571 static const int delayMilli = 800;
572
573 wxToolBar* toolBar = (wxToolBar*) client_data;
574 wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w);
575
576 if ( !tool )
577 return;
578
579 wxString tooltip = tool->GetShortHelp();
580 if ( !tooltip )
581 return;
582
583 if (!wxTheToolBarTimer)
584 wxTheToolBarTimer = new wxToolBarTimer;
585
586 wxToolBarTimer::buttonWidget = w;
587 wxToolBarTimer::helpString = tooltip;
588
589 /************************************************************/
590 /* Popup help label */
591 /************************************************************/
592 if (event->type == EnterNotify)
593 {
594 if (wxToolBarTimer::help_popup != (Widget) 0)
595 {
596 XtDestroyWidget (wxToolBarTimer::help_popup);
597 XtPopdown (wxToolBarTimer::help_popup);
598 }
599 wxToolBarTimer::help_popup = (Widget) 0;
600
601 // One shot
602 wxTheToolBarTimer->Start(delayMilli, TRUE);
603
604 }
605 /************************************************************/
606 /* Popdown help label */
607 /************************************************************/
608 else if (event->type == LeaveNotify)
609 {
610 if (wxTheToolBarTimer)
611 wxTheToolBarTimer->Stop();
612 if (wxToolBarTimer::help_popup != (Widget) 0)
613 {
614 XtDestroyWidget (wxToolBarTimer::help_popup);
615 XtPopdown (wxToolBarTimer::help_popup);
616 }
617 wxToolBarTimer::help_popup = (Widget) 0;
618 }
619 }
620
621 void wxToolBarTimer::Notify()
622 {
623 Position x, y;
624
625 /************************************************************/
626 /* Create shell without window decorations */
627 /************************************************************/
628 help_popup = XtVaCreatePopupShell ("shell",
629 overrideShellWidgetClass, (Widget) wxTheApp->GetTopLevelWidget(),
630 NULL);
631
632 /************************************************************/
633 /* Get absolute position on display of toolbar button */
634 /************************************************************/
635 XtTranslateCoords (buttonWidget,
636 (Position) 0,
637 (Position) 0,
638 &x, &y);
639
640 // Move the tooltip more or less above the button
641 int yOffset = 20; // TODO: What should be really?
642 y -= yOffset;
643 if (y < yOffset) y = 0;
644
645 /************************************************************/
646 /* Set the position of the help popup */
647 /************************************************************/
648 XtVaSetValues (help_popup,
649 XmNx, (Position) x,
650 XmNy, (Position) y,
651 NULL);
652
653 /************************************************************/
654 /* Create help label */
655 /************************************************************/
656 XmString text = XmStringCreateSimple ((char*) (const char*) helpString);
657 XtVaCreateManagedWidget ("help_label",
658 xmLabelWidgetClass, help_popup,
659 XmNlabelString, text,
660 XtVaTypedArg,
661 XmNforeground, XtRString, "black",
662 strlen("black")+1,
663 XtVaTypedArg,
664 XmNbackground, XtRString, "LightGoldenrod",
665 strlen("LightGoldenrod")+1,
666 NULL);
667 XmStringFree (text);
668
669 /************************************************************/
670 /* Popup help label */
671 /************************************************************/
672 XtPopup (help_popup, XtGrabNone);
673 }
674