TB2DsgnConverter.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. unit TB2DsgnConverter;
  2. {
  3. Toolbar2000
  4. Copyright (C) 1998-2005 by Jordan Russell
  5. All rights reserved.
  6. The contents of this file are subject to the "Toolbar2000 License"; you may
  7. not use or distribute this file except in compliance with the
  8. "Toolbar2000 License". A copy of the "Toolbar2000 License" may be found in
  9. TB2k-LICENSE.txt or at:
  10. https://jrsoftware.org/files/tb2k/TB2k-LICENSE.txt
  11. Alternatively, the contents of this file may be used under the terms of the
  12. GNU General Public License (the "GPL"), in which case the provisions of the
  13. GPL are applicable instead of those in the "Toolbar2000 License". A copy of
  14. the GPL may be found in GPL-LICENSE.txt or at:
  15. https://jrsoftware.org/files/tb2k/GPL-LICENSE.txt
  16. If you wish to allow use of your version of this file only under the terms of
  17. the GPL and not to allow others to use your version of this file under the
  18. "Toolbar2000 License", indicate your decision by deleting the provisions
  19. above and replace them with the notice and other provisions required by the
  20. GPL. If you do not delete the provisions above, a recipient may use your
  21. version of this file under either the "Toolbar2000 License" or the GPL.
  22. $jrsoftware: tb2k/Source/TB2DsgnConverter.pas,v 1.16 2005/01/06 03:56:50 jr Exp $
  23. }
  24. interface
  25. {$I TB2Ver.inc}
  26. uses
  27. Windows, SysUtils, Classes, Controls, Forms, Menus, StdCtrls,
  28. TB2Item;
  29. type
  30. TTBConverterForm = class(TForm)
  31. MessageList: TListBox;
  32. CloseButton: TButton;
  33. CopyButton: TButton;
  34. procedure CloseButtonClick(Sender: TObject);
  35. procedure CopyButtonClick(Sender: TObject);
  36. procedure FormClose(Sender: TObject; var Action: TCloseAction);
  37. end;
  38. procedure DoConvert(const ParentItem: TTBCustomItem; const Owner: TComponent);
  39. implementation
  40. {$R *.DFM}
  41. uses
  42. Clipbrd, TB2DsgnConvertOptions;
  43. procedure DoConvert(const ParentItem: TTBCustomItem; const Owner: TComponent);
  44. const
  45. SPropNotTransferred = 'Warning: %s property not transferred on ''%s''.';
  46. var
  47. ConverterForm: TTBConverterForm;
  48. procedure Log(const S: String);
  49. begin
  50. ConverterForm.MessageList.Items.Add(S);
  51. ConverterForm.MessageList.TopIndex := ConverterForm.MessageList.Items.Count-1;
  52. ConverterForm.Update;
  53. end;
  54. procedure Recurse(MenuItem: TMenuItem; TBItem: TTBCustomItem);
  55. var
  56. I: Integer;
  57. Src: TMenuItem;
  58. IsSep, IsSubmenu: Boolean;
  59. Dst: TTBCustomItem;
  60. N: String;
  61. begin
  62. for I := 0 to MenuItem.Count-1 do begin
  63. Src := MenuItem[I];
  64. IsSep := (Src.Caption = '-');
  65. IsSubmenu := False;
  66. if not IsSep then begin
  67. if Src.Count > 0 then
  68. IsSubmenu := True;
  69. if not IsSubmenu then
  70. Dst := TTBItem.Create(Owner)
  71. else
  72. Dst := TTBSubmenuItem.Create(Owner);
  73. Dst.Action := Src.Action;
  74. Dst.AutoCheck := Src.AutoCheck;
  75. Dst.Caption := Src.Caption;
  76. Dst.Checked := Src.Checked;
  77. if Src.Default then
  78. Dst.Options := Dst.Options + [tboDefault];
  79. Dst.Enabled := Src.Enabled;
  80. Dst.GroupIndex := Src.GroupIndex;
  81. Dst.HelpContext := Src.HelpContext;
  82. Dst.ImageIndex := Src.ImageIndex;
  83. Dst.RadioItem := Src.RadioItem;
  84. Dst.ShortCut := Src.ShortCut;
  85. Dst.SubMenuImages := Src.SubMenuImages;
  86. Dst.OnClick := Src.OnClick;
  87. end
  88. else begin
  89. Dst := TTBSeparatorItem.Create(Owner);
  90. end;
  91. Dst.Hint := Src.Hint;
  92. Dst.Tag := Src.Tag;
  93. Dst.Visible := Src.Visible;
  94. if not IsSep then
  95. { Temporarily clear the menu item's OnClick property, so that renaming
  96. the menu item doesn't cause the function name to change }
  97. Src.OnClick := nil;
  98. try
  99. N := Src.Name;
  100. Src.Name := N + '_OLD';
  101. Dst.Name := N;
  102. finally
  103. if not IsSep then
  104. Src.OnClick := Dst.OnClick;
  105. end;
  106. TBItem.Add(Dst);
  107. if @Src.OnAdvancedDrawItem <> nil then
  108. Log(Format(SPropNotTransferred, ['OnAdvancedDrawItem', Dst.Name]));
  109. if @Src.OnDrawItem <> nil then
  110. Log(Format(SPropNotTransferred, ['OnDrawItem', Dst.Name]));
  111. if @Src.OnMeasureItem <> nil then
  112. Log(Format(SPropNotTransferred, ['OnMeasureItem', Dst.Name]));
  113. if IsSubmenu then
  114. Recurse(Src, Dst);
  115. end;
  116. end;
  117. var
  118. OptionsForm: TTBConvertOptionsForm;
  119. I: Integer;
  120. C: TComponent;
  121. Menu: TMenu;
  122. begin
  123. OptionsForm := TTBConvertOptionsForm.Create(Application);
  124. try
  125. for I := 0 to Owner.ComponentCount-1 do begin
  126. C := Owner.Components[I];
  127. if (C is TMenu) and not(C is TTBPopupMenu) then
  128. OptionsForm.MenuCombo.Items.AddObject(C.Name, C);
  129. end;
  130. if OptionsForm.MenuCombo.Items.Count = 0 then
  131. raise Exception.Create('Could not find any menus on the form to convert');
  132. OptionsForm.MenuCombo.ItemIndex := 0;
  133. if (OptionsForm.ShowModal <> mrOK) or (OptionsForm.MenuCombo.ItemIndex < 0) then
  134. Exit;
  135. Menu := TMenu(OptionsForm.MenuCombo.Items.Objects[OptionsForm.MenuCombo.ItemIndex]);
  136. finally
  137. OptionsForm.Free;
  138. end;
  139. ParentItem.SubMenuImages := Menu.Images;
  140. ConverterForm := TTBConverterForm.Create(Application);
  141. ConverterForm.Show;
  142. ConverterForm.Update;
  143. Log(Format('Converting ''%s'', please wait...', [Menu.Name]));
  144. ParentItem.ViewBeginUpdate;
  145. try
  146. Recurse(Menu.Items, ParentItem);
  147. finally
  148. ParentItem.ViewEndUpdate;
  149. end;
  150. Log('Done!');
  151. ConverterForm.CloseButton.Enabled := True;
  152. ConverterForm.CopyButton.Enabled := True;
  153. end;
  154. { TTBConverterForm }
  155. procedure TTBConverterForm.FormClose(Sender: TObject;
  156. var Action: TCloseAction);
  157. begin
  158. Action := caFree;
  159. end;
  160. procedure TTBConverterForm.CloseButtonClick(Sender: TObject);
  161. begin
  162. Close;
  163. end;
  164. procedure TTBConverterForm.CopyButtonClick(Sender: TObject);
  165. begin
  166. Clipboard.AsText := MessageList.Items.Text;
  167. end;
  168. procedure FreeConverterForms;
  169. var
  170. I: Integer;
  171. Form: TCustomForm;
  172. label Restart;
  173. begin
  174. Restart:
  175. for I := 0 to Screen.CustomFormCount-1 do begin
  176. Form := Screen.CustomForms[I];
  177. if Form is TTBConverterForm then begin
  178. Form.Free;
  179. goto Restart;
  180. end;
  181. end;
  182. end;
  183. initialization
  184. finalization
  185. FreeConverterForms;
  186. end.