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