]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/toolbar.cpp
Nicer looking OS/2 Statusbar
[wxWidgets.git] / src / os2 / toolbar.cpp
index 23729eaae8d230a98aa247af8056450c5a1107fc..2cf452ba77d9830e34eb80998c5ff38650820bda 100644 (file)
@@ -21,6 +21,7 @@
     #include "wx/dcmemory.h"
 #endif
 
+#include "wx/tooltip.h"
 #include "wx/toolbar.h"
 
 bool                                wxToolBar::m_bInitialized = FALSE;
@@ -228,7 +229,10 @@ wxBitmap wxDisableBitmap(
         vError = ::WinGetLastError(vHabmain);
         sError = wxPMErrorToStr(vError);
     }
-    vNewBmp.SetMask(rBmp.GetMask());
+    wxMask*                         pNewMask;
+
+    pNewMask = new wxMask(pMask->GetMaskBitmap());
+    vNewBmp.SetMask(pNewMask);
     free(pucBits);
     ::GpiSetBitmap(hPS, NULLHANDLE);
     ::GpiDestroyPS(hPS);
@@ -301,6 +305,7 @@ BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase)
     EVT_PAINT(wxToolBar::OnPaint)
     EVT_KILL_FOCUS(wxToolBar::OnKillFocus)
     EVT_MOUSE_EVENTS(wxToolBar::OnMouseEvent)
+    EVT_TIMER(-1, wxToolBar::OnTimer)
 END_EVENT_TABLE()
 
 // ============================================================================
@@ -362,6 +367,8 @@ void wxToolBar::Init()
 
     m_defaultWidth = 16;
     m_defaultHeight = 15;
+
+    m_pToolTip = NULL;
 } // end of wxToolBar::Init
 
 wxToolBarToolBase* wxToolBar::DoAddTool(
@@ -396,6 +403,92 @@ wxToolBarToolBase* wxToolBar::DoAddTool(
                                    );
 } // end of wxToolBar::DoAddTool
 
+bool wxToolBar::DeleteTool(
+  int                               nId
+)
+{
+    bool                            bOk = wxToolBarBase::DeleteTool(nId);
+
+    if (bOk)
+    {
+        Realize();
+    }
+    return bOk;
+} // end of wxToolBar::DeleteTool
+
+bool wxToolBar::DeleteToolByPos(
+  size_t                            nPos
+)
+{
+    bool                            bOk = wxToolBarBase::DeleteToolByPos(nPos);
+
+    if (bOk)
+    {
+        Realize();
+    }
+    return bOk;
+} // end of wxToolBar::DeleteTool
+
+wxToolBarToolBase* wxToolBar::InsertControl(
+  size_t                            nPos
+, wxControl*                        pControl
+)
+{
+    wxToolBarToolBase*              pTool = wxToolBarBase::InsertControl( nPos
+                                                                         ,pControl
+                                                                        );
+    if (m_bInitialized)
+    {
+        Realize();
+        Refresh();
+    }
+    return pTool;
+} // end of wxToolBar::InsertControl
+
+wxToolBarToolBase* wxToolBar::InsertSeparator(
+  size_t                            nPos
+)
+{
+    wxToolBarToolBase*              pTool = wxToolBarBase::InsertSeparator(nPos);
+
+    if (m_bInitialized)
+    {
+        Realize();
+        Refresh();
+    }
+    return pTool;
+} // end of wxToolBar::InsertSeparator
+
+wxToolBarToolBase* wxToolBar::InsertTool(
+  size_t                            nPos
+, int                               nId
+, const wxString&                   rsLabel
+, const wxBitmap&                   rBitmap
+, const wxBitmap&                   rBmpDisabled
+, wxItemKind                        eKind
+, const wxString&                   rsShortHelp
+, const wxString&                   rsLongHelp
+, wxObject*                         pClientData
+)
+{
+    wxToolBarToolBase*              pTool = wxToolBarBase::InsertTool( nPos
+                                                                      ,nId
+                                                                      ,rsLabel
+                                                                      ,rBitmap
+                                                                      ,rBmpDisabled
+                                                                      ,eKind
+                                                                      ,rsShortHelp
+                                                                      ,rsLongHelp
+                                                                      ,pClientData
+                                                                     );
+    if (m_bInitialized)
+    {
+        Realize();
+        Refresh();
+    }
+    return pTool;
+} // end of wxToolBar::InsertTool
+
 bool wxToolBar::DoInsertTool(
   size_t                            WXUNUSED(nPos)
 , wxToolBarToolBase*                pToolBase
@@ -511,7 +604,7 @@ bool wxToolBar::Create(
         if (nHeight <= 0)
         {
             if (lStyle & wxTB_TEXT)
-                nHeight = m_defaultHeight + 18;
+                nHeight = m_defaultHeight + m_vTextY;
             else
                 nHeight = m_defaultHeight;
         }
@@ -545,6 +638,11 @@ bool wxToolBar::Create(
 
 wxToolBar::~wxToolBar()
 {
+    if (m_pToolTip)
+    {
+        delete m_pToolTip;
+        m_pToolTip = NULL;
+    }
 } // end of wxToolBar::~wxToolBar
 
 bool wxToolBar::Realize()
@@ -575,20 +673,45 @@ bool wxToolBar::Realize()
             //
             // Set the height according to the font and the border size
             //
-            nMaxToolWidth = m_vTextX;
+            if (pTool->GetWidth() > m_vTextX)
+                nMaxToolWidth = pTool->GetWidth() + 4;
+            else
+                nMaxToolWidth = m_vTextX;
             if (pTool->GetHeight() + m_vTextY > nMaxToolHeight)
                 nMaxToolHeight = pTool->GetHeight() + m_vTextY;
         }
         else
         {
             if (pTool->GetWidth() > nMaxToolWidth )
-                nMaxToolWidth = pTool->GetWidth();
+                nMaxToolWidth = pTool->GetWidth() + 4;
             if (pTool->GetHeight() > nMaxToolHeight)
                 nMaxToolHeight = pTool->GetHeight();
         }
         pNode = pNode->GetNext();
     }
 
+    wxCoord                         vTbWidth = 0L;
+    wxCoord                         vTbHeight = 0L;
+
+    GetSize( &vTbWidth
+            ,&vTbHeight
+           );
+    if (vTbHeight < nMaxToolHeight)
+    {
+        SetSize( -1L
+                ,-1L
+                ,vTbWidth
+                ,nMaxToolHeight + 4
+               );
+        if (GetParent()->IsKindOf(CLASSINFO(wxFrame)))
+        {
+            wxFrame*            pFrame = wxDynamicCast(GetParent(), wxFrame);
+
+            if (pFrame)
+                pFrame->PositionToolBar();
+        }
+    }
+
     int                             nSeparatorSize = m_toolSeparation;
 
     pNode = m_tools.GetFirst();
@@ -605,7 +728,7 @@ bool wxToolBar::Realize()
                 if (m_nCurrentRowsOrColumns >= m_maxCols)
                     m_vLastY += nSeparatorSize;
                 else
-                    m_vLastX += nSeparatorSize;
+                    m_vLastX += nSeparatorSize * 4;
             }
             else
             {
@@ -614,7 +737,7 @@ bool wxToolBar::Realize()
                 if (m_nCurrentRowsOrColumns >= m_maxRows)
                     m_vLastX += nSeparatorSize;
                 else
-                    m_vLastY += nSeparatorSize;
+                    m_vLastY += nSeparatorSize * 4;
             }
         }
         else if (pTool->IsButton())
@@ -671,6 +794,7 @@ bool wxToolBar::Realize()
 
     m_maxWidth += m_xMargin;
     m_maxHeight += m_yMargin;
+    m_bInitialized = TRUE;
     return TRUE;
 } // end of wxToolBar::Realize
 
@@ -695,6 +819,7 @@ void wxToolBar::OnPaint (
         return;
     nCount++;
 
+    ::WinFillRect(vDc.GetHPS(), &vDc.m_vRclPaint, GetBackgroundColour().GetPixel());
     for ( wxToolBarToolsList::Node* pNode = m_tools.GetFirst();
           pNode;
           pNode = pNode->GetNext() )
@@ -711,22 +836,39 @@ void wxToolBar::OnPaint (
                                                 );
             int                     nX;
             int                     nY;
-            int                     nHeight;
+            int                     nHeight = 0;
+            int                     nWidth = 0;
 
             vDc.SetPen(vDarkGreyPen);
-            if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsEmpty())
+            if (HasFlag(wxTB_TEXT))
             {
-                nX = pTool->m_vX;
-                nY = pTool->m_vY - (m_vTextY - 6);
-                nHeight = (m_vTextY - 2) + pTool->GetHeight();
+                if (HasFlag(wxTB_HORIZONTAL))
+                {
+                    nX = pTool->m_vX;
+                    nY = pTool->m_vY - (m_vTextY - 6);
+                    nHeight = (m_vTextY - 2) + pTool->GetHeight();
+                }
+                else
+                {
+                    nX = pTool->m_vX + m_xMargin + 10;
+                    nY = pTool->m_vY + m_vTextY + m_toolSeparation;
+                    nWidth = pTool->GetWidth() > m_vTextX ? pTool->GetWidth() : m_vTextX;
+                }
             }
             else
             {
                 nX = pTool->m_vX;
                 nY = pTool->m_vY;
-                nHeight = pTool->GetHeight() - 2;
+                if (HasFlag(wxTB_HORIZONTAL))
+                    nHeight = pTool->GetHeight() - 2;
+                else
+                {
+                    nX += m_xMargin + 10;
+                    nY +=  m_yMargin + m_toolSeparation;
+                    nWidth = pTool->GetWidth();
+                }
             }
-            vDc.DrawLine(nX, nY, nX, nY + nHeight);
+            vDc.DrawLine(nX, nY, nX + nWidth, nY + nHeight);
         }
     }
     nCount--;
@@ -753,11 +895,21 @@ void wxToolBar::OnMouseEvent(
   wxMouseEvent&                     rEvent
 )
 {
+    POINTL                          vPoint;
+    HWND                            hWnd;
     wxCoord                         vX;
     wxCoord                         vY;
     HPOINTER                        hPtr = ::WinQuerySysPointer(HWND_DESKTOP, SPTR_ARROW, FALSE);
 
     ::WinSetPointer(HWND_DESKTOP, hPtr);
+    ::WinQueryPointerPos(HWND_DESKTOP, &vPoint);
+    hWnd = ::WinWindowFromPoint(HWND_DESKTOP, &vPoint, TRUE);
+    if (hWnd != (HWND)GetHwnd())
+    {
+        m_vToolTimer.Stop();
+        return;
+    }
+
     rEvent.GetPosition(&vX, &vY);
 
     wxToolBarTool*            pTool = (wxToolBarTool *)FindToolForPosition( vX
@@ -775,12 +927,13 @@ void wxToolBar::OnMouseEvent(
 
     if (!pTool)
     {
+        m_vToolTimer.Stop();
         if (m_nCurrentTool > -1)
         {
             if (rEvent.LeftIsDown())
                 SpringUpButton(m_nCurrentTool);
             pTool = (wxToolBarTool *)FindById(m_nCurrentTool);
-            if (pTool && pTool->IsToggled())
+            if (pTool && !pTool->IsToggled())
             {
                 RaiseTool( pTool
                           ,FALSE
@@ -808,8 +961,23 @@ void wxToolBar::OnMouseEvent(
                 }
                 DrawTool(pTool);
             }
+            wxToolBarTool*          pOldTool = (wxToolBarTool*)FindById(m_nCurrentTool);
+
+            if (pOldTool && !pTool->IsToggled())
+                RaiseTool( pOldTool
+                          ,FALSE
+                         );
             m_nCurrentTool = pTool->GetId();
             OnMouseEnter(m_nCurrentTool);
+            if (!pTool->GetShortHelp().IsEmpty())
+            {
+                if (m_pToolTip)
+                    delete m_pToolTip;
+                m_pToolTip = new wxToolTip(pTool->GetShortHelp());
+                m_vXMouse = (wxCoord)vPoint.x;
+                m_vYMouse = (wxCoord)vPoint.y;
+                m_vToolTimer.Start(3000L, TRUE);
+            }
             if (!pTool->IsToggled())
                 RaiseTool(pTool);
         }
@@ -947,11 +1115,23 @@ void wxToolBar::DrawTool(
                               ,&vX
                               ,&vY
                              );
-            vLeft += (wxCoord)((m_vTextX - vX)/2);
-            rDc.DrawText( pTool->GetLabel()
-                         ,vLeft
-                         ,pTool->m_vY + m_vTextY + 4 // a bit of margin
-                        );
+            if (pTool->GetWidth() > vX) // large tools
+            {
+                vLeft = pTool->m_vX + (pTool->GetWidth() - vX);
+                GetSize(&vX, &vY);
+                rDc.DrawText( pTool->GetLabel()
+                             ,vLeft
+                             ,vY - (m_vTextY - 2)
+                            );
+            }
+            else  // normal tools
+            {
+                vLeft += (wxCoord)((m_vTextX - vX)/2);
+                rDc.DrawText( pTool->GetLabel()
+                             ,vLeft
+                             ,pTool->m_vY + m_vTextY + 4 // a bit of margin
+                            );
+            }
         }
     }
     else
@@ -1012,6 +1192,10 @@ wxToolBarToolBase* wxToolBar::FindToolForPosition(
     wxCoord                         vTextY = 0;
     wxCoord                         vTBarHeight = 0;
 
+    GetSize( NULL
+            ,&vTBarHeight
+           );
+    vY = vTBarHeight - vY;
     wxToolBarToolsList::Node* pNode = m_tools.GetFirst();
     while (pNode)
     {
@@ -1132,14 +1316,22 @@ void wxToolBar::LowerTool (
 
     if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsEmpty())
     {
-        vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
+        if (pTool->GetWidth() > m_vTextX)
+        {
+            vX = pTool->m_vX - 2;
+            vWidth = pTool->GetWidth() + 4;
+        }
+        else
+        {
+            vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
+            vWidth = m_vTextX + 4;
+        }
         vY = pTool->m_vY - 2;
-        vWidth = m_vTextX + 4;
         vHeight = pTool->GetHeight() + m_vTextY + 2;
     }
     else
     {
-        vX = pTool->m_vX;
+        vX = pTool->m_vX - 2;
         vY = pTool->m_vY - 2;
         vWidth = pTool->GetWidth() + 4;
         vHeight = pTool->GetHeight() + 4;
@@ -1204,14 +1396,22 @@ void wxToolBar::RaiseTool (
 
     if (HasFlag(wxTB_TEXT) && !pTool->GetLabel().IsEmpty())
     {
-        vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
+        if (pTool->GetWidth() > m_vTextX)
+        {
+            vX = pTool->m_vX - 2;
+            vWidth = pTool->GetWidth() + 4;
+        }
+        else
+        {
+            vX = pTool->m_vX - (wxCoord)(pTool->GetWidth()/2);
+            vWidth = m_vTextX + 4;
+        }
         vY = pTool->m_vY - 2;
-        vWidth = m_vTextX + 4;
         vHeight = pTool->GetHeight() + m_vTextY + 2;
     }
     else
     {
-        vX = pTool->m_vX;
+        vX = pTool->m_vX - 2;
         vY = pTool->m_vY - 2;
         vWidth = pTool->GetWidth() + 4;
         vHeight = pTool->GetHeight() + 4;
@@ -1235,4 +1435,26 @@ void wxToolBar::RaiseTool (
     }
 } // end of wxToolBar::RaiseTool
 
+void wxToolBar::OnTimer (
+  wxTimerEvent&                     rEvent
+)
+{
+    if (rEvent.GetId() == m_vToolTimer.GetTimerId())
+    {
+        wxPoint                     vPos( m_vXMouse
+                                         ,m_vYMouse
+                                        );
+
+        m_pToolTip->DisplayToolTipWindow(vPos);
+        m_vToolTimer.Stop();
+        m_vToolExpTimer.Start(3000L, TRUE);
+    }
+    else if (rEvent.GetId() == m_vToolExpTimer.GetTimerId())
+    {
+        m_pToolTip->HideToolTipWindow();
+        GetParent()->Refresh();
+        m_vToolExpTimer.Stop();
+    }
+} // end of wxToolBar::OnTimer
+
 #endif // ndef for wxUSE_TOOLBAR && wxUSE_TOOLBAR_NATIVE