home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / delphi / kompon / d45 / OPTIONS.ZIP / Units / ValueEditor.pas < prev   
Pascal/Delphi Source File  |  2002-06-14  |  6KB  |  264 lines

  1. unit ValueEditor;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, SysUtils, Forms, Controls, Dialogs, Graphics, DsgnIntf;
  7.  
  8. type
  9.   TDefValueProperty = class(TPropertyEditor)
  10.     function GetAttributes: TPropertyAttributes; override;
  11.     function GetValue: string; override;
  12.     procedure Edit; override;
  13.   end;
  14.  
  15. procedure Register;
  16.  
  17. implementation
  18.  
  19. uses
  20.   BooleanEditor, DateTimeEditor, FlagsEditor, ArrayEditor, Options;
  21.  
  22. { TDefValueProperty }
  23.  
  24. function TDefValueProperty.GetAttributes: TPropertyAttributes;
  25. begin
  26.   result := [paReadOnly, paDialog];
  27. end;
  28.  
  29. function TDefValueProperty.GetValue: string;
  30. begin
  31.   result := (GetComponent(0) as TOption).DefAsString;
  32. end;
  33.  
  34. procedure EditAsString(I: TOption; Cap: string);
  35. begin
  36.   I.DefAsString := InputBox(Cap + ' as string', 'String &value', I.DefAsString);
  37. end;
  38.  
  39. procedure EditAsInteger(I: TOption; Cap: string);
  40. var
  41.   s: string;
  42. begin
  43.   s := InputBox(Cap + ' as integer', 'Integer &value', I.DefAsString);
  44.   try
  45.     I.DefAsInteger := StrToInt(s);
  46.   except
  47.     raise EOptError.Create(oerNotInteger);
  48.   end;
  49. end;
  50.  
  51. procedure EditAsFloat(I: TOption; Cap: string);
  52. var
  53.   s: string;
  54. begin
  55.   s := InputBox(Cap + ' as float', 'Float &value', I.DefAsString);
  56.   try
  57.     I.DefAsFloat := StrToFloat(s);
  58.   except
  59.     raise EOptError.Create(oerNotFloat);
  60.   end;
  61. end;
  62.  
  63. procedure EditAsColor(I: TOption; Cap: string);
  64. begin
  65.   with TColorDialog.Create(Application) do
  66.     try
  67.       Color := I.DefAsColor;
  68.       if Execute then
  69.         I.DefAsColor := Color;
  70.     finally
  71.       Free;
  72.     end;
  73. end;
  74.  
  75. procedure EditAsFont(I: TOption; Cap: string);
  76. var
  77.   F: TFont;
  78. begin
  79.   with TFontDialog.Create(Application) do
  80.     try
  81.       F := I.DefAsFont;
  82.       try
  83.         Font.Assign(F);
  84.       finally
  85.         F.Free;
  86.       end;
  87.       if Execute then
  88.         I.DefAsFont := Font;
  89.     finally
  90.       Free;
  91.     end;
  92. end;
  93.  
  94. procedure EditAsBoolean(I: TOption; Cap: string);
  95. begin
  96.   with TfrmBooleanEditor.Create(Application) do
  97.     try
  98.       Caption := Cap + ' as boolean';
  99.       rgrBoolean.ItemIndex := ord(I.DefAsBoolean);
  100.       if ShowModal = mrOK then
  101.         I.DefAsBoolean := rgrBoolean.ItemIndex = 1;
  102.     finally
  103.       Free;
  104.     end;
  105. end;
  106.  
  107. procedure EditAsDateTime(I: TOption; Cap: string; K: TdtKind);
  108. begin
  109.   with TfrmDateTimeEditor.Create(Application) do
  110.     try
  111.       Kind := K;
  112.       case K of
  113.         dtkDate: begin
  114.           Cap := Cap + ' as date';
  115.           Date := I.DefAsDate;
  116.         end;
  117.         dtkTime: begin
  118.           Cap := Cap + ' as time';
  119.           Time := I.DefAsTime;
  120.         end;
  121.         dtkDateTime: begin
  122.           Cap := Cap + ' as date and time';
  123.           Date := I.DefAsDateTime;
  124.           Time := I.DefAsDateTime;
  125.         end;
  126.       end;
  127.       Caption := Cap;
  128.       if ShowModal = mrOK then
  129.         case K of
  130.           dtkDate: I.DefAsDate := Date;
  131.           dtkTime: I.DefAsTime := Time;
  132.           dtkDateTime: I.DefAsDateTime := Date + Time;
  133.         end;
  134.     finally
  135.       Free;
  136.     end;
  137. end;
  138.  
  139. procedure EditAsFlags(I: TOption; Cap: string);
  140. var
  141.   k: integer;
  142. begin
  143.   with TfrmFlagsEditor.Create(Application) do
  144.     try
  145.       Caption := Cap + ' as flags';
  146.       OptName := I.Name;
  147.       for k := 0 to I.DefCount - 1 do begin
  148.         clbFlags.Items.Add(FlagName(k));
  149.         clbFlags.Checked[k] := I.DefAsFlags[k];
  150.       end;
  151.       if ShowModal = mrOK then begin
  152.         I.DefAsString := '';
  153.         for k := 0 to clbFlags.Items.Count - 1 do
  154.           I.DefAsFlags[k] := clbFlags.Checked[k];
  155.       end;
  156.     finally
  157.       Free;
  158.     end;
  159. end;
  160.  
  161. procedure EditAsStrArray(I: TOption; Cap: string);
  162. begin
  163.   with TfrmArrayEditor.Create(Application) do
  164.     try
  165.       Caption := Cap + ' as string array';
  166.       memArray.Lines.BeginUpdate;
  167.       memArray.Lines.Text := I.DefAsString;
  168.       memArray.Lines.EndUpdate;
  169.       if ShowModal = mrOK then
  170.         I.DefAsString := memArray.Lines.Text;
  171.     finally
  172.       Free;
  173.     end;
  174. end;
  175.  
  176. procedure EditAsIntArray(I: TOption; Cap: string);
  177. var
  178.   k: integer;
  179.   s: string;
  180. begin
  181.   with TfrmArrayEditor.Create(Application) do
  182.     try
  183.       Caption := Cap + ' as integer array';
  184.       memArray.Lines.BeginUpdate;
  185.       memArray.Lines.Text := I.DefAsString;
  186.       memArray.Lines.EndUpdate;
  187.       if ShowModal = mrOK then begin
  188.         s := I.DefAsString;
  189.         I.DefAsString := '';
  190.         for k := 0 to memArray.Lines.Count - 1 do
  191.           try
  192.             I.DefAsIntArray[k] := StrToInt(memArray.Lines[k]);
  193.           except
  194.             I.DefAsString := s;
  195.             raise EOptError.Create(oerNotInteger);
  196.           end;
  197.       end;
  198.     finally
  199.       Free;
  200.     end;
  201. end;
  202.  
  203. procedure EditAsFloatArray(I: TOption; Cap: string);
  204. var
  205.   k: integer;
  206.   s: string;
  207. begin
  208.   with TfrmArrayEditor.Create(Application) do
  209.     try
  210.       Caption := Cap + ' as float array';
  211.       memArray.Lines.BeginUpdate;
  212.       memArray.Lines.Text := I.DefAsString;
  213.       memArray.Lines.EndUpdate;
  214.       if ShowModal = mrOK then begin
  215.         s := I.DefAsString;
  216.         I.DefAsString := '';
  217.         for k := 0 to memArray.Lines.Count - 1 do
  218.           try
  219.             I.DefAsFloatArray[k] := StrToFloat(memArray.Lines[k]);
  220.           except
  221.             I.DefAsString := s;
  222.             raise EOptError.Create(oerNotFloat);
  223.           end;
  224.       end;
  225.     finally
  226.       Free;
  227.     end;
  228. end;
  229.  
  230. procedure TDefValueProperty.Edit;
  231. var
  232.   I: TOption;
  233.   Cap, Old: string;
  234. begin
  235.   I := GetComponent(0) as TOption;
  236.   Old := I.DefAsString;
  237.   Cap := 'Editing ' + I.Name + '.DefValue';
  238.   case I.OptionType of
  239.     otString: EditAsString(I, Cap);
  240.     otInteger: EditAsInteger(I, Cap);
  241.     otFloat: EditAsFloat(I, Cap);
  242.     otBoolean: EditAsBoolean(I, Cap);
  243.     otColor: EditAsColor(I, Cap);
  244.     otDate: EditAsDateTime(I, Cap, dtkDate);
  245.     otTime: EditAsDateTime(I, Cap, dtkTime);
  246.     otDateTime: EditAsDateTime(I, Cap, dtkDateTime);
  247.     otFont: EditAsFont(I, Cap);
  248.     otFlags: EditAsFlags(I, Cap);
  249.     otStrArray: EditAsStrArray(I, Cap);
  250.     otIntArray: EditAsIntArray(I, Cap);
  251.     otFloatArray: EditAsFloatArray(I, Cap);
  252.   end;
  253.   if Old <> I.DefAsString then
  254.     Designer.Modified;
  255. end;
  256.  
  257. procedure Register;
  258. begin
  259.   RegisterPropertyEditor(TypeInfo(TDefValue), TOption, 'DefValue', TDefValueProperty);
  260. end;
  261.  
  262. end.
  263.  
  264.