]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/buttonbar.cpp
Rename delegates to try and stop name clashes, probably with wxWebKitCtrl.
[wxWidgets.git] / src / generic / buttonbar.cpp
index c632ec8ca3167bbaf84cafcea40145a2c779c603..cf9e0ba75b74d6721e740f24d506dc8e8b05b1bd 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     2006-04-13
 // Id:          $Id$
 // Copyright:   (c) Julian Smart, Robert Roebling, Vadim Zeitlin,
-//              SciTech Software, Inc. 
+//              SciTech Software, Inc.
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // Currently, only for Mac as a toolbar replacement.
 #if defined(__WXMAC__) && wxUSE_TOOLBAR && wxUSE_BMPBUTTON
 
+#include "wx/generic/buttonbar.h"
+
 #ifndef WX_PRECOMP
     #include "wx/utils.h"
     #include "wx/app.h"
+    #include "wx/log.h"
+    #include "wx/frame.h"
+    #include "wx/dcclient.h"
+    #include "wx/settings.h"
+    #include "wx/image.h"
 #endif
 
-#include "wx/generic/buttonbar.h"
-#include "wx/frame.h"
-#include "wx/image.h"
-#include "wx/log.h"
-#include "wx/settings.h"
-#include "wx/dcclient.h"
-
 // ----------------------------------------------------------------------------
 // wxButtonToolBarTool: our implementation of wxToolBarToolBase
 // ----------------------------------------------------------------------------
@@ -62,12 +62,14 @@ public:
         m_x = m_y = wxDefaultCoord;
         m_width =
         m_height = 0;
-        
+
         m_button = NULL;
     }
 
-    wxButtonToolBarTool(wxButtonToolBar *tbar, wxControl *control)
-        : wxToolBarToolBase(tbar, control)
+    wxButtonToolBarTool(wxButtonToolBar *tbar,
+                        wxControl *control,
+                        const wxString& label)
+        : wxToolBarToolBase(tbar, control, label)
     {
         m_x = m_y = wxDefaultCoord;
         m_width =
@@ -99,6 +101,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxButtonToolBar, wxControl)
 BEGIN_EVENT_TABLE(wxButtonToolBar, wxControl)
     EVT_BUTTON(wxID_ANY, wxButtonToolBar::OnCommand)
     EVT_PAINT(wxButtonToolBar::OnPaint)
+    EVT_LEFT_UP(wxButtonToolBar::OnLeftUp)
 END_EVENT_TABLE()
 
 // ----------------------------------------------------------------------------
@@ -113,11 +116,13 @@ void wxButtonToolBar::Init()
     // unknown widths for the tools and separators
     m_widthSeparator = wxDefaultCoord;
 
-    m_maxWidth =
-        m_maxHeight = 0;
+    m_maxWidth = m_maxHeight = 0;
+
+    m_labelMargin = 2;
+    m_labelHeight = 0;
 
-    SetMargins(8, 4);
-    SetToolPacking(5);
+    SetMargins(8, 2);
+    SetToolPacking(8);
 }
 
 bool wxButtonToolBar::Create(wxWindow *parent,
@@ -133,9 +138,20 @@ bool wxButtonToolBar::Create(wxWindow *parent,
         return false;
     }
 
-    // TODO: get the correct colour from the system
-    wxColour lightBackground(240, 240, 240);
-    SetBackgroundColour(lightBackground);
+    // wxColour lightBackground(244, 244, 244);
+
+    wxFont font(wxSMALL_FONT->GetPointSize(), wxNORMAL_FONT->GetFamily(), wxNORMAL_FONT->GetStyle(), wxNORMAL);
+    SetFont(font);
+
+    // Calculate the label height if necessary
+    if (GetWindowStyle() & wxTB_TEXT)
+    {
+        wxClientDC dc(this);
+        dc.SetFont(font);
+        int w, h;
+        dc.GetTextExtent(wxT("X"), & w, & h);
+        m_labelHeight = h;
+    }
     return true;
 }
 
@@ -191,7 +207,7 @@ void wxButtonToolBar::GetRectLimits(const wxRect& rect,
                               wxCoord *start,
                               wxCoord *end) const
 {
-    wxCHECK_RET( start && end, _T("NULL pointer in GetRectLimits") );
+    wxCHECK_RET( start && end, wxT("NULL pointer in GetRectLimits") );
 
     if ( IsVertical() )
     {
@@ -210,7 +226,7 @@ void wxButtonToolBar::SetToolShortHelp(int id, const wxString& help)
 {
     wxToolBarToolBase *tool = FindById(id);
 
-    wxCHECK_RET( tool, _T("SetToolShortHelp: no such tool") );
+    wxCHECK_RET( tool, wxT("SetToolShortHelp: no such tool") );
 
     // TODO: set tooltip/short help
     tool->SetShortHelp(help);
@@ -257,9 +273,10 @@ wxToolBarToolBase *wxButtonToolBar::CreateTool(int id,
                              clientData, shortHelp, longHelp);
 }
 
-wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control)
+wxToolBarToolBase *wxButtonToolBar::CreateTool(wxControl *control,
+                                               const wxString& label)
 {
-    return new wxButtonToolBarTool(this, control);
+    return new wxButtonToolBarTool(this, control, label);
 }
 
 // ----------------------------------------------------------------------------
@@ -272,7 +289,7 @@ wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
 
     wxRect rect;
 
-    wxCHECK_MSG( tool, rect, _T("GetToolRect: NULL tool") );
+    wxCHECK_MSG( tool, rect, wxT("GetToolRect: NULL tool") );
 
     // ensure that we always have the valid tool position
     if ( m_needsLayout )
@@ -280,8 +297,8 @@ wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
         wxConstCast(this, wxButtonToolBar)->DoLayout();
     }
 
-    rect.x = tool->m_x - m_xMargin;
-    rect.y = tool->m_y - m_yMargin;
+    rect.x = tool->m_x - (m_toolPacking/2);
+    rect.y = tool->m_y;
 
     if ( IsVertical() )
     {
@@ -290,7 +307,7 @@ wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
             rect.width = m_defaultWidth;
             rect.height = m_defaultHeight;
             if (tool->GetButton())
-                rect.SetSize(tool->GetButton()->GetSize());
+                rect.SetSize(wxSize(tool->m_width, tool->m_height));
         }
         else if (tool->IsSeparator())
         {
@@ -310,7 +327,7 @@ wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
             rect.width = m_defaultWidth;
             rect.height = m_defaultHeight;
             if (tool->GetButton())
-                rect.SetSize(tool->GetButton()->GetSize());
+                rect.SetSize(wxSize(tool->m_width, tool->m_height));
         }
         else if (tool->IsSeparator())
         {
@@ -324,8 +341,7 @@ wxRect wxButtonToolBar::GetToolRect(wxToolBarToolBase *toolBase) const
         }
     }
 
-    rect.width += 2*m_xMargin;
-    rect.height += 2*m_yMargin;
+    rect.width += m_toolPacking;
 
     return rect;
 }
@@ -334,11 +350,11 @@ bool wxButtonToolBar::Realize()
 {
     if ( !wxToolBarBase::Realize() )
         return false;
-    
+
     m_needsLayout = true;
     DoLayout();
-    
-    SetBestSize(wxSize(m_maxWidth, m_maxHeight));
+
+    SetInitialSize(wxSize(m_maxWidth, m_maxHeight));
 
     return true;
 }
@@ -365,28 +381,57 @@ void wxButtonToolBar::DoLayout()
 
         tool->m_x = x;
         tool->m_y = y;
-        
+
         if (tool->IsButton())
         {
             if (!tool->GetButton())
             {
                 wxBitmapButton* bmpButton = new wxBitmapButton(this, tool->GetId(), tool->GetNormalBitmap(), wxPoint(tool->m_x, tool->m_y), wxDefaultSize,
-                    wxBU_AUTODRAW|wxBORDER_NONE);
-                
+                                                               wxBU_AUTODRAW|wxBORDER_NONE);
+                if (!tool->GetShortHelp().empty())
+                    bmpButton->SetLabel(tool->GetShortHelp());
+
                 tool->SetButton(bmpButton);
             }
             else
             {
                 tool->GetButton()->Move(wxPoint(tool->m_x, tool->m_y));
             }
-            
+
             int w = widthTool;
             if (tool->GetButton())
             {
                 wxSize sz = tool->GetButton()->GetSize();
                 w = sz.x;
 
+                if (m_labelHeight > 0)
+                {
+                    sz.y += (m_labelHeight + m_labelMargin);
+
+                    if (!tool->GetShortHelp().empty())
+                    {
+                        wxClientDC dc(this);
+                        dc.SetFont(GetFont());
+                        int tw, th;
+                        dc.GetTextExtent(tool->GetShortHelp(), & tw, & th);
+
+                        // If the label is bigger than the icon, the label width
+                        // becomes the new tool width, and we need to centre the
+                        // the bitmap in this box.
+                        if (tw > sz.x)
+                        {
+                            int newX = int(tool->m_x + (tw - sz.x)/2.0);
+                            tool->GetButton()->Move(newX, tool->m_y);
+                            sz.x = tw;
+                        }
+                    }
+                }
+
                 maxHeight = wxMax(maxHeight, sz.y);
+
+                tool->m_width = sz.x;
+                tool->m_height = sz.y;
+                w = sz.x;
             }
 
             *pCur += (w + GetToolPacking());
@@ -448,30 +493,43 @@ void wxButtonToolBar::OnCommand(wxCommandEvent& event)
 }
 
 // paints a border
-void wxButtonToolBar::OnPaint(wxPaintEvent& event)
+void wxButtonToolBar::OnPaint(wxPaintEvent& WXUNUSED(event))
 {
     wxPaintDC dc(this);
 
+    dc.SetFont(GetFont());
+    dc.SetBackgroundMode(wxTRANSPARENT);
+
     for ( wxToolBarToolsList::compatibility_iterator node = m_tools.GetFirst();
           node;
           node = node->GetNext() )
     {
         wxButtonToolBarTool *tool =  (wxButtonToolBarTool*) node->GetData();
+        wxRect rectTool = GetToolRect(tool);
         if (tool->IsToggled())
         {
-            wxRect rectTool = GetToolRect(tool);
-            rectTool.y = 0; rectTool.height = GetClientSize().y;
-            wxBrush brush(wxColour(220, 220, 220));
-            wxPen pen(*wxLIGHT_GREY);
+            wxRect backgroundRect = rectTool;
+            backgroundRect.y = -1; backgroundRect.height = GetClientSize().y + 1;
+            wxBrush brush(wxColour(219, 219, 219));
+            wxPen pen(wxColour(159, 159, 159));
             dc.SetBrush(brush);
             dc.SetPen(pen);
-            dc.DrawRectangle(rectTool);
+            dc.DrawRectangle(backgroundRect);
+        }
+
+        if (m_labelHeight > 0 && !tool->GetShortHelp().empty())
+        {
+            int tw, th;
+            dc.GetTextExtent(tool->GetShortHelp(), & tw, & th);
+
+            int x = tool->m_x;
+            dc.DrawText(tool->GetShortHelp(), x, tool->m_y + tool->GetButton()->GetSize().y + m_labelMargin);
         }
     }
 
     if ((GetWindowStyle() & wxTB_NODIVIDER) == 0)
     {
-        wxPen pen(*wxLIGHT_GREY);
+        wxPen pen(wxColour(159, 159, 159));
         dc.SetPen(pen);
         int x1 = 0;
         int y1 = GetClientSize().y-1;
@@ -481,5 +539,20 @@ void wxButtonToolBar::OnPaint(wxPaintEvent& event)
     }
 }
 
-#endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON
+// detects mouse clicks outside buttons
+void wxButtonToolBar::OnLeftUp(wxMouseEvent& event)
+{
+    if (m_labelHeight > 0)
+    {
+        wxButtonToolBarTool* tool = (wxButtonToolBarTool*) FindToolForPosition(event.GetX(), event.GetY());
+        if (tool && tool->GetButton() && (event.GetY() > (tool->m_y + tool->GetButton()->GetSize().y)))
+        {
+            wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, tool->GetId());
+            event.SetEventObject(tool->GetButton());
+            if (!GetEventHandler()->ProcessEvent(event))
+                event.Skip();
+        }
+    }
+}
 
+#endif // wxUSE_TOOLBAR && wxUSE_BMPBUTTON