//
// 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 <basic.h>
#include <btron/WSDbtronNwDev.h>
#include <btron/WSDbtronAppDev.h>
#include <btron/WSDbtronDraw.h>
//#include <btron/WSxcom.h>
#include <WSCdevice.h>
#include <WSCbase.h>

WSMFclassInit(WSDbtronNwDev,WSDnwDev);

WSDdev* _wsdbtronNwdev_init_(){
//WSMFtrace("create WSDbtronNwDev \n");
  return new WSDbtronNwDev();
}

class _WSDbtronNwDev_init_ {
  public:_WSDbtronNwDev_init_(){
//WSMFtrace("set create handler of WSDbtronNwDev \n");
     WSGIappDevice()->setCreateHandler("nwDev",_wsdbtronNwdev_init_);
  };
};
static _WSDbtronNwDev_init_ _init_to_run_WSDbtronNwDev_;

static WSDbtronDraw* draw_of_btronNw = NULL;

WSDbtronNwDev::WSDbtronNwDev(){
WSMFtrace("WSDbtronNwDev::WSDbtronNwDev 0x%x 0x%x\n",this,*(long*)this);
  if (draw_of_btronNw == NULL){
    draw_of_btronNw = new WSDbtronDraw();
  }
  setDraw(draw_of_btronNw);
}

WSDbtronNwDev::~WSDbtronNwDev(){
}

//Pixmap WSDbtronNwDev::_pixmap = 0;
//GC     WSDbtronNwDev::_gc = 0;
#if 0
GID WSDbtronNwDev::getMemGid(){
  WSCushort w1 = _w;    
  WSCushort h1 = _h;    
  if (w1 > 4096){
    w1 = 4096;
  }
  if (h1 > 4096){
    h1 = 4096;
  }
  if (_pixmap_w < w1 || _pixmap_h < h1){
    if (_pixmap_w < w1){
      _pixmap_w = w1;
    }
    if (_pixmap_h < h1){
      _pixmap_h = h1;
    }
    if (_mem_gid != 0){
      gcls_env(_mem_gid);
      _mem_gid = 0;
    }
  }

  if (_mem_gid == 0){
    if (_pixmap_w == 0){
      _pixmap_w = 100;
    }
    if (_pixmap_h == 0){
      _pixmap_h = 100;
    }
    _mem_gid = WSGIbtronAppDev()->getNewMemGid(_pixmap_w,_pixmap_h);
  }

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

long WSDbtronNwDev::_copy_pixmap_to_window(){
#if 0
  if (_pixmap  !=0){
    Display* dpy = (Display*)getDeviceResource();
    Window   win = (Window)WSDnwDev::getWindowResource();
    XCopyArea(dpy,_pixmap,win,_gc,_copy_pixmap_x,_copy_pixmap_y,_copy_pixmap_w,_copy_pixmap_h,
                _back_x + _copy_pixmap_x,_back_y + _copy_pixmap_y);
  }
#endif
  return WS_NO_ERR;
}
long WSDbtronNwDev::getContextResource(){
  WSDdev* parent = getParentDev();
  if (parent == NULL){
    return -1;
  }
dbprintf("WSDbtronNwDev::getContextResource() use_pix=%d parent=0x%x\n",getUsePixmap(),parent);
//  if (getUsePixmap() != False){
//    long ret = parent->getContextResource();
//    if (ret != -1){
//      return getMemGid();
//    }else{
//      return -1;
//    }
//  }
dbprintf("WSDbtronNwDev::getContextResource() gid=0x%x\n",parent->getContextResource());
  return parent->getContextResource(); 
}
long WSDbtronNwDev::setValue(long kind,void* data){
  if (kind == WSDEV_USE_PIXMAP){
    return WS_NO_ERR;
  }
  return WSDnwDev::setValue(kind,data);
}
