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