[USER32] Fix RealUserDrawCaption() return type. Rename {HFONT,UISTATE}_GWL_OFFSET...
[reactos.git] / win32ss / user / user32 / controls / button.c
index 3846c73..2538a98 100644 (file)
  *  - Button_SetImageList
  *  - Button_SetTextMargin
  */
+
 #include <user32.h>
 
-#include <wine/debug.h>
 WINE_DEFAULT_DEBUG_CHANNEL(button);
 
 /* GetWindowLong offsets for window extra information */
 #define STATE_GWL_OFFSET  0
-#define HFONT_GWL_OFFSET  (sizeof(LONG))
-#define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET+sizeof(HFONT))
-#define UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
-#define NB_EXTRA_BYTES    (UISTATE_GWL_OFFSET+sizeof(LONG))
+#define BUTTON_HFONT_GWL_OFFSET  (sizeof(LONG))
+#define HIMAGE_GWL_OFFSET (BUTTON_HFONT_GWL_OFFSET+sizeof(HFONT))
+#define BUTTON_UISTATE_GWL_OFFSET (HIMAGE_GWL_OFFSET+sizeof(HFONT))
+#define NB_EXTRA_BYTES    (BUTTON_UISTATE_GWL_OFFSET+sizeof(LONG))
 
-  /* undocumented flags */
+/* undocumented flags */
 #define BUTTON_NSTATES         0x0F
 #define BUTTON_BTNPRESSED      0x40
 #define BUTTON_UNKNOWN2        0x20
 #define BUTTON_UNKNOWN3        0x10
+#ifdef __REACTOS__
 #define BUTTON_BMCLICK         0x100 // ReactOS Need to up to wine!
+#endif
 
 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
     do { /* Notify parent which has created this button control */ \
@@ -115,9 +117,9 @@ static const WORD maxCheckState[MAX_BTN_TYPE] =
     BST_UNCHECKED       /* BS_OWNERDRAW */
 };
 
-typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
+typedef void (*pfButtonPaint)( HWND hwnd, HDC hdc, UINT action );
 
-static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
+static const pfButtonPaint btnPaintFunc[MAX_BTN_TYPE] =
 {
     PB_Paint,    /* BS_PUSHBUTTON */
     PB_Paint,    /* BS_DEFPUSHBUTTON */
@@ -133,10 +135,6 @@ static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
     OB_Paint     /* BS_OWNERDRAW */
 };
 
-static HBITMAP hbitmapCheckBoxes = 0;
-static WORD checkBoxWidth = 0, checkBoxHeight = 0;
-
-
 /*********************************************************************
  * button class descriptor
  */
@@ -145,8 +143,12 @@ const struct builtin_class_descr BUTTON_builtin_class =
 {
     buttonW,             /* name */
     CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style  */
+#ifdef __REACTOS__
     ButtonWndProcA,      /* procA */
     ButtonWndProcW,      /* procW */
+#else
+    WINPROC_BUTTON,      /* proc */
+#endif
     NB_EXTRA_BYTES,      /* extra */
     IDC_ARROW,           /* cursor */
     0                    /* brush */
@@ -163,24 +165,28 @@ static inline void set_button_state( HWND hwnd, LONG state )
     SetWindowLongPtrW( hwnd, STATE_GWL_OFFSET, state );
 }
 
+#ifdef __REACTOS__
+
 static __inline void set_ui_state( HWND hwnd, LONG flags )
 {
-    SetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET, flags );
+    SetWindowLongPtrW( hwnd, BUTTON_UISTATE_GWL_OFFSET, flags );
 }
 
 static __inline LONG get_ui_state( HWND hwnd )
 {
-    return GetWindowLongPtrW( hwnd, UISTATE_GWL_OFFSET );
+    return GetWindowLongPtrW( hwnd, BUTTON_UISTATE_GWL_OFFSET );
 }
 
-__inline static HFONT get_button_font( HWND hwnd )
+#endif /* __REACTOS__ */
+
+static inline HFONT get_button_font( HWND hwnd )
 {
-    return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
+    return (HFONT)GetWindowLongPtrW( hwnd, BUTTON_HFONT_GWL_OFFSET );
 }
 
 static inline void set_button_font( HWND hwnd, HFONT font )
 {
-    SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG_PTR)font );
+    SetWindowLongPtrW( hwnd, BUTTON_HFONT_GWL_OFFSET, (LONG_PTR)font );
 }
 
 static inline UINT get_button_type( LONG window_style )
@@ -208,6 +214,7 @@ static inline WCHAR *get_button_text( HWND hwnd )
     return buffer;
 }
 
+#ifdef __REACTOS__
 /* Retrieve the UI state for the control */
 static BOOL button_update_uistate(HWND hwnd, BOOL unicode)
 {
@@ -228,6 +235,7 @@ static BOOL button_update_uistate(HWND hwnd, BOOL unicode)
 
     return FALSE;
 }
+#endif
 
 /***********************************************************************
  *           ButtonWndProc_common
@@ -259,8 +267,12 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
              return 0;
           }
        }
-    }    
-#endif    
+    }
+    else
+       return 0;
+#else
+    if (!IsWindow( hWnd )) return 0;
+#endif
 
     pt.x = (short)LOWORD(lParam);
     pt.y = (short)HIWORD(lParam);
@@ -284,30 +296,23 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
         break;
 
     case WM_CREATE:
-        if (!hbitmapCheckBoxes)
-        {
-            BITMAP bmp;
-            hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
-            GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
-            checkBoxWidth  = bmp.bmWidth / 4;
-            checkBoxHeight = bmp.bmHeight / 3;
-        }
         if (btn_type >= MAX_BTN_TYPE)
             return -1; /* abort */
 
         /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
         if (btn_type == BS_USERBUTTON )
         {
-#ifdef __REACTOS__
             style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
-            SetWindowLongPtrW( hWnd, GWL_STYLE, style );
+#ifdef __REACTOS__
+            NtUserAlterWindowStyle(hWnd, GWL_STYLE, style );
 #else
-            style = (style & ~BS_TYPEMASK) | BS_PUSHBUTTON;
             WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
 #endif
         }
         set_button_state( hWnd, BST_UNCHECKED );
+#ifdef __REACTOS__
         button_update_uistate( hWnd, unicode );
+#endif
         return 0;
 
 #ifdef __REACTOS__
@@ -325,7 +330,14 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
             HBRUSH hBrush;
             HWND parent = GetParent(hWnd);
             if (!parent) parent = hWnd;
+#ifdef __REACTOS__
             hBrush = GetControlColor( parent, hWnd, hdc, WM_CTLCOLORBTN);
+#else
+            hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
+            if (!hBrush) /* did the app forget to call defwindowproc ? */
+                hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
+                                                (WPARAM)hdc, (LPARAM)hWnd);
+#endif
             GetClientRect(hWnd, &rc);
             FillRect(hdc, &rc, hBrush);
         }
@@ -333,16 +345,18 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
 
     case WM_PRINTCLIENT:
     case WM_PAINT:
+    {
+        PAINTSTRUCT ps;
+        HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
         if (btnPaintFunc[btn_type])
         {
-            PAINTSTRUCT ps;
-            HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
             int nOldMode = SetBkMode( hdc, OPAQUE );
             (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
             SetBkMode(hdc, nOldMode); /*  reset painting mode */
-            if( !wParam ) EndPaint( hWnd, &ps );
         }
+        if ( !wParam ) EndPaint( hWnd, &ps );
         break;
+    }
 
     case WM_KEYDOWN:
        if (wParam == VK_SPACE)
@@ -375,6 +389,9 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
            break;
        /* fall through */
     case WM_LBUTTONUP:
+#ifdef _REACTOS_
+        BOOL TellParent = FALSE; //// ReactOS see note below.
+#endif
         state = get_button_state( hWnd );
         if (!(state & BUTTON_BTNPRESSED)) break;
         state &= BUTTON_NSTATES;
@@ -385,7 +402,6 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
             break;
         }
         SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
-        ReleaseCapture();
         GetClientRect( hWnd, &rect );
        if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
         {
@@ -403,12 +419,27 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
                                 (state & BST_INDETERMINATE) ? 0 : ((state & 3) + 1), 0 );
                 break;
             }
+#ifdef _REACTOS_
+            TellParent = TRUE; // <---- Fix CORE-10194, Notify parent after capture is released.
+#else
+            ReleaseCapture();
             BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
+#endif
+        }
+#ifdef _REACTOS_
+        ReleaseCapture();
+        if (TellParent) BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
+#else
+        else
+        {
+            ReleaseCapture();
         }
+#endif
         break;
 
     case WM_CAPTURECHANGED:
         TRACE("WM_CAPTURECHANGED %p\n", hWnd);
+        if (hWnd == (HWND)lParam) break;
         state = get_button_state( hWnd );
         if (state & BUTTON_BTNPRESSED)
         {
@@ -430,29 +461,44 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
     {
         /* Clear an old text here as Windows does */
 //
+// ReactOS Note :
 // wine Bug: http://bugs.winehq.org/show_bug.cgi?id=25790
 // Patch: http://source.winehq.org/patches/data/70889
 // By: Alexander LAW, Replicate Windows behavior of WM_SETTEXT handler regarding WM_CTLCOLOR*
 //
+#ifdef __REACTOS__
         if (style & WS_VISIBLE)
+#else
+        if (IsWindowVisible(hWnd))
+#endif
         {
             HDC hdc = GetDC(hWnd);
             HBRUSH hbrush;
             RECT client, rc;
             HWND parent = GetParent(hWnd);
-            UINT ctlMessage=(btn_type == BS_PUSHBUTTON ||
-                      btn_type == BS_DEFPUSHBUTTON ||
-                      btn_type == BS_PUSHLIKE ||
-                      btn_type == BS_USERBUTTON ||
-                      btn_type == BS_OWNERDRAW) ?
-                      WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
+            UINT message = (btn_type == BS_PUSHBUTTON ||
+                            btn_type == BS_DEFPUSHBUTTON ||
+                            btn_type == BS_PUSHLIKE ||
+                            btn_type == BS_USERBUTTON ||
+                            btn_type == BS_OWNERDRAW) ?
+                            WM_CTLCOLORBTN : WM_CTLCOLORSTATIC;
 
             if (!parent) parent = hWnd;
-
-            hbrush = GetControlColor( parent, hWnd, hdc, ctlMessage);
+#ifdef __REACTOS__
+            hbrush = GetControlColor(parent, hWnd, hdc, message);
+#else
+            hbrush = (HBRUSH)SendMessageW(parent, message,
+                                          (WPARAM)hdc, (LPARAM)hWnd);
+            if (!hbrush) /* did the app forget to call DefWindowProc ? */
+                hbrush = (HBRUSH)DefWindowProcW(parent, message,
+                                                (WPARAM)hdc, (LPARAM)hWnd);
+#endif
 
             GetClientRect(hWnd, &client);
             rc = client;
+            /* FIXME: check other BS_* handlers */
+            if (btn_type == BS_GROUPBOX)
+                InflateRect(&rc, -7, 1); /* GB_Paint does this */
             BUTTON_CalcLabelRect(hWnd, hdc, &rc);
             /* Clip by client rect bounds */
             if (rc.right > client.right) rc.right = client.right;
@@ -460,7 +506,7 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
             FillRect(hdc, &rc, hbrush);
             ReleaseDC(hWnd, hdc);
         }
-////
+
         if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
         else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
         if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
@@ -505,11 +551,13 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
         break;
 
     case BM_SETSTYLE:
-        if ((wParam & BS_TYPEMASK) >= MAX_BTN_TYPE) break;
         btn_type = wParam & BS_TYPEMASK;
         style = (style & ~BS_TYPEMASK) | btn_type;
-        SetWindowLongPtrW( hWnd, GWL_STYLE, style );
-        //WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
+#ifdef __REACTOS__
+        NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
+#else
+        WIN_SetStyle( hWnd, style, BS_TYPEMASK & ~style );
+#endif
 
         /* Only redraw if lParam flag is set.*/
         if (lParam)
@@ -518,20 +566,20 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
         break;
 
     case BM_CLICK:
-        //// ReactOS
-        state = get_button_state( hWnd );
+#ifdef __REACTOS__
+        state = get_button_state(hWnd);
         if (state & BUTTON_BMCLICK)
            break;
-        set_button_state( hWnd, state | BUTTON_BMCLICK ); // Tracked in STATE_GWL_OFFSET.
-        ////
+        set_button_state(hWnd, state | BUTTON_BMCLICK); // Tracked in STATE_GWL_OFFSET.
+#endif
        SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
        SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
-        ////
-        state = get_button_state( hWnd );
+#ifdef __REACTOS__
+        state = get_button_state(hWnd);
         if (!(state & BUTTON_BMCLICK)) break;
         state &= ~BUTTON_BMCLICK;
-        set_button_state( hWnd, state );
-        ////
+        set_button_state(hWnd, state);
+#endif
        break;
 
     case BM_SETIMAGE:
@@ -565,7 +613,7 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
 #ifdef __REACTOS__
             if (wParam) style |= WS_TABSTOP;
             else style &= ~WS_TABSTOP;
-            SetWindowLongPtrW( hWnd, GWL_STYLE, style );
+            NtUserAlterWindowStyle(hWnd, GWL_STYLE, style);
 #else
             if (wParam) WIN_SetStyle( hWnd, WS_TABSTOP, 0 );
             else WIN_SetStyle( hWnd, 0, WS_TABSTOP );
@@ -593,6 +641,7 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
         paint_button( hWnd, btn_type, ODA_SELECT );
         break;
 
+#ifdef __REACTOS__
     case WM_UPDATEUISTATE:
         if (unicode)
             DefWindowProcW(hWnd, uMsg, wParam, lParam);
@@ -602,6 +651,7 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
         if (button_update_uistate(hWnd, unicode))
             paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
         break;
+#endif
 
     case WM_NCHITTEST:
         if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
@@ -613,28 +663,30 @@ LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
     return 0;
 }
 
+#ifdef __REACTOS__
+
 /***********************************************************************
  *           ButtonWndProcW
  * The button window procedure. This is just a wrapper which locks
  * the passed HWND and calls the real window procedure (with a WND*
  * pointer pointing to the locked windowstructure).
  */
-LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+LRESULT WINAPI ButtonWndProcW(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
-    if (!IsWindow( hWnd )) return 0;
-    return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
+    if (!IsWindow(hWnd)) return 0;
+    return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, TRUE);
 }
 
-
 /***********************************************************************
  *           ButtonWndProcA
  */
-LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
+LRESULT WINAPI ButtonWndProcA(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 {
-    if (!IsWindow( hWnd )) return 0;
-    return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
+    if (!IsWindow(hWnd)) return 0;
+    return ButtonWndProc_common(hWnd, uMsg, wParam, lParam, FALSE);
 }
 
+#endif /* __REACTOS__ */
 
 /**********************************************************************
  * Convert button styles to flags used by DrawText.
@@ -710,18 +762,26 @@ static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
    switch (style & (BS_ICON|BS_BITMAP))
    {
       case BS_TEXT:
+      {
+          HFONT hFont, hPrevFont = 0;
+
           if (!(text = get_button_text( hwnd ))) goto empty_rect;
           if (!text[0])
           {
               HeapFree( GetProcessHeap(), 0, text );
               goto empty_rect;
           }
+
+          if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hdc, hFont );
           DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
+          if (hPrevFont) SelectObject( hdc, hPrevFont );
           HeapFree( GetProcessHeap(), 0, text );
-
-          if (get_ui_state( hwnd ) & UISF_HIDEACCEL)
+#ifdef __REACTOS__
+          if (get_ui_state(hwnd) & UISF_HIDEACCEL)
               dtStyle |= DT_HIDEPREFIX;
+#endif
           break;
+      }
 
       case BS_ICON:
          if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
@@ -793,11 +853,8 @@ static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
 {
    RECT rc;
-   rc.left = 0;
-   rc.top = 0;
-   rc.right = cx;
-   rc.bottom = cy;
 
+   SetRect(&rc, 0, 0, cx, cy);
    DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
    return TRUE;
 }
@@ -839,8 +896,10 @@ static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, const RECT *rc)
          lp = (LPARAM)text;
          wp = (WPARAM)dtFlags;
 
+#ifdef __REACTOS__
          if (dtFlags & DT_HIDEPREFIX)
              flags |= DSS_HIDEPREFIX;
+#endif
          break;
 
       case BS_ICON:
@@ -886,7 +945,11 @@ static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
     parent = GetParent(hwnd);
     if (!parent) parent = hwnd;
+#ifdef __REACTOS__
     GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
+#else
+    SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
+#endif
 
     hrgn = set_control_clipping( hDC, &rc );
 #ifdef __REACTOS__
@@ -944,11 +1007,15 @@ static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
 draw_focus:
     if (action == ODA_FOCUS || (state & BST_FOCUS))
     {
+#ifdef __REACTOS__
         if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
         {
+#endif
             InflateRect( &rc, -2, -2 );
             DrawFocusRect( hDC, &rc );
+#ifdef __REACTOS__
         }
+#endif
     }
 
  cleanup:
@@ -967,11 +1034,12 @@ static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
 {
     RECT rbox, rtext, client;
     HBRUSH hBrush;
-    int delta;
+    int delta, text_offset, checkBoxWidth, checkBoxHeight;
     UINT dtFlags;
     HFONT hFont;
     LONG state = get_button_state( hwnd );
     LONG style = GetWindowLongPtrW( hwnd, GWL_STYLE );
+    LONG ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
     HWND parent;
     HRGN hrgn;
 
@@ -984,23 +1052,36 @@ static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
     GetClientRect(hwnd, &client);
     rbox = rtext = client;
 
+    checkBoxWidth  = 12 * GetDeviceCaps( hDC, LOGPIXELSX ) / 96 + 1;
+    checkBoxHeight = 12 * GetDeviceCaps( hDC, LOGPIXELSY ) / 96 + 1;
+
     if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
+    GetCharWidthW( hDC, '0', '0', &text_offset );
+    text_offset /= 2;
 
     parent = GetParent(hwnd);
     if (!parent) parent = hwnd;
-    hBrush = GetControlColor( parent, hwnd, hDC, WM_CTLCOLORSTATIC);
+#ifdef __REACTOS__
+    hBrush = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
+#else
+    hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
+                                 (WPARAM)hDC, (LPARAM)hwnd);
+    if (!hBrush) /* did the app forget to call defwindowproc ? */
+        hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
+                                       (WPARAM)hDC, (LPARAM)hwnd );
+#endif
     hrgn = set_control_clipping( hDC, &client );
 
-    if (style & BS_LEFTTEXT)
+    if (style & BS_LEFTTEXT || ex_style & WS_EX_RIGHT)
     {
        /* magic +4 is what CTL3D expects */
 
-        rtext.right -= checkBoxWidth + 4;
+        rtext.right -= checkBoxWidth + text_offset;;
         rbox.left = rbox.right - checkBoxWidth;
     }
     else
     {
-        rtext.left += checkBoxWidth + 4;
+        rtext.left += checkBoxWidth + text_offset;;
         rbox.right = checkBoxWidth;
     }
 
@@ -1029,8 +1110,8 @@ static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
        else if (state & BST_INDETERMINATE) flags = DFCS_BUTTON3STATE;
        else flags = DFCS_BUTTONCHECK;
 
-       if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
-       if (state & BST_PUSHED) flags |= DFCS_PUSHED;
+       if (state & (BST_CHECKED | BST_INDETERMINATE)) flags |= DFCS_CHECKED;
+       if (state & BST_PUSHED) flags |= DFCS_PUSHED;
 
        if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
 
@@ -1075,13 +1156,17 @@ static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
     /* ... and focus */
     if (action == ODA_FOCUS || (state & BST_FOCUS))
     {
+#ifdef __REACTOS__
         if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
         {
+#endif
             rtext.left--;
             rtext.right++;
             IntersectRect(&rtext, &rtext, &client);
             DrawFocusRect( hDC, &rtext );
+#ifdef __REACTOS__
         }
+#endif
     }
     SelectClipRgn( hDC, hrgn );
     if (hrgn) DeleteObject( hrgn );
@@ -1130,8 +1215,14 @@ static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
     /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
     parent = GetParent(hwnd);
     if (!parent) parent = hwnd;
-    hbr = GetControlColor( parent, hwnd, hDC, WM_CTLCOLORSTATIC);
-    
+#ifdef __REACTOS__
+    hbr = GetControlColor(parent, hwnd, hDC, WM_CTLCOLORSTATIC);
+#else
+    hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
+    if (!hbr) /* did the app forget to call defwindowproc ? */
+        hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
+                                    (WPARAM)hDC, (LPARAM)hwnd);
+#endif
     GetClientRect( hwnd, &rc);
     rcFrame = rc;
     hrgn = set_control_clipping( hDC, &rc );
@@ -1180,14 +1271,25 @@ static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
 
     parent = GetParent(hwnd);
     if (!parent) parent = hwnd;
+#ifdef __REACTOS__
     hBrush = GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
+#else
+    hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
+    if (!hBrush) /* did the app forget to call defwindowproc ? */
+        hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
+                                       (WPARAM)hDC, (LPARAM)hwnd);
+#endif
 
     FillRect( hDC, &rc, hBrush );
     if (action == ODA_FOCUS || (state & BST_FOCUS))
+#ifdef __REACTOS__
     {
         if (!(get_ui_state(hwnd) & UISF_HIDEFOCUS))
+#endif
             DrawFocusRect( hDC, &rc );
+#ifdef __REACTOS__
     }
+#endif
 
     switch (action)
     {
@@ -1234,7 +1336,11 @@ static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
     if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
     parent = GetParent(hwnd);
     if (!parent) parent = hwnd;
+#ifdef __REACTOS__
     GetControlColor( parent, hwnd, hDC, WM_CTLCOLORBTN);
+#else
+    SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
+#endif
 
     hrgn = set_control_clipping( hDC, &dis.rcItem );