home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TMagicMacros Delphi / TMM2_D4T / SIMPLEED.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-15  |  13.6 KB  |  387 lines

  1. unit SimpleEdit_f;
  2.   /////////////////////////////////////////////////////////
  3.   //                                                     //
  4.   //           TMagicMacros-Component v2.0               //
  5.   //                                                     //
  6.   // (C) Niels Vanspauwen, 15/10/98                      //
  7.   //     E-mail: Niels.Vanspauwen@Kagi.com               //
  8.   //     Homepage: http://magicmacros.8m.com             //
  9.   //                                                     //
  10.   /////////////////////////////////////////////////////////
  11.   {This is a demo-project that comes with TMagicMacros 2.0.
  12.  
  13.    It's a VERY simple texteditor (appropriatly called SimpleEdit),
  14.    which supports VisualHelp to explain some of it's features.
  15.    Just open the helpfile, select a topic and press the "Show Me" button
  16.    to experience a whole other approach to help!
  17.  
  18.    If you want, you can also call VisualHelp directly (in this app,
  19.    from a menu under the "How can I..." section in the Help menu).
  20.  
  21.    The last example of how you can use TMagicMacros is to make a
  22.    demo of your application. I did this for SimpleEdit: just click on
  23.    "Tourguide" in the Help menu. (Of course, you can also make it so
  24.    that a macro is started automatically when you start your application.
  25.    This is great for mailings: busy managers don't have the time to experiment
  26.    with every demoprogram they receive in the mail, nor are they likely to
  27.    read every advertisment letter you send them, but they always have time
  28.    to watch a 5 minute show, that clearly illustrates the power of your app!)
  29.  
  30.    Following methods, properties and events are clearly illustrated by this
  31.    demo-project: Play, PlayVH, Pause, Resume, ShowInfoBox, WaitForInfoBox,
  32.    BeforePlay, OnPlayEnded, OnPlayCancelled, BeforeVisualHelp, OnVisualHelp,
  33.    OnVisualHelpEnded, OnVisualHelpCancelled, OnProgress.
  34.   }
  35.  
  36. interface
  37.  
  38. uses
  39.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  40.   Buttons, ExtCtrls, StdCtrls, ComCtrls, Menus;
  41.  
  42. type
  43.   TSimpleEditForm = class(TForm)
  44.     RichEdit1: TRichEdit;
  45.     Panel1: TPanel;
  46.     btnItalic: TSpeedButton;
  47.     btnBold: TSpeedButton;
  48.     btnFont: TSpeedButton;
  49.     MagicMacros1: TMagicMacros;
  50.     FontDialog1: TFontDialog;
  51.     MainMenu1: TMainMenu;
  52.     File1: TMenuItem;
  53.     Exit1: TMenuItem;
  54.     Options1: TMenuItem;
  55.     Italic1: TMenuItem;
  56.     Bold1: TMenuItem;
  57.     N1: TMenuItem;
  58.     Font1: TMenuItem;
  59.     Help1: TMenuItem;
  60.     Index1: TMenuItem;
  61.     StatusBar1: TStatusBar;
  62.     N2: TMenuItem;
  63.     HowcanI1: TMenuItem;
  64.     Maketextbold1: TMenuItem;
  65.     Maketextitalic1: TMenuItem;
  66.     Changethefont1: TMenuItem;
  67.     Tourguide1: TMenuItem;
  68.     procedure btnFontClick(Sender: TObject);
  69.     procedure btnItalicClick(Sender: TObject);
  70.     procedure btnBoldClick(Sender: TObject);
  71.     procedure Exit1Click(Sender: TObject);
  72.     procedure Index1Click(Sender: TObject);
  73.     procedure MagicMacros1Progress(PercentDone: Integer);
  74.     procedure Maketextbold1Click(Sender: TObject);
  75.     procedure Maketextitalic1Click(Sender: TObject);
  76.     procedure Changethefont1Click(Sender: TObject);
  77.     procedure Panel1DblClick(Sender: TObject);
  78.     procedure Tourguide1Click(Sender: TObject);
  79.     procedure MagicMacros1PlayCancelled(Sender: TObject);
  80.     procedure MagicMacros1PlayEnded(Sender: TObject);
  81.     procedure MagicMacros1VisualHelp(Sender: TObject; ID: Integer);
  82.     procedure MagicMacros1VisualHelpCancelled(Sender: TObject;
  83.       ID: Integer);
  84.     procedure MagicMacros1VisualHelpEnded(Sender: TObject; ID: Integer);
  85.     procedure MagicMacros1BeforePlay(Sender: TObject);
  86.     procedure MagicMacros1BeforeVisualHelp(Sender: TObject; ID: Integer);
  87.   private
  88.     BackupEdit: TRichEdit;
  89.     { Private declarations }
  90.   public
  91.     { Public declarations }
  92.   end;
  93.  
  94. var
  95.   SimpleEditForm: TSimpleEditForm;
  96.  
  97. implementation
  98.  
  99. {$R *.DFM}
  100.  
  101. {-------------------------------------------------------------------------------
  102.  Following eventhandlers interact with the TMagicMacros component to provide
  103.  VisualHelp directly from the "How can I..." menu-item.
  104.  -------------------------------------------------------------------------------}
  105. const
  106.   {ID's of VisualHelp macro's}
  107.   BOLD_TEXT_MACRO = -1001;
  108.   ITALIC_TEXT_MACRO = -1002;
  109.   CHANGE_FONT_MACRO = -1003;
  110.   TOURGUIDE = 'tour.mcr';
  111.  
  112. procedure TSimpleEditForm.MagicMacros1BeforeVisualHelp(Sender: TObject;
  113.   ID: Integer);
  114. begin
  115.   {Backup the contents of the RichEdit}
  116.   BackupEdit := TRichEdit.Create(Self);
  117.   BackupEdit.Parent := Self;
  118.   BackupEdit.Height := 0;
  119.   BackupEdit.Width := 0;
  120.   RichEdit1.SelectAll;
  121.   RichEdit1.CopyToClipboard;
  122.   BackupEdit.PasteFromClipboard;
  123.   RichEdit1.Clear;
  124.   {Clear out contents of RichEdit1, so we can safely run our show, without
  125.    wrecking the user's data}
  126.   RichEdit1.Clear;
  127. end;
  128.  
  129. procedure TSimpleEditForm.Maketextbold1Click(Sender: TObject);
  130. begin
  131.   with MagicMacros1 do begin
  132.     PlayVH(BOLD_TEXT_MACRO); //Play the VisualHelp file
  133.     Pause; //Pause playback immediatly to inform the user what we're about to do
  134.     ShowInfoBox('VisualHelp: How can I make text bold?',
  135.                 'We''re about to show you how to make text appear bold...'+#13+
  136.                 'Just sit back and enjoy the show!', 5000);
  137.     WaitForInfoBox; //Wait until the Infobox is closed before resuming
  138.     Resume; //Let the show begin
  139.   end;
  140. end;
  141.  
  142. procedure TSimpleEditForm.Maketextitalic1Click(Sender: TObject);
  143. begin
  144.   with MagicMacros1 do begin
  145.     PlayVH(ITALIC_TEXT_MACRO); //Play the VisualHelp file
  146.     Pause; //Pause playback immediatly to inform the user what we're about to do
  147.     ShowInfoBox('VisualHelp: How can I make text italic?',
  148.                 'We''re about to show you how to make text appear italic...'+#13+
  149.                 'Just sit back and enjoy the show!', 5000);
  150.     WaitForInfoBox; //Wait until the Infobox is closed before resuming
  151.     Resume; //Let the show begin
  152.   end;
  153. end;
  154.  
  155. procedure TSimpleEditForm.Changethefont1Click(Sender: TObject);
  156. begin
  157.   with MagicMacros1 do begin
  158.     PlayVH(CHANGE_FONT_MACRO); //Play the VisualHelp file
  159.     Pause; //Pause playback immediatly to inform the user what we're about to do
  160.     ShowInfoBox('VisualHelp: How can I change the font?',
  161.                 'We''re about to show you how you can change the fontsettings...'+#13+
  162.                 'Just sit back and enjoy the show!', 5000);
  163.     WaitForInfoBox; //Wait until the Infobox is closed before resuming
  164.     Resume; //Let the show begin
  165.   end;
  166. end;
  167.  
  168.  
  169. {-------------------------------------------------------------------------------
  170.  The code below was used to implement the Tourguide feature.
  171.  -------------------------------------------------------------------------------}
  172. procedure TSimpleEditForm.MagicMacros1BeforePlay(Sender: TObject);
  173. begin
  174.   {Backup the contents of the RichEdit}
  175.   BackupEdit := TRichEdit.Create(Self);
  176.   BackupEdit.Parent := Self;
  177.   BackupEdit.Height := 0;
  178.   BackupEdit.Width := 0;
  179.   RichEdit1.SelectAll;
  180.   RichEdit1.CopyToClipboard;
  181.   BackupEdit.PasteFromClipboard;
  182.   RichEdit1.Clear;
  183.   {Clear out contents of RichEdit1, so we can safely run our show, without
  184.    wrecking the user's data}
  185.   RichEdit1.Clear;
  186. end;
  187.  
  188. procedure TSimpleEditForm.Tourguide1Click(Sender: TObject);
  189. begin
  190.   with MagicMacros1 do begin
  191.     Macroname := TOURGUIDE;
  192.     Play; //Start the tourguide
  193.     Pause; //Pause immediatly to let the user know what we're about to do
  194.     ShowInfoBox('SimpleEdit: Tourguide',
  195.                 'Welcome to SimpleEdit, the easiest texteditor in the world!'+#13+
  196.                 'In this short tour, we''ll show the most important features'+#13+
  197.                 'of SimpleEdit, so just sit back, put your feet up and learn!',
  198.                 8000);
  199.     {Update the statusbar}
  200.     StatusBar1.SimpleText := 'Playing tourguide.';
  201.     StatusBar1.Repaint;
  202.     WaitForInfoBox; //Wait untill the infobox is closed before resuming
  203.     Resume; //Carry on
  204.   end;
  205. end;
  206.  
  207.  
  208. {-------------------------------------------------------------------------------
  209.  The eventhandlers below are used to update the statusbar and restore the
  210.  user's data.
  211.  -------------------------------------------------------------------------------}
  212. procedure TSimpleEditForm.MagicMacros1Progress(PercentDone: Integer);
  213. begin
  214.   if msPlaying in MagicMacros1.State then
  215.     StatusBar1.SimpleText := Format('Loading: %d%%',
  216.                                     [PercentDone]);
  217.   StatusBar1.Repaint;
  218. end;
  219.  
  220. procedure TSimpleEditForm.MagicMacros1VisualHelp(Sender: TObject;
  221.   ID: Integer);
  222. begin
  223.   {Update the statusbar}
  224.   StatusBar1.SimpleText := 'Playing VisualHelp.';
  225.   StatusBar1.Repaint;
  226. end;
  227.  
  228. procedure TSimpleEditForm.MagicMacros1VisualHelpCancelled(Sender: TObject;
  229.   ID: Integer);
  230. begin
  231.   {Update the statusbar}
  232.   StatusBar1.SimpleText := 'VisualHelp cancelled.';
  233.   StatusBar1.Repaint;
  234.   MessageDlg('There you go!'+#13+'Click OK to continue...', mtInformation, [mbOK], 0);
  235.   {Copy the user's data back}
  236.   BackupEdit.SelectAll;
  237.   BackupEdit.CopyToClipboard;
  238.   BackupEdit.Free;
  239.   RichEdit1.Clear;
  240.   RichEdit1.PasteFromClipboard;
  241.   StatusBar1.SimpleText := '';
  242.   StatusBar1.Repaint;
  243. end;
  244.  
  245. procedure TSimpleEditForm.MagicMacros1VisualHelpEnded(Sender: TObject;
  246.   ID: Integer);
  247. begin
  248.   {Update the statusbar}
  249.   StatusBar1.SimpleText := 'VisualHelp finished.';
  250.   StatusBar1.Repaint;
  251.   MessageDlg('There you go!'+#13+'Click OK to continue...', mtInformation, [mbOK], 0);
  252.   {Copy the user's data back}
  253.   BackupEdit.SelectAll;
  254.   BackupEdit.CopyToClipboard;
  255.   BackupEdit.Free;
  256.   RichEdit1.Clear;
  257.   RichEdit1.PasteFromClipboard;
  258.   StatusBar1.SimpleText := '';
  259.   StatusBar1.Repaint;
  260. end;
  261.  
  262. procedure TSimpleEditForm.MagicMacros1PlayCancelled(Sender: TObject);
  263. begin
  264.   {Update the statusbar}
  265.   StatusBar1.SimpleText := 'Tourguide cancelled.';
  266.   StatusBar1.Repaint;
  267.   MessageDlg('Thank you for watching the tourguide and enjoy SimpleEdit!'+#13+'Click OK to continue...', mtInformation, [mbOK], 0);
  268.   {Copy the user's data back}
  269.   BackupEdit.SelectAll;
  270.   BackupEdit.CopyToClipboard;
  271.   BackupEdit.Free;
  272.   RichEdit1.Clear;
  273.   RichEdit1.PasteFromClipboard;
  274.   StatusBar1.SimpleText := '';
  275.   StatusBar1.Repaint;
  276. end;
  277.  
  278. procedure TSimpleEditForm.MagicMacros1PlayEnded(Sender: TObject);
  279. begin
  280.   {Update the statusbar}
  281.   StatusBar1.SimpleText := 'Tourguide finished.';
  282.   StatusBar1.Repaint;
  283.   MessageDlg('Thank you for watching the tourguide and enjoy SimpleEdit!'+#13+'Click OK to continue...', mtInformation, [mbOK], 0);
  284.   {Copy the user's data back}
  285.   BackupEdit.SelectAll;
  286.   BackupEdit.CopyToClipboard;
  287.   BackupEdit.Free;
  288.   RichEdit1.Clear;
  289.   RichEdit1.PasteFromClipboard;
  290.   StatusBar1.SimpleText := '';
  291.   StatusBar1.Repaint;
  292. end;
  293.  
  294. procedure TSimpleEditForm.Panel1DblClick(Sender: TObject);
  295. begin
  296. { THIS IS THE CODE I USED TO RECORD THE MACRO's
  297. var
  298.   Name: string;
  299. begin
  300.   if not InputQuery('VisualHelp', 'Enter name', Name) then EXIT;
  301.   MagicMacros1.Macroname := Name;
  302.   MagicMacros1.StartRecording;
  303. }
  304. end;
  305.  
  306. {-------------------------------------------------------------------------------
  307.  The methods below contain the code that pops up the InfoBoxes while playing
  308.  a macro or VisualHelp.
  309.  -------------------------------------------------------------------------------}
  310. procedure TSimpleEditForm.btnFontClick(Sender: TObject);
  311. begin
  312.   with MagicMacros1 do begin
  313.     {All the methods below will return immediatly if we are not currently
  314.      playing a macro or VisualHelp}
  315.     Pause; //Pause playback
  316.     ShowInfoBox('Change font',
  317.                 'Clicking the speedbutton or the menu-item will bring up '+
  318.                 'a Font-dialog, which you can use to change the font of '+
  319.                 'the selected text.', 3000);
  320.     WaitForInfoBox; //Wait untill the box is closed
  321.     Resume; //Resume playback
  322.   end;
  323.   if not FontDialog1.Execute then EXIT;
  324.   with RichEdit1.SelAttributes do begin
  325.     Name := FontDialog1.Font.Name;
  326.     Color := FontDialog1.Font.Color;
  327.     Pitch := FontDialog1.Font.Pitch;
  328.     Size := FontDialog1.Font.Size;
  329.     Style := FontDialog1.Font.Style;
  330.     Height := FontDialog1.Font.Height;
  331.   end;
  332. end;
  333.  
  334. procedure TSimpleEditForm.btnItalicClick(Sender: TObject);
  335. begin
  336.   with MagicMacros1 do begin
  337.     {All the methods below will return immediatly if we are not currently
  338.      playing a macro or VisualHelp}
  339.     Pause; //Pause playback
  340.     ShowInfoBox('Italic',
  341.                 'Clicking the speedbutton or the menu-item will cause '+
  342.                 'the selected text to appear in italic.', 3000);
  343.     WaitForInfoBox; //Wait untill the box is closed
  344.     Resume; //Resume playback
  345.   end;
  346.   with RichEdit1.SelAttributes do
  347.     if (fsItalic in Style) then begin
  348.       Style := Style - [fsItalic];
  349.     end
  350.     else begin
  351.       Style := Style + [fsItalic];
  352.     end;
  353. end;
  354.  
  355. procedure TSimpleEditForm.btnBoldClick(Sender: TObject);
  356. begin
  357.   with MagicMacros1 do begin
  358.     {All the methods below will return immediatly if we are not currently
  359.      playing a macro or VisualHelp}
  360.     Pause; //Pause playback
  361.     ShowInfoBox('Bold',
  362.                 'Clicking the speedbutton or the menu-item will cause '+
  363.                 'the selected text to appear in bold.', 3000);
  364.     WaitForInfoBox; //Wait untill the box is closed
  365.     Resume; //Resume playback
  366.   end;
  367.   with RichEdit1.SelAttributes do
  368.     if (fsBold in Style) then begin
  369.       Style := Style - [fsBold];
  370.     end
  371.     else begin
  372.       Style := Style + [fsBold];
  373.     end;
  374. end;
  375.  
  376. procedure TSimpleEditForm.Exit1Click(Sender: TObject);
  377. begin
  378.   Close;
  379. end;
  380.  
  381. procedure TSimpleEditForm.Index1Click(Sender: TObject);
  382. begin
  383.   Application.HelpCommand(HELP_CONTENTS, 0);
  384. end;
  385.  
  386. end.
  387.