TB2DsgnConverter.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. http://www.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. http://www.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. {$IFDEF JR_D6}
  75. Dst.AutoCheck := Src.AutoCheck;
  76. {$ENDIF}
  77. Dst.Caption := Src.Caption;
  78. Dst.Checked := Src.Checked;
  79. if Src.Default then
  80. Dst.Options := Dst.Options + [tboDefault];
  81. Dst.Enabled := Src.Enabled;
  82. Dst.GroupIndex := Src.GroupIndex;
  83. Dst.HelpContext := Src.HelpContext;
  84. Dst.ImageIndex := Src.ImageIndex;
  85. Dst.RadioItem := Src.RadioItem;
  86. Dst.ShortCut := Src.ShortCut;
  87. {$IFDEF JR_D5}
  88. Dst.SubMenuImages := Src.SubMenuImages;
  89. {$ENDIF}
  90. Dst.OnClick := Src.OnClick;
  91. end
  92. else begin
  93. Dst := TTBSeparatorItem.Create(Owner);
  94. end;
  95. Dst.Hint := Src.Hint;
  96. Dst.Tag := Src.Tag;
  97. Dst.Visible := Src.Visible;
  98. if not IsSep then
  99. { Temporarily clear the menu item's OnClick property, so that renaming
  100. the menu item doesn't cause the function name to change }
  101. Src.OnClick := nil;
  102. try
  103. N := Src.Name;
  104. Src.Name := N + '_OLD';
  105. Dst.Name := N;
  106. finally
  107. if not IsSep then
  108. Src.OnClick := Dst.OnClick;
  109. end;
  110. TBItem.Add(Dst);
  111. {$IFDEF JR_D5}
  112. if @Src.OnAdvancedDrawItem <> nil then
  113. Log(Format(SPropNotTransferred, ['OnAdvancedDrawItem', Dst.Name]));
  114. {$ENDIF}
  115. if @Src.OnDrawItem <> nil then
  116. Log(Format(SPropNotTransferred, ['OnDrawItem', Dst.Name]));
  117. if @Src.OnMeasureItem <> nil then
  118. Log(Format(SPropNotTransferred, ['OnMeasureItem', Dst.Name]));
  119. if IsSubmenu then
  120. Recurse(Src, Dst);
  121. end;
  122. end;
  123. var
  124. OptionsForm: TTBConvertOptionsForm;
  125. I: Integer;
  126. C: TComponent;
  127. Menu: TMenu;
  128. begin
  129. Menu := nil;
  130. OptionsForm := TTBConvertOptionsForm.Create(Application);
  131. try
  132. for I := 0 to Owner.ComponentCount-1 do begin
  133. C := Owner.Components[I];
  134. if (C is TMenu) and not(C is TTBPopupMenu) then
  135. OptionsForm.MenuCombo.Items.AddObject(C.Name, C);
  136. end;
  137. if OptionsForm.MenuCombo.Items.Count = 0 then
  138. raise Exception.Create('Could not find any menus on the form to convert');
  139. OptionsForm.MenuCombo.ItemIndex := 0;
  140. if (OptionsForm.ShowModal <> mrOK) or (OptionsForm.MenuCombo.ItemIndex < 0) then
  141. Exit;
  142. Menu := TMenu(OptionsForm.MenuCombo.Items.Objects[OptionsForm.MenuCombo.ItemIndex]);
  143. finally
  144. OptionsForm.Free;
  145. end;
  146. ParentItem.SubMenuImages := Menu.Images;
  147. ConverterForm := TTBConverterForm.Create(Application);
  148. ConverterForm.Show;
  149. ConverterForm.Update;
  150. Log(Format('Converting ''%s'', please wait...', [Menu.Name]));
  151. ParentItem.ViewBeginUpdate;
  152. try
  153. Recurse(Menu.Items, ParentItem);
  154. finally
  155. ParentItem.ViewEndUpdate;
  156. end;
  157. Log('Done!');
  158. ConverterForm.CloseButton.Enabled := True;
  159. ConverterForm.CopyButton.Enabled := True;
  160. end;
  161. { TTBConverterForm }
  162. procedure TTBConverterForm.FormClose(Sender: TObject;
  163. var Action: TCloseAction);
  164. begin
  165. Action := caFree;
  166. end;
  167. procedure TTBConverterForm.CloseButtonClick(Sender: TObject);
  168. begin
  169. Close;
  170. end;
  171. procedure TTBConverterForm.CopyButtonClick(Sender: TObject);
  172. begin
  173. Clipboard.AsText := MessageList.Items.Text;
  174. end;
  175. procedure FreeConverterForms;
  176. var
  177. I: Integer;
  178. Form: TCustomForm;
  179. label Restart;
  180. begin
  181. Restart:
  182. for I := 0 to Screen.CustomFormCount-1 do begin
  183. Form := Screen.CustomForms[I];
  184. if Form is TTBConverterForm then begin
  185. Form.Free;
  186. goto Restart;
  187. end;
  188. end;
  189. end;
  190. initialization
  191. finalization
  192. FreeConverterForms;
  193. end.