]>
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 | FixupStyle(); | |
208 | ||
209 | m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
210 | ||
211 | Widget parentWidget = (Widget) parent->GetClientWidget(); | |
212 | ||
213 | Widget toolbar = XtVaCreateManagedWidget("toolbar", | |
214 | xmBulletinBoardWidgetClass, (Widget) parentWidget, | |
215 | XmNmarginWidth, 0, | |
216 | XmNmarginHeight, 0, | |
217 | XmNresizePolicy, XmRESIZE_NONE, | |
218 | NULL); | |
219 | /* | |
220 | Widget toolbar = XtVaCreateManagedWidget("toolbar", | |
221 | xmFormWidgetClass, (Widget) m_clientWidget, | |
222 | XmNtraversalOn, False, | |
223 | XmNhorizontalSpacing, 0, | |
224 | XmNverticalSpacing, 0, | |
225 | XmNleftOffset, 0, | |
226 | XmNrightOffset, 0, | |
227 | XmNmarginWidth, 0, | |
228 | XmNmarginHeight, 0, | |
229 | NULL); | |
230 | */ | |
231 | ||
232 | m_mainWidget = (WXWidget) toolbar; | |
233 | ||
234 | ChangeFont(false); | |
235 | ||
236 | wxPoint rPos = pos; | |
237 | wxSize rSize = size; | |
238 | ||
239 | if( rPos.x == -1 ) rPos.x = 0; | |
240 | if( rPos.y == -1 ) rPos.y = 0; | |
241 | if( rSize.x == -1 && GetParent() ) | |
242 | rSize.x = GetParent()->GetSize().x; | |
243 | ||
244 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, | |
245 | rPos.x, rPos.y, rSize.x, rSize.y); | |
246 | ||
247 | ChangeBackgroundColour(); | |
248 | ||
249 | return true; | |
250 | } | |
251 | ||
252 | wxToolBar::~wxToolBar() | |
253 | { | |
254 | delete wxTheToolBarTimer; | |
255 | wxTheToolBarTimer = NULL; | |
256 | } | |
257 | ||
258 | bool wxToolBar::Realize() | |
259 | { | |
260 | if ( m_tools.GetCount() == 0 ) | |
261 | { | |
262 | // nothing to do | |
263 | return true; | |
264 | } | |
265 | ||
266 | bool isVertical = GetWindowStyle() & wxTB_VERTICAL; | |
267 | ||
268 | // Separator spacing | |
269 | const int separatorSize = GetToolSeparation(); // 8; | |
270 | wxSize margins = GetToolMargins(); | |
271 | int packing = GetToolPacking(); | |
272 | int marginX = margins.x; | |
273 | int marginY = margins.y; | |
274 | ||
275 | int currentX = marginX; | |
276 | int currentY = marginY; | |
277 | ||
278 | int buttonHeight = 0, buttonWidth = 0; | |
279 | ||
280 | Widget button; | |
281 | Pixmap pixmap, insensPixmap; | |
282 | wxBitmap bmp, insensBmp; | |
283 | ||
284 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
285 | while ( node ) | |
286 | { | |
287 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
288 | ||
289 | switch ( tool->GetStyle() ) | |
290 | { | |
291 | case wxTOOL_STYLE_CONTROL: | |
292 | { | |
293 | wxControl* control = tool->GetControl(); | |
294 | wxSize sz = control->GetSize(); | |
295 | wxPoint pos = control->GetPosition(); | |
296 | // Allow a control to specify a y[x]-offset by setting | |
297 | // its initial position, but still don't allow it to | |
298 | // position itself above the top[left] margin. | |
299 | int controlY = (pos.y > 0) ? pos.y : currentY; | |
300 | int controlX = (pos.x > 0) ? pos.x : currentX; | |
301 | control->Move( isVertical ? controlX : currentX, | |
302 | isVertical ? currentY : controlY ); | |
303 | if ( isVertical ) | |
304 | currentY += sz.y + packing; | |
305 | else | |
306 | currentX += sz.x + packing; | |
307 | ||
308 | break; | |
309 | } | |
310 | case wxTOOL_STYLE_SEPARATOR: | |
311 | // skip separators for vertical toolbars | |
312 | if( !isVertical ) | |
313 | { | |
314 | currentX += separatorSize; | |
315 | } | |
316 | break; | |
317 | ||
318 | case wxTOOL_STYLE_BUTTON: | |
319 | button = (Widget) 0; | |
320 | ||
321 | if ( tool->CanBeToggled() && !tool->GetButtonWidget() ) | |
322 | { | |
323 | button = XtVaCreateWidget("toggleButton", | |
324 | xmToggleButtonWidgetClass, (Widget) m_mainWidget, | |
325 | XmNx, currentX, XmNy, currentY, | |
326 | XmNindicatorOn, False, | |
327 | XmNshadowThickness, 2, | |
328 | XmNborderWidth, 0, | |
329 | XmNspacing, 0, | |
330 | XmNmarginWidth, 0, | |
331 | XmNmarginHeight, 0, | |
332 | XmNmultiClick, XmMULTICLICK_KEEP, | |
333 | XmNlabelType, XmPIXMAP, | |
334 | NULL); | |
335 | XtAddCallback ((Widget) button, | |
336 | XmNvalueChangedCallback, | |
337 | (XtCallbackProc) wxToolButtonCallback, | |
338 | (XtPointer) this); | |
339 | ||
340 | XtVaSetValues ((Widget) button, | |
341 | XmNselectColor, | |
342 | m_backgroundColour.AllocColour | |
343 | (XtDisplay((Widget) button)), | |
344 | NULL); | |
345 | } | |
346 | else if( !tool->GetButtonWidget() ) | |
347 | { | |
348 | button = XtVaCreateWidget("button", | |
349 | xmPushButtonWidgetClass, (Widget) m_mainWidget, | |
350 | XmNx, currentX, XmNy, currentY, | |
351 | XmNpushButtonEnabled, True, | |
352 | XmNmultiClick, XmMULTICLICK_KEEP, | |
353 | XmNlabelType, XmPIXMAP, | |
354 | NULL); | |
355 | XtAddCallback (button, | |
356 | XmNactivateCallback, | |
357 | (XtCallbackProc) wxToolButtonCallback, | |
358 | (XtPointer) this); | |
359 | } | |
360 | ||
361 | if( !tool->GetButtonWidget() ) | |
362 | { | |
363 | wxDoChangeBackgroundColour((WXWidget) button, | |
364 | m_backgroundColour, true); | |
365 | ||
366 | tool->SetWidget(button); | |
367 | } | |
368 | else | |
369 | { | |
370 | button = (Widget)tool->GetButtonWidget(); | |
371 | XtVaSetValues( button, | |
372 | XmNx, currentX, XmNy, currentY, | |
373 | NULL ); | |
374 | } | |
375 | ||
376 | // For each button, if there is a mask, we must create | |
377 | // a new wxBitmap that has the correct background colour | |
378 | // for the button. Otherwise the background will just be | |
379 | // e.g. black if a transparent XPM has been loaded. | |
380 | bmp = tool->GetNormalBitmap(); | |
381 | insensBmp = tool->GetDisabledBitmap(); | |
382 | if ( bmp.GetMask() || insensBmp.GetMask() ) | |
383 | { | |
384 | WXPixel backgroundPixel; | |
385 | XtVaGetValues(button, XmNbackground, &backgroundPixel, | |
386 | NULL); | |
387 | ||
388 | wxColour col; | |
389 | col.SetPixel(backgroundPixel); | |
390 | ||
391 | if( bmp.Ok() && bmp.GetMask() ) | |
392 | { | |
393 | bmp = wxCreateMaskedBitmap(bmp, col); | |
394 | tool->SetNormalBitmap(bmp); | |
395 | } | |
396 | ||
397 | if( insensBmp.Ok() && insensBmp.GetMask() ) | |
398 | { | |
399 | insensBmp = wxCreateMaskedBitmap(insensBmp, col); | |
400 | tool->SetDisabledBitmap(insensBmp); | |
401 | } | |
402 | } | |
403 | ||
404 | // Create a selected/toggled bitmap. If there isn't a 2nd | |
405 | // bitmap, we need to create it (with a darker, selected | |
406 | // background) | |
407 | WXPixel backgroundPixel; | |
408 | if ( tool->CanBeToggled() ) | |
409 | XtVaGetValues(button, XmNselectColor, &backgroundPixel, | |
410 | NULL); | |
411 | else | |
412 | XtVaGetValues(button, XmNarmColor, &backgroundPixel, | |
413 | NULL); | |
414 | wxColour col; | |
415 | col.SetPixel(backgroundPixel); | |
416 | ||
417 | pixmap = (Pixmap) bmp.GetDrawable(); | |
418 | { | |
419 | wxBitmap tmp = tool->GetDisabledBitmap(); | |
420 | ||
421 | insensPixmap = tmp.Ok() ? | |
422 | (Pixmap)tmp.GetDrawable() : | |
423 | tool->GetInsensPixmap(); | |
424 | } | |
425 | ||
426 | if (tool->CanBeToggled()) | |
427 | { | |
428 | // Toggle button | |
429 | Pixmap pixmap2 = tool->GetArmPixmap(); | |
430 | Pixmap insensPixmap2 = tool->GetInsensPixmap(); | |
431 | ||
432 | XtVaSetValues (button, | |
433 | XmNfillOnSelect, True, | |
434 | XmNlabelPixmap, pixmap, | |
435 | XmNselectPixmap, pixmap2, | |
436 | XmNlabelInsensitivePixmap, insensPixmap, | |
437 | XmNselectInsensitivePixmap, insensPixmap2, | |
438 | XmNlabelType, XmPIXMAP, | |
439 | NULL); | |
440 | } | |
441 | else | |
442 | { | |
443 | Pixmap pixmap2 = tool->GetArmPixmap(); | |
444 | ||
445 | // Normal button | |
446 | XtVaSetValues(button, | |
447 | XmNlabelPixmap, pixmap, | |
448 | XmNlabelInsensitivePixmap, insensPixmap, | |
449 | XmNarmPixmap, pixmap2, | |
450 | NULL); | |
451 | } | |
452 | ||
453 | XtManageChild(button); | |
454 | ||
455 | { | |
456 | Dimension width, height; | |
457 | XtVaGetValues(button, | |
458 | XmNwidth, &width, | |
459 | XmNheight, & height, | |
460 | NULL); | |
461 | if ( isVertical ) | |
462 | currentY += height + packing; | |
463 | else | |
464 | currentX += width + packing; | |
465 | buttonHeight = wxMax(buttonHeight, height); | |
466 | buttonWidth = wxMax(buttonWidth, width); | |
467 | } | |
468 | ||
469 | XtAddEventHandler (button, EnterWindowMask | LeaveWindowMask, | |
470 | False, wxToolButtonPopupCallback, (XtPointer) this); | |
471 | ||
472 | break; | |
473 | } | |
474 | ||
475 | node = node->GetNext(); | |
476 | } | |
477 | ||
478 | SetSize( -1, -1, | |
479 | isVertical ? buttonWidth + 2 * marginX : -1, | |
480 | isVertical ? -1 : buttonHeight + 2*marginY ); | |
481 | ||
482 | return true; | |
483 | } | |
484 | ||
485 | wxToolBarToolBase *wxToolBar::FindToolForPosition(wxCoord WXUNUSED(x), | |
486 | wxCoord WXUNUSED(y)) const | |
487 | { | |
488 | wxFAIL_MSG( _T("TODO") ); | |
489 | ||
490 | return (wxToolBarToolBase *)NULL; | |
491 | } | |
492 | ||
493 | bool wxToolBar::DoInsertTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) | |
494 | { | |
495 | tool->Attach(this); | |
496 | ||
497 | return true; | |
498 | } | |
499 | ||
500 | bool wxToolBar::DoDeleteTool(size_t WXUNUSED(pos), wxToolBarToolBase *tool) | |
501 | { | |
502 | tool->Detach(); | |
503 | ||
504 | bool isVertical = GetWindowStyle() & wxTB_VERTICAL; | |
505 | const int separatorSize = GetToolSeparation(); // 8; | |
506 | int packing = GetToolPacking(); | |
507 | int offset = 0; | |
508 | ||
509 | for( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
510 | node; node = node->GetNext() ) | |
511 | { | |
512 | wxToolBarTool *t = (wxToolBarTool*)node->GetData(); | |
513 | ||
514 | if( t == tool ) | |
515 | { | |
516 | switch ( t->GetStyle() ) | |
517 | { | |
518 | case wxTOOL_STYLE_CONTROL: | |
519 | { | |
520 | wxSize size = t->GetControl()->GetSize(); | |
521 | offset = isVertical ? size.y : size.x; | |
522 | offset += packing; | |
523 | break; | |
524 | } | |
525 | case wxTOOL_STYLE_SEPARATOR: | |
526 | offset = isVertical ? 0 : separatorSize; | |
527 | break; | |
528 | case wxTOOL_STYLE_BUTTON: | |
529 | { | |
530 | Widget w = t->GetButtonWidget(); | |
531 | Dimension width, height; | |
532 | ||
533 | XtVaGetValues( w, | |
534 | XmNwidth, &width, | |
535 | XmNheight, &height, | |
536 | NULL ); | |
537 | ||
538 | offset = isVertical ? height : width; | |
539 | offset += packing; | |
540 | break; | |
541 | } | |
542 | } | |
543 | } | |
544 | else if( offset ) | |
545 | { | |
546 | switch ( t->GetStyle() ) | |
547 | { | |
548 | case wxTOOL_STYLE_CONTROL: | |
549 | { | |
550 | wxPoint location = t->GetControl()->GetPosition(); | |
551 | ||
552 | if( isVertical ) | |
553 | location.y -= offset; | |
554 | else | |
555 | location.x -= offset; | |
556 | ||
557 | t->GetControl()->Move( location ); | |
558 | break; | |
559 | } | |
560 | case wxTOOL_STYLE_SEPARATOR: | |
561 | break; | |
562 | case wxTOOL_STYLE_BUTTON: | |
563 | { | |
564 | Dimension x, y; | |
565 | XtVaGetValues( t->GetButtonWidget(), | |
566 | XmNx, &x, | |
567 | XmNy, &y, | |
568 | NULL ); | |
569 | ||
570 | if( isVertical ) | |
571 | y = (Dimension)(y - offset); | |
572 | else | |
573 | x = (Dimension)(x - offset); | |
574 | ||
575 | XtVaSetValues( t->GetButtonWidget(), | |
576 | XmNx, x, | |
577 | XmNy, y, | |
578 | NULL ); | |
579 | break; | |
580 | } | |
581 | } | |
582 | } | |
583 | } | |
584 | ||
585 | return true; | |
586 | } | |
587 | ||
588 | void wxToolBar::DoEnableTool(wxToolBarToolBase *toolBase, bool enable) | |
589 | { | |
590 | wxToolBarTool *tool = (wxToolBarTool *)toolBase; | |
591 | if (tool->GetButtonWidget()){ | |
592 | XtSetSensitive(tool->GetButtonWidget(), (Boolean) enable); | |
593 | } else if (wxTOOL_STYLE_CONTROL == tool->GetStyle()){ | |
594 | // Controls (such as wxChoice) do not have button widgets | |
595 | tool->GetControl()->Enable(enable); | |
596 | } | |
597 | } | |
598 | ||
599 | void wxToolBar::DoToggleTool(wxToolBarToolBase *toolBase, bool toggle) | |
600 | { | |
601 | wxToolBarTool *tool = (wxToolBarTool *)toolBase; | |
602 | ||
603 | XmToggleButtonSetState(tool->GetButtonWidget(), (Boolean) toggle, False); | |
604 | } | |
605 | ||
606 | void wxToolBar::DoSetToggle(wxToolBarToolBase * WXUNUSED(tool), | |
607 | bool WXUNUSED(toggle)) | |
608 | { | |
609 | // nothing to do | |
610 | } | |
611 | ||
612 | void wxToolBar::DoSetSize(int x, int y, int width, int height, int sizeFlags) | |
613 | { | |
614 | int old_width, old_height; | |
615 | GetSize(&old_width, &old_height); | |
616 | ||
617 | // Correct width and height if needed. | |
618 | if ( width == -1 || height == -1 ) | |
619 | { | |
620 | wxSize defaultSize = GetSize(); | |
621 | ||
622 | if ( width == -1 ) | |
623 | width = defaultSize.x; | |
624 | if ( height == -1 ) | |
625 | height = defaultSize.y; | |
626 | } | |
627 | ||
628 | wxToolBarBase::DoSetSize(x, y, width, height, sizeFlags); | |
629 | ||
630 | // We must refresh the frame size when the toolbar changes size | |
631 | // otherwise the toolbar can be shown incorrectly | |
632 | if ( old_width != width || old_height != height ) | |
633 | { | |
634 | // But before we send the size event check it | |
635 | // we have a frame that is not being deleted. | |
636 | wxFrame *frame = wxDynamicCast(GetParent(), wxFrame); | |
637 | if ( frame && !frame->IsBeingDeleted() ) | |
638 | { | |
639 | frame->SendSizeEvent(); | |
640 | } | |
641 | } | |
642 | } | |
643 | ||
644 | // ---------------------------------------------------------------------------- | |
645 | // Motif callbacks | |
646 | // ---------------------------------------------------------------------------- | |
647 | ||
648 | wxToolBarToolBase *wxToolBar::FindToolByWidget(WXWidget w) const | |
649 | { | |
650 | wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst(); | |
651 | while ( node ) | |
652 | { | |
653 | wxToolBarTool *tool = (wxToolBarTool *)node->GetData(); | |
654 | if ( tool->GetButtonWidget() == w) | |
655 | { | |
656 | return tool; | |
657 | } | |
658 | ||
659 | node = node->GetNext(); | |
660 | } | |
661 | ||
662 | return (wxToolBarToolBase *)NULL; | |
663 | } | |
664 | ||
665 | static void wxToolButtonCallback(Widget w, | |
666 | XtPointer clientData, | |
667 | XtPointer WXUNUSED(ptr)) | |
668 | { | |
669 | wxToolBar *toolBar = (wxToolBar *) clientData; | |
670 | wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w); | |
671 | if ( !tool ) | |
672 | return; | |
673 | ||
674 | if ( tool->CanBeToggled() ) | |
675 | tool->Toggle(); | |
676 | ||
677 | if ( !toolBar->OnLeftClick(tool->GetId(), tool->IsToggled()) ) | |
678 | { | |
679 | // revert | |
680 | tool->Toggle(); | |
681 | } | |
682 | } | |
683 | ||
684 | ||
685 | static void wxToolButtonPopupCallback(Widget w, | |
686 | XtPointer client_data, | |
687 | XEvent *event, | |
688 | Boolean *WXUNUSED(continue_to_dispatch)) | |
689 | { | |
690 | // TODO: retrieve delay before popping up tooltip from wxSystemSettings. | |
691 | static const int delayMilli = 800; | |
692 | ||
693 | wxToolBar* toolBar = (wxToolBar*) client_data; | |
694 | wxToolBarToolBase *tool = toolBar->FindToolByWidget((WXWidget) w); | |
695 | ||
696 | if ( !tool ) | |
697 | return; | |
698 | ||
699 | wxString tooltip = tool->GetShortHelp(); | |
700 | if ( !tooltip ) | |
701 | return; | |
702 | ||
703 | if (!wxTheToolBarTimer) | |
704 | wxTheToolBarTimer = new wxToolBarTimer; | |
705 | ||
706 | wxToolBarTimer::buttonWidget = w; | |
707 | wxToolBarTimer::helpString = tooltip; | |
708 | ||
709 | /************************************************************/ | |
710 | /* Popup help label */ | |
711 | /************************************************************/ | |
712 | if (event->type == EnterNotify) | |
713 | { | |
714 | if (wxToolBarTimer::help_popup != (Widget) 0) | |
715 | { | |
716 | XtDestroyWidget (wxToolBarTimer::help_popup); | |
717 | XtPopdown (wxToolBarTimer::help_popup); | |
718 | } | |
719 | wxToolBarTimer::help_popup = (Widget) 0; | |
720 | ||
721 | // One shot | |
722 | wxTheToolBarTimer->Start(delayMilli, true); | |
723 | ||
724 | } | |
725 | /************************************************************/ | |
726 | /* Popdown help label */ | |
727 | /************************************************************/ | |
728 | else if (event->type == LeaveNotify) | |
729 | { | |
730 | if (wxTheToolBarTimer) | |
731 | wxTheToolBarTimer->Stop(); | |
732 | if (wxToolBarTimer::help_popup != (Widget) 0) | |
733 | { | |
734 | XtDestroyWidget (wxToolBarTimer::help_popup); | |
735 | XtPopdown (wxToolBarTimer::help_popup); | |
736 | } | |
737 | wxToolBarTimer::help_popup = (Widget) 0; | |
738 | } | |
739 | } | |
740 | ||
741 | void wxToolBarTimer::Notify() | |
742 | { | |
743 | Position x, y; | |
744 | ||
745 | /************************************************************/ | |
746 | /* Create shell without window decorations */ | |
747 | /************************************************************/ | |
748 | help_popup = XtVaCreatePopupShell ("shell", | |
749 | overrideShellWidgetClass, (Widget) wxTheApp->GetTopLevelWidget(), | |
750 | NULL); | |
751 | ||
752 | /************************************************************/ | |
753 | /* Get absolute position on display of toolbar button */ | |
754 | /************************************************************/ | |
755 | XtTranslateCoords (buttonWidget, | |
756 | (Position) 0, | |
757 | (Position) 0, | |
758 | &x, &y); | |
759 | ||
760 | // Move the tooltip more or less above the button | |
761 | int yOffset = 20; // TODO: What should be really? | |
762 | y = (Position)(y - yOffset); | |
763 | if (y < yOffset) y = 0; | |
764 | ||
765 | /************************************************************/ | |
766 | /* Set the position of the help popup */ | |
767 | /************************************************************/ | |
768 | XtVaSetValues (help_popup, | |
769 | XmNx, (Position) x, | |
770 | XmNy, (Position) y, | |
771 | NULL); | |
772 | ||
773 | /************************************************************/ | |
774 | /* Create help label */ | |
775 | /************************************************************/ | |
776 | XmString text = XmStringCreateSimple ((char*) (const char*) helpString); | |
777 | XtVaCreateManagedWidget ("help_label", | |
778 | xmLabelWidgetClass, help_popup, | |
779 | XmNlabelString, text, | |
780 | XtVaTypedArg, | |
781 | XmNforeground, XtRString, "black", | |
782 | strlen("black")+1, | |
783 | XtVaTypedArg, | |
784 | XmNbackground, XtRString, "LightGoldenrod", | |
785 | strlen("LightGoldenrod")+1, | |
786 | NULL); | |
787 | XmStringFree (text); | |
788 | ||
789 | /************************************************************/ | |
790 | /* Popup help label */ | |
791 | /************************************************************/ | |
792 | XtPopup (help_popup, XtGrabNone); | |
793 | } |