VCLCommon.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "VCLCommon.h"
  5. #include <Common.h>
  6. #include <TextsWin.h>
  7. #include <FileCtrl.hpp>
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. void __fastcall AdjustListColumnsWidth(TListView* ListView)
  12. {
  13. int OriginalWidth, NewWidth, i, CWidth;
  14. OriginalWidth = 0;
  15. for (i = 0; i < ListView->Columns->Count; i++)
  16. {
  17. OriginalWidth += ListView->Columns->Items[i]->Width;
  18. }
  19. NewWidth = 0;
  20. CWidth = ListView->ClientWidth;
  21. if ((ListView->VisibleRowCount < ListView->Items->Count) &&
  22. (ListView->Width - ListView->ClientWidth < GetSystemMetrics(SM_CXVSCROLL)))
  23. {
  24. CWidth -= GetSystemMetrics(SM_CXVSCROLL);
  25. }
  26. for (i = 0; i < ListView->Columns->Count-1;i++)
  27. {
  28. if (ListView->Columns->Items[i]->Tag == 0)
  29. {
  30. ListView->Columns->Items[i]->Width =
  31. (CWidth * ListView->Columns->Items[i]->Width) / OriginalWidth;
  32. }
  33. NewWidth += ListView->Columns->Items[i]->Width;
  34. }
  35. ListView->Columns->Items[ListView->Columns->Count-1]->Width = CWidth-NewWidth;
  36. }
  37. //---------------------------------------------------------------------------
  38. void __fastcall EnableControl(TControl * Control, bool Enable)
  39. {
  40. if (Control->Enabled != Enable)
  41. {
  42. if (Control->InheritsFrom(__classid(TWinControl)) &&
  43. (((TWinControl*)Control)->ControlCount > 0))
  44. {
  45. for (Integer Index = 0; Index < ((TWinControl*)Control)->ControlCount; Index++)
  46. EnableControl(((TWinControl*)Control)->Controls[Index], Enable);
  47. }
  48. Control->Enabled = Enable;
  49. }
  50. if (Control->InheritsFrom(__classid(TCustomEdit)) ||
  51. Control->InheritsFrom(__classid(TCustomComboBox)))
  52. {
  53. if (Enable) ((TEdit*)Control)->Color = clWindow;
  54. else ((TEdit*)Control)->Color = clBtnFace;
  55. }
  56. };
  57. //---------------------------------------------------------------------------
  58. struct TSavedSystemSettings
  59. {
  60. AnsiString FontName;
  61. bool Flipped;
  62. };
  63. //---------------------------------------------------------------------------
  64. void __fastcall UseSystemSettings(TCustomForm * Control, void ** Settings)
  65. {
  66. bool Flip;
  67. AnsiString FlipStr = LoadStr(FLIP_CHILDREN);
  68. Flip = !FlipStr.IsEmpty() && static_cast<bool>(StrToInt(FlipStr));
  69. if (Settings)
  70. {
  71. TSavedSystemSettings * SSettings = new TSavedSystemSettings();
  72. *Settings = static_cast<void*>(SSettings);
  73. SSettings->FontName = Control->Font->Name;
  74. SSettings->Flipped = Flip;
  75. }
  76. assert(Control && Control->Font);
  77. Control->Font->Name = "MS Shell Dlg";
  78. if (Flip)
  79. {
  80. Control->FlipChildren(true);
  81. }
  82. };
  83. //---------------------------------------------------------------------------
  84. void __fastcall RevokeSystemSettings(TCustomForm * Control,
  85. void * Settings)
  86. {
  87. assert(Settings);
  88. if (static_cast<TSavedSystemSettings*>(Settings)->Flipped)
  89. {
  90. Control->FlipChildren(true);
  91. }
  92. delete Settings;
  93. };
  94. //---------------------------------------------------------------------------
  95. void __fastcall LinkLabel(TLabel * Label)
  96. {
  97. Label->ParentFont = true;
  98. Label->Font->Style = Label->Font->Style << fsUnderline;
  99. Label->Font->Color = clBlue;
  100. }
  101. //---------------------------------------------------------------------------
  102. class TPublicForm : public TForm
  103. {
  104. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage);
  105. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  106. };
  107. //---------------------------------------------------------------------------
  108. struct TShowAsModalStorage
  109. {
  110. void * FocusWindowList;
  111. void * FocusActiveWindow;
  112. };
  113. //---------------------------------------------------------------------------
  114. void __fastcall ShowAsModal(TForm * Form, void *& Storage)
  115. {
  116. CancelDrag();
  117. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  118. ReleaseCapture();
  119. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  120. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  121. AStorage->FocusActiveWindow = GetActiveWindow();
  122. AStorage->FocusWindowList = DisableTaskWindows(0);
  123. Form->Show();
  124. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  125. Storage = AStorage;
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  129. {
  130. assert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  131. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  132. Storage = NULL;
  133. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  134. if (GetActiveWindow() != Form->Handle)
  135. {
  136. AStorage->FocusActiveWindow = 0;
  137. }
  138. Form->Hide();
  139. EnableTaskWindows(AStorage->FocusWindowList);
  140. if (AStorage->FocusActiveWindow != 0)
  141. {
  142. SetActiveWindow(AStorage->FocusActiveWindow);
  143. }
  144. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  145. delete AStorage;
  146. }
  147. //---------------------------------------------------------------------------
  148. void __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  149. {
  150. if (Storage != NULL)
  151. {
  152. HideAsModal(Form, Storage);
  153. }
  154. }
  155. //---------------------------------------------------------------------------
  156. bool __fastcall SelectDirectory(AnsiString & Path, const AnsiString Prompt,
  157. bool PreserveFileName)
  158. {
  159. bool Result;
  160. unsigned int ErrorMode;
  161. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  162. try
  163. {
  164. AnsiString Directory;
  165. AnsiString FileName;
  166. if (!PreserveFileName || DirectoryExists(Path))
  167. {
  168. Directory = Path;
  169. }
  170. else
  171. {
  172. Directory = ExtractFilePath(Path);
  173. FileName = ExtractFileName(Path);
  174. }
  175. Result = SelectDirectory(Prompt, "", Directory);
  176. if (Result)
  177. {
  178. Path = Directory;
  179. if (!FileName.IsEmpty())
  180. {
  181. Path = IncludeTrailingBackslash(Path) + FileName;
  182. }
  183. }
  184. }
  185. __finally
  186. {
  187. SetErrorMode(ErrorMode);
  188. }
  189. return Result;
  190. }