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