unit wButtonPush0;

{ Feel free to use this unit as you want.
  First developped by Bernard Gisin

  to facilitate to have a Push Button in a window
  ----------------------------------------------------------------}

// To tell FreePascal that the application is a Graphical User Interface
// i.e. is running in windows, not in text mode.
{$APPTYPE GUI}

INTERFACE
//"""""""

Uses wButtonBase0, wControl0, wWindowBase0, wGlobals2, Windows;

type
  PButtonPush0 = ^TButtonPush0;
  TButtonPush0 = object(TButtonBase)
  //""""""""""""""""""""""""""""""""
//  hWin     : HWND;  // Handle on the edit box, got from TWindowBase
//  hParent : HWND;  // Handle on the parent, got from TWindowBase
//  poParent: PWindowBase0;  // Pointer on the parent, got from TWindowBase

  constructor Init(poParentW:PWindowBase0);
//  destructor  Done; virtual;

  procedure CtrlCreate; virtual;
  // Creates the push button control window
  // Must have a parent object window, if not, nothing happens.
  // It can be easily changed to another kind of button.

  function CtrlWMCTLCOLOR(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
  // Special case where controls colors can be defines by their parent.
  // The controls can define the colors directly through this call.
  // lPar = handle on the control who wants to be redrawn
  // wPar = Device context used to draw the control.
  // It can be redefined by a descentent. In this case, the
  // inherited method is not called.
  end; // TButtonPush0 = object(TControl)
  TButtonPush =  TButtonPush0;
  PButtonPush = ^TButtonPush;

IMPLEMENTATION
//""""""""""""

Uses wErrors2, wDebug0;

var nNotUsed : integer; // only to avoid an error message while compliling in DevPascal 1.9

constructor TButtonPush0.Init(poParentW:PWindowBase0);
//====================================================
begin
inherited Init(poParentW);

wWindowType:=GGLCTE_CTRL_ButtonPush;

//dwExStyle :=0;  // no extra styles by default, modified in the descendent
dwStyle   :=  WS_VISIBLE or WS_CHILD or WS_TABSTOP or BS_PUSHBUTTON;
end;  // TButtonPush0.Init

procedure TButtonPush0.CtrlCreate;
//================================
Begin
inherited CtrlCreate;

If (hWin = 0) Then Begin
   // Error has already been reported in CtrlCreate.
   ErrorWriteMSG(2001211001);
   exit;  // can't do anything more
   end;
End;  // TButtonPush0.CtrlCreate

function TButtonPush0.CtrlWMCTLCOLOR(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT;
//==============================================================================================
// Special case where controls colors can be defines by their parent.
// The controls can define the colors directly through this call.
// lPar = handle on the control who wants to be redrawn
// wPar = Device context used to draw the control.
// hWindow = parent window.
//var   hDevC       : HDC;

begin
hWindow:=hWindow; wMsg:=wMsg; wPar:=wPar; lPar:=lPar; // to remove hints
CtrlWMCTLCOLOR := 0; // ask to use default message handler
exit;

{
// For Push Button, this doesn't work...
//"""""""""""""""""""""""""""""""""""""""
   hDevC:=HDC(wPar);
   SetTextColor(hDevC, RGB(255,0,0));
   SetBkColor(hDevC,  RGB(0,255,0));
   SetBkMode(hDevC, OPAQUE);  // OPAQUE or TRANSPARENT
      CtrlWMCTLCOLOR:=GetStockObject(NULL_BRUSH);

if fUseDefaultColors then begin
   // default handling.
   CtrlWMCTLCOLOR := 0;
   exit;
   end;

if (wMsg = WM_CTLCOLORSTATIC) then begin
   // the button is beeing redrawn.
   // The device context has been coded in wPar, standard Windows
   hDevC:=HDC(wPar);

   // You can change the text color and the back colors
   SetTextColor(hDevC, colTextFont);
   SetBkColor(hDevC,  colTextBack);
   SetBkMode(hDevC, colTextTransp);  // OPAQUE or TRANSPARENT

   if (colWindowBack = word32(-1)) then begin
      CtrlWMCTLCOLOR:=GetStockObject(NULL_BRUSH);
      exit;
      end;

   if (hBrushCtrl = 0) or (colWindowBack <> colBrushCtrl) then begin
      if (hBrushCtrl <> 0) then DeleteObject(hBrushCtrl); // must free the resource memory

      // create a new brush for the control background color.
      // Don't forget to delete this Brush in the destructor.
      hBrushCtrl:=CreateSolidBrush(colWindowBack);

      colBrushCtrl:=colWindowBack; // remember the brush color
      end;

   // A brush must be send back to fill the rest of the static window.
   CtrlWMCTLCOLOR:=hBrushCtrl;
   end;
}
end;  // TButtonPush0.CtrlWMCTLCOLOR

End.  // unit wButtonPush0;

