]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/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 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __VMS | |
24 | #define XtDisplay XTDISPLAY | |
25 | #endif | |
26 | ||
27 | #include "wx/toolbar.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
31 | #include "wx/frame.h" | |
32 | #include "wx/timer.h" | |
33 | #include "wx/settings.h" | |
34 | #endif | |
35 | ||
36 | #ifdef __VMS__ | |
37 | #pragma message disable nosimpint | |
38 | #endif | |
39 | #include <Xm/Xm.h> | |
40 | #include <Xm/PushBG.h> | |
41 | #include <Xm/PushB.h> | |
42 | #include <Xm/Label.h> | |
43 | #include <Xm/ToggleB.h> | |
44 | #include <Xm/ToggleBG.h> | |
45 | #include <Xm/Form.h> | |
46 | #ifdef __VMS__ | |
47 | #pragma message enable nosimpint | |
48 | #endif | |
49 | ||
50 | #include "wx/motif/private.h" | |
51 | #include "wx/motif/bmpmotif.h" | |
52 | ||
53 | // ---------------------------------------------------------------------------- | |
54 | // wxWin macros | |
55 | // ---------------------------------------------------------------------------- | |
56 | ||
57 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl) | |
58 | ||
59 | // ---------------------------------------------------------------------------- | |
60 | // private functions | |
61 | // ---------------------------------------------------------------------------- | |
62 | ||
63 | static void wxToolButtonCallback (Widget w, XtPointer clientData, | |
64 | XtPointer ptr); | |
65 | static void wxToolButtonPopupCallback (Widget w, XtPointer client_data, | |
66 | XEvent *event, Boolean *continue_to_dispatch); | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // private classes | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | class wxToolBarTimer : public wxTimer | |
73 | { | |
74 | public: | |
75 | virtual void Notify(); | |
76 | ||
77 | static Widget help_popup; | |
78 | static Widget buttonWidget; | |
79 | static wxString helpString; | |
80 | }; | |
81 | ||
82 | class wxToolBarTool : public wxToolBarToolBase | |
83 | { | |
84 | public: | |
85 | wxToolBarTool(wxToolBar *tbar, | |
86 | int id, | |
87 | const wxString& label, | |
88 | const wxBitmap& bmpNormal, | |
89 | const wxBitmap& bmpToggled, | |
90 | wxItemKind kind, | |
91 | wxObject *clientData, | |
92 | const wxString& shortHelp, | |
93 | const wxString& longHelp) | |
94 | : wxToolBarToolBase(tbar, id, label, bmpNormal, bmpToggled, kind, | |
95 | clientData, shortHelp, longHelp) | |
96 | { | |
97 | Init(); | |
98 | } | |
99 | ||
100 | wxToolBarTool(wxToolBar *tbar, wxControl *control) | |
101 | : wxToolBarToolBase(tbar, control) | |
102 | { | |
103 | Init(); | |
104 | } | |
105 | ||
106 | virtual ~wxToolBarTool(); | |
107 | ||
108 | // accessors | |
109 | void SetWidget(Widget widget) { m_widget = widget; } | |
110 | Widget GetButtonWidget() const { return m_widget; } | |
111 | ||
112 | Pixmap GetArmPixmap() | |
113 | { | |
114 | m_bitmapCache.SetBitmap( GetNormalBitmap() ); | |
115 | return (Pixmap)m_bitmapCache.GetArmPixmap( (WXWidget)m_widget ); | |
116 | } | |
117 | ||
118 | Pixmap GetInsensPixmap() | |
119 | { | |
120 | m_bitmapCache.SetBitmap( GetNormalBitmap() ); | |
121 | return (Pixmap)m_bitmapCache.GetInsensPixmap( (WXWidget)m_widget ); | |
122 | } | |
123 | protected: | |
124 | void Init(); | |
125 | ||
126 | Widget m_widget; | |
127 | wxBitmapCache m_bitmapCache; | |
128 | }; | |
129 | ||
130 | // ---------------------------------------------------------------------------- | |
131 | // globals | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
134 | static wxToolBarTimer* wxTheToolBarTimer = (wxToolBarTimer*) NULL; | |
135 | ||
136 | Widget wxToolBarTimer::help_popup = (Widget) 0; | |
137 | Widget wxToolBarTimer::buttonWidget = (Widget) 0; | |
138 | wxString wxToolBarTimer::helpString; | |
139 | ||
140 | // ============================================================================ | |
141 | // implementation | |
142 | // ============================================================================ | |
143 | ||
144 | // ---------------------------------------------------------------------------- | |
145 | // wxToolBarTool | |
146 | // ---------------------------------------------------------------------------- | |
147 | ||
148 | wxToolBarToolBase *wxToolBar::CreateTool(int id, | |
149 | const wxString& label, | |
150 | const wxBitmap& bmpNormal, | |
151 | const wxBitmap& bmpToggled, | |
152 | wxItemKind kind, | |
153 | wxObject *clientData, | |
154 | const wxString& shortHelp, | |
155 | const wxString& longHelp) | |
156 | { | |
157 | return new wxToolBarTool(this, id, label, bmpNormal, bmpToggled, kind, | |
158 | clientData, shortHelp, longHelp); | |
159 | } | |
160 | ||
161 | ||
162 | wxToolBarToolBase *wxToolBar::CreateTool(wxControl *control) | |
163 | { | |
164 | return new wxToolBarTool(this, control); | |
165 | } | |
166 | ||
167 | void wxToolBarTool::Init() | |
168 | { | |
169 | m_widget = (Widget)0; | |
170 | } | |
171 | ||
172 | wxToolBarTool::~wxToolBarTool() | |
173 | { | |
174 | if ( m_widget ) | |
175 | XtDestroyWidget(m_widget); | |
176 | } | |
177 | ||
178 | // ---------------------------------------------------------------------------- | |
179 | // wxToolBar construction | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
182 | void wxToolBar::Init() | |
183 | { | |
184 | m_maxWidth = -1; | |
185 | m_maxHeight = -1; | |
186 | m_defaultWidth = 24; | |
187 | m_defaultHeight = 22; | |
188 | m_toolPacking = 2; | |
189 | m_toolSeparation = 8; | |
190 | m_xMargin = 2; | |
191 | m_yMargin = 2; | |
192 | m_maxRows = 100; | |
193 | m_maxCols = 100; | |
194 | } | |
195 | ||
196 | bool wxToolBar::Create(wxWindow *parent, | |
197 | wxWindowID id, | |
198 | const wxPoint& pos, | |
199 | const wxSize& size, | |
200 | long style, | |
201 | const wxString& name) | |
202 | { | |
203 | if( !wxControl::CreateControl( parent, id, pos, size, style, | |
204 | wxDefaultValidator, name ) ) | |
205 | return false; | |
206 | ||
207 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
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 | ChangeFont(false); | |
233 | ||
234 | wxPoint rPos = pos; | |
235 | wxSize rSize = size; | |
236 | ||
237 | if( rPos.x == -1 ) rPos.x = 0; | |
238 | if( rPos.y == -1 ) rPos.y = 0; | |
239 | if( rSize.x == -1 && GetParent() ) | |
240 | rSize.x = GetParent()->GetSize().x; | |
241 | ||
242 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, | |
243 | rPos.x, rPos.y, rSize.x, rSize.y); | |
244 | ||
245 | ChangeBackgroundColour(); | |
246 | ||
247 | return true; | |
248 | } | |
249 | ||
250 | wxToolBar::~wxToolBar() | |
251 | { | |
252 | delete wxTheToolBarTimer; | |
253 | wxTheToolBarTimer = NULL; | |
254 | } | |
255 | ||
256 | bool wxToolBar::Realize() | |
257 | { | |
258 | if ( m_tools.GetCount() == 0 ) | |
259 | { | |
260 | // nothing to do | |
261 | return true; | |
262 | } | |
263 | ||
264 | bool isVertical = GetWindowStyle() & wxTB_VERTICAL; | |
265 | ||
266 | // Separator spacing | |
267 | const int separatorSize = GetToolSeparation(); // 8; | |
268 | wxSize margins = GetToolMargins(); | |
269 | int packing = GetToolPacking(); | |
270 | int marginX = margins.x; | |
271 | int marginY = margins.y; | |
272 | ||
273 | int currentX = marginX; | |
274 | int currentY = marginY; | |
275 | ||
276 | int buttonHeight = 0, buttonWidth = 0; | |
277 | ||
278 | Widget button; | |
279 | Pixmap pixmap, insensPixmap; | |
280 | wxBitmap bmp, insensBmp; | |
281 | ||
282 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
283 | while ( node ) | |
284 | { | |
285 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
286 | ||
287 | switch ( tool->GetStyle() ) | |
288 | { | |
289 | case wxTOOL_STYLE_CONTROL: | |
290 | { | |
291 | wxControl* control = tool->GetControl(); | |
292 | wxSize sz = control->GetSize(); | |
293 | wxPoint pos = control->GetPosition(); | |
294 | // Allow a control to specify a y[x]-offset by setting | |
295 | // its initial position, but still don't allow it to | |
296 | // position itself above the top[left] margin. | |
297 | int controlY = (pos.y > 0) ? pos.y : currentY; | |
298 | int controlX = (pos.x > 0) ? pos.x : currentX; | |
299 | control->Move( isVertical ? controlX : currentX, | |
300 | isVertical ? currentY : controlY ); | |
301 | if ( isVertical ) | |
302 | currentY += sz.y + packing; | |
303 | else | |
304 | currentX += sz.x + packing; | |
305 | ||
306 | break; | |
307 | } | |
308 | case wxTOOL_STYLE_SEPARATOR: | |
309 | // skip separators for vertical toolbars | |
310 | if( !isVertical ) | |
311 | { | |
312 | currentX += separatorSize; | |
313 | } | |
314 | break; | |
315 | ||
316 | case wxTOOL_STYLE_BUTTON: | |
317 | button = (Widget) 0; | |
318 | ||
319 | if ( tool->CanBeToggled() && !tool->GetButtonWidget() ) | |
320 | { | |
321 | button = XtVaCreateWidget("toggleButton", | |
322 | xmToggleButtonWidgetClass, (Widget) m_mainWidget, | |
323 | XmNx, currentX, XmNy, currentY, | |
324 | XmNindicatorOn, False, | |
325 | XmNshadowThickness, 2, | |
326 | XmNborderWidth, 0, | |
327 | XmNspacing, 0, | |
328 | XmNmarginWidth, 0, | |
329 | XmNmarginHeight, 0, | |
330 | XmNmultiClick, XmMULTICLICK_KEEP, | |
331 | XmNlabelType, XmPIXMAP, | |
332 | NULL); | |
333 | XtAddCallback ((Widget) button, | |
334 | XmNvalueChangedCallback, | |
335 | (XtCallbackProc) wxToolButtonCallback, | |
336 | (XtPointer) this); | |
337 | ||
338 | XtVaSetValues ((Widget) button, | |
339 | XmNselectColor, | |
340 | m_backgroundColour.AllocColour | |
341 | (XtDisplay((Widget) button)), | |
342 | NULL); | |
343 | } | |
344 | else if( !tool->GetButtonWidget() ) | |
345 | { | |
346 | button = XtVaCreateWidget("button", | |
347 | xmPushButtonWidgetClass, (Widget) m_mainWidget, | |
348 | XmNx, currentX, XmNy, currentY, | |
349 | XmNpushButtonEnabled, True, | |
350 | XmNmultiClick, XmMULTICLICK_KEEP, | |
351 | XmNlabelType, XmPIXMAP, | |
352 | NULL); | |
353 | XtAddCallback (button, | |
354 | XmNactivateCallback, | |
355 | (XtCallbackProc) wxToolButtonCallback, | |
356 | (XtPointer) this); | |
357 | } | |
358 | ||
359 | if( !tool->GetButtonWidget() ) | |
360 | { | |
361 | wxDoChangeBackgroundColour((WXWidget) button, | |
362 | m_backgroundColour, true); | |
363 | ||
364 | tool->SetWidget(button); | |
365 | } | |
366 | else | |
367 | { | |
368 | button = (Widget)tool->GetButtonWidget(); | |
369 | XtVaSetValues( button, | |
370 | XmNx, currentX, XmNy, currentY, | |
371 | NULL ); | |
372 | } | |
373 | ||
374 | // For each button, if there is a mask, we must create | |
375 | // a new wxBitmap that has the correct background colour | |
376 | // for the button. Otherwise the background will just be | |
377 | // e.g. black if a transparent XPM has been loaded. | |
378 | bmp = tool->GetNormalBitmap(); | |
379 | insensBmp = tool->GetDisabledBitmap(); | |
380 | if ( bmp.GetMask() || insensBmp.GetMask() ) | |
381 | { | |
382 | int backgroundPixel; | |
383 | XtVaGetValues(button, XmNbackground, &backgroundPixel, | |
384 | NULL); | |
385 | ||
386 | wxColour col; | |
387 | col.SetPixel(backgroundPixel); | |
388 | ||
389 | if( bmp.Ok() && bmp.GetMask() ) | |
390 | { | |
391 | bmp = wxCreateMaskedBitmap(bmp, col); | |
392 | tool->SetNormalBitmap(bmp); | |
393 | } | |
394 | ||
395 | if( insensBmp.Ok() && insensBmp.GetMask() ) | |
396 | { | |
397 | insensBmp = wxCreateMaskedBitmap(insensBmp, col); | |
398 | tool->SetDisabledBitmap(insensBmp); | |
399 | } | |
400 | } | |
401 | ||
402 | // Create a selected/toggled bitmap. If there isn't a 2nd | |
403 | // bitmap, we need to create it (with a darker, selected | |
404 | // background) | |
405 | int backgroundPixel; | |
406 | if ( tool->CanBeToggled() ) | |
407 | XtVaGetValues(button, XmNselectColor, &backgroundPixel, | |
408 | NULL); | |
409 | else | |
410 | XtVaGetValues(button, XmNarmColor, &backgroundPixel, | |
411 | NULL); | |
412 | wxColour col; | |
413 | col.SetPixel(backgroundPixel); | |
414 | ||
415 | pixmap = (Pixmap) bmp.GetDrawable(); | |
416 | { | |
417 | wxBitmap tmp = tool->GetDisabledBitmap(); | |
418 | ||
419 | insensPixmap = tmp.Ok() ? | |
420 | (Pixmap)tmp.GetDrawable() : | |
421 | tool->GetInsensPixmap(); | |
422 | } | |
423 | ||
424 | if (tool->CanBeToggled()) | |
425 | { | |
426 | // Toggle button | |
427 | Pixmap pixmap2 = tool->GetArmPixmap(); | |
428 | Pixmap insensPixmap2 = tool->GetInsensPixmap(); | |
429 | ||
430 | XtVaSetValues (button, | |
431 | XmNfillOnSelect, True, | |
432 | XmNlabelPixmap, pixmap, | |
433 | XmNselectPixmap, pixmap2, | |
434 | XmNlabelInsensitivePixmap, insensPixmap, | |
435 | XmNselectInsensitivePixmap, insensPixmap2, | |
436 | XmNlabelType, XmPIXMAP, | |
437 | NULL); | |
438 | } | |
439 | else | |
440 | { | |
441 | Pixmap pixmap2 = tool->GetArmPixmap(); | |
442 | ||
443 | // Normal button | |
444 | XtVaSetValues(button, | |
445 | XmNlabelPixmap, pixmap, | |
446 | XmNlabelInsensitivePixmap, insensPixmap, | |
447 | XmNarmPixmap, pixmap2, | |
448 | NULL); | |
449 | } | |
450 | ||
451 | XtManageChild(button); | |
452 | ||
453 | { | |
454 | Dimension width, height; | |
455 | XtVaGetValues(button, | |
456 | XmNwidth, &width, | |
457 | XmNheight, & height, | |
458 | NULL); | |
459 | if ( isVertical ) | |
460 | currentY += height + packing; | |
461 | else | |
462 | currentX += width + packing; | |
463 | buttonHeight = wxMax(buttonHeight, height); | |
464 | buttonWidth = wxMax(buttonWidth, width); | |
465 | } | |
466 | ||
467 | XtAddEventHandler (button, EnterWindowMask | LeaveWindowMask, | |
468 | False, wxToolButtonPopupCallback, (XtPointer) this); | |
469 | ||
470 | break; | |
471 | } | |
472 | ||
473 | node = node->GetNext(); | |
474 | } | |
475 | ||
476 | SetSize( -1, -1, | |
477 | isVertical ? buttonWidth + 2 * marginX : -1, | |
478 | isVertical ? -1 : buttonHeight + 2*marginY ); | |
479 | ||
480 | return true; | |
481 | } | |
482 | ||
483 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), | |
484 | wxCoord WXUNUSED(y)) const | |
485 | { | |
486 | wxFAIL_MSG( _T("TODO") ); | |
487 | ||
488 | return (wxToolBarToolBase *)NULL; | |
489 | } | |
490 | ||
491 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) | |
492 | { | |
493 | tool->Attach(this); | |
494 | ||
495 | return true; | |
496 | } | |
497 | ||
498 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) | |
499 | { | |
500 | tool->Detach(); | |
501 | ||
502 | bool isVertical = GetWindowStyle() & wxTB_VERTICAL; | |
503 | const int separatorSize = GetToolSeparation(); // 8; | |
504 | int packing = GetToolPacking(); | |
505 | int offset = 0; | |
506 | ||
507 | for( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
508 | node; node = node->GetNext() ) | |
509 | { | |
510 | wxToolBarTool *t = (wxToolBarTool*)node->GetData(); | |
511 | ||
512 | if( t == tool ) | |
513 | { | |
514 | switch ( t->GetStyle() ) | |
515 | { | |
516 | case wxTOOL_STYLE_CONTROL: | |
517 | { | |
518 | wxSize size = t->GetControl()->GetSize(); | |
519 | offset = isVertical ? size.y : size.x; | |
520 | offset += packing; | |
521 | break; | |
522 | } | |
523 | case wxTOOL_STYLE_SEPARATOR: | |
524 | offset = isVertical ? 0 : separatorSize; | |
525 | break; | |
526 | case wxTOOL_STYLE_BUTTON: | |
527 | { | |
528 | Widget w = t->GetButtonWidget(); | |
529 | Dimension width, height; | |
530 | ||
531 | XtVaGetValues( w, | |
532 | XmNwidth, &width, | |
533 | XmNheight, &height, | |
534 | NULL ); | |
535 | ||
536 | offset = isVertical ? height : width; | |
537 | offset += packing; | |
538 | break; | |
539 | } | |
540 | } | |
541 | } | |
542 | else if( offset ) | |
543 | { | |
544 | switch ( t->GetStyle() ) | |
545 | { | |
546 | case wxTOOL_STYLE_CONTROL: | |
547 | { | |
548 | wxPoint location = t->GetControl()->GetPosition(); | |
549 | ||
550 | if( isVertical ) | |
551 | location.y -= offset; | |
552 | else | |
553 | location.x -= offset; | |
554 | ||
555 | t->GetControl()->Move( location ); | |
556 | break; | |
557 | } | |
558 | case wxTOOL_STYLE_SEPARATOR: | |
559 | break; | |
560 | case wxTOOL_STYLE_BUTTON: | |
561 | { | |
562 | Dimension x, y; | |
563 | XtVaGetValues( t->GetButtonWidget(), | |
564 | XmNx, &x, | |
565 | XmNy, &y, | |
566 | NULL ); | |
567 | ||
568 | if( isVertical ) | |
569 | y = (Dimension)(y - offset); | |
570 | else | |
571 | x = (Dimension)(x - offset); | |
572 | ||
573 | XtVaSetValues( t->GetButtonWidget(), | |
574 | XmNx, x, | |
575 | XmNy, y, | |
576 | NULL ); | |
577 | break; | |
578 | } | |
579 | } | |
580 | } | |
581 | } | |
582 | ||
583 | return true; | |
584 | } | |
585 | ||
586 | void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable) | |
587 | { | |
588 | wxToolBarTool *tool = (wxToolBarTool *)toolBase; | |
589 | if (tool->GetButtonWidget()){ | |
590 | XtSetSensitive(tool->GetButtonWidget(), (Boolean) enable); | |
591 | } else if (wxTOOL_STYLE_CONTROL == tool->GetStyle()){ | |
592 | // Controls (such as wxChoice) do not have button widgets | |
593 | tool->GetControl()->Enable(enable); | |
594 | } | |
595 | } | |
596 | ||
597 | void wxToolBar::DoToggleTool(wxToolBarToolBase *toolBase, bool toggle) | |
598 | { | |
599 | wxToolBarTool *tool = (wxToolBarTool *)toolBase; | |
600 | ||
601 | XmToggleButtonSetState(tool->GetButtonWidget(), (Boolean) toggle, False); | |
602 | } | |
603 | ||
604 | void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool), | |
605 | bool WXUNUSED(toggle)) | |
606 | { | |
607 | // nothing to do | |
608 | } | |
609 | ||
610 | void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
611 | { | |
612 | int old_width, old_height; | |
613 | GetSize(&old_width, &old_height); | |
614 | ||
615 | // Correct width and height if needed. | |
616 | if ( width == -1 || height == -1 ) | |
617 | { | |
618 | wxSize defaultSize = GetSize(); | |
619 | ||
620 | if ( width == -1 ) | |
621 | width = defaultSize.x; | |
622 | if ( height == -1 ) | |
623 | height = defaultSize.y; | |
624 | } | |
625 | ||
626 | wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags); | |
627 | ||
628 | // We must refresh the frame size when the toolbar changes size | |
629 | // otherwise the toolbar can be shown incorrectly | |
630 | if ( old_width != width || old_height != height ) | |
631 | { | |
632 | // But before we send the size event check it | |
633 | // we have a frame that is not being deleted. | |
634 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); | |
635 | if ( frame && !frame->IsBeingDeleted() ) | |
636 | { | |
637 | frame->SendSizeEvent(); | |
638 | } | |
639 | } | |
640 | } | |
641 | ||
642 | // ---------------------------------------------------------------------------- | |
643 | // Motif callbacks | |
644 | // ---------------------------------------------------------------------------- | |
645 | ||
646 | wxToolBarToolBase *wxToolBar::FindToolByWidget(WXWidget w) const | |
647 | { | |
648 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
649 | while ( node ) | |
650 | { | |
651 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
652 | if ( tool->GetButtonWidget() == w) | |
653 | { | |
654 | return tool; | |
655 | } | |
656 | ||
657 | node = node->GetNext(); | |
658 | } | |
659 | ||
660 | return (wxToolBarToolBase *)NULL; | |
661 | } | |
662 | ||
663 | static void wxToolButtonCallback(Widget w, | |
664 | XtPointer clientData, | |
665 | XtPointer WXUNUSED(ptr)) | |
666 | { | |
667 | wxToolBar *toolBar = (wxToolBar *) clientData; | |
668 | wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w); | |
669 | if ( !tool ) | |
670 | return; | |
671 | ||
672 | if ( tool->CanBeToggled() ) | |
673 | tool->Toggle(); | |
674 | ||
675 | if ( !toolBar->OnLeftClick(tool->GetId(), tool->IsToggled()) ) | |
676 | { | |
677 | // revert | |
678 | tool->Toggle(); | |
679 | } | |
680 | } | |
681 | ||
682 | ||
683 | static void wxToolButtonPopupCallback(Widget w, | |
684 | XtPointer client_data, | |
685 | XEvent *event, | |
686 | Boolean *WXUNUSED(continue_to_dispatch)) | |
687 | { | |
688 | // TODO: retrieve delay before popping up tooltip from wxSystemSettings. | |
689 | static const int delayMilli = 800; | |
690 | ||
691 | wxToolBar* toolBar = (wxToolBar*) client_data; | |
692 | wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w); | |
693 | ||
694 | if ( !tool ) | |
695 | return; | |
696 | ||
697 | wxString tooltip = tool->GetShortHelp(); | |
698 | if ( !tooltip ) | |
699 | return; | |
700 | ||
701 | if (!wxTheToolBarTimer) | |
702 | wxTheToolBarTimer = new wxToolBarTimer; | |
703 | ||
704 | wxToolBarTimer::buttonWidget = w; | |
705 | wxToolBarTimer::helpString = tooltip; | |
706 | ||
707 | /************************************************************/ | |
708 | /* Popup help label */ | |
709 | /************************************************************/ | |
710 | if (event->type == EnterNotify) | |
711 | { | |
712 | if (wxToolBarTimer::help_popup != (Widget) 0) | |
713 | { | |
714 | XtDestroyWidget (wxToolBarTimer::help_popup); | |
715 | XtPopdown (wxToolBarTimer::help_popup); | |
716 | } | |
717 | wxToolBarTimer::help_popup = (Widget) 0; | |
718 | ||
719 | // One shot | |
720 | wxTheToolBarTimer->Start(delayMilli, true); | |
721 | ||
722 | } | |
723 | /************************************************************/ | |
724 | /* Popdown help label */ | |
725 | /************************************************************/ | |
726 | else if (event->type == LeaveNotify) | |
727 | { | |
728 | if (wxTheToolBarTimer) | |
729 | wxTheToolBarTimer->Stop(); | |
730 | if (wxToolBarTimer::help_popup != (Widget) 0) | |
731 | { | |
732 | XtDestroyWidget (wxToolBarTimer::help_popup); | |
733 | XtPopdown (wxToolBarTimer::help_popup); | |
734 | } | |
735 | wxToolBarTimer::help_popup = (Widget) 0; | |
736 | } | |
737 | } | |
738 | ||
739 | void wxToolBarTimer::Notify() | |
740 | { | |
741 | Position x, y; | |
742 | ||
743 | /************************************************************/ | |
744 | /* Create shell without window decorations */ | |
745 | /************************************************************/ | |
746 | help_popup = XtVaCreatePopupShell ("shell", | |
747 | overrideShellWidgetClass, (Widget) wxTheApp->GetTopLevelWidget(), | |
748 | NULL); | |
749 | ||
750 | /************************************************************/ | |
751 | /* Get absolute position on display of toolbar button */ | |
752 | /************************************************************/ | |
753 | XtTranslateCoords (buttonWidget, | |
754 | (Position) 0, | |
755 | (Position) 0, | |
756 | &x, &y); | |
757 | ||
758 | // Move the tooltip more or less above the button | |
759 | int yOffset = 20; // TODO: What should be really? | |
760 | y = (Position)(y - yOffset); | |
761 | if (y < yOffset) y = 0; | |
762 | ||
763 | /************************************************************/ | |
764 | /* Set the position of the help popup */ | |
765 | /************************************************************/ | |
766 | XtVaSetValues (help_popup, | |
767 | XmNx, (Position) x, | |
768 | XmNy, (Position) y, | |
769 | NULL); | |
770 | ||
771 | /************************************************************/ | |
772 | /* Create help label */ | |
773 | /************************************************************/ | |
774 | XmString text = XmStringCreateSimple ((char*) (const char*) helpString); | |
775 | XtVaCreateManagedWidget ("help_label", | |
776 | xmLabelWidgetClass, help_popup, | |
777 | XmNlabelString, text, | |
778 | XtVaTypedArg, | |
779 | XmNforeground, XtRString, "black", | |
780 | strlen("black")+1, | |
781 | XtVaTypedArg, | |
782 | XmNbackground, XtRString, "LightGoldenrod", | |
783 | strlen("LightGoldenrod")+1, | |
784 | NULL); | |
785 | XmStringFree (text); | |
786 | ||
787 | /************************************************************/ | |
788 | /* Popup help label */ | |
789 | /************************************************************/ | |
790 | XtPopup (help_popup, XtGrabNone); | |
791 | } |