//
// Copyright (C) 1999-2002 Toshikaz Hirabayashi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// TOSHIKAZ HIRABAYASHI BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
// OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Except as contained in this notice, the name of Toshikaz Hirabayashi shall
// not be used in advertising or otherwise to promote the sale, use or other
// dealings in this Software without prior written authorization from
// Toshikaz Hirabayashi.

#include <win/WSDwinnwDev.h>
#include <win/WSDwinAppDev.h>
#include <WSCdevice.h>
#include <win/WSDwindraw.h>
#include <WSCbase.h>

WSMFclassInit(WSDwinnwDev,WSDnwDev);
static WSCushort _p_w = 0;
static WSCushort _p_h = 0;
WSDdev* _wsdwinnwdev_init_(){
//WSMFtrace("create WSDwinnwDev \n");
  return new WSDwinnwDev();
}

//static WSDwindraw* draw_of_winnw = NULL;


class _WSDwinnwDev_init_ {
  public:_WSDwinnwDev_init_(){
//WSMFtrace("set create handler of WSDwinnwDev \n");
     WSGIappDevice()->setCreateHandler("nwDev",_wsdwinnwdev_init_);
  };
};
static _WSDwinnwDev_init_ _init_to_run_WSDwinnwDev_;

WSDwinnwDev::WSDwinnwDev(){
//WSMFtrace("WSDwinnwDev::WSDwinnwDev 0x%x 0x%x\n",this,*(long*)this);
//  if (draw_of_winnw == NULL){
//    draw_of_winnw = new WSDwindraw();
//  }
//  setDraw(draw_of_winnw);
}

WSDwinnwDev::~WSDwinnwDev(){
}

HBITMAP WSDwinnwDev::_pixmap = 0;
HDC     WSDwinnwDev::_mdc = 0;

long WSDwinnwDev::getPixmap(){
    WSCushort w1;
    WSCushort h1;
    getWH(&w1,&h1);
WSMFtrace("XXZZpx1 w1=%d _w=%d\n",w1,(long)_w);
WSMFtrace("XXZZpx1 h1=%d _h=%d\n",h1,(long)_h);
    if (w1 > 1600){
      w1 = 1600;
    }
    if (h1 > 1280){
      h1 = 1280;
    }

    if (_p_w < w1 || _p_h < h1){
      if (_p_w < w1){
        _p_w = w1;
      }
      if (_p_h < h1){
        _p_h = h1;
      }
      if (_pixmap != 0){
        DeleteObject((void*)_pixmap);
        _pixmap = 0;
      }
    }
WSMFtrace("XXZZpx11 _p_w=%d _p_h=%d\n",_p_w,_p_h);
  
    if (_pixmap == 0){
      WSDwinAppDev* app   = WSGIwinAppDev();
      if (_p_w == 0){
        _p_w = 100;
      }
      if (_p_h == 0){
        _p_h = 100;
      }

      HDC dc = GetDC(app->getHWND());
WSMFtrace("XXZZpx1 dc=0x%x %d,%d\n",dc,_p_w,_p_h);
      if (dc != 0){
        _pixmap = CreateCompatibleBitmap(dc,_p_w,_p_h);
        if (_pixmap != 0){
          if (_mdc == 0){
            _mdc = CreateCompatibleDC(dc);
          }
          if (_mdc != 0){
            SelectObject(_mdc,_pixmap);
          }
        }
      }
      ReleaseDC(app->getHWND(),dc);
    }

    //fault
    if (_pixmap == 0){
WSMFtrace("Error!   WSDwinnwDev::getPixmap cannot create pixmap.\n");
WSMFtrace("terminate application.\n");
      exit(1);
    }
    return (long)_pixmap;
}

WSCbool WSDwinnwDev::getReady(){
  WSDdev* parent = getParentDev();
  if (parent == NULL){
    return False;
  } 
  return parent->getReady();
}
long WSDwinnwDev::_copy_pixmap_to_window(){
WSMFtrace("WSDwinnwDev::_copy_pixmap_to_window 0x%x\n",_pixmap);
  if (_pixmap  !=0){
    short x,y;
    short bx,by;
    WSCushort w,h;
    getCopyPixmapGeom(&x,&y,&w,&h);
    getBackXY(&bx,&by);
    WSDdev* parent = getParentDev();
    if (parent != NULL){
WSMFtrace("WSDwinnwDev::_copy_pixmap_to_window %d,%d,%d,%d->%d,%d\n",x,y,w,h,x+bx,y+by);
      HDC  dc = (HDC)parent->getContextResource(); 
      BitBlt(dc,bx + x, by + y, w,h, _mdc, x, y, SRCCOPY);
    }
  }
  return WS_NO_ERR;
}
long WSDwinnwDev::getContextResource(){
  WSDdev* parent = getParentDev();
  if (getUsePixmap() != False){
//    if (_mdc == 0){
      getPixmap();
//    }
WSMFtrace("WSDwinnwDev::getContextResource 0x%x\n",_mdc);
    return (long)_mdc;
  }else{
WSMFtrace("WSDwinnwDev::getContextResource2 0x%x\n",parent->getContextResource());
    return parent->getContextResource(); 
  }
}
