]> git.saurik.com Git - wxWidgets.git/blob - src/os2/toolbar.cpp
guard for 10.2
[wxWidgets.git] / src / os2 / toolbar.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: toolbar.cpp
3 // Purpose: wxToolBar
4 // Author: David Webster
5 // Modified by:
6 // Created: 06/30/02
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE
16
17 #ifndef WX_PRECOMP
18 #include "wx/settings.h"
19 #include "wx/window.h"
20 #include "wx/frame.h"
21 #include "wx/app.h"
22 #include "wx/dcclient.h"
23 #include "wx/dcmemory.h"
24 #endif
25
26 #include "wx/tooltip.h"
27 #include "wx/toolbar.h"
28
29 bool wxToolBar::m_bInitialized = FALSE;
30
31 // ----------------------------------------------------------------------------
32 // private classes
33 // ----------------------------------------------------------------------------
34
35 class wxToolBarTool : public wxToolBarToolBase
36 {
37 public:
38 inline wxToolBarTool( wxToolBar* pTbar
39 ,int vId
40 ,const wxString& rsLabel
41 ,const wxBitmap& rBitmap1
42 ,const wxBitmap& rBitmap2
43 ,wxItemKind vKind
44 ,wxObject* pClientData
45 ,const wxString& rsShortHelpString
46 ,const wxString& rsLongHelpString
47 ) : wxToolBarToolBase( pTbar
48 ,vId
49 ,rsLabel
50 ,rBitmap1
51 ,rBitmap2
52 ,vKind
53 ,pClientData
54 ,rsShortHelpString
55 ,rsLongHelpString
56 )
57 {
58 }
59
60 inline wxToolBarTool( wxToolBar* pTbar
61 ,wxControl* pControl
62 ) : wxToolBarToolBase( pTbar
63 ,pControl
64 )
65 {
66 }
67
68 void SetSize(const wxSize& rSize)
69 {
70 m_vWidth = rSize.x;
71 m_vHeight = rSize.y;
72 }
73
74 wxCoord GetWidth(void) const { return m_vWidth; }
75 wxCoord GetHeight(void) const { return m_vHeight; }
76
77 wxCoord m_vX;
78 wxCoord m_vY;
79 wxCoord m_vWidth;
80 wxCoord m_vHeight;
81 }; // end of CLASS wxToolBarTool
82
83 // ----------------------------------------------------------------------------
84 // wxWin macros
85 // ----------------------------------------------------------------------------
86
87 IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxControl)
88
89 BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
90 EVT_SIZE(wxToolBar::OnSize)
91 EVT_PAINT(wxToolBar::OnPaint)
92 EVT_KILL_FOCUS(wxToolBar::OnKillFocus)
93 EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
94 EVT_TIMER(-1, wxToolBar::OnTimer)
95 END_EVENT_TABLE()
96
97 // ============================================================================
98 // implementation
99 // ============================================================================
100
101 // ----------------------------------------------------------------------------
102 // tool bar tools creation
103 // ----------------------------------------------------------------------------
104
105 wxToolBarToolBase* wxToolBar::CreateTool(
106 int nId
107 , const wxString& rsLabel
108 , const wxBitmap& rBmpNormal
109 , const wxBitmap& rBmpDisabled
110 , wxItemKind eKind
111 , wxObject* pClientData
112 , const wxString& rsShortHelp
113 , const wxString& rsLongHelp
114 )
115 {
116 return new wxToolBarTool( this
117 ,nId
118 ,rsLabel
119 ,rBmpNormal
120 ,rBmpDisabled
121 ,eKind
122 ,pClientData
123 ,rsShortHelp
124 ,rsLongHelp
125 );
126 } // end of wxToolBarSimple::CreateTool
127
128 wxToolBarToolBase *wxToolBar::CreateTool(
129 wxControl* pControl
130 )
131 {
132 return new wxToolBarTool( this
133 ,pControl
134 );
135 } // end of wxToolBarSimple::CreateTool
136
137 // ----------------------------------------------------------------------------
138 // wxToolBarSimple creation
139 // ----------------------------------------------------------------------------
140
141 void wxToolBar::Init()
142 {
143 m_nCurrentRowsOrColumns = 0;
144
145 m_vLastX = m_vLastY = 0;
146 m_vMaxWidth = m_vMaxHeight = 0;
147 m_nPressedTool = m_nCurrentTool = -1;
148 m_vXPos = m_vYPos = -1;
149 m_vTextX = m_vTextY = 0;
150
151 m_toolPacking = 1;
152 m_toolSeparation = 5;
153
154 m_defaultWidth = 16;
155 m_defaultHeight = 15;
156
157 m_pToolTip = NULL;
158 } // end of wxToolBar::Init
159
160 wxToolBarToolBase* wxToolBar::DoAddTool(
161 int vId
162 , const wxString& rsLabel
163 , const wxBitmap& rBitmap
164 , const wxBitmap& rBmpDisabled
165 , wxItemKind eKind
166 , const wxString& rsShortHelp
167 , const wxString& rsLongHelp
168 , wxObject* pClientData
169 , wxCoord vXPos
170 , wxCoord vYPos
171 )
172 {
173 //
174 // Rememeber the position for DoInsertTool()
175 //
176 m_vXPos = vXPos;
177 m_vYPos = vYPos;
178
179 return wxToolBarBase::DoAddTool( vId
180 ,rsLabel
181 ,rBitmap
182 ,rBmpDisabled
183 ,eKind
184 ,rsShortHelp
185 ,rsLongHelp
186 ,pClientData
187 ,vXPos
188 ,vYPos
189 );
190 } // end of wxToolBar::DoAddTool
191
192 bool wxToolBar::DeleteTool(
193 int nId
194 )
195 {
196 bool bOk = wxToolBarBase::DeleteTool(nId);
197
198 if (bOk)
199 {
200 Realize();
201 }
202 return bOk;
203 } // end of wxToolBar::DeleteTool
204
205 bool wxToolBar::DeleteToolByPos(
206 size_t nPos
207 )
208 {
209 bool bOk = wxToolBarBase::DeleteToolByPos(nPos);
210
211 if (bOk)
212 {
213 Realize();
214 }
215 return bOk;
216 } // end of wxToolBar::DeleteTool
217
218 wxToolBarToolBase* wxToolBar::InsertControl(
219 size_t nPos
220 , wxControl* pControl
221 )
222 {
223 wxToolBarToolBase* pTool = wxToolBarBase::InsertControl( nPos
224 ,pControl
225 );
226 if (m_bInitialized)
227 {
228 Realize();
229 Refresh();
230 }
231 return pTool;
232 } // end of wxToolBar::InsertControl
233
234 wxToolBarToolBase* wxToolBar::InsertSeparator(
235 size_t nPos
236 )
237 {
238 wxToolBarToolBase* pTool = wxToolBarBase::InsertSeparator(nPos);
239
240 if (m_bInitialized)
241 {
242 Realize();
243 Refresh();
244 }
245 return pTool;
246 } // end of wxToolBar::InsertSeparator
247
248 wxToolBarToolBase* wxToolBar::InsertTool(
249 size_t nPos
250 , int nId
251 , const wxString& rsLabel
252 , const wxBitmap& rBitmap
253 , const wxBitmap& rBmpDisabled
254 , wxItemKind eKind
255 , const wxString& rsShortHelp
256 , const wxString& rsLongHelp
257 , wxObject* pClientData
258 )
259 {
260 wxToolBarToolBase* pTool = wxToolBarBase::InsertTool( nPos
261 ,nId
262 ,rsLabel
263 ,rBitmap
264 ,rBmpDisabled
265 ,eKind
266 ,rsShortHelp
267 ,rsLongHelp
268 ,pClientData
269 );
270 if (m_bInitialized)
271 {
272 Realize();
273 Refresh();
274 }
275 return pTool;
276 } // end of wxToolBar::InsertTool
277
278 bool wxToolBar::DoInsertTool(
279 size_t WXUNUSED(nPos)
280 , wxToolBarToolBase* pToolBase
281 )
282 {
283 wxToolBarTool* pTool = (wxToolBarTool *)pToolBase;
284
285 pTool->m_vX = m_vXPos;
286 if (pTool->m_vX == -1)
287 pTool->m_vX = m_xMargin;
288
289 pTool->m_vY = m_vYPos;
290 if (pTool->m_vY == -1)
291 pTool->m_vX = m_yMargin;
292
293 pTool->SetSize(GetToolSize());
294
295 if (pTool->IsButton())
296 {
297 //
298 // Calculate reasonable max size in case Layout() not called
299 //
300 if ((pTool->m_vX + pTool->GetNormalBitmap().GetWidth() + m_xMargin) > m_vMaxWidth)
301 m_vMaxWidth = (wxCoord)((pTool->m_vX + pTool->GetWidth() + m_xMargin));
302
303 if ((pTool->m_vY + pTool->GetNormalBitmap().GetHeight() + m_yMargin) > m_vMaxHeight)
304 m_vMaxHeight = (wxCoord)((pTool->m_vY + pTool->GetHeight() + m_yMargin));
305 }
306 return TRUE;
307 } // end of wxToolBar::DoInsertTool
308
309 bool wxToolBar::DoDeleteTool(
310 size_t WXUNUSED(nPos)
311 , wxToolBarToolBase* pTool
312 )
313 {
314 pTool->Detach();
315 Refresh();
316 return TRUE;
317 } // end of wxToolBar::DoDeleteTool
318
319 bool wxToolBar::Create(
320 wxWindow* pParent
321 , wxWindowID vId
322 , const wxPoint& rPos
323 , const wxSize& rSize
324 , long lStyle
325 , const wxString& rsName
326 )
327 {
328 if ( !wxWindow::Create( pParent
329 ,vId
330 ,rPos
331 ,rSize
332 ,lStyle
333 ,rsName
334 ))
335 return FALSE;
336
337 // Set it to grey (or other 3D face colour)
338 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_MENUBAR));
339 SetFont(*wxSMALL_FONT);
340
341 if (GetWindowStyleFlag() & wxTB_VERTICAL)
342 {
343 m_vLastX = 7;
344 m_vLastY = 3;
345
346 m_maxRows = 32000; // a lot
347 m_maxCols = 1;
348 }
349 else
350 {
351 m_vLastX = 3;
352 m_vLastY = 7;
353
354 m_maxRows = 1;
355 m_maxCols = 32000; // a lot
356 }
357 SetCursor(*wxSTANDARD_CURSOR);
358
359 //
360 // The toolbar's tools, if they have labels and the winTB_TEXT
361 // style is set, then we need to take into account the size of
362 // the text when drawing tool bitmaps and the text
363 //
364 if (HasFlag(wxTB_TEXT))
365 {
366 wxClientDC vDC(this);
367
368 vDC.SetFont(GetFont());
369 vDC.GetTextExtent( "XXXX"
370 ,&m_vTextX
371 ,&m_vTextY
372 );
373 }
374
375 //
376 // Position it
377 //
378 int nX = rPos.x;
379 int nY = rPos.y;
380 int nWidth = rSize.x;
381 int nHeight = rSize.y;
382
383 if (lStyle & wxTB_HORIZONTAL)
384 {
385 if (nWidth <= 0)
386 {
387 nWidth = pParent->GetClientSize().x;
388 }
389 if (nHeight <= 0)
390 {
391 if (lStyle & wxTB_TEXT)
392 nHeight = m_defaultHeight + m_vTextY;
393 else
394 nHeight = m_defaultHeight;
395 }
396 }
397 else
398 {
399 if (nHeight <= 0)
400 {
401 nHeight = pParent->GetClientSize().y;
402 }
403 if (nWidth <= 0)
404 {
405 if (lStyle & wxTB_TEXT)
406 nWidth = m_vTextX + (int)(m_vTextX/2); // a little margin
407 else
408 nWidth = m_defaultWidth + (int)(m_defaultWidth/2); // a little margin
409 }
410 }
411 if (nX < 0)
412 nX = 0;
413 if (nY < 0)
414 nY = 0;
415
416 SetSize( nX
417 ,nY
418 ,nWidth
419 ,nHeight
420 );
421 return TRUE;
422 } // end of wxToolBar::Create
423
424 wxToolBar::~wxToolBar()
425 {
426 if (m_pToolTip)
427 {
428 delete m_pToolTip;
429 m_pToolTip = NULL;
430 }
431 } // end of wxToolBar::~wxToolBar
432
433 bool wxToolBar::Realize()
434 {
435 int nMaxToolWidth = 0;
436 int nMaxToolHeight = 0;
437
438 m_nCurrentRowsOrColumns = 0;
439 m_vLastX = m_xMargin;
440 m_vLastY = m_yMargin;
441 m_vMaxWidth = 0;
442 m_vMaxHeight = 0;
443
444
445 //
446 // Find the maximum tool width and height
447 //
448 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
449
450 while (node )
451 {
452 wxToolBarTool* pTool = (wxToolBarTool *)node->GetData();
453
454 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsEmpty())
455 {
456 //
457 // Set the height according to the font and the border size
458 //
459 if (pTool->GetWidth() > m_vTextX)
460 nMaxToolWidth = pTool->GetWidth() + 4;
461 else
462 nMaxToolWidth = m_vTextX;
463 if (pTool->GetHeight() + m_vTextY > nMaxToolHeight)
464 nMaxToolHeight = pTool->GetHeight() + m_vTextY;
465 }
466 else
467 {
468 if (pTool->GetWidth() > nMaxToolWidth )
469 nMaxToolWidth = pTool->GetWidth() + 4;
470 if (pTool->GetHeight() > nMaxToolHeight)
471 nMaxToolHeight = pTool->GetHeight();
472 }
473 node = node->GetNext();
474 }
475
476 wxCoord vTbWidth = 0L;
477 wxCoord vTbHeight = 0L;
478
479 GetSize( &vTbWidth
480 ,&vTbHeight
481 );
482 if (vTbHeight < nMaxToolHeight)
483 {
484 SetSize( -1L
485 ,-1L
486 ,vTbWidth
487 ,nMaxToolHeight + 4
488 );
489 if (GetParent()->IsKindOf(CLASSINFO(wxFrame)))
490 {
491 wxFrame* pFrame = wxDynamicCast(GetParent(), wxFrame);
492
493 if (pFrame)
494 pFrame->PositionToolBar();
495 }
496 }
497
498 int nSeparatorSize = m_toolSeparation;
499
500 node = m_tools.GetFirst();
501 while (node)
502 {
503 wxToolBarTool* pTool = (wxToolBarTool *)node->GetData();
504
505 if (pTool->IsSeparator())
506 {
507 if (GetWindowStyleFlag() & wxTB_HORIZONTAL)
508 {
509 pTool->m_vX = m_vLastX + nSeparatorSize;
510 pTool->m_vHeight = m_defaultHeight + m_vTextY;
511 if (m_nCurrentRowsOrColumns >= m_maxCols)
512 m_vLastY += nSeparatorSize;
513 else
514 m_vLastX += nSeparatorSize * 4;
515 }
516 else
517 {
518 pTool->m_vY = m_vLastY + nSeparatorSize;
519 pTool->m_vHeight = m_defaultHeight + m_vTextY;
520 if (m_nCurrentRowsOrColumns >= m_maxRows)
521 m_vLastX += nSeparatorSize;
522 else
523 m_vLastY += nSeparatorSize * 4;
524 }
525 }
526 else if (pTool->IsButton())
527 {
528 if (GetWindowStyleFlag() & wxTB_HORIZONTAL)
529 {
530 if (m_nCurrentRowsOrColumns >= m_maxCols)
531 {
532 m_nCurrentRowsOrColumns = 0;
533 m_vLastX = m_xMargin;
534 m_vLastY += nMaxToolHeight + m_toolPacking;
535 }
536 pTool->m_vX = m_vLastX + (nMaxToolWidth - ((int)(nMaxToolWidth/2) + (int)(pTool->GetWidth()/2)));
537 if (HasFlag(wxTB_TEXT))
538 pTool->m_vY = m_vLastY + nSeparatorSize - 2; // just bit of adjustment
539 else
540 pTool->m_vY = m_vLastY + (nMaxToolHeight - (int)(pTool->GetHeight()/2));
541 m_vLastX += nMaxToolWidth + m_toolPacking + m_toolSeparation;
542 }
543 else
544 {
545 if (m_nCurrentRowsOrColumns >= m_maxRows)
546 {
547 m_nCurrentRowsOrColumns = 0;
548 m_vLastX += (nMaxToolWidth + m_toolPacking);
549 m_vLastY = m_yMargin;
550 }
551 pTool->m_vX = m_vLastX + pTool->GetWidth();
552 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
553 pTool->m_vY = m_vLastY + (nMaxToolHeight - m_vTextY) + m_toolPacking;
554 else
555 pTool->m_vY = m_vLastY + (nMaxToolHeight - (int)(pTool->GetHeight()/2));
556 m_vLastY += nMaxToolHeight + m_toolPacking + m_toolSeparation;
557 }
558 m_nCurrentRowsOrColumns++;
559 }
560 else
561 {
562 // TODO: support the controls
563 }
564
565 if (m_vLastX > m_maxWidth)
566 m_maxWidth = m_vLastX;
567 if (m_vLastY > m_maxHeight)
568 m_maxHeight = m_vLastY;
569
570 node = node->GetNext();
571 }
572
573 if ( GetWindowStyleFlag() & wxTB_HORIZONTAL )
574 m_maxWidth += nMaxToolWidth;
575 else
576 m_maxHeight += nMaxToolHeight;
577
578 m_maxWidth += m_xMargin;
579 m_maxHeight += m_yMargin;
580 m_bInitialized = TRUE;
581 return TRUE;
582 } // end of wxToolBar::Realize
583
584 // ----------------------------------------------------------------------------
585 // event handlers
586 // ----------------------------------------------------------------------------
587
588 void wxToolBar::OnPaint (
589 wxPaintEvent& WXUNUSED(rEvent)
590 )
591 {
592 wxPaintDC vDc(this);
593
594 PrepareDC(vDc);
595
596 static int nCount = 0;
597
598 //
599 // Prevent reentry of OnPaint which would cause wxMemoryDC errors.
600 //
601 if (nCount > 0)
602 return;
603 nCount++;
604
605 ::WinFillRect(vDc.GetHPS(), &vDc.m_vRclPaint, GetBackgroundColour().GetPixel());
606 for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
607 node;
608 node = node->GetNext() )
609 {
610 wxToolBarTool* pTool = (wxToolBarTool*)node->GetData();
611
612 if (pTool->IsButton() )
613 DrawTool(vDc, pTool);
614 if (pTool->IsSeparator())
615 {
616 wxPen vDarkGreyPen( wxColour(85, 85, 85)
617 ,1
618 ,wxSOLID
619 );
620 int nX;
621 int nY;
622 int nHeight = 0;
623 int nWidth = 0;
624
625 vDc.SetPen(vDarkGreyPen);
626 if (HasFlag(wxTB_TEXT))
627 {
628 if (HasFlag(wxTB_HORIZONTAL))
629 {
630 nX = pTool->m_vX;
631 nY = pTool->m_vY - (m_vTextY - 6);
632 nHeight = (m_vTextY - 2) + pTool->GetHeight();
633 }
634 else
635 {
636 nX = pTool->m_vX + m_xMargin + 10;
637 nY = pTool->m_vY + m_vTextY + m_toolSeparation;
638 nWidth = pTool->GetWidth() > m_vTextX ? pTool->GetWidth() : m_vTextX;
639 }
640 }
641 else
642 {
643 nX = pTool->m_vX;
644 nY = pTool->m_vY;
645 if (HasFlag(wxTB_HORIZONTAL))
646 nHeight = pTool->GetHeight() - 2;
647 else
648 {
649 nX += m_xMargin + 10;
650 nY += m_yMargin + m_toolSeparation;
651 nWidth = pTool->GetWidth();
652 }
653 }
654 vDc.DrawLine(nX, nY, nX + nWidth, nY + nHeight);
655 }
656 }
657 nCount--;
658 } // end of wxToolBar::OnPaint
659
660 void wxToolBar::OnSize (
661 wxSizeEvent& WXUNUSED(rEvent)
662 )
663 {
664 #if wxUSE_CONSTRAINTS
665 if (GetAutoLayout())
666 Layout();
667 #endif
668 } // end of wxToolBar::OnSize
669
670 void wxToolBar::OnKillFocus(
671 wxFocusEvent& WXUNUSED(rEvent)
672 )
673 {
674 OnMouseEnter(m_nPressedTool = m_nCurrentTool = -1);
675 } // end of wxToolBar::OnKillFocus
676
677 void wxToolBar::OnMouseEvent(
678 wxMouseEvent& rEvent
679 )
680 {
681 POINTL vPoint;
682 HWND hWnd;
683 wxCoord vX;
684 wxCoord vY;
685 HPOINTER hPtr = ::WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);
686
687 ::WinSetPointer(HWND_DESKTOP, hPtr);
688 ::WinQueryPointerPos(HWND_DESKTOP, &vPoint);
689 hWnd = ::WinWindowFromPoint(HWND_DESKTOP, &vPoint, TRUE);
690 if (hWnd != (HWND)GetHwnd())
691 {
692 m_vToolTimer.Stop();
693 return;
694 }
695
696 rEvent.GetPosition(&vX, &vY);
697
698 wxToolBarTool* pTool = (wxToolBarTool *)FindToolForPosition( vX
699 ,vY
700 );
701
702 if (rEvent.LeftDown())
703 {
704 CaptureMouse();
705 }
706 if (rEvent.LeftUp())
707 {
708 ReleaseMouse();
709 }
710
711 if (!pTool)
712 {
713 m_vToolTimer.Stop();
714 if (m_nCurrentTool > -1)
715 {
716 if (rEvent.LeftIsDown())
717 SpringUpButton(m_nCurrentTool);
718 pTool = (wxToolBarTool *)FindById(m_nCurrentTool);
719 if (pTool && !pTool->IsToggled())
720 {
721 RaiseTool( pTool
722 ,FALSE
723 );
724 }
725 m_nCurrentTool = -1;
726 OnMouseEnter(-1);
727 }
728 return;
729 }
730 if (!rEvent.IsButton())
731 {
732 if (pTool->GetId() != m_nCurrentTool)
733 {
734 //
735 // If the left button is kept down and moved over buttons,
736 // press those buttons.
737 //
738 if (rEvent.LeftIsDown() && pTool->IsEnabled())
739 {
740 SpringUpButton(m_nCurrentTool);
741 if (pTool->CanBeToggled())
742 {
743 pTool->Toggle();
744 }
745 DrawTool(pTool);
746 }
747 wxToolBarTool* pOldTool = (wxToolBarTool*)FindById(m_nCurrentTool);
748
749 if (pOldTool && !pTool->IsToggled())
750 RaiseTool( pOldTool
751 ,FALSE
752 );
753 m_nCurrentTool = pTool->GetId();
754 OnMouseEnter(m_nCurrentTool);
755 if (!pTool->GetShortHelp().IsEmpty())
756 {
757 if (m_pToolTip)
758 delete m_pToolTip;
759 m_pToolTip = new wxToolTip(pTool->GetShortHelp());
760 m_vXMouse = (wxCoord)vPoint.x;
761 m_vYMouse = (wxCoord)vPoint.y;
762 m_vToolTimer.Start(1000L, TRUE);
763 }
764 if (!pTool->IsToggled())
765 RaiseTool(pTool);
766 }
767 return;
768 }
769
770 // Left button pressed.
771 if (rEvent.LeftDown() && pTool->IsEnabled())
772 {
773 if (pTool->CanBeToggled())
774 {
775 pTool->Toggle();
776 }
777 DrawTool(pTool);
778 }
779 else if (rEvent.RightDown())
780 {
781 OnRightClick( pTool->GetId()
782 ,vX
783 ,vY
784 );
785 }
786
787 //
788 // Left Button Released. Only this action confirms selection.
789 // If the button is enabled and it is not a toggle tool and it is
790 // in the pressed state, then raise the button and call OnLeftClick.
791 //
792 if (rEvent.LeftUp() && pTool->IsEnabled() )
793 {
794 //
795 // Pass the OnLeftClick event to tool
796 //
797 if (!OnLeftClick( pTool->GetId()
798 ,pTool->IsToggled()) &&
799 pTool->CanBeToggled())
800 {
801 //
802 // If it was a toggle, and OnLeftClick says No Toggle allowed,
803 // then change it back
804 //
805 pTool->Toggle();
806 }
807 DrawTool(pTool);
808 }
809 } // end of wxToolBar::OnMouseEvent
810
811 // ----------------------------------------------------------------------------
812 // drawing
813 // ----------------------------------------------------------------------------
814
815 void wxToolBar::DrawTool(
816 wxToolBarToolBase* pTool
817 )
818 {
819 wxClientDC vDc(this);
820
821 DrawTool( vDc
822 ,pTool
823 );
824 } // end of wxToolBar::DrawTool
825
826 void wxToolBar::DrawTool(
827 wxDC& rDc
828 , wxToolBarToolBase* pToolBase
829 )
830 {
831 wxToolBarTool* pTool = (wxToolBarTool *)pToolBase;
832 wxPen vDarkGreyPen( wxColour( 85,85,85 )
833 ,1
834 ,wxSOLID
835 );
836 wxPen vWhitePen( wxT("WHITE")
837 ,1
838 ,wxSOLID
839 );
840 wxPen vBlackPen( wxT("BLACK")
841 ,1
842 ,wxSOLID
843 );
844 wxBitmap vBitmap = pTool->GetNormalBitmap();
845 bool bUseMask = FALSE;
846 wxMask* pMask = NULL;
847
848 PrepareDC(rDc);
849
850 if (!vBitmap.Ok())
851 return;
852 if ((pMask = vBitmap.GetMask()) != NULL)
853 if (pMask->GetMaskBitmap() != NULLHANDLE)
854 bUseMask = TRUE;
855
856 if (!pTool->IsToggled())
857 {
858 LowerTool(pTool, FALSE);
859 if (!pTool->IsEnabled())
860 {
861 wxColour vColor("GREY");
862
863 rDc.SetTextForeground(vColor);
864 if (!pTool->GetDisabledBitmap().Ok())
865 pTool->SetDisabledBitmap(wxDisableBitmap( vBitmap
866 ,(long)GetBackgroundColour().GetPixel()
867 ));
868 rDc.DrawBitmap( pTool->GetDisabledBitmap()
869 ,pTool->m_vX
870 ,pTool->m_vY
871 ,bUseMask
872 );
873 }
874 else
875 {
876 wxColour vColor("BLACK");
877
878 rDc.SetTextForeground(vColor);
879 rDc.DrawBitmap( vBitmap
880 ,pTool->m_vX
881 ,pTool->m_vY
882 ,bUseMask
883 );
884 }
885 if (m_windowStyle & wxTB_3DBUTTONS)
886 {
887 RaiseTool(pTool);
888 }
889 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
890 {
891 wxCoord vX;
892 wxCoord vY;
893 wxCoord vLeft = pTool->m_vX - (int)(pTool->GetWidth()/2);
894
895 rDc.SetFont(GetFont());
896 rDc.GetTextExtent( pTool->GetLabel()
897 ,&vX
898 ,&vY
899 );
900 if (pTool->GetWidth() > vX) // large tools
901 {
902 vLeft = pTool->m_vX + (pTool->GetWidth() - vX);
903 GetSize(&vX, &vY);
904 rDc.DrawText( pTool->GetLabel()
905 ,vLeft
906 ,vY - (m_vTextY - 2)
907 );
908 }
909 else // normal tools
910 {
911 vLeft += (wxCoord)((m_vTextX - vX)/2);
912 rDc.DrawText( pTool->GetLabel()
913 ,vLeft
914 ,pTool->m_vY + m_vTextY + 4 // a bit of margin
915 );
916 }
917 }
918 }
919 else
920 {
921 wxColour vColor("GREY");
922
923 LowerTool(pTool);
924 rDc.SetTextForeground(vColor);
925 if (!pTool->GetDisabledBitmap().Ok())
926 pTool->SetDisabledBitmap(wxDisableBitmap( vBitmap
927 ,(long)GetBackgroundColour().GetPixel()
928 ));
929 rDc.DrawBitmap( pTool->GetDisabledBitmap()
930 ,pTool->m_vX
931 ,pTool->m_vY
932 ,bUseMask
933 );
934 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
935 {
936 wxCoord vX;
937 wxCoord vY;
938 wxCoord vLeft = pTool->m_vX - (int)(pTool->GetWidth()/2);
939
940 rDc.SetFont(GetFont());
941 rDc.GetTextExtent( pTool->GetLabel()
942 ,&vX
943 ,&vY
944 );
945 vLeft += (wxCoord)((m_vTextX - vX)/2);
946 rDc.DrawText( pTool->GetLabel()
947 ,vLeft
948 ,pTool->m_vY + m_vTextY + 4 // a bit of margin
949 );
950 }
951 }
952 } // end of wxToolBar::DrawTool
953
954 // ----------------------------------------------------------------------------
955 // toolbar geometry
956 // ----------------------------------------------------------------------------
957
958 void wxToolBar::SetRows(
959 int nRows
960 )
961 {
962 wxCHECK_RET( nRows != 0, _T("max number of rows must be > 0") );
963
964 m_maxCols = (GetToolsCount() + nRows - 1) / nRows;
965 Refresh();
966 } // end of wxToolBar::SetRows
967
968 wxToolBarToolBase* wxToolBar::FindToolForPosition(
969 wxCoord vX
970 , wxCoord vY
971 ) const
972 {
973 wxCoord vTBarHeight = 0;
974
975 GetSize( NULL
976 ,&vTBarHeight
977 );
978 vY = vTBarHeight - vY;
979 wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
980 while (node)
981 {
982 wxToolBarTool* pTool = (wxToolBarTool *)node->GetData();
983
984 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsNull())
985 {
986 if ((vX >= (pTool->m_vX - ((wxCoord)(pTool->GetWidth()/2) - 2))) &&
987 (vY >= (pTool->m_vY - 2)) &&
988 (vX <= (pTool->m_vX + pTool->GetWidth())) &&
989 (vY <= (pTool->m_vY + pTool->GetHeight() + m_vTextY + 2)))
990 {
991 return pTool;
992 }
993 }
994 else
995 {
996 if ((vX >= pTool->m_vX) &&
997 (vY >= pTool->m_vY) &&
998 (vX <= (pTool->m_vX + pTool->GetWidth())) &&
999 (vY <= (pTool->m_vY + pTool->GetHeight())))
1000 {
1001 return pTool;
1002 }
1003 }
1004 node = node->GetNext();
1005 }
1006 return (wxToolBarToolBase *)NULL;
1007 } // end of wxToolBar::FindToolForPosition
1008
1009 // ----------------------------------------------------------------------------
1010 // tool state change handlers
1011 // ----------------------------------------------------------------------------
1012
1013 void wxToolBar::DoEnableTool(
1014 wxToolBarToolBase* pTool
1015 , bool WXUNUSED(bEnable)
1016 )
1017 {
1018 DrawTool(pTool);
1019 } // end of wxToolBar::DoEnableTool
1020
1021 void wxToolBar::DoToggleTool(
1022 wxToolBarToolBase* pTool
1023 , bool WXUNUSED(bToggle)
1024 )
1025 {
1026 DrawTool(pTool);
1027 } // end of wxToolBar::DoToggleTool
1028
1029 void wxToolBar::DoSetToggle(
1030 wxToolBarToolBase* WXUNUSED(pTool)
1031 , bool WXUNUSED(bToggle)
1032 )
1033 {
1034 // nothing to do
1035 } // end of wxToolBar::DoSetToggle
1036
1037 //
1038 // Okay, so we've left the tool we're in ... we must check if the tool we're
1039 // leaving was a 'sprung push button' and if so, spring it back to the up
1040 // state.
1041 //
1042 void wxToolBar::SpringUpButton(
1043 int vId
1044 )
1045 {
1046 wxToolBarToolBase* pTool = FindById(vId);
1047
1048 if (pTool && pTool->CanBeToggled())
1049 {
1050 if (pTool->IsToggled())
1051 pTool->Toggle();
1052
1053 DrawTool(pTool);
1054 }
1055 } // end of wxToolBar::SpringUpButton
1056
1057 // ----------------------------------------------------------------------------
1058 // private helpers
1059 // ----------------------------------------------------------------------------
1060
1061 void wxToolBar::LowerTool (
1062 wxToolBarToolBase* pToolBase
1063 , bool bLower
1064 )
1065 {
1066 wxToolBarTool* pTool = (wxToolBarTool*)pToolBase;
1067 wxCoord vX;
1068 wxCoord vY;
1069 wxCoord vWidth;
1070 wxCoord vHeight;
1071 wxPen vDarkGreyPen( wxColour(85, 85, 85)
1072 ,1
1073 ,wxSOLID
1074 );
1075 wxPen vWhitePen( "WHITE"
1076 ,1
1077 ,wxSOLID
1078 );
1079 wxPen vClearPen( GetBackgroundColour()
1080 ,1
1081 ,wxSOLID
1082 );
1083 wxClientDC vDC(this);
1084
1085 if (!pTool)
1086 return;
1087
1088 if (pTool->IsSeparator())
1089 return;
1090
1091 //
1092 // We only do this for flat toolbars
1093 //
1094 if (!HasFlag(wxTB_FLAT))
1095 return;
1096
1097 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsEmpty())
1098 {
1099 if (pTool->GetWidth() > m_vTextX)
1100 {
1101 vX = pTool->m_vX - 2;
1102 vWidth = pTool->GetWidth() + 4;
1103 }
1104 else
1105 {
1106 vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
1107 vWidth = m_vTextX + 4;
1108 }
1109 vY = pTool->m_vY - 2;
1110 vHeight = pTool->GetHeight() + m_vTextY + 2;
1111 }
1112 else
1113 {
1114 vX = pTool->m_vX - 2;
1115 vY = pTool->m_vY - 2;
1116 vWidth = pTool->GetWidth() + 4;
1117 vHeight = pTool->GetHeight() + 4;
1118 }
1119 if (bLower)
1120 {
1121 vDC.SetPen(vWhitePen);
1122 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1123 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
1124 vDC.SetPen(vDarkGreyPen);
1125 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1126 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1127 }
1128 else
1129 {
1130 vDC.SetPen(vClearPen);
1131 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1132 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
1133 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1134 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1135 }
1136 } // end of WinGuiBase_CToolBarTool::LowerTool
1137
1138 void wxToolBar::RaiseTool (
1139 wxToolBarToolBase* pToolBase
1140 , bool bRaise
1141 )
1142 {
1143 wxToolBarTool* pTool = (wxToolBarTool*)pToolBase;
1144 wxCoord vX;
1145 wxCoord vY;
1146 wxCoord vWidth;
1147 wxCoord vHeight;
1148 wxPen vDarkGreyPen( wxColour(85, 85, 85)
1149 ,1
1150 ,wxSOLID
1151 );
1152 wxPen vWhitePen( "WHITE"
1153 ,1
1154 ,wxSOLID
1155 );
1156 wxPen vClearPen( GetBackgroundColour()
1157 ,1
1158 ,wxSOLID
1159 );
1160 wxClientDC vDC(this);
1161
1162 if (!pTool)
1163 return;
1164
1165 if (pTool->IsSeparator())
1166 return;
1167
1168 if (!pTool->IsEnabled())
1169 return;
1170
1171 //
1172 // We only do this for flat toolbars
1173 //
1174 if (!HasFlag(wxTB_FLAT))
1175 return;
1176
1177 if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsEmpty())
1178 {
1179 if (pTool->GetWidth() > m_vTextX)
1180 {
1181 vX = pTool->m_vX - 2;
1182 vWidth = pTool->GetWidth() + 4;
1183 }
1184 else
1185 {
1186 vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
1187 vWidth = m_vTextX + 4;
1188 }
1189 vY = pTool->m_vY - 2;
1190 vHeight = pTool->GetHeight() + m_vTextY + 2;
1191 }
1192 else
1193 {
1194 vX = pTool->m_vX - 2;
1195 vY = pTool->m_vY - 2;
1196 vWidth = pTool->GetWidth() + 4;
1197 vHeight = pTool->GetHeight() + 4;
1198 }
1199 if (bRaise)
1200 {
1201 vDC.SetPen(vDarkGreyPen);
1202 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1203 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
1204 vDC.SetPen(vWhitePen);
1205 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1206 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1207 }
1208 else
1209 {
1210 vDC.SetPen(vClearPen);
1211 vDC.DrawLine(vX + vWidth, vY + vHeight, vX, vY + vHeight);
1212 vDC.DrawLine(vX + vWidth, vY, vX + vWidth, vY + vHeight);
1213 vDC.DrawLine(vX, vY, vX + vWidth, vY);
1214 vDC.DrawLine(vX, vY + vHeight, vX, vY);
1215 }
1216 } // end of wxToolBar::RaiseTool
1217
1218 void wxToolBar::OnTimer (
1219 wxTimerEvent& rEvent
1220 )
1221 {
1222 if (rEvent.GetId() == m_vToolTimer.GetTimerId())
1223 {
1224 wxPoint vPos( m_vXMouse
1225 ,m_vYMouse
1226 );
1227
1228 m_pToolTip->DisplayToolTipWindow(vPos);
1229 m_vToolTimer.Stop();
1230 m_vToolExpTimer.Start(4000L, TRUE);
1231 }
1232 else if (rEvent.GetId() == m_vToolExpTimer.GetTimerId())
1233 {
1234 m_pToolTip->HideToolTipWindow();
1235 GetParent()->Refresh();
1236 m_vToolExpTimer.Stop();
1237 }
1238 } // end of wxToolBar::OnTimer
1239
1240 #endif // ndef for wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE