home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PCTV3N3.ZIP / MDITOOL.ZIP / MDITOOL.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-05-26  |  5.2 KB  |  196 lines

  1. { mditool.pas --Demonstrate TPW 1.5 Toolbar unit. By Tom Swan }
  2.  
  3. {$R mditool.res}
  4.  
  5. program MDITool;
  6.  
  7. uses WinTypes, WinProcs, WObjects, Toolbar, IDs;
  8.  
  9. const
  10.   NumWindows: Word = 0;  { Number of MDI windows open }
  11.  
  12. type
  13.   TMDIToolApp = object(TApplication)
  14.     procedure InitInstance; virtual;
  15.     procedure InitMainWindow; virtual;
  16.   end;
  17.  
  18.   PMDIToolFrame = ^TMDIToolFrame;
  19.   TMDIToolFrame = object(TMDIWindow)
  20.     Toolbar: PToolbar;
  21.     constructor Init(ATitle: PChar; AMenu: HMenu);
  22.     procedure SetupWindow; virtual;
  23.     procedure GetWindowClass(var AWndClass: TWndClass); virtual;
  24.     function GetClassName: PChar; virtual;
  25.     procedure EnableCommand(Cmd: Word; EnableCmd: Boolean);
  26.     function InitChild: PWindowsObject; virtual;
  27.     procedure CMFileExit(var Msg: TMessage);
  28.       virtual cm_First + cm_FileExit;
  29.     procedure CMHelpAbout(var Msg: TMessage);
  30.       virtual cm_First + cm_HelpAbout;
  31.     procedure WMSize(var Msg: TMessage);
  32.       virtual wm_First + wm_Size;
  33.   end;
  34.  
  35.   PMDIToolWin = ^TMDIToolWin;
  36.   TMDIToolWin = object(TWindow)
  37.     constructor Init(AParent: PWindowsObject; ATitle: PChar);
  38.     destructor Done; virtual;
  39.   end;
  40.  
  41. {- Enable or disable the Window menu's commands }
  42. procedure EnableWindowMenu(Enable: Boolean);
  43. var
  44.   pw: PWindowsObject;
  45. begin
  46.   pw := Application^.MainWindow;
  47.   if pw^.HWindow <> 0 then with PMDIToolFrame(pw)^ do
  48.   begin
  49.     EnableCommand(cm_CascadeChildren, Enable);
  50.     EnableCommand(cm_TileChildren, Enable);
  51.     EnableCommand(cm_ArrangeIcons, Enable);
  52.     EnableCommand(cm_CloseChildren, Enable);
  53.   end;
  54. end;
  55.  
  56. {- Initialize this instance of the application }
  57. procedure TMDIToolApp.InitInstance;
  58. begin
  59.   TApplication.InitInstance;
  60.   HAccTable := LoadAccelerators(HInstance, PChar(id_Acc));
  61. end;
  62.  
  63. {- Initialize TMDIToolApp object's window }
  64. procedure TMDIToolApp.InitMainWindow;
  65. begin
  66.   MainWindow := New(PMDIToolFrame, Init('MDI Toolbar Demonstration',
  67.     LoadMenu(HInstance, PChar(id_Menu))));
  68. end;
  69.  
  70. {- Construct TMDIDemoWin desktop object }
  71. constructor TMDIToolFrame.Init(ATitle: PChar; AMenu: HMenu);
  72. begin
  73.   TMDIWindow.Init(ATitle, AMenu);
  74.   with Attr do
  75.   begin
  76.     X := GetSystemMetrics(sm_CXScreen) div 8;
  77.     Y := GetSystemMetrics(sm_CYScreen) div 8;
  78.     H := Y * 6;
  79.     W := X * 6;
  80.   end;
  81.   ChildMenuPos := 1;   { Location of Window menu }
  82.   Toolbar := New(PToolbar, Init(@Self, 'ids_Toolbar'));
  83. end;
  84.  
  85. {- Perform window initializations that require HWindow }
  86. procedure TMDIToolFrame.SetupWindow;
  87. begin
  88.   TMDIWindow.SetupWindow;
  89.   EnableWindowMenu(false);
  90. end;
  91.  
  92. {- Modify window class to attach a desktop icon }
  93. procedure TMDIToolFrame.GetWindowClass(var AWndClass: TWndClass);
  94. begin
  95.   TMDIWindow.GetWindowClass(AWndClass);
  96.   AWndClass.HIcon := LoadIcon(HInstance, PChar(id_Icon));
  97. end;
  98.  
  99. {- Return new class name }
  100. function TMDIToolFrame.GetClassName: PChar;
  101. begin
  102.   GetClassName := 'TMDIToolFrame';
  103. end;
  104.  
  105. {- Enable or disable a menu and toolbar command icon }
  106. procedure TMDIToolFrame.EnableCommand(Cmd: Word; EnableCmd: Boolean);
  107. var
  108.   CmdFlags: Word;
  109. begin
  110.   if EnableCmd then
  111.     CmdFlags := mf_ByCommand or mf_Enabled
  112.   else
  113.     CmdFlags := mf_ByCommand or mf_Disabled or mf_Grayed;
  114.   EnableMenuItem(Attr.Menu, Cmd, CmdFlags);
  115.   if Toolbar <> nil
  116.     then Toolbar^.EnableTool(Cmd, EnableCmd);
  117. end;
  118.  
  119. {- Create child MDI window. }
  120. function TMDIToolFrame.InitChild: PWindowsObject;
  121. begin
  122.   InitChild := New(PMDIToolWin, Init(@Self, '[untitled]'));
  123. end;
  124.  
  125. {- Execute File:Exit command }
  126. procedure TMDIToolFrame.CMFileExit(var Msg: TMessage);
  127. begin
  128.   if MessageBox(HWindow, 'End program now?', 'MDITool',
  129.     mb_IconQuestion or mb_YesNo) = idYes then
  130.     CloseWindow;
  131. end;
  132.  
  133. {- Execute Help:About command }
  134. procedure TMDIToolFrame.CMHelpAbout(var Msg: TMessage);
  135. var
  136.   Dialog: TDialog;
  137. begin
  138.   Dialog.Init(@Self, PChar(id_About));
  139.   Dialog.Execute;
  140.   Dialog.Done;
  141. end;
  142.  
  143. {- Handle window size (wm_Size) message }
  144. procedure TMDIToolFrame.WMSize(var Msg: TMessage);
  145. var
  146.   R: TRect;
  147.   {- Tell children not to "play" outside of the frame's yard! }
  148.   procedure NotifyChild(P: PWindow); far;
  149.   begin
  150.     if P^.HWindow <> 0 then
  151.       SendMessage(P^.HWindow, am_SetParentClientRect,
  152.         Msg.WParam, Longint(@R));
  153.   end;
  154. begin
  155.   if (Scroller <> nil) and (Msg.WParam <> sizeIconic) then
  156.     Scroller^.SetPageSize;
  157.   if Msg.wParam = sizeNormal then
  158.   begin
  159.     GetWindowRect(HWindow, R);
  160.     Attr.H := R.bottom - R.top;
  161.     Attr.W := R.right - R.left;
  162.   end;
  163.   GetClientRect(HWindow, R);
  164.   ForEach(@NotifyChild);
  165.   with R do
  166.     SetWindowPos(ClientWnd^.HWindow, 0,
  167.       Left, Top, Right - Left, Bottom - Top, swp_NoZOrder);
  168. end;
  169.  
  170. {- Construct new MDI child window }
  171. constructor TMDIToolWin.Init(AParent: PWindowsObject; ATitle: PChar);
  172. begin
  173.   TWindow.Init(AParent, ATitle);
  174.   if NumWindows = 0 then
  175.     EnableWindowMenu(true);
  176.   inc(NumWindows);
  177. end;
  178.  
  179. {- Destroy MDI child window }
  180. destructor TMDIToolWin.Done;
  181. begin
  182.   if NumWindows > 0 then
  183.     dec(NumWindows);
  184.   if NumWindows = 0 then
  185.     EnableWindowMenu(false);
  186.   TWindow.Done;
  187. end;
  188.  
  189. var
  190.   MDIToolApp: TMDIToolApp;
  191. begin
  192.   MDIToolApp.Init('MDITool');
  193.   MDIToolApp.Run;
  194.   MDIToolApp.Done;
  195. end.
  196.