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