]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/button.cpp
getting rid of redundant redraws
[wxWidgets.git] / src / os2 / button.cpp
index 3c1331876b77c6abaa1acbe248ab7c7cddc7e8ba..05a44eb6cc5db80a0057eaffaa307f2bd3c0e334 100644 (file)
 #include "wx/wxprec.h"
 
 #ifndef WX_PRECOMP
+    #include "wx/app.h"
     #include "wx/button.h"
     #include "wx/brush.h"
     #include "wx/panel.h"
     #include "wx/bmpbuttn.h"
     #include "wx/settings.h"
     #include "wx/dcscreen.h"
+    #include "wx/scrolwin.h"
 #endif
 
+#include "wx/stockitem.h"
 #include "wx/os2/private.h"
 
 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
@@ -41,16 +44,20 @@ IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
 bool wxButton::Create(
   wxWindow*                         pParent
 , wxWindowID                        vId
-, const wxString&                   rsLabel
+, const wxString&                   rsLbl
 , const wxPoint&                    rPos
 , const wxSize&                     rSize
 , long                              lStyle
-#if wxUSE_VALIDATORS
 , const wxValidator&                rValidator
-#endif
 , const wxString&                   rsName
 )
 {
+    wxString rsLabel(rsLbl);
+    if (rsLabel.empty() && wxIsStockID(vId))
+        rsLabel = wxGetStockLabel(vId);
+
+    wxString                        sLabel = ::wxPMTextToLabel(rsLabel);
+
     SetName(rsName);
 #if wxUSE_VALIDATORS
     SetValidator(rValidator);
@@ -70,9 +77,10 @@ bool wxButton::Create(
     //
     if (m_windowStyle & wxCLIP_SIBLINGS )
         lStyle |= WS_CLIPSIBLINGS;
+
     m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent)   // Parent handle
                                        ,WC_BUTTON            // A Button class window
-                                       ,(PSZ)rsLabel.c_str() // Button text
+                                       ,(PSZ)sLabel.c_str()  // Button text
                                        ,lStyle               // Button style
                                        ,0, 0, 0, 0           // Location and size
                                        ,GetHwndOf(pParent)   // Owner handle
@@ -90,12 +98,20 @@ bool wxButton::Create(
     // Subclass again for purposes of dialog editing mode
     //
     SubclassWin(m_hWnd);
-    SetFont(pParent->GetFont());
+    wxFont*                          pButtonFont = new wxFont( 8
+                                                              ,wxSWISS
+                                                              ,wxNORMAL
+                                                              ,wxNORMAL
+                                                             );
+    SetFont(*pButtonFont);
+    SetXComp(0);
+    SetYComp(0);
     SetSize( rPos.x
             ,rPos.y
             ,rSize.x
             ,rSize.y
            );
+    delete pButtonFont;
     return TRUE;
 } // end of wxButton::Create
 
@@ -150,15 +166,21 @@ wxSize wxButton::DoGetBestSize() const
     //
     // Need a little extra to make it look right
     //
-    nHeightButton += nHeightChar/1.5;
+    nHeightButton += (int)(nHeightChar/1.5);
 
-    wxSize                          vSize = GetDefaultSize();
+    if (!HasFlag(wxBU_EXACTFIT))
+    {
+        wxSize                      vSize = GetDefaultSize();
 
-    if (nWidthButton > vSize.x)
-        vSize.x = nWidthButton;
-    if (nHeightButton > vSize.y)
-        vSize.y = nHeightButton;
-    return vSize;
+        if (nWidthButton > vSize.x)
+            vSize.x = nWidthButton;
+        if (nHeightButton > vSize.y)
+            vSize.y = nHeightButton;
+        return vSize;
+    }
+    return wxSize( nWidthButton
+                  ,nHeightButton
+                 );
 } // end of wxButton::DoGetBestSize
 
 /* static */
@@ -170,7 +192,7 @@ wxSize wxButton::GetDefaultSize()
     {
         wxScreenDC                  vDc;
 
-        vDc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
+        vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
 
         //
         // The size of a standard button in the dialog units is 50x14,
@@ -211,31 +233,95 @@ bool wxButton::SendClickEvent()
 void wxButton::SetDefault()
 {
     wxWindow*                       pParent = GetParent();
-    wxButton*                       pBtnOldDefault = NULL;
-    wxPanel*                        pPanel = wxDynamicCast(pParent, wxPanel);
-    long                            lStyle = 0L;
 
-    if (pParent)
-    {
-        wxWindow*                   pWinOldDefault = pParent->SetDefaultItem(this);
+    wxCHECK_RET( pParent, _T("button without parent?") );
 
-        pBtnOldDefault = wxDynamicCast(pWinOldDefault, wxButton);
-    }
-    if (pBtnOldDefault && pBtnOldDefault != this)
+    //
+    // Set this one as the default button both for wxWidgets and Windows
+    //
+    wxWindow*                       pWinOldDefault = pParent->SetDefaultItem(this);
+
+    SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton)
+                    ,FALSE
+                   );
+    SetDefaultStyle( this
+                    ,TRUE
+                   );
+} // end of wxButton::SetDefault
+
+void wxButton::SetTmpDefault()
+{
+    wxWindow*                       pParent = GetParent();
+
+    wxCHECK_RET( pParent, _T("button without parent?") );
+
+    wxWindow*                       pWinOldDefault = pParent->GetDefaultItem();
+
+    pParent->SetTmpDefaultItem(this);
+    SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton)
+                    ,FALSE
+                   );
+    SetDefaultStyle( this
+                    ,TRUE
+                   );
+} // end of wxButton::SetTmpDefault
+
+void wxButton::UnsetTmpDefault()
+{
+    wxWindow*                       pParent = GetParent();
+
+    wxCHECK_RET( pParent, _T("button without parent?") );
+
+    pParent->SetTmpDefaultItem(NULL);
+
+    wxWindow*                       pWinOldDefault = pParent->GetDefaultItem();
+
+    SetDefaultStyle( this
+                    ,FALSE
+                   );
+    SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton)
+                    ,TRUE
+                   );
+} // end of wxButton::UnsetTmpDefault
+
+void wxButton::SetDefaultStyle(
+  wxButton*                         pBtn
+, bool                              bOn
+)
+{
+    long                            lStyle;
+    //
+    // We may be called with NULL pointer -- simpler to do the check here than
+    // in the caller which does wxDynamicCast()
+    //
+    if (!pBtn)
+        return;
+
+    //
+    // First, let DefDlgProc() know about the new default button
+    //
+    if (bOn)
     {
-        //
-        // Remove the BS_DEFPUSHBUTTON style from the other button
-        //
-        lStyle = ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE);
+        if (!wxTheApp->IsActive())
+            return;
 
         //
-        // Don't do it with the owner drawn buttons because it will reset
-        // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
+        // In OS/2 the dialog/panel doesn't really know it has a default
+        // button, the default button simply has that style.  We'll just
+        // simulate by setting focus to it
         //
+        pBtn->SetFocus();
+    }
+    lStyle = ::WinQueryWindowULong(GetHwndOf(pBtn), QWL_STYLE);
+    if (!(lStyle & BS_DEFAULT) == bOn)
+    {
         if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
         {
-            lStyle &= ~BS_DEFAULT;
-            ::WinSetWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE, lStyle);
+            if (bOn)
+                lStyle |= BS_DEFAULT;
+            else
+                lStyle &= ~BS_DEFAULT;
+            ::WinSetWindowULong(GetHwndOf(pBtn), QWL_STYLE, lStyle);
         }
         else
         {
@@ -243,20 +329,10 @@ void wxButton::SetDefault()
             // Redraw the button - it will notice itself that it's not the
             // default one any longer
             //
-            pBtnOldDefault->Refresh();
+            pBtn->Refresh();
         }
     }
-
-    //
-    // Set this button as the default
-    //
-    lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
-    if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
-    {
-        lStyle != BS_DEFAULT;
-        ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
-    }
-} // end of wxButton::SetDefault
+} // end of wxButton::UpdateDefaultStyle
 
 // ----------------------------------------------------------------------------
 // event/message handlers
@@ -308,7 +384,28 @@ void wxButton::MakeOwnerDrawn()
         lStyle |= BS_USERBUTTON;
         ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
     }
-} // end of wxCButton::MakeOwnerDrawn
+} // end of wxButton::MakeOwnerDrawn
+
+WXDWORD wxButton::OS2GetStyle(
+  long                              lStyle
+, WXDWORD*                          pdwExstyle
+) const
+{
+    //
+    // Buttons never have an external border, they draw their own one
+    //
+    WXDWORD                         dwStyle = wxControl::OS2GetStyle( (lStyle & ~wxBORDER_MASK) | wxBORDER_NONE
+                                                                     ,pdwExstyle
+                                                                    );
+
+    //
+    // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
+    // each other in any resizeable dialog which has more than one button in
+    // the bottom
+    //
+    dwStyle |= WS_CLIPSIBLINGS;
+    return dwStyle;
+} // end of wxButton::OS2GetStyle
 
 MRESULT wxButton::WindowProc(
   WXUINT                            uMsg
@@ -317,12 +414,16 @@ MRESULT wxButton::WindowProc(
 )
 {
     //
-    // When we receive focus, we want to become the default button in our
-    // parent panel
+    // When we receive focus, we want to temporary become the default button in
+    // our parent panel so that pressing "Enter" would activate us -- and when
+    // losing it we should restore the previous default button as well
     //
     if (uMsg == WM_SETFOCUS)
     {
-        SetDefault();
+        if (SHORT1FROMMP(lParam) == TRUE)
+            SetTmpDefault();
+        else
+            UnsetTmpDefault();
 
         //
         // Let the default processign take place too
@@ -352,5 +453,5 @@ MRESULT wxButton::WindowProc(
                                      ,wParam
                                      ,lParam
                                     ));
-} // end of wxW indowProc
+} // end of wxWindowProc