1 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r 
   2   For conditions of distribution and use, see copyright notice in zlib_license.txt
\r 
   3   which is included in the package
\r 
   4   ----------------------------------------------------------------------------- }
\r 
   6 unit unitwindowobject;
\r 
  13     windows,messages,wmessages,
\r 
  17     {$define windows := lmessages}
\r 
  23   twindowobjectbase=class(tobject)
\r 
  25     onmsg:function(msg,wparam,lparam:taddrint):boolean of object;
\r 
  26     exitloopflag:boolean;
\r 
  27     exstyle,style:integer;
\r 
  28     docreatewindow:boolean;
\r 
  29     function windowprocaddr:pointer; virtual;
\r 
  30     procedure init_window(dwexstyle,dwstyle:cardinal);
\r 
  31     procedure init; virtual;
\r 
  32     procedure initinvisible;
\r 
  33     function settimer(id,timeout:taddrint):integer;
\r 
  34     function killtimer(id:taddrint):boolean;
\r 
  35     procedure postmessage(msg,wparam,lparam:taddrint);
\r 
  36     procedure messageloop;
\r 
  38       procedure processmessages; virtual;
\r 
  39       function processmessage:boolean;
\r 
  41     constructor create; virtual;
\r 
  42     destructor destroy; override;
\r 
  45   {this type exists for compatibility with the original one in bewarehttpd,
\r 
  46   therefore it inits on create}
\r 
  47   twindowobject=class(twindowobjectbase)
\r 
  48     constructor create; override;
\r 
  51 function WindowProc_windowobjectbase(ahWnd:HWND; auMsg:Integer; awParam:WPARAM; alParam:LPARAM):Integer; stdcall;
\r 
  54   twindowobject_Class : TWndClass = (style:0; lpfnWndProc:nil;
\r 
  55   cbClsExtra:0; cbWndExtra:sizeof(pointer); hInstance:thinstance(0); hIcon:hicon(0); hCursor:hcursor(0);
\r 
  56   hbrBackground:hbrush(0);lpszMenuName:nil; lpszClassName:'twindowobject_class');
\r 
  63 {------------------------------------------------------------------------------}
\r 
  65 function WindowProc_windowobjectbase(ahWnd:HWND; auMsg:Integer; awParam:WPARAM; alParam:LPARAM):Integer; stdcall;
\r 
  69   ////swriteln('in unitwindowobject.windowproc');
\r 
  70   Result := 0;  // This means we handled the message
\r 
  71   if ahwnd <> hwnd(0) then i := getwindowlongptr(ahwnd,0) else i := 0;
\r 
  72   if i <> 0 then begin
\r 
  73     if assigned(twindowobjectbase(i).onmsg) then begin
\r 
  74       if not twindowobjectbase(i).onmsg(aumsg,awparam,alparam) then i := 0;
\r 
  77   if i = 0 then Result := DefWindowProc(ahWnd, auMsg, awParam, alParam)
\r 
  81 function twindowobjectbase.windowprocaddr;
\r 
  83   result := @WindowProc_windowobjectbase;
\r 
  86 procedure twindowobjectbase.initinvisible;
\r 
  88   init_window(WS_EX_TOOLWINDOW,WS_POPUP);
\r 
  91 procedure twindowobjectbase.init;
\r 
  96 function twindowobjectbase.settimer;
\r 
  98   result := windows.settimer(hwndmain,id,timeout,nil);
\r 
 101 function twindowobjectbase.killtimer;
\r 
 103   result := windows.killtimer(hwndmain,id);
\r 
 108 procedure twindowobjectbase.init_window;
\r 
 110     //swriteln('in twindowobject.create, about to call registerclass');
\r 
 111   twindowobject_Class.lpfnWndProc := windowprocaddr;
\r 
 112   Windows.RegisterClass(twindowobject_Class);
\r 
 113   //swriteln('about to call createwindowex');
\r 
 116   exstyle := dwexstyle;
\r 
 117   hWndMain := CreateWindowEx(dwexstyle, twindowobject_Class.lpszClassName,
\r 
 118     '', dwstyle, CW_USEDEFAULT, CW_USEDEFAULT,100, 100, hwnd(0), 0, HInstance, nil);
\r 
 119   //swriteln('about to check result of createwindowex');
\r 
 120   if hWndMain = hwnd(0) then raise exception.create('CreateWindowEx failed');
\r 
 121   //swriteln('about to store reference to self in extra windo memory');
\r 
 122   setwindowlongptr(hwndmain,0,taddrint(self));
\r 
 123   //swriteln('finished twindowobject.create , hwndmain='+inttohex(taddrint(hwndmain),16));
\r 
 127 constructor twindowobjectbase.create;
\r 
 133 destructor twindowobjectbase.destroy;
\r 
 135   if hWndMain <> hwnd(0) then DestroyWindow(hwndmain);
\r 
 139 procedure twindowobjectbase.postmessage;
\r 
 141   windows.postmessage(hwndmain,msg,wparam,lparam);
\r 
 145 function twindowobjectbase.ProcessMessage : Boolean;
\r 
 150     if PeekMessage(MsgRec, 0, 0, 0, PM_REMOVE) then begin
\r 
 152       TranslateMessage(MsgRec);
\r 
 153       DispatchMessage(MsgRec);
\r 
 157 procedure twindowobjectbase.processmessages;
\r 
 159   while processmessage do;
\r 
 163 procedure twindowobjectbase.messageloop;
\r 
 167   while GetMessage(MsgRec, hwnd(0), 0, 0) do begin
\r 
 168     {$ifdef mswindowss}
\r 
 169     TranslateMessage(MsgRec);
\r 
 171     DispatchMessage(MsgRec);
\r 
 172     if exitloopflag then exit;
\r 
 173     {if not peekmessage(msgrec,0,0,0,PM_NOREMOVE) then onidle}
\r 
 178 {------------------------------------------------------------------------------}
\r 
 180 constructor twindowobject.create;
\r 
 186 {------------------------------------------------------------------------------}
\r