]> git.saurik.com Git - wxWidgets.git/blob - src/motif/toolbar.cpp
allow TABbing into multiline text controls even if they're not editable
[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 // note: do not delete m_pixmap here because it will be deleted
168 // by the base class when the bitmap is destroyed.
169 }
170
171 // ----------------------------------------------------------------------------
172 // wxToolBar construction
173 // ----------------------------------------------------------------------------
174
175 void wxToolBar::Init()
176 {
177 m_maxWidth = -1;
178 m_maxHeight = -1;
179 m_defaultWidth = 24;
180 m_defaultHeight = 22;
181 m_toolPacking = 2;
182 m_toolSeparation = 8;
183 m_xMargin = 2;
184 m_yMargin = 2;
185 m_maxRows = 100;
186 m_maxCols = 100;
187 }
188
189 bool wxToolBar::Create(wxWindow *parent,
190 wxWindowID id,
191 const wxPoint& pos,
192 const wxSize& size,
193 long style,
194 const wxString& name)
195 {
196 Init();
197
198 m_windowId = id;
199
200 SetName(name);
201 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
202 m_foregroundColour = parent->GetForegroundColour();
203 m_windowStyle = style;
204
205 SetParent(parent);
206
207 if (parent) parent->AddChild(this);
208
209 Widget parentWidget = (Widget) parent->GetClientWidget();
210
211 Widget toolbar = XtVaCreateManagedWidget("toolbar",
212 xmBulletinBoardWidgetClass, (Widget) parentWidget,
213 XmNmarginWidth, 0,
214 XmNmarginHeight, 0,
215 XmNresizePolicy, XmRESIZE_NONE,
216 NULL);
217 /*
218 Widget toolbar = XtVaCreateManagedWidget("toolbar",
219 xmFormWidgetClass, (Widget) m_clientWidget,
220 XmNtraversalOn, False,
221 XmNhorizontalSpacing, 0,
222 XmNverticalSpacing, 0,
223 XmNleftOffset, 0,
224 XmNrightOffset, 0,
225 XmNmarginWidth, 0,
226 XmNmarginHeight, 0,
227 NULL);
228 */
229
230 m_mainWidget = (WXWidget) toolbar;
231
232 m_font = parent->GetFont();
233 ChangeFont(FALSE);
234
235 SetCanAddEventHandler(TRUE);
236 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
237
238 ChangeBackgroundColour();
239
240 return TRUE;
241 }
242
243 wxToolBar::~wxToolBar()
244 {
245 delete wxTheToolBarTimer;
246 wxTheToolBarTimer = NULL;
247 }
248
249 bool wxToolBar::Realize()
250 {
251 if ( m_tools.GetCount() == 0 )
252 {
253 // nothing to do
254 return TRUE;
255 }
256
257 // Separator spacing
258 const int separatorSize = GetToolSeparation(); // 8;
259 wxSize margins = GetToolMargins();
260 int packing = GetToolPacking();
261 int marginX = margins.x;
262 int marginY = margins.y;
263
264 int currentX = marginX;
265 int currentY = marginY;
266
267 int buttonHeight = 0;
268
269 int currentSpacing = 0;
270
271 Widget button;
272 Pixmap pixmap, insensPixmap;
273 wxBitmap bmp;
274
275 wxToolBarToolsList::Node *node = m_tools.GetFirst();
276 while ( node )
277 {
278 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
279
280 switch ( tool->GetStyle() )
281 {
282 case wxTOOL_STYLE_CONTROL:
283 {
284 wxControl* control = tool->GetControl();
285 wxSize sz = control->GetSize();
286 wxPoint pos = control->GetPosition();
287 // Allow a control to specify a y-offset by setting its initial position,
288 // but still don't allow it to position itself above the top margin.
289 int controlY = (pos.y > 0) ? currentY + pos.y : currentY;
290 control->Move(currentX, controlY);
291 currentX += sz.x + packing;
292
293 break;
294 }
295 case wxTOOL_STYLE_SEPARATOR:
296 currentX += separatorSize;
297 break;
298
299 case wxTOOL_STYLE_BUTTON:
300 button = (Widget) 0;
301
302 if ( tool->CanBeToggled() )
303 {
304 button = XtVaCreateWidget("toggleButton",
305 xmToggleButtonWidgetClass, (Widget) m_mainWidget,
306 XmNx, currentX, XmNy, currentY,
307 XmNindicatorOn, False,
308 XmNshadowThickness, 2,
309 XmNborderWidth, 0,
310 XmNspacing, 0,
311 XmNmarginWidth, 0,
312 XmNmarginHeight, 0,
313 XmNmultiClick, XmMULTICLICK_KEEP,
314 XmNlabelType, XmPIXMAP,
315 NULL);
316 XtAddCallback ((Widget) button, XmNvalueChangedCallback, (XtCallbackProc) wxToolButtonCallback,
317 (XtPointer) this);
318
319 XtVaSetValues ((Widget) button,
320 XmNselectColor, m_backgroundColour.AllocColour(XtDisplay((Widget) button)),
321 NULL);
322 }
323 else
324 {
325 button = XtVaCreateWidget("button",
326 xmPushButtonWidgetClass, (Widget) m_mainWidget,
327 XmNx, currentX, XmNy, currentY,
328 XmNpushButtonEnabled, True,
329 XmNmultiClick, XmMULTICLICK_KEEP,
330 XmNlabelType, XmPIXMAP,
331 NULL);
332 XtAddCallback (button,
333 XmNactivateCallback, (XtCallbackProc) wxToolButtonCallback,
334 (XtPointer) this);
335 }
336
337 DoChangeBackgroundColour((WXWidget) button, m_backgroundColour, TRUE);
338
339 tool->SetWidget(button);
340
341 // For each button, if there is a mask, we must create
342 // a new wxBitmap that has the correct background colour
343 // for the button. Otherwise the background will just be
344 // e.g. black if a transparent XPM has been loaded.
345 bmp = tool->GetNormalBitmap();
346 if ( bmp.GetMask() )
347 {
348 int backgroundPixel;
349 XtVaGetValues(button, XmNbackground, &backgroundPixel,
350 NULL);
351
352 wxColour col;
353 col.SetPixel(backgroundPixel);
354
355 bmp = wxCreateMaskedBitmap(bmp, col);
356
357 tool->SetNormalBitmap(bmp);
358 }
359
360 // Create a selected/toggled bitmap. If there isn't a 2nd
361 // bitmap, we need to create it (with a darker, selected
362 // background)
363 int backgroundPixel;
364 if ( tool->CanBeToggled() )
365 XtVaGetValues(button, XmNselectColor, &backgroundPixel,
366 NULL);
367 else
368 XtVaGetValues(button, XmNarmColor, &backgroundPixel,
369 NULL);
370
371 wxColour col;
372 col.SetPixel(backgroundPixel);
373
374 // FIXME: we use disabled bitmap as the bitmap for the toggled
375 // state, we probably need a GetToggledBitmap() instead
376 wxBitmap bmpToggled = tool->GetDisabledBitmap();
377 if ( bmpToggled.Ok() && bmpToggled.GetMask())
378 {
379 // Use what's there
380 wxBitmap newBitmap = wxCreateMaskedBitmap(bmpToggled, col);
381 tool->SetDisabledBitmap(newBitmap);
382 }
383 else
384 {
385 // Use unselected bitmap
386 if ( bmp.GetMask() )
387 {
388 wxBitmap newBitmap = wxCreateMaskedBitmap(bmp, col);
389 tool->SetDisabledBitmap(newBitmap);
390 }
391 else
392 tool->SetDisabledBitmap(bmp);
393 }
394
395 // FIXME: and here we should use GetDisabledBitmap()...
396 pixmap = (Pixmap) bmp.GetPixmap();
397 insensPixmap = (Pixmap) bmp.GetInsensPixmap();
398
399 if (tool->CanBeToggled())
400 {
401 // Toggle button
402 Pixmap pixmap2 = (Pixmap) 0;
403 Pixmap insensPixmap2 = (Pixmap) 0;
404
405 // If there's a bitmap for the toggled state, use it,
406 // otherwise generate one.
407 //
408 // FIXME: see above
409 if ( bmpToggled.Ok() )
410 {
411 pixmap2 = (Pixmap) bmpToggled.GetPixmap();
412 insensPixmap2 = (Pixmap) bmpToggled.GetInsensPixmap();
413 }
414 else
415 {
416 pixmap2 = (Pixmap) bmp.GetArmPixmap(button);
417 insensPixmap2 = XCreateInsensitivePixmap((Display*) wxGetDisplay(), pixmap2);
418 }
419
420 tool->SetPixmap(pixmap2);
421
422 XtVaSetValues (button,
423 XmNfillOnSelect, True,
424 XmNlabelPixmap, pixmap,
425 XmNselectPixmap, pixmap2,
426 XmNlabelInsensitivePixmap, insensPixmap,
427 XmNselectInsensitivePixmap, insensPixmap2,
428 XmNlabelType, XmPIXMAP,
429 NULL);
430 }
431 else
432 {
433 Pixmap pixmap2 = (Pixmap) 0;
434
435 // If there's a bitmap for the armed state, use it,
436 // otherwise generate one.
437 if ( bmpToggled.Ok() )
438 {
439 pixmap2 = (Pixmap) bmpToggled.GetPixmap();
440 }
441 else
442 {
443 pixmap2 = (Pixmap) bmp.GetArmPixmap(button);
444
445 }
446
447 tool->SetPixmap(pixmap2);
448
449 // Normal button
450 XtVaSetValues(button,
451 XmNlabelPixmap, pixmap,
452 XmNlabelInsensitivePixmap, insensPixmap,
453 XmNarmPixmap, pixmap2,
454 NULL);
455 }
456
457 XtManageChild(button);
458
459 {
460 Dimension width, height;
461 XtVaGetValues(button,
462 XmNwidth, &width,
463 XmNheight, & height,
464 NULL);
465 currentX += width + packing;
466 buttonHeight = wxMax(buttonHeight, height);
467 }
468
469 XtAddEventHandler (button, EnterWindowMask | LeaveWindowMask,
470 False, wxToolButtonPopupCallback, (XtPointer) this);
471
472 currentSpacing = 0;
473 break;
474 }
475
476 node = node->GetNext();
477 }
478
479 SetSize(-1, -1, currentX, buttonHeight + 2*marginY);
480
481 return TRUE;
482 }
483
484 wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x),
485 wxCoord WXUNUSED(y)) const
486 {
487 wxFAIL_MSG( _T("TODO") );
488
489 return (wxToolBarToolBase *)NULL;
490 }
491
492 bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
493 {
494 tool->Attach(this);
495
496 return TRUE;
497 }
498
499 bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool)
500 {
501 tool->Detach();
502
503 return TRUE;
504 }
505
506 void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable)
507 {
508 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
509
510 XtSetSensitive(tool->GetButtonWidget(), (Boolean) enable);
511 }
512
513 void wxToolBar::DoToggleTool(wxToolBarToolBase *toolBase, bool toggle)
514 {
515 wxToolBarTool *tool = (wxToolBarTool *)toolBase;
516
517 XmToggleButtonSetState(tool->GetButtonWidget(), (Boolean) toggle, False);
518 }
519
520 void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool),
521 bool WXUNUSED(toggle))
522 {
523 // nothing to do
524 }
525
526 // ----------------------------------------------------------------------------
527 // Motif callbacks
528 // ----------------------------------------------------------------------------
529
530 wxToolBarToolBase *wxToolBar::FindToolByWidget(WXWidget w) const
531 {
532 wxToolBarToolsList::Node* node = m_tools.GetFirst();
533 while ( node )
534 {
535 wxToolBarTool *tool = (wxToolBarTool *)node->GetData();
536 if ( tool->GetButtonWidget() == w)
537 {
538 return tool;
539 }
540
541 node = node->GetNext();
542 }
543
544 return (wxToolBarToolBase *)NULL;
545 }
546
547 static void wxToolButtonCallback(Widget w,
548 XtPointer clientData,
549 XtPointer WXUNUSED(ptr))
550 {
551 wxToolBar *toolBar = (wxToolBar *) clientData;
552 wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w);
553 if ( !tool )
554 return;
555
556 if ( tool->CanBeToggled() )
557 tool->Toggle();
558
559 if ( !toolBar->OnLeftClick(tool->GetId(), tool->IsToggled()) )
560 {
561 // revert
562 tool->Toggle();
563 }
564 }
565
566
567 static void wxToolButtonPopupCallback(Widget w,
568 XtPointer client_data,
569 XEvent *event,
570 Boolean *WXUNUSED(continue_to_dispatch))
571 {
572 // TODO: retrieve delay before popping up tooltip from wxSystemSettings.
573 static const int delayMilli = 800;
574
575 wxToolBar* toolBar = (wxToolBar*) client_data;
576 wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w);
577
578 if ( !tool )
579 return;
580
581 wxString tooltip = tool->GetShortHelp();
582 if ( !tooltip )
583 return;
584
585 if (!wxTheToolBarTimer)
586 wxTheToolBarTimer = new wxToolBarTimer;
587
588 wxToolBarTimer::buttonWidget = w;
589 wxToolBarTimer::helpString = tooltip;
590
591 /************************************************************/
592 /* Popup help label */
593 /************************************************************/
594 if (event->type == EnterNotify)
595 {
596 if (wxToolBarTimer::help_popup != (Widget) 0)
597 {
598 XtDestroyWidget (wxToolBarTimer::help_popup);
599 XtPopdown (wxToolBarTimer::help_popup);
600 }
601 wxToolBarTimer::help_popup = (Widget) 0;
602
603 // One shot
604 wxTheToolBarTimer->Start(delayMilli, TRUE);
605
606 }
607 /************************************************************/
608 /* Popdown help label */
609 /************************************************************/
610 else if (event->type == LeaveNotify)
611 {
612 if (wxTheToolBarTimer)
613 wxTheToolBarTimer->Stop();
614 if (wxToolBarTimer::help_popup != (Widget) 0)
615 {
616 XtDestroyWidget (wxToolBarTimer::help_popup);
617 XtPopdown (wxToolBarTimer::help_popup);
618 }
619 wxToolBarTimer::help_popup = (Widget) 0;
620 }
621 }
622
623 void wxToolBarTimer::Notify()
624 {
625 Position x, y;
626
627 /************************************************************/
628 /* Create shell without window decorations */
629 /************************************************************/
630 help_popup = XtVaCreatePopupShell ("shell",
631 overrideShellWidgetClass, (Widget) wxTheApp->GetTopLevelWidget(),
632 NULL);
633
634 /************************************************************/
635 /* Get absolute position on display of toolbar button */
636 /************************************************************/
637 XtTranslateCoords (buttonWidget,
638 (Position) 0,
639 (Position) 0,
640 &x, &y);
641
642 // Move the tooltip more or less above the button
643 int yOffset = 20; // TODO: What should be really?
644 y -= yOffset;
645 if (y < yOffset) y = 0;
646
647 /************************************************************/
648 /* Set the position of the help popup */
649 /************************************************************/
650 XtVaSetValues (help_popup,
651 XmNx, (Position) x,
652 XmNy, (Position) y,
653 NULL);
654
655 /************************************************************/
656 /* Create help label */
657 /************************************************************/
658 XmString text = XmStringCreateSimple ((char*) (const char*) helpString);
659 XtVaCreateManagedWidget ("help_label",
660 xmLabelWidgetClass, help_popup,
661 XmNlabelString, text,
662 XtVaTypedArg,
663 XmNforeground, XtRString, "black",
664 strlen("black")+1,
665 XtVaTypedArg,
666 XmNbackground, XtRString, "LightGoldenrod",
667 strlen("LightGoldenrod")+1,
668 NULL);
669 XmStringFree (text);
670
671 /************************************************************/
672 /* Popup help label */
673 /************************************************************/
674 XtPopup (help_popup, XtGrabNone);
675 }
676