unit wButtonBase0;

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

  to facilitate to have different kind of Buttons in a window.
  Its the parent of
  TButtonBitMap, TButtonCheck, TButtonPush, TButtonRadio
  ----------------------------------------------------------------}

// 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 wControl0, wWindowBase0, wGlobals2, Windows;

type
  PButtonPush0 = ^TButtonBase0;
  TButtonBase0 = object(TControl)
  //"""""""""""""""""""""""""""""
//  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 AutoSize(nLeft,nTop:integer; var nWidth, nHeight:int32); virtual;
  // Computes the sizes automatically, if no sizes where specified.
  // Gives as infor the values of nLeft and nTop
  // Units are pixels

  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.

  procedure CtrlNotifMessageHandle(wNotif:UINT; lPar:LPARAM); virtual;
  // Handle notification messages, see wControl0.pp for doc.

  function CtrlWindowProc(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
  // Handles the window messages
  // It is usually overriden
  // A SendMessage(hWin, ...) must never be called from here, since
  // it would be a calling loop
  end; // TButtonBase0 = object(TControl)
  TButtonBase =  TButtonBase0;
  PButtonBase = ^TButtonBase;

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

Uses wErrors2, wDebug0;

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

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

wWindowType:=GGLCTE_CTRL_ButtonBase;

strClassName  :='BUTTON';
strCtrlLabel  :='Button 1'; // MUST be redefined in the main application
// strCtrlHint   :='';      // can be redefined in the main application

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

destructor  TButtonBase0.Done;
//============================
begin
inherited Done;
end;  // TButtonBase0.Done

procedure TButtonBase0.AutoSize(nLeft,nTop:integer; var nWidth,nHeight:int32);
//============================================================================
// Computes the sizes automatically, if no sizes where specified.
// Gives as infor the values of nLeft and nTop
// Units are pixels
var
  tm          : TEXTMETRIC;  // for the text width and height
  hDCPar      : HDC;
//  rRec        : RECT;  // parent window size
  rSize       : SIZE;

begin
nLeft:=nLeft; nTop:=nTop; // to avoid hints

// gets the text height and witdth of characters
// Will be used to define default sizes.
hDCPar:=GetDC(hParent);
GetTextMetrics(hDCPar, @tm);

// get the exact size of the text on the button.
GetTextExtentPoint32( hDCPar, @strCtrlLabel[1], length(strCtrlLabel),
                      @rSize);

ReleaseDC(hParent, hDCPar);

// Exact character width.
if (nWidth < rSize.cx) then nWidth:=rSize.cx;

// Usual Height is 2 character high
if (nHeight < tm.tmHeight) then nHeight:=2*tm.tmHeight;
end;  // TButtonBase0.AutoSize

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

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

procedure TButtonBase0.CtrlNotifMessageHandle(wNotif:UINT; lPar:LPARAM);
//======================================================================
// Handles the notification messages that the control sended to
// its parent.
begin
// If the button is in a dialog which is in a window,
// Notify the window that the button has been clicked.
if (wNotif = BN_CLICKED) then begin
   if      (poGrParent <> NIL) then poGrParent^.ButtonClicked(@Self, wCtrlID)
   else if (poParent   <> NIL) then poParent^.ButtonClicked(@Self, wCtrlID);
   end;

inherited CtrlNotifMessageHandle(wNotif, lPar);
end;  // TButtonBase0.CtrlNotifMessageHandle

function TButtonBase0.CtrlWindowProc(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT;
//==============================================================================================
// Handles the notification messages that the control sended to
// its parent.
begin
CtrlWindowProc:=inherited CtrlWindowProc(hWindow, wMsg, wPar, lPar);
end;  // TButtonBase0.CtrlWindowProc

End.  // unit wButtonBase0;

