{ Test of creating a toolBar.
  Birth of a much more general toolbar than the one of tested in tEx048.pp

  In the  wToolBar0.pp  unit, is defined the TToolBar window which
  allow to put any control in the toolbar.

  A New control has been defined. It is a push button, with a bitmap
  on it. TButtonBitMap

  in  tEx052.pp  a much better example is demonstrated
  --------------------------------------------}
program tEx049;

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

Uses
  wApplication0
 ,wWindow0
 ,wDialogCreator2
 ,wToolBar0
 ,wButtonBitMap0
 ,wVariableNumLink2
 ,wWindowBase0
 ,wObjectCreatorLOW2 // for gglpoObjCreate
 ,wErrors0
 ,wGlobals2
 ,wDebug0
 ,Windows;

type
  PMyWindow = ^TMyWindow;
  TMyWindow = object(TWindow)
  //"""""""""""""""""""""""""
  strComboDemo : ShortString;
  // Just to see how to put a combo box in the toolbar.

  constructor Init(strWinTitlePrm:ShortString);
//  destructor Done; virtual;

  procedure WinCreate(strWinClass:ShortString); virtual;
  // When creating the window, we ask to show the dialog box, not the window

  procedure Test1;
  // create a window (the same way a dialog box is bulid),
  // then use it as a toolBar

  procedure ButtonClicked(poCtrl:PWindowBase0; wCtrlID:Word32); virtual;
  // When a button is clicked, and it is in a dialog which belongs to a Window,
  // then the button calls this method, with a pointer on itself
  // and the ID it has been given.

  function WMCommand(wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
  // Handles the menu messages

//  function WindowProc(hWindow:HWND; wMsg:UINT; wPar:WPARAM; lPar:LPARAM):LRESULT; virtual;
    // Not Used
  end; // TMyWindow = object(TWindow)

// var glnNotUsed : integer; // To avoid an error in the DevPascal 1.9 environment
var
  oApp        : TApplication;
  poMyMainWin : PMyWindow;

//******************************************************************
// TMyWindow = object(TWindow)
//******************************************************************

constructor TMyWindow.Init(strWinTitlePrm:ShortString);
//=====================================================
begin
inherited Init(strWinTitlePrm);

// Just to demonstrate a combo box in the toolbar
StrField(101, strComboDemo, SizeOf(strComboDemo));
strComboDemo:='First line';

// This line is all what is needed to have a status line.
new(poStatusLine, Init(@Self));
end;  // TMyWindow.Init

procedure TMyWindow.WinCreate(strWinClass:ShortString);
//=====================================================
Begin
inherited WinCreate(strWinClass);

// Here calling :  DialogExecMSG(11199, @Self);  doesn't work,
// But posting a message to open the dialog box at startup is possible.
// Opens also the help window, indicating the key action.
//PostMessage(hWin, WM_COMMAND, 101, 0);
end;  // TMyWindow.WinCreate

procedure TMyWindow.Test1;
//========================
var
  poDialCr    : PDialogCreator2;
  poTBar      : PToolBar;
  poBtnBM     : PButtonBitMap;

  rRec        : RECT;
  hBMapOld    : HBITMAP; // to define a new Hatch
  hDCWin,     // Device context of the window
  hDCMem      : HDC; // to draw the new pattern in a memory device context
  hBRRed
              : HBRUSH; // which brush style to use

begin
// Creates the dialog creator, and initialises it.
poDialCr:=gglpoObjCreate^.CreateDialogCreator;

// Creates the dialog, and return a pointer on it.
poTBar:=PToolBar(poDialCr^.ToolBarCreation(11199, @self));

// We have the dialog, the dialog creator can be disposed.
dispose(poDialCr, Done);

// Default style of the dialog box
poTBar^.dwStyle   :=WS_CHILD or WS_BORDER;
poTBar^.dwExStyle :=0;
poTBar^.nXPosPix  :=0;
poTBar^.nYPosPix  :=0; // nXSizePix, nYSizePix;

///////////////////////////////////////////
// ADD a bitmap button
////////////////////////////////////////////
new(poBtnBM, Init(poTBar));
poBtnBM^.poGrParent:=@Self;

// Drawing a bitmap for the button
//""""""""""""""""""""""""""""""""
// create first a bitmap, compatible with the used device context
// of a given size. It will be created in memory.
hDCWin:=GetDC(hWin);
poBtnBM^.hBitMap:=CreateCompatibleBitmap(hDCWin, 24,24); // 24,24 = width, height

// We don't need the window DC
ReleaseDC(hWin, hDCWin);

// you must create a memory device context, do draw in the bitmap
hDCMem:=CreateCompatibleDC(hDCWin);

// to draw on the bit map, select it in the memory device context.
// remember the original bitmap used.
hBMapOld:=SelectObject(hDCMem, poBtnBM^.hBitMap);

// To fill with red, create a red brush.
hBRRed:=CreateSolidBrush(RGB(255, 0, 0));

// fill the bitmap with the red color
rRec.Left   := 0;
rRec.Top    := 0;
rRec.Right  := 24;
rRec.Bottom := 24;
FillRect(hDCMem, rRec, hBRRed);

// The created brush must be deleted.
DeleteObject(hBRRed);

// put a black text over the red color
SetTextColor(hDCMem, RGB(0,0,0)); // Not necessary, it is already black
//SetBkColor(hDCMem, RGB(255,0,0)); // one possibility
SetBkMode(hDCMem, TRANSPARENT);    // another possibility
TextOut(hDCMem, 8, 4, 'A', 1); // 1 = length of the string

// You must delete the memory device when not used any more.
// But before, select the original bitmap associated with it at creation.
SelectObject(hDCMem, hBMapOld);
DeleteDC(hDCMem);

// The button bit map will be deleted when the bit map will no more
// be necessary.

// The BitMap object button has been added to the toolBar, thus
// its window will be created automatically.

// Remove the first button to leave place, for the tests.
poTBar^.RemoveChildObjectWindow(poTBar^.GetControlFromID(201));

// Set the ID of the bitmap button and position
poBtnBM^.vXSizeCh:=7;
poBtnBM^.wCtrlID:=211;

poTBar^.ToolBarCreate;
end;  // TMyWindow.Test1

procedure TMyWindow.ButtonClicked(poCtrl:PWindowBase0; wCtrlID:Word32);
//=====================================================================
begin
poCtrl:=poCtrl; // to avoid a hint;

DBW(1, 'Button clicked=' + ITS(wCtrlID));
end;  // TMyWindow.ButtonClicked

function TMyWindow.WMCommand(wPar:WPARAM; lPar:LPARAM):LRESULT;
//=============================================================
// There are no menus treatment, only one dialog box
var
  wNrMenu     : word32;

begin
if (lPar <> 0) then begin
   // call the parent WMCommand method.
   WMCommand:=inherited WMCommand(wPar, lPar);
   exit;
   end;

// for the menus,  lPar = 0
//=========================
wNrMenu   := LoWord(wPar);
Case wNrMenu Of
   101 : Test1;

  201..499 : DBW(1, 'From toolBar=' + ITS5(wPar));

   else WMCommand:=inherited WMCommand(wPar, lPar);
   end;  // Case NrMenu Of
end;  // TMyWindow.WMCommand

//**********************************************************************
Begin // MAIN ******
// Name under which the main window is registered. Spaces are not allowed
oApp.Init('Example049');

// Creation of the main window
new(poMyMainWin, Init('tEx049.  Test to create a ToolBar'));

// The App.WindowClass can be modified here, if wanted. (Not usual)
oApp.Run;
oApp.Done;  // The main window object is disposed here.
End.

