*** backup/SWin32.h	2007-11-30 11:47:42.000000000 +0900
--- SWin32.h	2007-11-30 22:20:28.000000000 +0900
***************
*** 120,124 ****
    virtual void setModal (SWindow*w, bool decorated);
  
-   void* cdc;
    static void setPixmapCacheSize(unsigned int _size);
    static void setPixmapCacheOn (bool _on);
--- 120,123 ----
***************
*** 143,146 ****
--- 142,152 ----
    virtual void setDoubleBuffer (bool isOn);
    virtual bool isDubleBufferEnabled () const;
+ 
+   bool dbufferOn; // double buffer
+   void* dbuffer;
+   long id;
+   void* cdc;
+   SColor background;
+ 
  private:
    bool dcin();
***************
*** 149,156 ****
    void*   clipRegion;
    SGEngine*     engine;
-   SColor background;
    SPen   pen;
    SW32Impl* impl;
-   long id;
    SString name;
    SString imname;
--- 155,160 ----
*** backup/SWin32.cpp	2007-11-30 11:47:42.000000000 +0900
--- SWin32.cpp	2007-12-01 14:20:52.000000000 +0900
***************
*** 19,23 ****
  
  #define SS_YUDIT_DIALOG_STYLE \
!      (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME)
  
  #define SS_YUDIT_TOPLEVEL_STYLE \
--- 19,23 ----
  
  #define SS_YUDIT_DIALOG_STYLE \
!      (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_CLIPCHILDREN)
  
  #define SS_YUDIT_TOPLEVEL_STYLE \
***************
*** 77,80 ****
--- 77,100 ----
  
  
+ class SDoubleBuffer 
+ {
+ public:
+   SDoubleBuffer (HWND _id, HDC _gotDC, 
+      const SColor& background,
+      int _x, int _y, 
+      unsigned int _w, unsigned int _h);
+   copyToScreen  (HWND _id, HDC _gotDC);
+   ~SDoubleBuffer ();
+ 
+   HDC     bitmapHDC;
+   HBITMAP bitmap; 
+ 
+   int x;
+   int y;
+   unsigned int width;
+   unsigned int height;
+ };
+ 
+ 
  /**
   * Clipboard Stuff.
***************
*** 162,165 ****
--- 182,235 ----
   * START
   *-------------------------------------------------------------------------*/
+ SDoubleBuffer::SDoubleBuffer (HWND _id, HDC _gotDC, 
+      const SColor& background, int _x, int _y, 
+      unsigned int _w, unsigned int _h)
+ {
+   HDC winDC = (_gotDC == 0) ? GetDC(_id) : _gotDC;
+ 
+   bitmapHDC = CreateCompatibleDC (winDC);
+ //  fprintf (stderr, "bitmapHDC=%u\n", (unsigned int) bitmapHDC);
+ 
+   x = _x; y = _y;
+   width = _w; height = _h;
+ 
+   if (width == 0) width = 1;
+   if (height == 0) height = 1;
+ 
+   // can't use bitmapHDC, don't ask me why
+   bitmap = CreateCompatibleBitmap(winDC, x+width, y+height);
+   SelectObject(bitmapHDC, bitmap);
+   if (_gotDC == 0) ReleaseDC (_id, winDC);
+ 
+   if (bitmap == 0)
+   {
+     fprintf (stderr, "Bitmap is null...\n");
+   }
+   // Clear the bitmap with background
+   RECT rect; rect.left = x; rect.top = y;
+   rect.right = x + (int) width; rect.bottom = y + (int) height;
+   HBRUSH brush = getSolidBrush (background);
+   int mode = SetMapMode (bitmapHDC, MM_TEXT);
+   FillRect (bitmapHDC, &rect,  brush);
+   SetMapMode (bitmapHDC, mode);
+   // brush is cached, dont delete it
+ }
+ 
+ SDoubleBuffer::copyToScreen  (HWND _id, HDC _gotDC)
+ {
+   HDC winDC = (_gotDC == 0) ? GetDC(_id) : _gotDC;
+ 
+   // SelectClipRgn ((HDC)bitmapHDC, (HRGN) 0);
+   // SelectClipRgn (winDC, (HRGN) 0);
+   BitBlt (winDC, x, y, width, height, bitmapHDC, x, y, SRCCOPY);
+ 
+   if (_gotDC == 0) ReleaseDC (_id, winDC);
+ }
+ 
+ SDoubleBuffer::~SDoubleBuffer ()
+ {
+   DeleteDC (bitmapHDC);
+   DeleteObject (bitmap);
+ }
  class SBitmapItem
  {
***************
*** 507,512 ****
            continue;
          }
          /* FIXME : if window is not yet visible continue */
!         if (evt->clear && evt->width > 0 && evt->height > 0)
          {
            swid->repaintBackground (
--- 577,594 ----
            continue;
          }
+ 
+         // swid->dbuffer IS 0
+         if (swid->dbufferOn && swid->dbuffer == 0)
+         {
+           swid->dbuffer = (evt->width == 0 || evt->height == 0)
+             ? new SDoubleBuffer ((HWND) swid->id, 0, 
+                swid->background, 0, 0, 
+                swid->getWidth(), swid->getHeight())
+             : new SDoubleBuffer ((HWND) swid->id, 0, 
+                swid->background, evt->x, evt->y, evt->width, evt->height);
+         }
          /* FIXME : if window is not yet visible continue */
!         if (swid->dbuffer == 0 && 
! 	    evt->clear && evt->width > 0 && evt->height > 0)
          {
            swid->repaintBackground (
***************
*** 523,526 ****
--- 605,616 ----
            li->redraw (swid, evt->x, evt->y, evt->width, evt->height);
          }
+         if (swid->dbuffer)
+         {
+            ((SDoubleBuffer*) swid->dbuffer)->copyToScreen (
+                (HWND) swid->id, 
+                (HDC)  0);
+            delete (SDoubleBuffer*) swid->dbuffer;
+            swid->dbuffer = 0;
+         }
        }
      }
***************
*** 818,821 ****
--- 908,913 ----
    clipChained = false;
    clipChain = 0;
+   dbufferOn = 0;
+   dbuffer = 0;
    windowHashtable.put (id, this);
  }
***************
*** 823,826 ****
--- 915,919 ----
  SW32Window::~SW32Window()
  {
+   if (dbuffer) delete (SDoubleBuffer*) dbuffer;
    if (engine) delete engine;
    windowHashtable.remove (id);
***************
*** 905,910 ****
      {
        EnableWindow ((HWND)modalID, true);
-       //SetForegroundWindow ((HWND)modalID);
        SetActiveWindow ((HWND)modalID);
        SW32Window* swid = (SW32Window*) windowHashtable.get(modalID);
        if (swid) 
--- 998,1003 ----
      {
        EnableWindow ((HWND)modalID, true);
        SetActiveWindow ((HWND)modalID);
+       SetForegroundWindow ((HWND)modalID);
        SW32Window* swid = (SW32Window*) windowHashtable.get(modalID);
        if (swid) 
***************
*** 1186,1190 ****
  #else
              w->cdc = dc;
!             if (pstr.fErase)
              {
                w->repaintBackground (
--- 1279,1297 ----
  #else
              w->cdc = dc;
! 
!         // w->dbuffer IS 0
!         if (w->dbufferOn && w->dbuffer == 0)
!         {
!           w->dbuffer = ( pstr.rcPaint.bottom == pstr.rcPaint.top 
!                  || pstr.rcPaint.left == pstr.rcPaint.right)
!             ? new SDoubleBuffer ((HWND) w->id, (HDC) w->cdc, 
!                w->background, 0, 0,
!                w->getWidth(), w->getHeight())
!             : new SDoubleBuffer ((HWND) w->id, (HDC) w->cdc, 
!                w->background, pstr.rcPaint.left, pstr.rcPaint.top, 
!                pstr.rcPaint.right-pstr.rcPaint.left, 
!                pstr.rcPaint.bottom-pstr.rcPaint.top);
!         }
!             if (w->dbuffer == 0 && pstr.fErase)
              {
                w->repaintBackground (
***************
*** 1195,1198 ****
--- 1302,1313 ----
                pstr.rcPaint.right-pstr.rcPaint.left,
                pstr.rcPaint.bottom-pstr.rcPaint.top);
+         if (w->dbuffer)
+         {
+            ((SDoubleBuffer*) w->dbuffer)->copyToScreen (
+                (HWND) w->id, 
+                (HDC)  w->cdc);
+            delete (SDoubleBuffer*) w->dbuffer;
+            w->dbuffer = 0;
+         }
              w->cdc = 0;
  #endif
***************
*** 2233,2236 ****
--- 2348,2357 ----
    }
    dcins++;
+   if (dbuffer)
+   {
+     cdc = ((SDoubleBuffer*)dbuffer)->bitmapHDC;
+     SelectClipRgn ((HDC)cdc, (HRGN) clipRegion);
+     return false;
+   }
    if (cdc != 0)
    {
***************
*** 2255,2259 ****
    }
    SelectClipRgn ((HDC)cdc, 0);
!   if (wasin)
    {
      ReleaseDC ((HWND)id, (HDC)cdc);
--- 2376,2380 ----
    }
    SelectClipRgn ((HDC)cdc, 0);
!   if (wasin && dbuffer == 0)
    {
      ReleaseDC ((HWND)id, (HDC)cdc);
***************
*** 3301,3304 ****
--- 3422,3426 ----
  SW32Window::setDoubleBuffer (bool isOn)
  {
+   dbufferOn = isOn;
  }
  
***************
*** 3306,3309 ****
  SW32Window::isDubleBufferEnabled () const
  {
!   return false;
  }
--- 3428,3431 ----
  SW32Window::isDubleBufferEnabled () const
  {
!   return dbufferOn;
  }
