home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 November / Chip_2003-11_cd1.bin / program / delphi / kompon / DIHtmlLabel.exe / Demos / DIHtmlLabel / Main.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2003-09-01  |  7.1 KB  |  268 lines

  1. { DIHtmlLabel example. }
  2.  
  3. unit Main;
  4.  
  5. {$I DI.inc}
  6.  
  7. {.$DEFINE Debug}// Default: Off
  8.  
  9. interface
  10.  
  11. uses
  12.   Classes, Forms, StdCtrls, ExtCtrls, Controls,
  13.  
  14.   CategCh,
  15.  
  16.   DIHtmlLabel;
  17.  
  18. type
  19.   TfrmMain = class(TForm)
  20.     DIHtmlLabel: TDIHtmlLabel;
  21.     edtDynamic: TEdit;
  22.     pnlTop: TPanel;
  23.     lblClose: TLabel;
  24.     lblWelcome: TLabel;
  25.     lblHtmlLabel: TLabel;
  26.     pnlBlackLine: TPanel;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure FormKeyPress(Sender: TObject; var Key: Char);
  29.     procedure lblCloseClick(Sender: TObject);
  30.     procedure DIHtmlLabelLinkClick(const Sender: TObject; const LinkHref: WideString; const LinkText: WideString);
  31.     procedure DIHtmlLabelDynamicTagInit(const Sender: TObject; const ID: WideString; out Source: string);
  32.     procedure pnlTopMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; x, y: Integer);
  33.     procedure edtDynamicChange(Sender: TObject);
  34.     procedure Button1Click(Sender: TObject);
  35.   private
  36.     FCategoryChooser: TCategoryChooser;
  37.     procedure CategoryChooserCatChange(Sender: TObject);
  38.   protected
  39.     procedure CreateParams(var Params: TCreateParams); override;
  40.     procedure OpenFormPlay;
  41.   end;
  42.  
  43. var
  44.   frmMain: TfrmMain;
  45.  
  46. implementation
  47.  
  48. {$R *.DFM}
  49.  
  50. uses
  51.   Windows, Messages, ShellAPI, Graphics, Dialogs,
  52.  
  53.   InfoStrings, Play,
  54.  
  55.   DIHtmlCharSetPlugin,
  56.   DIUtils;
  57.  
  58. { ---------------------------------------------------------------------------- }
  59.  
  60. procedure TfrmMain.FormCreate(Sender: TObject);
  61.  
  62.   procedure TryFonts(const FontNames: array of AnsiString);
  63.   var
  64.     i: Integer;
  65.   begin
  66.     for i := Low(FontNames) to High(FontNames) do
  67.       if Screen.Fonts.IndexOf(FontNames[i]) >= 0 then
  68.         begin
  69.           Font.Name := FontNames[i];
  70.           Break;
  71.         end;
  72.   end;
  73.  
  74. var
  75.   i: Integer;
  76. begin
  77.   DoubleBuffered := True;
  78.   TryFonts(['Verdana', 'Tahoma', 'Arial', 'Times New Roman']);
  79.  
  80.   lblWelcome.Font.Color := clHighlightText;
  81.   lblWelcome.Font.Size := 14;
  82.   lblWelcome.Font.Style := [fsBold];
  83.  
  84.   lblHtmlLabel.Font.Color := clHighlightText;
  85.   lblHtmlLabel.Font.Size := 24;
  86.   lblHtmlLabel.Font.Style := [fsBold];
  87.  
  88.   lblClose.Font.Color := clHighlightText;
  89.   lblClose.Font.Size := 12;
  90.   lblClose.Font.Style := [fsBold, fsUnderline];
  91.   lblClose.Cursor := crHandPoint;
  92.  
  93.   DIHtmlLabel.Font.Size := 9;
  94.  
  95.   { We create the TCategoryChooser dynamically,
  96.     so that the user doesn't need to install it. }
  97.   FCategoryChooser := TCategoryChooser.Create(Self);
  98.   FCategoryChooser.SetBounds(
  99.     0,
  100.     pnlBlackLine.Top + pnlBlackLine.Height,
  101.     DIHtmlLabel.Left - 4,
  102.     ClientHeight - pnlBlackLine.Top - pnlBlackLine.Height);
  103.   FCategoryChooser.OnCatChange := CategoryChooserCatChange;
  104.   for i := Low(Headings) to High(Headings) do
  105.     FCategoryChooser.CatList.Add(Headings[i]);
  106.   FCategoryChooser.Parent := Self;
  107.   FCategoryChooser.Font.Style := [fsBold];
  108.   CategoryChooserCatChange(FCategoryChooser);
  109.  
  110.   {$IFDEF Debug}
  111.   OpenFormPlay;
  112.   {$ENDIF}
  113. end;
  114.  
  115. { ---------------------------------------------------------------------------- }
  116.  
  117. procedure TfrmMain.CreateParams(var Params: TCreateParams);
  118. begin
  119.   { We should really create a new TCustomForm-descendant, called
  120.     TMovableBorderForm or something like that, that we'd inherit from, instead
  121.     of placing all this nasty Windows-specific code directly in our main form.
  122.     However, that would probably cause trouble with the form designer, and we'd
  123.     wind up with creating everything dynamically. Visual form inheritance could
  124.     probably help us out, but this is just a demo application, and we'd just be
  125.     complicating matters unnecessarily. }
  126.   inherited;
  127.   with Params do
  128.     Style := (Style or WS_POPUP or WS_BORDER) and not WS_DLGFRAME;
  129. end;
  130.  
  131. { ---------------------------------------------------------------------------- }
  132.  
  133. procedure TfrmMain.CategoryChooserCatChange(Sender: TObject);
  134. begin
  135.   with FCategoryChooser do
  136.     if SelectedCat in [Low(Info)..High(Info)] then
  137.       DIHtmlLabel.Caption := Info[SelectedCat];
  138. end;
  139.  
  140. { ---------------------------------------------------------------------------- }
  141.  
  142. procedure TfrmMain.FormKeyPress(Sender: TObject; var Key: Char);
  143. begin
  144.   if Key = #27 { Escape } then
  145.     begin
  146.       Close;
  147.       Key := #0;
  148.     end;
  149. end;
  150.  
  151. { ---------------------------------------------------------------------------- }
  152.  
  153. procedure TfrmMain.lblCloseClick(Sender: TObject);
  154. begin
  155.   Close;
  156. end;
  157.  
  158. { ---------------------------------------------------------------------------- }
  159.  
  160. procedure TfrmMain.OpenFormPlay;
  161. begin
  162.   with TfrmPlay.Create(nil) do
  163.     try
  164.       ShowModal;
  165.     finally
  166.       Free;
  167.     end;
  168. end;
  169.  
  170. { ---------------------------------------------------------------------------- }
  171.  
  172. procedure TfrmMain.DIHtmlLabelLinkClick(
  173.   const Sender: TObject;
  174.   const LinkHref: WideString;
  175.   const LinkText: WideString);
  176.  
  177.   procedure GoToNextCategory(const i: Integer);
  178.   begin
  179.     { This should really belong to the TCategoryChooser itself. }
  180.     with FCategoryChooser do
  181.       SelectedCat := SelectedCat + i;
  182.   end;
  183.  
  184.   { ---------- }
  185.  
  186.   procedure DisplayLink;
  187.   begin
  188.     ShowMessage(
  189.       'This link is identified by:' +
  190.       CRLF + CRLF + '"' + LinkHref + '"' + CRLF + CRLF +
  191.       'It contains the following text:'
  192.       + CRLF + CRLF + '"' + LinkText + '"');
  193.   end;
  194.  
  195.   { ---------- }
  196.  
  197.   procedure OpenUrl;
  198.   begin
  199.     ShellExecute(0, 'open', PAnsiChar(AnsiString(LinkHref)), nil, nil, SW_SHOWNORMAL);
  200.   end;
  201.  
  202.   { ---------- }
  203.  
  204. begin
  205.   if StrSameIW(LinkHref, 'Next') then
  206.     GoToNextCategory(1)
  207.   else
  208.     if StrSameIW(LinkHref, 'Next2') then
  209.       GoToNextCategory(2)
  210.     else
  211.       if StrSameIW(LinkHref, 'Next3') then
  212.         GoToNextCategory(3)
  213.       else
  214.         if StrSameIW(LinkHref, 'Next4') then
  215.           GoToNextCategory(4)
  216.         else
  217.           if StrSameIW(LinkHref, 'Show') then
  218.             DisplayLink
  219.           else
  220.             if StrSameIW(LinkHref, 'Play') then
  221.               OpenFormPlay
  222.             else
  223.               if StrSameStartIW(LinkHref, 'http://') then
  224.                 OpenUrl;
  225. end;
  226.  
  227. { ---------------------------------------------------------------------------- }
  228.  
  229. procedure TfrmMain.DIHtmlLabelDynamicTagInit(
  230.   const Sender: TObject;
  231.   const ID: WideString;
  232.   out Source: string);
  233. begin
  234.   Source := edtDynamic.Text;
  235. end;
  236.  
  237. { ---------------------------------------------------------------------------- }
  238.  
  239. procedure TfrmMain.pnlTopMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; x, y: Integer);
  240. const
  241.   SC_DRAGMOVE = SC_MOVE + HTCAPTION;
  242. begin
  243.   ReleaseCapture;
  244.   Perform(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
  245. end;
  246.  
  247. { ---------------------------------------------------------------------------- }
  248.  
  249. procedure TfrmMain.edtDynamicChange(Sender: TObject);
  250. begin
  251.   DIHtmlLabel.UpdateDynamicTag('', edtDynamic.Text);
  252. end;
  253.  
  254. { ---------------------------------------------------------------------------- }
  255.  
  256. procedure TfrmMain.Button1Click(Sender: TObject);
  257. begin
  258.   ShowMessage(DIHtmlLabel.GetDynamicTagContents(''));
  259. end;
  260.  
  261. { ---------------------------------------------------------------------------- }
  262.  
  263. initialization
  264.   RegisterCharSets;
  265.  
  266. end.
  267.  
  268.