VCLCommon.cpp 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "WinInterface.h"
  5. #include "VCLCommon.h"
  6. #include <Common.h>
  7. #include <TextsWin.h>
  8. #include <RemoteFiles.h>
  9. #include <GUITools.h>
  10. #include <Tools.h>
  11. #include <CustomWinConfiguration.h>
  12. #include <Vcl.StdActns.hpp>
  13. #include <PasswordEdit.hpp>
  14. #include <FileCtrl.hpp>
  15. #include <PathLabel.hpp>
  16. #include <PasTools.hpp>
  17. #include <StrUtils.hpp>
  18. #include <Vcl.Imaging.pngimage.hpp>
  19. #include <Math.hpp>
  20. #include <TB2ExtItems.hpp>
  21. #include <TBXExtItems.hpp>
  22. #include <IEListView.hpp>
  23. #include <WinApi.h>
  24. #include <vssym32.h>
  25. //---------------------------------------------------------------------------
  26. const UnicodeString LinkAppLabelMark(TraceInitStr(L" \x00BB"));
  27. //---------------------------------------------------------------------------
  28. #pragma package(smart_init)
  29. //---------------------------------------------------------------------------
  30. void __fastcall FixListColumnWidth(TListView * TListView, int Index)
  31. {
  32. if (Index < 0)
  33. {
  34. Index = TListView->Columns->Count + Index;
  35. }
  36. TListView->Column[Index]->Tag = 1;
  37. }
  38. //---------------------------------------------------------------------------
  39. static int __fastcall GetColumnTextWidth(TListView * ListView, int ColumnPadding, const UnicodeString & Text)
  40. {
  41. return
  42. ColumnPadding +
  43. ListView->Canvas->TextExtent(Text).Width;
  44. }
  45. //---------------------------------------------------------------------------
  46. void __fastcall AutoSizeListColumnsWidth(TListView * ListView, int ColumnToShrinkIndex)
  47. {
  48. // Preallocate handle to precreate columns, otherwise our changes may get
  49. // overwritten once the handle is created.
  50. // This should actually get called only once the handle are allocated.
  51. // Otherwise we end up recreating the handles,
  52. // what may cause a flicker of the currently focused window title.
  53. ListView->HandleNeeded();
  54. SendMessage(ListView->Handle, WM_SETREDRAW, false, 0);
  55. try
  56. {
  57. int ColumnPadding = 2 * ScaleByTextHeightRunTime(ListView, 6);
  58. int ColumnShrinkMinWidth = ScaleByTextHeightRunTime(ListView, 60);
  59. int ResizableWidth = 0;
  60. int NonResizableWidth = 0;
  61. int LastResizable = -1;
  62. for (int Index = 0; Index < ListView->Columns->Count; Index++)
  63. {
  64. TListColumn * Column = ListView->Columns->Items[Index];
  65. int CaptionWidth = GetColumnTextWidth(ListView, ColumnPadding, Column->Caption);
  66. int OrigWidth = -1;
  67. bool NonResizable = (Column->Tag != 0);
  68. if (NonResizable) // optimization
  69. {
  70. OrigWidth = ListView_GetColumnWidth(ListView->Handle, Index);
  71. }
  72. int Width;
  73. // LVSCW_AUTOSIZE does not work for OwnerData list views
  74. if (!ListView->OwnerData)
  75. {
  76. ListView_SetColumnWidth(ListView->Handle, Index, LVSCW_AUTOSIZE);
  77. Width = ListView_GetColumnWidth(ListView->Handle, Index);
  78. }
  79. else
  80. {
  81. Width = 0;
  82. for (int ItemIndex = 0; ItemIndex < ListView->Items->Count; ItemIndex++)
  83. {
  84. TListItem * Item = ListView->Items->Item[ItemIndex];
  85. UnicodeString Text;
  86. if (Index == 0)
  87. {
  88. Text = Item->Caption;
  89. }
  90. // Particularly EditorListView3 on Preferences dialog does not have all subitems filled for internal editor
  91. else if (Index <= Item->SubItems->Count)
  92. {
  93. Text = Item->SubItems->Strings[Index - 1];
  94. }
  95. int TextWidth = GetColumnTextWidth(ListView, ColumnPadding, Text);
  96. if (TextWidth > Width)
  97. {
  98. Width = TextWidth;
  99. }
  100. }
  101. }
  102. Width = Max(Width, CaptionWidth);
  103. // Never shrink the non-resizable columns
  104. if (NonResizable && (Width < OrigWidth))
  105. {
  106. Width = OrigWidth;
  107. }
  108. Column->Width = Width;
  109. if (NonResizable)
  110. {
  111. NonResizableWidth += Width;
  112. }
  113. else
  114. {
  115. LastResizable = Index;
  116. ResizableWidth += Width;
  117. }
  118. }
  119. DebugAssert(LastResizable >= 0);
  120. int ClientWidth = ListView->ClientWidth;
  121. int RowCount = ListView->Items->Count;
  122. if ((ListView->VisibleRowCount < RowCount) &&
  123. (ListView->Width - ListView->ClientWidth < GetSystemMetricsForControl(ListView, SM_CXVSCROLL)))
  124. {
  125. ClientWidth -= GetSystemMetricsForControl(ListView, SM_CXVSCROLL);
  126. }
  127. if (DebugAlwaysTrue(NonResizableWidth < ClientWidth))
  128. {
  129. int Remaining = ClientWidth - NonResizableWidth;
  130. bool ProportionalResize = true;
  131. bool Shrinking = (Remaining < ResizableWidth);
  132. // If columns are too wide to fit and we have dedicated shrink column, shrink it
  133. if (Shrinking &&
  134. (ColumnToShrinkIndex >= 0))
  135. {
  136. TListColumn * ColumnToShrink = ListView->Columns->Items[ColumnToShrinkIndex];
  137. int ColumnToShrinkCaptionWidth = GetColumnTextWidth(ListView, ColumnPadding, ColumnToShrink->Caption);
  138. int ColumnToShrinkMinWidth = Max(ColumnShrinkMinWidth, ColumnToShrinkCaptionWidth);
  139. // This falls back to proprotional shrinking when the shrink column would fall below min width.
  140. // Question is whether we should not shrink to min width instead.
  141. if ((ResizableWidth - ColumnToShrink->Width) < (Remaining - ColumnToShrinkMinWidth))
  142. {
  143. int ColumnToShrinkWidth = Remaining - (ResizableWidth - ColumnToShrink->Width);
  144. ColumnToShrink->Width = ColumnToShrinkWidth;
  145. ProportionalResize = false;
  146. }
  147. }
  148. if (ProportionalResize)
  149. {
  150. for (int Index = 0; Index <= LastResizable; Index++)
  151. {
  152. TListColumn * Column = ListView->Columns->Items[Index];
  153. if (Column->Tag == 0)
  154. {
  155. int Width = ListView_GetColumnWidth(ListView->Handle, Index);
  156. int AutoWidth = Width;
  157. if (Index < LastResizable)
  158. {
  159. Width = (Remaining * Width) / ResizableWidth;
  160. }
  161. else
  162. {
  163. Width = Remaining;
  164. }
  165. int CaptionWidth = GetColumnTextWidth(ListView, ColumnPadding, Column->Caption);
  166. Width = Max(Width, CaptionWidth);
  167. Width = Max(Width, Min(ColumnShrinkMinWidth, AutoWidth));
  168. Column->Width = Width;
  169. Remaining -= Min(Width, Remaining);
  170. }
  171. }
  172. DebugAssert(Remaining == 0);
  173. }
  174. }
  175. }
  176. __finally
  177. {
  178. SendMessage(ListView->Handle, WM_SETREDRAW, true, 0);
  179. // As recommended by documentation for WM_SETREDRAW.
  180. // Without this, session list on Import dialog stops working after the list is reloaded while visible
  181. // (i.e. when chaing import source)
  182. RedrawWindow(ListView->Handle, NULL, NULL, RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
  183. }
  184. }
  185. //---------------------------------------------------------------------------
  186. static void __fastcall SetParentColor(TControl * Control)
  187. {
  188. TColor Color = clBtnFace;
  189. if (UseThemes)
  190. {
  191. bool OnTabSheet = false;
  192. TWinControl * Parent = Control->Parent;
  193. while ((Parent != NULL) && !OnTabSheet)
  194. {
  195. TTabSheet * TabSheet = dynamic_cast<TTabSheet *>(Parent);
  196. OnTabSheet = (TabSheet != NULL) && TabSheet->TabVisible;
  197. Parent = Parent->Parent;
  198. }
  199. if (OnTabSheet)
  200. {
  201. HTHEME Theme = OpenThemeData(NULL, L"tab");
  202. if (Theme != NULL)
  203. {
  204. COLORREF RGB;
  205. // XP with classic theme: Does not get past OpenThemeData, clBtnFace is exact color
  206. // XP with XP theme: not the exact color (probably same as clBtnFace), but close
  207. // Vista - ok
  208. // 2016 without desktop - ok
  209. // 7 with classic and high contrast themes: Do not get past OpenThemeData, clBtnFace is exact color
  210. // 7 with 7 and basic themes - ok
  211. // 10 with high contrast themes - ok (note the difference to 7 with high contract themes)
  212. // 10 - ok
  213. if (GetThemeColor(Theme, TABP_AEROWIZARDBODY, TIS_NORMAL, TMT_FILLCOLOR, &RGB) == S_OK)
  214. {
  215. Color = static_cast<TColor>(RGB);
  216. }
  217. CloseThemeData(Theme);
  218. }
  219. }
  220. }
  221. ((TEdit*)Control)->Color = Color;
  222. }
  223. //---------------------------------------------------------------------------
  224. void __fastcall EnableControl(TControl * Control, bool Enable)
  225. {
  226. if (Control->Enabled != Enable)
  227. {
  228. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  229. if ((WinControl != NULL) &&
  230. (WinControl->ControlCount > 0))
  231. {
  232. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  233. {
  234. EnableControl(WinControl->Controls[Index], Enable);
  235. }
  236. }
  237. Control->Enabled = Enable;
  238. }
  239. if ((dynamic_cast<TCustomEdit *>(Control) != NULL) ||
  240. (dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  241. (dynamic_cast<TCustomListView *>(Control) != NULL) ||
  242. (dynamic_cast<TCustomTreeView *>(Control) != NULL))
  243. {
  244. if (Enable)
  245. {
  246. ((TEdit*)Control)->Color = clWindow;
  247. }
  248. else
  249. {
  250. // This does not work for list view with
  251. // LVS_EX_DOUBLEBUFFER (TCustomDirView).
  252. // It automatically gets gray background.
  253. // Though on Windows 7, the control has to be disabled
  254. // only after it is showing already (see TCustomScpExplorerForm::UpdateControls())
  255. ((TEdit*)Control)->Color = clBtnFace;
  256. }
  257. }
  258. };
  259. //---------------------------------------------------------------------------
  260. void __fastcall DoReadOnlyControl(TControl * Control, bool ReadOnly, bool Color);
  261. //---------------------------------------------------------------------------
  262. void __fastcall ReadOnlyAndEnabledControl(TControl * Control, bool ReadOnly, bool Enabled)
  263. {
  264. // Change color only in only one of EnableControl and DoReadOnlyControl to prevent flicker
  265. if (ReadOnly)
  266. {
  267. DebugAssert(dynamic_cast<TWinControl *>(Control)->ControlCount == 0);
  268. // As EnableControl, but with no color change
  269. Control->Enabled = Enabled;
  270. DoReadOnlyControl(Control, ReadOnly, true);
  271. }
  272. else
  273. {
  274. DoReadOnlyControl(Control, ReadOnly, false);
  275. EnableControl(Control, Enabled);
  276. }
  277. }
  278. //---------------------------------------------------------------------------
  279. static void __fastcall ReadOnlyEditContextPopup(void * /*Data*/, TObject * Sender, const TPoint & MousePos, bool & Handled)
  280. {
  281. TEdit * Edit = static_cast<TEdit *>(Sender);
  282. if (Edit->ReadOnly)
  283. {
  284. MenuPopup(Sender, MousePos, Handled);
  285. }
  286. }
  287. //---------------------------------------------------------------------------
  288. void __fastcall DoReadOnlyControl(TControl * Control, bool ReadOnly, bool Color)
  289. {
  290. if (dynamic_cast<TCustomEdit *>(Control) != NULL)
  291. {
  292. TEdit * Edit = static_cast<TEdit *>(Control);
  293. Edit->ReadOnly = ReadOnly;
  294. TMemo * Memo = dynamic_cast<TMemo *>(Control);
  295. if (ReadOnly)
  296. {
  297. if (Color)
  298. {
  299. SetParentColor(Control);
  300. }
  301. if (Memo != NULL)
  302. {
  303. // Is true by default and makes the control swallow not only
  304. // returns but also escapes.
  305. // See also MemoKeyDown
  306. Memo->WantReturns = false;
  307. }
  308. if ((Edit->PopupMenu == NULL) && (dynamic_cast<TPasswordEdit *>(Control) == NULL))
  309. {
  310. std::unique_ptr<TPopupMenu> PopupMenu(new TPopupMenu(Edit));
  311. TMenuItem * Item;
  312. Item = new TMenuItem(PopupMenu.get());
  313. PopupMenu->Items->Add(Item);
  314. Item->Action = new TEditCopy(Item);
  315. Item->Caption = LoadStr(EDIT_COPY);
  316. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  317. Item = new TMenuItem(PopupMenu.get());
  318. PopupMenu->Items->Add(Item);
  319. Item->Action = new TEditSelectAll(Item);
  320. Item->Caption = LoadStr(EDIT_SELECT_ALL);
  321. Item->ShortCut = ShortCut(L'A', TShiftState() << ssCtrl);
  322. Edit->PopupMenu = PopupMenu.release();
  323. Edit->OnContextPopup = MakeMethod<TContextPopupEvent>(NULL, ReadOnlyEditContextPopup);
  324. }
  325. }
  326. else
  327. {
  328. if (Color)
  329. {
  330. Edit->Color = clWindow;
  331. }
  332. // not supported atm, we need to persist previous value of WantReturns
  333. DebugAssert(Memo == NULL);
  334. if ((Edit->PopupMenu != NULL) && (Edit->PopupMenu->Owner == Edit))
  335. {
  336. delete Edit->PopupMenu;
  337. }
  338. }
  339. }
  340. else if ((dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  341. (dynamic_cast<TCustomTreeView *>(Control) != NULL))
  342. {
  343. EnableControl(Control, !ReadOnly);
  344. }
  345. else
  346. {
  347. DebugFail();
  348. }
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall ReadOnlyControl(TControl * Control, bool ReadOnly)
  352. {
  353. DoReadOnlyControl(Control, ReadOnly, true);
  354. }
  355. //---------------------------------------------------------------------------
  356. // Some of MainFormLike code can now be obsolete, thanks to Application->OnGetMainFormHandle.
  357. static TForm * MainLikeForm = NULL;
  358. //---------------------------------------------------------------------------
  359. TForm * __fastcall GetMainForm()
  360. {
  361. TForm * Result;
  362. if (MainLikeForm != NULL)
  363. {
  364. Result = MainLikeForm;
  365. }
  366. else
  367. {
  368. Result = Application->MainForm;
  369. }
  370. return Result;
  371. }
  372. //---------------------------------------------------------------------------
  373. bool __fastcall IsMainFormHidden()
  374. {
  375. bool Result = (GetMainForm() != NULL) && !GetMainForm()->Visible;
  376. // we do not expect this to return true when MainLikeForm is set
  377. DebugAssert(!Result || (MainLikeForm == NULL));
  378. return Result;
  379. }
  380. //---------------------------------------------------------------------------
  381. bool __fastcall IsMainFormLike(TCustomForm * Form)
  382. {
  383. return
  384. (GetMainForm() == Form) ||
  385. // this particularly happens if error occurs while main
  386. // window is being shown (e.g. non existent local directory when opening
  387. // explorer)
  388. IsMainFormHidden();
  389. }
  390. //---------------------------------------------------------------------------
  391. UnicodeString __fastcall FormatMainFormCaption(const UnicodeString & Caption, const UnicodeString & SessionName)
  392. {
  393. UnicodeString Suffix = AppName;
  394. if (!SessionName.IsEmpty())
  395. {
  396. Suffix = SessionName + TitleSeparator + Suffix;
  397. }
  398. UnicodeString Result = Caption;
  399. if (Result.IsEmpty())
  400. {
  401. Result = Suffix;
  402. }
  403. else
  404. {
  405. Suffix = TitleSeparator + Suffix;
  406. if (!EndsStr(Suffix, Result))
  407. {
  408. Result += Suffix;
  409. }
  410. }
  411. return Result;
  412. }
  413. //---------------------------------------------------------------------------
  414. UnicodeString __fastcall FormatFormCaption(
  415. TCustomForm * Form, const UnicodeString & Caption, const UnicodeString & SessionName)
  416. {
  417. UnicodeString Result = Caption;
  418. if (IsMainFormLike(Form))
  419. {
  420. Result = FormatMainFormCaption(Result, SessionName);
  421. }
  422. return Result;
  423. }
  424. //---------------------------------------------------------------------------
  425. class TPublicWinControl : public TWinControl
  426. {
  427. friend TWndMethod __fastcall ControlWndProc(TWinControl * Control);
  428. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  429. };
  430. //---------------------------------------------------------------------------
  431. TWndMethod __fastcall ControlWndProc(TWinControl * Control)
  432. {
  433. TPublicWinControl * PublicWinControl = static_cast<TPublicWinControl *>(Control);
  434. return &PublicWinControl->WndProc;
  435. }
  436. //---------------------------------------------------------------------
  437. class TPublicControl : public TControl
  438. {
  439. friend void __fastcall RealignControl(TControl * Control);
  440. friend void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc, TMessage & Message);
  441. friend TCanvas * CreateControlCanvas(TControl * Control);
  442. };
  443. //---------------------------------------------------------------------
  444. class TPublicForm : public TForm
  445. {
  446. friend void __fastcall ChangeFormPixelsPerInch(TForm * Form, int PixelsPerInch);
  447. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage, bool BringToFront);
  448. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  449. friend void __fastcall ShowFormNoActivate(TForm * Form);
  450. };
  451. //---------------------------------------------------------------------------
  452. void __fastcall RealignControl(TControl * Control)
  453. {
  454. TPublicControl * PublicControl = static_cast<TPublicControl *>(Control);
  455. PublicControl->RequestAlign();
  456. }
  457. //---------------------------------------------------------------------------
  458. static Forms::TMonitor * LastMonitor = NULL;
  459. //---------------------------------------------------------------------------
  460. static int __fastcall GetTextHeightAtPixelsPerInch(TForm * Form, int PixelsPerInch)
  461. {
  462. std::unique_ptr<TCanvas> Canvas(new TCanvas());
  463. Canvas->Handle = GetDC(0);
  464. Canvas->Font->Assign(Form->Font);
  465. Canvas->Font->PixelsPerInch = PixelsPerInch; // Must be set BEFORE size
  466. Canvas->Font->Size = Form->Font->Size;
  467. // VCLCOPY: this is what TCustomForm.GetTextHeight does
  468. return Canvas->TextHeight(L"0");
  469. }
  470. //---------------------------------------------------------------------------
  471. class TRescaleComponent : public TComponent
  472. {
  473. public:
  474. __fastcall TRescaleComponent(TRescaleEvent AOnRescale, TObject * AToken, bool AOwnsToken) :
  475. TComponent(NULL)
  476. {
  477. OnRescale = AOnRescale;
  478. Token = AToken;
  479. OwnsToken = AOwnsToken;
  480. }
  481. virtual __fastcall ~TRescaleComponent()
  482. {
  483. if (OwnsToken)
  484. {
  485. delete Token;
  486. }
  487. }
  488. TRescaleEvent OnRescale;
  489. TObject * Token;
  490. bool OwnsToken;
  491. };
  492. //---------------------------------------------------------------------------
  493. void __fastcall SetRescaleFunction(
  494. TComponent * Component, TRescaleEvent OnRescale, TObject * Token, bool OwnsToken)
  495. {
  496. TRescaleComponent * RescaleComponent = new TRescaleComponent(OnRescale, Token, OwnsToken);
  497. RescaleComponent->Name = TRescaleComponent::QualifiedClassName();
  498. Component->InsertComponent(RescaleComponent);
  499. }
  500. //---------------------------------------------------------------------------
  501. static void __fastcall ChangeControlScale(TControl * Control, int M, int D)
  502. {
  503. // VCLCOPY This is what TCustomListView.ChangeScale does,
  504. // but it does that for initial loading only, when ScalingFlags includes sfWidth.
  505. // But ScalingFlags is reset in TControl.ChangeScale (seems like a bug).
  506. TCustomListView * CustomListView = dynamic_cast<TCustomListView *>(Control);
  507. if (CustomListView != NULL)
  508. {
  509. TListView * ListView = reinterpret_cast<TListView *>(CustomListView);
  510. for (int Index = 0; Index < ListView->Columns->Count; Index++)
  511. {
  512. TListColumn * Column = ListView->Columns->Items[Index];
  513. if ((Column->Width != LVSCW_AUTOSIZE) &&
  514. (Column->Width != LVSCW_AUTOSIZE_USEHEADER))
  515. {
  516. Column->Width = MulDiv(Column->Width, M, D);
  517. }
  518. }
  519. }
  520. TCustomCombo * CustomCombo = dynamic_cast<TCustomCombo *>(Control);
  521. if (CustomCombo != NULL)
  522. {
  523. // WORKAROUND
  524. // Some combo boxes (e.g. path box on Copy dialog) visualize selection after rescaling, even when not focused
  525. if (!CustomCombo->Focused())
  526. {
  527. CustomCombo->SelLength = 0;
  528. }
  529. }
  530. for (int Index = 0; Index < Control->ComponentCount; Index++)
  531. {
  532. TComponent * Component = Control->Components[Index];
  533. TControl * ChildControl = dynamic_cast<TControl *>(Component);
  534. if (ChildControl != NULL)
  535. {
  536. ChangeControlScale(ChildControl, M, D);
  537. }
  538. TRescaleComponent * RescaleComponent =
  539. dynamic_cast<TRescaleComponent *>(Component->FindComponent(TRescaleComponent::QualifiedClassName()));
  540. if (RescaleComponent != NULL)
  541. {
  542. RescaleComponent->OnRescale(Component, RescaleComponent->Token);
  543. }
  544. }
  545. Control->Perform(CM_DPICHANGED, M, D);
  546. }
  547. //---------------------------------------------------------------------------
  548. typedef std::pair<int, int> TRatio;
  549. typedef std::map<TRatio, TRatio > TRatioMap;
  550. //---------------------------------------------------------------------------
  551. class TFormRescaleComponent : public TComponent
  552. {
  553. public:
  554. __fastcall TFormRescaleComponent() :
  555. TComponent(NULL)
  556. {
  557. ImplicitRescaleAdded = false;
  558. Rescaling = false;
  559. }
  560. bool Rescaling;
  561. bool ImplicitRescaleAdded;
  562. TRatioMap RatioMap;
  563. };
  564. //---------------------------------------------------------------------------
  565. static TFormRescaleComponent * GetFormRescaleComponent(TForm * Form)
  566. {
  567. TFormRescaleComponent * FormRescaleComponent =
  568. dynamic_cast<TFormRescaleComponent *>(Form->FindComponent(TFormRescaleComponent::QualifiedClassName()));
  569. if (FormRescaleComponent == NULL)
  570. {
  571. FormRescaleComponent = new TFormRescaleComponent();
  572. FormRescaleComponent->Name = TFormRescaleComponent::QualifiedClassName();
  573. Form->InsertComponent(FormRescaleComponent);
  574. }
  575. return FormRescaleComponent;
  576. }
  577. //---------------------------------------------------------------------------
  578. void __fastcall RecordFormImplicitRescale(TForm * Form)
  579. {
  580. if (Form->Scaled)
  581. {
  582. TFormRescaleComponent * FormRescaleComponent = GetFormRescaleComponent(Form);
  583. if (!FormRescaleComponent->ImplicitRescaleAdded)
  584. {
  585. TRatio Ratio;
  586. GetFormScaleRatio(Form, Ratio.first, Ratio.second);
  587. TRatio RescaleKeyRatio(Form->PixelsPerInch, USER_DEFAULT_SCREEN_DPI);
  588. FormRescaleComponent->RatioMap[RescaleKeyRatio] = Ratio;
  589. FormRescaleComponent->ImplicitRescaleAdded = true;
  590. }
  591. }
  592. }
  593. //---------------------------------------------------------------------------
  594. static void GetFormRescaleRatio(TForm * Form, int PixelsPerInch, int & M, int & D)
  595. {
  596. TFormRescaleComponent * FormRescaleComponent = GetFormRescaleComponent(Form);
  597. TRatio ReverseRescaleKeyRatio(Form->PixelsPerInch, PixelsPerInch);
  598. if (FormRescaleComponent->RatioMap.count(ReverseRescaleKeyRatio) > 0)
  599. {
  600. M = FormRescaleComponent->RatioMap[ReverseRescaleKeyRatio].second;
  601. D = FormRescaleComponent->RatioMap[ReverseRescaleKeyRatio].first;
  602. }
  603. else
  604. {
  605. M = GetTextHeightAtPixelsPerInch(Form, PixelsPerInch);
  606. D = GetTextHeightAtPixelsPerInch(Form, Form->PixelsPerInch);
  607. }
  608. TRatio RescaleKeyRatio(PixelsPerInch, Form->PixelsPerInch);
  609. FormRescaleComponent->RatioMap[RescaleKeyRatio] = TRatio(M, D);
  610. }
  611. //---------------------------------------------------------------------------
  612. static void __fastcall ChangeFormPixelsPerInch(TForm * Form, int PixelsPerInch)
  613. {
  614. RecordFormImplicitRescale(Form);
  615. TFormRescaleComponent * FormRescaleComponent = GetFormRescaleComponent(Form);
  616. if ((Form->PixelsPerInch != PixelsPerInch) && // optimization
  617. !FormRescaleComponent->Rescaling)
  618. {
  619. TAutoFlag RescalingFlag(FormRescaleComponent->Rescaling);
  620. int M, D;
  621. GetFormRescaleRatio(Form, PixelsPerInch, M, D);
  622. Form->PixelsPerInch = PixelsPerInch;
  623. TPublicForm * PublicCustomForm = static_cast<TPublicForm *>(Form);
  624. // WORKAROUND
  625. // TCustomForm.ChangeScale scales contraints only after rescaling the size,
  626. // so the unscaled constraints apply to the new size
  627. // (e.g. with TLoginDialog)
  628. std::unique_ptr<TSizeConstraints> Constraints(new TSizeConstraints(NULL));
  629. Constraints->Assign(PublicCustomForm->Constraints);
  630. std::unique_ptr<TSizeConstraints> NoConstraints(new TSizeConstraints(NULL));
  631. PublicCustomForm->Constraints = NoConstraints.get();
  632. // Does not seem to have any effect
  633. PublicCustomForm->DisableAlign();
  634. try
  635. {
  636. int PrevFontHeight = Form->Font->Height;
  637. PublicCustomForm->ChangeScale(M, D);
  638. // Re-rescale by Height as it has higher granularity, hence lower risk of loosing precision due to rounding
  639. Form->Font->Height = MulDiv(PrevFontHeight, M, D);
  640. ChangeControlScale(Form, M, D);
  641. Constraints->MinWidth = MulDiv(Constraints->MinWidth, M, D);
  642. Constraints->MaxWidth = MulDiv(Constraints->MaxWidth, M, D);
  643. Constraints->MinHeight = MulDiv(Constraints->MinHeight, M, D);
  644. Constraints->MaxHeight = MulDiv(Constraints->MaxHeight, M, D);
  645. PublicCustomForm->Constraints = Constraints.get();
  646. }
  647. __finally
  648. {
  649. PublicCustomForm->EnableAlign();
  650. }
  651. }
  652. }
  653. //---------------------------------------------------------------------------
  654. static void __fastcall FormShowingChanged(TForm * Form, TWndMethod WndProc, TMessage & Message)
  655. {
  656. if (IsMainFormLike(Form))
  657. {
  658. if (Form->Showing)
  659. {
  660. if (Application->MainForm != Form)
  661. {
  662. MainLikeForm = Form;
  663. // When main form is hidden, no taskbar button for it is shown and
  664. // this window does not become main window, so there won't be no taskbar
  665. // icon created automatically (by VCL). So we force it manually here.
  666. // This particularly happen for all windows/dialogs of command-line
  667. // operations (e.g. /synchronize) after CreateScpExplorer() happens.
  668. // Also CM_SHOWINGCHANGED happens twice, and the WS_EX_APPWINDOW flag
  669. // from the first call is not preserved, re-applying on the second call too.
  670. // TODO: What about minimize to tray?
  671. int Style = GetWindowLong(Form->Handle, GWL_EXSTYLE);
  672. if (FLAGCLEAR(Style, WS_EX_APPWINDOW))
  673. {
  674. Style |= WS_EX_APPWINDOW;
  675. SetWindowLong(Form->Handle, GWL_EXSTYLE, Style);
  676. }
  677. }
  678. if (Form->Perform(WM_MANAGES_CAPTION, 0, 0) == 0)
  679. {
  680. Form->Caption = FormatFormCaption(Form, Form->Caption);
  681. }
  682. SendMessage(Form->Handle, WM_SETICON, ICON_BIG, reinterpret_cast<long>(Application->Icon->Handle));
  683. }
  684. else
  685. {
  686. if (MainLikeForm == Form)
  687. {
  688. // Do not bother with hiding the WS_EX_APPWINDOW flag
  689. // as the window is closing anyway.
  690. MainLikeForm = NULL;
  691. }
  692. }
  693. }
  694. // Part of following code (but actually not all, TODO), has to happen
  695. // for all windows when VCL main window is hidden (particularly the last branch).
  696. // This is different from above brach, that should happen only for top-level visible window.
  697. if ((Application->MainForm == Form) ||
  698. // this particularly happens if error occurs while main
  699. // window is being shown (e.g. non existent local directory when opening
  700. // explorer)
  701. ((Application->MainForm != NULL) && !Application->MainForm->Visible))
  702. {
  703. if (!Form->Showing)
  704. {
  705. // when closing main form, remember its monitor,
  706. // so that the next form is shown on the same one
  707. LastMonitor = Form->Monitor;
  708. }
  709. else if ((LastMonitor != NULL) && (LastMonitor != Form->Monitor) &&
  710. Form->Showing)
  711. {
  712. // would actually always be poScreenCenter, see _SafeFormCreate
  713. if ((Form->Position == poMainFormCenter) ||
  714. (Form->Position == poOwnerFormCenter) ||
  715. (Form->Position == poScreenCenter))
  716. {
  717. // this would typically be a standalone message box (e.g. /UninstallCleanup or /Update)
  718. // If DPI changes (as the form moves to a monitor with a non-system DPI),
  719. // we have to re-center as the form size changed too.
  720. int PixelsPerInch;
  721. do
  722. {
  723. PixelsPerInch = Form->PixelsPerInch;
  724. // taken from TCustomForm::SetWindowToMonitor
  725. Form->SetBounds(LastMonitor->Left + ((LastMonitor->Width - Form->Width) / 2),
  726. LastMonitor->Top + ((LastMonitor->Height - Form->Height) / 2),
  727. Form->Width, Form->Height);
  728. Form->Position = poDesigned;
  729. }
  730. while (PixelsPerInch != Form->PixelsPerInch);
  731. }
  732. else if ((Form->Position != poDesigned) &&
  733. (Form->Position != poDefaultPosOnly))
  734. {
  735. // we do not expect any other positioning
  736. DebugFail();
  737. }
  738. }
  739. // otherwise it would not get centered
  740. else if ((Form->Position == poMainFormCenter) ||
  741. (Form->Position == poOwnerFormCenter))
  742. {
  743. Form->Position = poScreenCenter;
  744. }
  745. }
  746. if (Form->Showing)
  747. {
  748. // At least on single monitor setup, monitor DPI is 100% but system DPI is higher,
  749. // WM_DPICHANGED is not sent. But VCL scales the form using system DPI.
  750. // Also we have to do this always for implicitly placed forms (poDefaultPosOnly), like TEditorForm,
  751. // as thay never get the WM_DPICHANGED.
  752. // Call this before WndProc below, i.e. before OnShow event (particularly important for the TEditorForm::FormShow.
  753. // GetControlPixelsPerInch would return Form.PixelsPerInch, but we want to get a new DPI of the form monitor.
  754. ChangeFormPixelsPerInch(Form, GetMonitorPixelsPerInch(GetMonitorFromControl(Form)));
  755. }
  756. bool WasFormCenter =
  757. (Form->Position == poMainFormCenter) ||
  758. (Form->Position == poOwnerFormCenter);
  759. WndProc(Message);
  760. // Make sure dialogs are shown on-screen even if center of the main window
  761. // is off-screen. Occurs e.g. if you move the main window so that
  762. // only window title is visible above taksbar.
  763. if (Form->Showing && WasFormCenter && (Form->Position == poDesigned))
  764. {
  765. TRect Rect;
  766. // Reading Form.Left/Form.Top instead here does not work, likely due to some
  767. // bug, when querying TProgressForm opened from TEditorForm (reloading remote file)
  768. GetWindowRect(Form->Handle, &Rect);
  769. int Left = Rect.Left;
  770. int Top = Rect.Top;
  771. TRect WorkArea = Form->Monitor->WorkareaRect;
  772. if (Left + Rect.Width() > WorkArea.Right)
  773. {
  774. Left = WorkArea.Right - Rect.Width();
  775. }
  776. if (Left < WorkArea.Left)
  777. {
  778. Left = WorkArea.Left;
  779. }
  780. if (Top + Rect.Height() > WorkArea.Bottom)
  781. {
  782. Top = WorkArea.Bottom - Rect.Height();
  783. }
  784. if (Top < WorkArea.Top)
  785. {
  786. Top = WorkArea.Top;
  787. }
  788. if ((Left != Rect.Left) ||
  789. (Top != Rect.Top))
  790. {
  791. SetWindowPos(Form->Handle, 0, Left, Top, Rect.Width(), Rect.Height(),
  792. SWP_NOZORDER + SWP_NOACTIVATE);
  793. }
  794. }
  795. }
  796. //---------------------------------------------------------------------------
  797. static TCustomForm * WindowPrintForm = NULL;
  798. static DWORD WindowPrintPrevClick = 0;
  799. static unsigned int WindowPrintClickCount = 0;
  800. //---------------------------------------------------------------------------
  801. void __fastcall CountClicksForWindowPrint(TForm * Form)
  802. {
  803. if ((WinConfiguration != NULL) && WinConfiguration->AllowWindowPrint)
  804. {
  805. DWORD Tick = GetTickCount();
  806. if (WindowPrintForm != Form)
  807. {
  808. WindowPrintForm = Form;
  809. WindowPrintClickCount = 0;
  810. }
  811. if (WindowPrintPrevClick < Tick - 500)
  812. {
  813. WindowPrintClickCount = 0;
  814. }
  815. WindowPrintClickCount++;
  816. WindowPrintPrevClick = Tick;
  817. if (WindowPrintClickCount == 3)
  818. {
  819. WindowPrintClickCount = 0;
  820. TInstantOperationVisualizer Visualizer;
  821. // get the device context of the screen
  822. HDC ScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  823. // and a device context to put it in
  824. HDC MemoryDC = CreateCompatibleDC(ScreenDC);
  825. try
  826. {
  827. bool Sizable = (Form->BorderStyle == bsSizeable);
  828. int Frame = GetSystemMetrics(Sizable ? SM_CXSIZEFRAME : SM_CXFIXEDFRAME) - 1;
  829. int Width = Form->Width - 2*Frame;
  830. int Height = Form->Height - Frame;
  831. // maybe worth checking these are positive values
  832. HBITMAP Bitmap = CreateCompatibleBitmap(ScreenDC, Width, Height);
  833. try
  834. {
  835. // get a new bitmap
  836. HBITMAP OldBitmap = static_cast<HBITMAP>(SelectObject(MemoryDC, Bitmap));
  837. BitBlt(MemoryDC, 0, 0, Width, Height, ScreenDC, Form->Left + Frame, Form->Top, SRCCOPY);
  838. Bitmap = static_cast<HBITMAP>(SelectObject(MemoryDC, OldBitmap));
  839. OpenClipboard(NULL);
  840. try
  841. {
  842. EmptyClipboard();
  843. SetClipboardData(CF_BITMAP, Bitmap);
  844. }
  845. __finally
  846. {
  847. CloseClipboard();
  848. }
  849. }
  850. __finally
  851. {
  852. DeleteObject(Bitmap);
  853. }
  854. }
  855. __finally
  856. {
  857. DeleteDC(MemoryDC);
  858. DeleteDC(ScreenDC);
  859. }
  860. }
  861. }
  862. }
  863. //---------------------------------------------------------------------------
  864. inline void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc,
  865. TMessage & Message)
  866. {
  867. TForm * AForm = dynamic_cast<TForm *>(Form);
  868. DebugAssert(AForm != NULL);
  869. if ((Message.Msg == WM_SYSCOMMAND) &&
  870. (Message.WParam == SC_CONTEXTHELP))
  871. {
  872. FormHelp(Form);
  873. Message.Result = 1;
  874. }
  875. else if (Message.Msg == CM_SHOWINGCHANGED)
  876. {
  877. FormShowingChanged(AForm, WndProc, Message);
  878. }
  879. else if (Message.Msg == WM_SETICON)
  880. {
  881. // WORKAROUND: Swallow VCL attempt to clear the icon from TCustomForm.WMDestroy.
  882. // The clearing still happens to be visualised before the form is hidden.
  883. // On resizable forms, the icon gets replaced by the default application icon,
  884. // on the other forms, the icon disappears and window caption is shifted left.
  885. if (Message.LParam != 0)
  886. {
  887. WndProc(Message);
  888. }
  889. }
  890. else if (Message.Msg == WM_GETDPISCALEDSIZE)
  891. {
  892. WndProc(Message);
  893. int M, D;
  894. GetFormRescaleRatio(AForm, LOWORD(Message.WParam), M, D);
  895. SIZE & Size = *(reinterpret_cast<SIZE *>(Message.LParam));
  896. Size.cx = MulDiv(Size.cx, M, D);
  897. Size.cy = MulDiv(Size.cy, M, D);
  898. Message.Result = TRUE;
  899. }
  900. else if (Message.Msg == WM_DPICHANGED)
  901. {
  902. ChangeFormPixelsPerInch(AForm, LOWORD(Message.WParam));
  903. WndProc(Message);
  904. }
  905. else if ((Message.Msg == WM_LBUTTONDOWN) || (Message.Msg == WM_LBUTTONDBLCLK))
  906. {
  907. CountClicksForWindowPrint(AForm);
  908. WndProc(Message);
  909. }
  910. else
  911. {
  912. WndProc(Message);
  913. }
  914. }
  915. //---------------------------------------------------------------------------
  916. static void __fastcall FormWindowProc(void * Data, TMessage & Message)
  917. {
  918. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  919. DoFormWindowProc(Form, ControlWndProc(Form), Message);
  920. }
  921. //---------------------------------------------------------------------------
  922. void __fastcall InitializeSystemSettings()
  923. {
  924. }
  925. //---------------------------------------------------------------------------
  926. void __fastcall FinalizeSystemSettings()
  927. {
  928. }
  929. //---------------------------------------------------------------------------
  930. #ifdef _DEBUG
  931. void __fastcall VerifyControl(TControl * Control)
  932. {
  933. // If at this time the control has allocated persistence data that are used
  934. // for delayed handle recreation, we are at potential risk, as the future
  935. // de-persistence may overwrite meanwhile changed data.
  936. // For instance it may happen with list view that DPI scaling gets lost.
  937. // This for example happens when the list view has both design time
  938. // ReadOnly = true and some items set. We cannot usually explicitly
  939. // check for the presence of items as while the listview does not have
  940. // a handle allocated, item count querying does not work
  941. // (see also a check below)
  942. if (ControlHasRecreationPersistenceData(Control))
  943. {
  944. // Though if RTL bidi mode is set, the controls are recreated always,
  945. // as we cannot really prevent it. So we force creation here.
  946. DebugAssert(Application->BiDiMode != bdLeftToRight);
  947. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  948. // It must be TWinControl if ControlHasRecreationPersistenceData returned true
  949. if (DebugAlwaysTrue(WinControl != NULL))
  950. {
  951. WinControl->HandleNeeded();
  952. }
  953. }
  954. TCustomListView * ListView = dynamic_cast<TCustomListView *>(Control);
  955. if (ListView != NULL)
  956. {
  957. // As of now the HandleAllocated check is pointless as
  958. // ListView->Items->Count returns 0 when the handle is not allocated yet.
  959. // But we want to know if the implementation ever changes to allocate the handle
  960. // on the call. Because we do not want to allocate a handle here as
  961. // that would change the debug mode behavior from release behavior,
  962. // possibly hiding away some problems.
  963. DebugAssert(!ListView->HandleAllocated() || (ListView->Items->Count == 0));
  964. }
  965. }
  966. #endif
  967. //---------------------------------------------------------------------------
  968. void __fastcall ApplySystemSettingsOnControl(TControl * Control)
  969. {
  970. #ifdef _DEBUG
  971. VerifyControl(Control);
  972. #endif
  973. // WORKAROUND
  974. // VCL does not scale status par panels (while for instance it does
  975. // scale list view headers). Remove this if they ever "fix" this.
  976. // Neither they scale the status bar size if UseSystemFont is true.
  977. // they claim it should scale with font size, but it does not,
  978. // probably because they eat WM_SIZE in TCustomStatusBar.WMSize.
  979. // (used to be issue 83599 on Embarcadero QC, when it was still online)
  980. // Check TCustomStatusBar.ChangeScale() for changes.
  981. // For TBX status bars, this is implemented in TTBXCustomStatusBar.ChangeScale
  982. TStatusBar * StatusBar = dynamic_cast<TStatusBar *>(Control);
  983. if (StatusBar != NULL)
  984. {
  985. // We should have UseSystemFont and bottom alignment set for all status bars.
  986. if (DebugAlwaysTrue(StatusBar->UseSystemFont) &&
  987. DebugAlwaysTrue(StatusBar->Align == alBottom))
  988. {
  989. StatusBar->Height = ScaleByTextHeight(StatusBar, StatusBar->Height);
  990. }
  991. for (int Index = 0; Index < StatusBar->Panels->Count; Index++)
  992. {
  993. TStatusPanel * Panel = StatusBar->Panels->Items[Index];
  994. Panel->Width = ScaleByTextHeight(StatusBar, Panel->Width);
  995. }
  996. }
  997. TCustomListView * ListView = dynamic_cast<TCustomListView *>(Control);
  998. TCustomIEListView * IEListView = dynamic_cast<TCustomIEListView *>(Control);
  999. // For IEListView, this is (somewhat) handled in the TCustomListViewColProperties
  1000. if ((ListView != NULL) && (IEListView == NULL))
  1001. {
  1002. TListView * PublicListView = reinterpret_cast<TListView *>(ListView);
  1003. for (int Index = 0; Index < PublicListView->Columns->Count; Index++)
  1004. {
  1005. TListColumn * Column = ListView->Column[Index];
  1006. Column->MaxWidth = ScaleByTextHeight(ListView, Column->MaxWidth);
  1007. Column->MinWidth = ScaleByTextHeight(ListView, Column->MinWidth);
  1008. }
  1009. }
  1010. // WORKAROUND for lack of public API for mimicking Explorer-style mouse selection
  1011. // See https://stackoverflow.com/q/15750842/850848
  1012. if (IEListView != NULL)
  1013. {
  1014. // It should not be a problem to call the LVM_QUERYINTERFACE
  1015. // on earlier versions of Windows. It should be noop.
  1016. if (IsWin7())
  1017. {
  1018. IListView_Win7 * ListViewIntf = NULL;
  1019. SendMessage(IEListView->Handle, LVM_QUERYINTERFACE, reinterpret_cast<WPARAM>(&IID_IListView_Win7), reinterpret_cast<LPARAM>(&ListViewIntf));
  1020. if (ListViewIntf != NULL)
  1021. {
  1022. ListViewIntf->SetSelectionFlags(1, 1);
  1023. ListViewIntf->Release();
  1024. }
  1025. else
  1026. {
  1027. DebugAssert(IsWine());
  1028. }
  1029. }
  1030. }
  1031. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  1032. if (WinControl != NULL)
  1033. {
  1034. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  1035. {
  1036. ApplySystemSettingsOnControl(WinControl->Controls[Index]);
  1037. }
  1038. }
  1039. }
  1040. //---------------------------------------------------------------------------
  1041. // Settings that must be set as soon as possible.
  1042. void __fastcall UseSystemSettingsPre(TCustomForm * Control)
  1043. {
  1044. LocalSystemSettings(Control);
  1045. TWndMethod WindowProc;
  1046. ((TMethod*)&WindowProc)->Data = Control;
  1047. ((TMethod*)&WindowProc)->Code = FormWindowProc;
  1048. Control->WindowProc = WindowProc;
  1049. if (Control->HelpKeyword.IsEmpty())
  1050. {
  1051. // temporary help keyword to enable F1 key in all forms
  1052. Control->HelpKeyword = L"start";
  1053. }
  1054. ApplySystemSettingsOnControl(Control);
  1055. };
  1056. //---------------------------------------------------------------------------
  1057. static void FlipAnchors(TControl * Control)
  1058. {
  1059. // WORKAROUND VCL flips the Align, but not the Anchors
  1060. TAnchors Anchors = Control->Anchors;
  1061. if (Anchors.Contains(akLeft) != Anchors.Contains(akRight))
  1062. {
  1063. if (Anchors.Contains(akLeft))
  1064. {
  1065. Anchors << akRight;
  1066. Anchors >> akLeft;
  1067. }
  1068. else
  1069. {
  1070. Anchors << akLeft;
  1071. Anchors >> akRight;
  1072. }
  1073. }
  1074. Control->Anchors = Anchors;
  1075. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  1076. if (WinControl != NULL)
  1077. {
  1078. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  1079. {
  1080. FlipAnchors(WinControl->Controls[Index]);
  1081. }
  1082. }
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. // Settings that must be set only after whole form is constructed
  1086. void __fastcall UseSystemSettingsPost(TCustomForm * Control)
  1087. {
  1088. // When showing an early error message
  1089. if (WinConfiguration != NULL)
  1090. {
  1091. UnicodeString FlipStr = LoadStr(FLIP_CHILDREN);
  1092. int FlipChildrenFlag =
  1093. AdjustLocaleFlag(FlipStr, WinConfiguration->FlipChildrenOverride, false, true, false);
  1094. if (static_cast<bool>(FlipChildrenFlag))
  1095. {
  1096. Control->FlipChildren(true);
  1097. FlipAnchors(Control);
  1098. }
  1099. }
  1100. ResetSystemSettings(Control);
  1101. };
  1102. //---------------------------------------------------------------------------
  1103. void __fastcall UseSystemSettings(TCustomForm * Control)
  1104. {
  1105. UseSystemSettingsPre(Control);
  1106. UseSystemSettingsPost(Control);
  1107. };
  1108. //---------------------------------------------------------------------------
  1109. void __fastcall ResetSystemSettings(TCustomForm * /*Control*/)
  1110. {
  1111. // noop
  1112. }
  1113. //---------------------------------------------------------------------------
  1114. struct TShowAsModalStorage
  1115. {
  1116. void * FocusWindowList;
  1117. HWND FocusActiveWindow;
  1118. TFocusState FocusState;
  1119. TCursor SaveCursor;
  1120. int SaveCount;
  1121. };
  1122. //---------------------------------------------------------------------------
  1123. void __fastcall ShowAsModal(TForm * Form, void *& Storage, bool BringToFront)
  1124. {
  1125. SetCorrectFormParent(Form);
  1126. CancelDrag();
  1127. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  1128. ReleaseCapture();
  1129. Application->ModalStarted();
  1130. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  1131. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  1132. AStorage->FocusActiveWindow = GetActiveWindow();
  1133. AStorage->FocusState = SaveFocusState();
  1134. Screen->SaveFocusedList->Insert(0, Screen->FocusedForm);
  1135. Screen->FocusedForm = Form;
  1136. AStorage->SaveCursor = Screen->Cursor;
  1137. // This is particularly used when displaing progress window
  1138. // while downloading via temporary folder
  1139. // (the mouse cursor is yet hidden at this point).
  1140. // While the code was added to workaround the above problem,
  1141. // it is taken from TCustomForm.ShowModal and
  1142. // should have been here ever since.
  1143. Screen->Cursor = crDefault;
  1144. AStorage->SaveCount = Screen->CursorCount;
  1145. AStorage->FocusWindowList = DisableTaskWindows(0);
  1146. // VCLCOPY (TCustomForm::Show)
  1147. Form->Visible = true;
  1148. if (BringToFront)
  1149. {
  1150. Form->BringToFront();
  1151. }
  1152. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  1153. Storage = AStorage;
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  1157. {
  1158. DebugAssert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  1159. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  1160. Storage = NULL;
  1161. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  1162. if (GetActiveWindow() != Form->Handle)
  1163. {
  1164. AStorage->FocusActiveWindow = 0;
  1165. }
  1166. Form->Hide();
  1167. if (Screen->CursorCount == AStorage->SaveCount)
  1168. {
  1169. Screen->Cursor = AStorage->SaveCursor;
  1170. }
  1171. else
  1172. {
  1173. Screen->Cursor = crDefault;
  1174. }
  1175. EnableTaskWindows(AStorage->FocusWindowList);
  1176. if (Screen->SaveFocusedList->Count > 0)
  1177. {
  1178. Screen->FocusedForm = static_cast<TCustomForm *>(Screen->SaveFocusedList->First());
  1179. Screen->SaveFocusedList->Remove(Screen->FocusedForm);
  1180. }
  1181. else
  1182. {
  1183. Screen->FocusedForm = NULL;
  1184. }
  1185. if (AStorage->FocusActiveWindow != 0)
  1186. {
  1187. SetActiveWindow(AStorage->FocusActiveWindow);
  1188. }
  1189. RestoreFocusState(AStorage->FocusState);
  1190. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  1191. Application->ModalFinished();
  1192. delete AStorage;
  1193. }
  1194. //---------------------------------------------------------------------------
  1195. bool __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  1196. {
  1197. bool Result = (Storage != NULL);
  1198. if (Result)
  1199. {
  1200. HideAsModal(Form, Storage);
  1201. }
  1202. return Result;
  1203. }
  1204. //---------------------------------------------------------------------------
  1205. bool __fastcall SelectDirectory(UnicodeString & Path, const UnicodeString Prompt,
  1206. bool PreserveFileName)
  1207. {
  1208. bool Result;
  1209. unsigned int ErrorMode;
  1210. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  1211. try
  1212. {
  1213. UnicodeString Directory;
  1214. UnicodeString FileName;
  1215. // We do not have any real use for the PreserveFileName
  1216. if (!PreserveFileName || DirectoryExists(ApiPath(Path)))
  1217. {
  1218. Directory = Path;
  1219. }
  1220. else
  1221. {
  1222. Directory = ExtractFilePath(Path);
  1223. FileName = ExtractFileName(Path);
  1224. }
  1225. Result = SelectDirectory(Prompt, L"", Directory);
  1226. if (Result)
  1227. {
  1228. Path = Directory;
  1229. if (!FileName.IsEmpty())
  1230. {
  1231. Path = IncludeTrailingBackslash(Path) + FileName;
  1232. }
  1233. }
  1234. }
  1235. __finally
  1236. {
  1237. SetErrorMode(ErrorMode);
  1238. }
  1239. return Result;
  1240. }
  1241. //---------------------------------------------------------------------------
  1242. void SelectDirectoryForEdit(THistoryComboBox * Edit)
  1243. {
  1244. UnicodeString OriginalDirectory = ExpandEnvironmentVariables(Edit->Text);
  1245. UnicodeString Directory = OriginalDirectory;
  1246. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true) &&
  1247. !SamePaths(OriginalDirectory, Directory))
  1248. {
  1249. Edit->Text = Directory;
  1250. if (Edit->OnChange != NULL)
  1251. {
  1252. Edit->OnChange(Edit);
  1253. }
  1254. }
  1255. }
  1256. //---------------------------------------------------------------------------
  1257. bool __fastcall ListViewAnyChecked(TListView * ListView, bool Checked)
  1258. {
  1259. bool AnyChecked = false;
  1260. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1261. {
  1262. if (ListView->Items->Item[Index]->Checked == Checked)
  1263. {
  1264. AnyChecked = true;
  1265. break;
  1266. }
  1267. }
  1268. return AnyChecked;
  1269. }
  1270. //---------------------------------------------------------------------------
  1271. void __fastcall ListViewCheckAll(TListView * ListView,
  1272. TListViewCheckAll CheckAll)
  1273. {
  1274. bool Check;
  1275. if (CheckAll == caToggle)
  1276. {
  1277. Check = ListViewAnyChecked(ListView, false);
  1278. }
  1279. else
  1280. {
  1281. Check = (CheckAll == caCheck);
  1282. }
  1283. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1284. {
  1285. ListView->Items->Item[Index]->Checked = Check;
  1286. }
  1287. }
  1288. //---------------------------------------------------------------------------
  1289. void __fastcall ComboAutoSwitchInitialize(TComboBox * ComboBox)
  1290. {
  1291. int PrevIndex = ComboBox->ItemIndex;
  1292. ComboBox->Items->BeginUpdate();
  1293. try
  1294. {
  1295. ComboBox->Clear();
  1296. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_AUTO));
  1297. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_OFF));
  1298. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_ON));
  1299. }
  1300. __finally
  1301. {
  1302. ComboBox->Items->EndUpdate();
  1303. }
  1304. DebugAssert(PrevIndex < ComboBox->Items->Count);
  1305. ComboBox->ItemIndex = PrevIndex;
  1306. }
  1307. //---------------------------------------------------------------------------
  1308. void __fastcall ComboAutoSwitchLoad(TComboBox * ComboBox, TAutoSwitch Value)
  1309. {
  1310. ComboBox->ItemIndex = 2 - Value;
  1311. if (ComboBox->ItemIndex < 0)
  1312. {
  1313. ComboBox->ItemIndex = 0;
  1314. }
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. TAutoSwitch __fastcall ComboAutoSwitchSave(TComboBox * ComboBox)
  1318. {
  1319. return (TAutoSwitch)(2 - ComboBox->ItemIndex);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. void __fastcall CheckBoxAutoSwitchLoad(TCheckBox * CheckBox, TAutoSwitch Value)
  1323. {
  1324. switch (Value)
  1325. {
  1326. case asOn:
  1327. CheckBox->State = cbChecked;
  1328. break;
  1329. case asOff:
  1330. CheckBox->State = cbUnchecked;
  1331. break;
  1332. default:
  1333. CheckBox->State = cbGrayed;
  1334. break;
  1335. }
  1336. }
  1337. //---------------------------------------------------------------------------
  1338. TAutoSwitch __fastcall CheckBoxAutoSwitchSave(TCheckBox * CheckBox)
  1339. {
  1340. switch (CheckBox->State)
  1341. {
  1342. case cbChecked:
  1343. return asOn;
  1344. case cbUnchecked:
  1345. return asOff;
  1346. default:
  1347. return asAuto;
  1348. }
  1349. }
  1350. //---------------------------------------------------------------------------
  1351. static const wchar_t PathWordDelimiters[] = L"\\/ ;,.\r\n=";
  1352. //---------------------------------------------------------------------------
  1353. static bool IsPathWordDelimiter(wchar_t Ch)
  1354. {
  1355. return (wcschr(PathWordDelimiters, Ch) != NULL);
  1356. }
  1357. //---------------------------------------------------------------------------
  1358. // Windows algorithm is as follows (tested on W2k):
  1359. // right:
  1360. // is_delimiter(current)
  1361. // false:
  1362. // right(left(current) + 1)
  1363. // true:
  1364. // right(right(current) + 1)
  1365. // left:
  1366. // right(left(current) + 1)
  1367. int CALLBACK PathWordBreakProc(wchar_t * Ch, int Current, int Len, int Code)
  1368. {
  1369. int Result;
  1370. UnicodeString ACh(Ch, Len);
  1371. if (Code == WB_ISDELIMITER)
  1372. {
  1373. // we return negacy of what WinAPI docs says
  1374. Result = !IsPathWordDelimiter(ACh[Current + 1]);
  1375. }
  1376. else if (Code == WB_LEFT)
  1377. {
  1378. // skip consecutive delimiters
  1379. while ((Current > 0) &&
  1380. IsPathWordDelimiter(ACh[Current]))
  1381. {
  1382. Current--;
  1383. }
  1384. Result = ACh.SubString(1, Current - 1).LastDelimiter(PathWordDelimiters);
  1385. }
  1386. else if (Code == WB_RIGHT)
  1387. {
  1388. if (Current == 0)
  1389. {
  1390. // will be called again with Current == 1
  1391. Result = 0;
  1392. }
  1393. else
  1394. {
  1395. const wchar_t * P = wcspbrk(ACh.c_str() + Current - 1, PathWordDelimiters);
  1396. if (P == NULL)
  1397. {
  1398. Result = Len;
  1399. }
  1400. else
  1401. {
  1402. Result = P - ACh.c_str() + 1;
  1403. // skip consecutive delimiters
  1404. while ((Result < Len) &&
  1405. IsPathWordDelimiter(ACh[Result + 1]))
  1406. {
  1407. Result++;
  1408. }
  1409. }
  1410. }
  1411. }
  1412. else
  1413. {
  1414. DebugFail();
  1415. Result = 0;
  1416. }
  1417. return Result;
  1418. }
  1419. //---------------------------------------------------------------------------
  1420. class TPathWordBreakProcComponent : public TComponent
  1421. {
  1422. public:
  1423. __fastcall TPathWordBreakProcComponent() :
  1424. TComponent(NULL)
  1425. {
  1426. }
  1427. void __fastcall PathWordBreakEditWindowProc(TMessage & Message);
  1428. TWinControl * WinControl;
  1429. TWndMethod PrevWindowProc;
  1430. };
  1431. //---------------------------------------------------------------------------
  1432. void __fastcall TPathWordBreakProcComponent::PathWordBreakEditWindowProc(TMessage & Message)
  1433. {
  1434. bool Handled = false;
  1435. if (Message.Msg == WM_CHAR)
  1436. {
  1437. // Ctrl+Backspace
  1438. // Ctrl+Backspace is handled in WM_CHAR as any other char,
  1439. // so we have to swallow it here to prevent it getting inserted to the text
  1440. TWMChar & CharMessage = *reinterpret_cast<TWMChar *>(&Message);
  1441. if (CharMessage.CharCode == '\x7F')
  1442. {
  1443. TCustomEdit * Edit = dynamic_cast<TCustomEdit *>(WinControl);
  1444. TCustomComboBox * ComboBox = dynamic_cast<TCustomComboBox *>(WinControl);
  1445. if (((Edit != NULL) && (Edit->SelLength == 0)) ||
  1446. ((ComboBox != NULL) && (ComboBox->SelLength == 0)))
  1447. {
  1448. // See TCustomMaskEdit.SetCursor
  1449. TKeyboardState KeyState;
  1450. GetKeyboardState(KeyState);
  1451. TKeyboardState NewKeyState;
  1452. memset(NewKeyState, 0, sizeof(NewKeyState));
  1453. NewKeyState[VK_CONTROL] = 0x81;
  1454. NewKeyState[VK_SHIFT] = 0x81;
  1455. SetKeyboardState(NewKeyState);
  1456. SendMessage(WinControl->Handle, WM_KEYDOWN, VK_LEFT, 1);
  1457. NewKeyState[VK_SHIFT] = 0;
  1458. NewKeyState[VK_CONTROL] = 0;
  1459. SetKeyboardState(NewKeyState);
  1460. SendMessage(WinControl->Handle, WM_KEYDOWN, VK_DELETE, 1);
  1461. SetKeyboardState(KeyState);
  1462. }
  1463. Message.Result = 1;
  1464. Handled = true;
  1465. }
  1466. }
  1467. if (!Handled)
  1468. {
  1469. if (PrevWindowProc != NULL)
  1470. {
  1471. PrevWindowProc(Message);
  1472. }
  1473. else
  1474. {
  1475. ControlWndProc(WinControl)(Message);
  1476. }
  1477. }
  1478. }
  1479. //---------------------------------------------------------------------------
  1480. class TPublicCustomCombo : public TCustomCombo
  1481. {
  1482. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  1483. };
  1484. //---------------------------------------------------------------------------
  1485. void __fastcall InstallPathWordBreakProc(TWinControl * Control)
  1486. {
  1487. // Since we are setting Application->ModalPopupMode = pmAuto,
  1488. // this has to be called from OnShow, not from constructor anymore,
  1489. // to have any effect
  1490. HWND Wnd;
  1491. if (dynamic_cast<TCustomCombo*>(Control) != NULL)
  1492. {
  1493. TPublicCustomCombo * Combo =
  1494. static_cast<TPublicCustomCombo *>(dynamic_cast<TCustomCombo *>(Control));
  1495. Combo->HandleNeeded();
  1496. Wnd = Combo->EditHandle;
  1497. }
  1498. else
  1499. {
  1500. Wnd = Control->Handle;
  1501. }
  1502. SendMessage(Wnd, EM_SETWORDBREAKPROC, 0, (LPARAM)(EDITWORDBREAKPROC)PathWordBreakProc);
  1503. TPathWordBreakProcComponent * PathWordBreakProcComponent = new TPathWordBreakProcComponent();
  1504. PathWordBreakProcComponent->Name = TPathWordBreakProcComponent::QualifiedClassName();
  1505. Control->InsertComponent(PathWordBreakProcComponent);
  1506. PathWordBreakProcComponent->WinControl = Control;
  1507. // Have to remember the proc because of TTBEditItemViewer.EditWndProc
  1508. PathWordBreakProcComponent->PrevWindowProc =
  1509. // Test is probably redundant, it's there to limit impact of the change.
  1510. ((Control->WindowProc != ControlWndProc(Control)) ? Control->WindowProc : TWndMethod());
  1511. Control->WindowProc = PathWordBreakProcComponent->PathWordBreakEditWindowProc;
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. static void __fastcall RemoveHiddenControlsFromOrder(TControl ** ControlsOrder, int & Count)
  1515. {
  1516. int Shift = 0;
  1517. for (int Index = 0; Index < Count; Index++)
  1518. {
  1519. if (ControlsOrder[Index]->Visible)
  1520. {
  1521. ControlsOrder[Index - Shift] = ControlsOrder[Index];
  1522. }
  1523. else
  1524. {
  1525. Shift++;
  1526. }
  1527. }
  1528. Count -= Shift;
  1529. }
  1530. //---------------------------------------------------------------------------
  1531. void __fastcall SetVerticalControlsOrder(TControl ** ControlsOrder, int Count)
  1532. {
  1533. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1534. if (Count > 0)
  1535. {
  1536. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1537. CommonParent->DisableAlign();
  1538. try
  1539. {
  1540. int Top = 0;
  1541. for (int Index = 0; Index < Count; Index++)
  1542. {
  1543. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1544. if ((Index == 0) || (Top > ControlsOrder[Index]->Top))
  1545. {
  1546. Top = ControlsOrder[Index]->Top;
  1547. }
  1548. }
  1549. for (int Index = 0; Index < Count; Index++)
  1550. {
  1551. TControl * Control = ControlsOrder[Index];
  1552. Control->Top = Top;
  1553. if (((Control->Align == alTop) || (Control->Align == alBottom)) ||
  1554. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom)))
  1555. {
  1556. Top += Control->Height;
  1557. }
  1558. }
  1559. }
  1560. __finally
  1561. {
  1562. CommonParent->EnableAlign();
  1563. }
  1564. }
  1565. }
  1566. //---------------------------------------------------------------------------
  1567. void __fastcall SetHorizontalControlsOrder(TControl ** ControlsOrder, int Count)
  1568. {
  1569. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1570. if (Count > 0)
  1571. {
  1572. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1573. CommonParent->DisableAlign();
  1574. try
  1575. {
  1576. int Left = 0;
  1577. for (int Index = 0; Index < Count; Index++)
  1578. {
  1579. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1580. if ((Index == 0) || (Left > ControlsOrder[Index]->Left))
  1581. {
  1582. Left = ControlsOrder[Index]->Left;
  1583. }
  1584. }
  1585. for (int Index = 0; Index < Count; Index++)
  1586. {
  1587. TControl * Control = ControlsOrder[Index];
  1588. Control->Left = Left;
  1589. if (((Control->Align == alLeft) || (Control->Align == alRight)) ||
  1590. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alRight)))
  1591. {
  1592. Left += Control->Width;
  1593. }
  1594. // vertical alignment has priority, so alBottom-aligned controls start
  1595. // at the very left, even if there are any alLeft/alRight controls.
  1596. // for the reason this code is not necessary in SetVerticalControlsOrder.
  1597. // we could exit the loop as well here.
  1598. if ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom))
  1599. {
  1600. Left = 0;
  1601. }
  1602. }
  1603. }
  1604. __finally
  1605. {
  1606. CommonParent->EnableAlign();
  1607. }
  1608. }
  1609. }
  1610. //---------------------------------------------------------------------------
  1611. void __fastcall MakeNextInTabOrder(TWinControl * Control, TWinControl * After)
  1612. {
  1613. if (After->TabOrder > Control->TabOrder)
  1614. {
  1615. After->TabOrder = Control->TabOrder;
  1616. }
  1617. else if (After->TabOrder < Control->TabOrder - 1)
  1618. {
  1619. After->TabOrder = static_cast<TTabOrder>(Control->TabOrder - 1);
  1620. }
  1621. }
  1622. //---------------------------------------------------------------------------
  1623. void __fastcall CutFormToDesktop(TForm * Form)
  1624. {
  1625. DebugAssert(Form->Monitor != NULL);
  1626. TRect Workarea = Form->Monitor->WorkareaRect;
  1627. if (Form->Top + Form->Height > Workarea.Bottom)
  1628. {
  1629. Form->Height = Workarea.Bottom - Form->Top;
  1630. }
  1631. if (Form->Left + Form->Width >= Workarea.Right)
  1632. {
  1633. Form->Width = Workarea.Right - Form->Left;
  1634. }
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. void __fastcall UpdateFormPosition(TCustomForm * Form, TPosition Position)
  1638. {
  1639. if ((Position == poScreenCenter) ||
  1640. (Position == poOwnerFormCenter) ||
  1641. (Position == poMainFormCenter))
  1642. {
  1643. TCustomForm * CenterForm = NULL;
  1644. if ((Position == poOwnerFormCenter) ||
  1645. (Position == poMainFormCenter))
  1646. {
  1647. CenterForm = Application->MainForm;
  1648. if ((Position == poOwnerFormCenter) &&
  1649. (dynamic_cast<TCustomForm*>(Form->Owner) != NULL))
  1650. {
  1651. CenterForm = dynamic_cast<TCustomForm*>(Form->Owner);
  1652. }
  1653. }
  1654. TRect Bounds = Form->BoundsRect;
  1655. int X, Y;
  1656. if (CenterForm != NULL)
  1657. {
  1658. X = ((((TForm *)CenterForm)->Width - Bounds.Width()) / 2) +
  1659. ((TForm *)CenterForm)->Left;
  1660. Y = ((((TForm *)CenterForm)->Height - Bounds.Height()) / 2) +
  1661. ((TForm *)CenterForm)->Top;
  1662. }
  1663. else
  1664. {
  1665. X = (Form->Monitor->Width - Bounds.Width()) / 2;
  1666. Y = (Form->Monitor->Height - Bounds.Height()) / 2;
  1667. }
  1668. if (X < 0)
  1669. {
  1670. X = 0;
  1671. }
  1672. if (Y < 0)
  1673. {
  1674. Y = 0;
  1675. }
  1676. Form->SetBounds(X, Y, Bounds.Width(), Bounds.Height());
  1677. }
  1678. }
  1679. //---------------------------------------------------------------------------
  1680. void __fastcall ResizeForm(TCustomForm * Form, int Width, int Height)
  1681. {
  1682. // This has to be called only after DoFormWindowProc(CM_SHOWINGCHANGED),
  1683. // so that a correct monitor is considered.
  1684. // Note that we cannot use LastMonitor(), as ResizeForm is also called from
  1685. // TConsoleDialog::DoAdjustWindow, where we need to use the actual monitor
  1686. // (in case user moves the console window to a different monitor,
  1687. // than where a main window is [no matter how unlikely that is])
  1688. TRect WorkareaRect = Form->Monitor->WorkareaRect;
  1689. if (Height > WorkareaRect.Height())
  1690. {
  1691. Height = WorkareaRect.Height();
  1692. }
  1693. if (Width > WorkareaRect.Width())
  1694. {
  1695. Width = WorkareaRect.Width();
  1696. }
  1697. if (Height < Form->Constraints->MinHeight)
  1698. {
  1699. Height = Form->Constraints->MinHeight;
  1700. }
  1701. if (Width < Form->Constraints->MinWidth)
  1702. {
  1703. Width = Form->Constraints->MinWidth;
  1704. }
  1705. TRect Bounds = Form->BoundsRect;
  1706. int Top = Bounds.Top + ((Bounds.Height() - Height) / 2);
  1707. int Left = Bounds.Left + ((Bounds.Width() - Width) / 2);
  1708. if (Top + Height > WorkareaRect.Bottom)
  1709. {
  1710. Top = WorkareaRect.Bottom - Height;
  1711. }
  1712. if (Left + Width >= WorkareaRect.Right)
  1713. {
  1714. Left = WorkareaRect.Right - Width;
  1715. }
  1716. // WorkareaRect.Left is not 0, when secondary monitor is placed left of primary one.
  1717. // Similarly for WorkareaRect.Top.
  1718. if (Top < WorkareaRect.Top)
  1719. {
  1720. Top = WorkareaRect.Top;
  1721. }
  1722. if (Left < WorkareaRect.Left)
  1723. {
  1724. Left = WorkareaRect.Left;
  1725. }
  1726. Form->SetBounds(Left, Top, Width, Height);
  1727. Bounds = Form->BoundsRect;
  1728. // due to constraints, form can remain larger, make sure it is centered although
  1729. Left = Bounds.Left + ((Width - Bounds.Width()) / 2);
  1730. Top = Bounds.Top + ((Height - Bounds.Height()) / 2);
  1731. Form->SetBounds(Left, Top, Width, Height);
  1732. }
  1733. //---------------------------------------------------------------------------
  1734. TComponent * __fastcall GetFormOwner()
  1735. {
  1736. if (Screen->ActiveForm != NULL)
  1737. {
  1738. return Screen->ActiveForm;
  1739. }
  1740. else
  1741. {
  1742. return Application;
  1743. }
  1744. }
  1745. //---------------------------------------------------------------------------
  1746. void __fastcall SetCorrectFormParent(TForm * /*Form*/)
  1747. {
  1748. // noop
  1749. // remove
  1750. }
  1751. //---------------------------------------------------------------------------
  1752. void __fastcall InvokeHelp(TWinControl * Control)
  1753. {
  1754. DebugAssert(Control != NULL);
  1755. HELPINFO HelpInfo;
  1756. HelpInfo.cbSize = sizeof(HelpInfo);
  1757. HelpInfo.iContextType = HELPINFO_WINDOW;
  1758. HelpInfo.iCtrlId = 0;
  1759. HelpInfo.hItemHandle = Control->Handle;
  1760. HelpInfo.dwContextId = 0;
  1761. HelpInfo.MousePos.x = 0;
  1762. HelpInfo.MousePos.y = 0;
  1763. SendMessage(Control->Handle, WM_HELP, NULL, reinterpret_cast<long>(&HelpInfo));
  1764. }
  1765. //---------------------------------------------------------------------------
  1766. //---------------------------------------------------------------------------
  1767. static void __fastcall FocusableLabelCanvas(TStaticText * StaticText,
  1768. TCanvas ** ACanvas, TRect & R)
  1769. {
  1770. TCanvas * Canvas = CreateControlCanvas(StaticText);
  1771. try
  1772. {
  1773. R = StaticText->ClientRect;
  1774. TSize TextSize;
  1775. if (StaticText->AutoSize)
  1776. {
  1777. // We possibly could use the same code as in !AutoSize branch,
  1778. // keeping this to avoid problems in existing code
  1779. UnicodeString Caption = StaticText->Caption;
  1780. bool AccelChar = false;
  1781. if (StaticText->ShowAccelChar)
  1782. {
  1783. Caption = StripHotkey(Caption);
  1784. AccelChar = (Caption != StaticText->Caption);
  1785. }
  1786. TextSize = Canvas->TextExtent(Caption);
  1787. DebugAssert(StaticText->BorderStyle == sbsNone); // not taken into account
  1788. if (AccelChar)
  1789. {
  1790. TextSize.cy += 2;
  1791. }
  1792. }
  1793. else
  1794. {
  1795. TRect TextRect;
  1796. SetRect(&TextRect, 0, 0, StaticText->Width, 0);
  1797. DrawText(Canvas->Handle, StaticText->Caption.c_str(), -1, &TextRect,
  1798. DT_CALCRECT | DT_WORDBREAK |
  1799. StaticText->DrawTextBiDiModeFlagsReadingOnly());
  1800. TextSize = TextRect.GetSize();
  1801. }
  1802. R.Bottom = R.Top + TextSize.cy;
  1803. // Should call ChangeBiDiModeAlignment when UseRightToLeftAlignment(),
  1804. // but the label seems to draw the text wrongly aligned, even though
  1805. // the alignment is correctly flipped in TCustomStaticText.CreateParams
  1806. switch (StaticText->Alignment)
  1807. {
  1808. case taLeftJustify:
  1809. R.Right = R.Left + TextSize.cx;
  1810. break;
  1811. case taRightJustify:
  1812. R.Left = Max(0, R.Right - TextSize.cx);
  1813. break;
  1814. case taCenter:
  1815. {
  1816. DebugFail(); // not used branch, possibly untested
  1817. int Diff = R.Width() - TextSize.cx;
  1818. R.Left += Diff / 2;
  1819. R.Right -= Diff - (Diff / 2);
  1820. }
  1821. break;
  1822. }
  1823. }
  1824. __finally
  1825. {
  1826. if (ACanvas == NULL)
  1827. {
  1828. delete Canvas;
  1829. }
  1830. }
  1831. if (ACanvas != NULL)
  1832. {
  1833. *ACanvas = Canvas;
  1834. }
  1835. }
  1836. //---------------------------------------------------------------------------
  1837. static void __fastcall FocusableLabelWindowProc(void * Data, TMessage & Message,
  1838. bool & Clicked)
  1839. {
  1840. Clicked = false;
  1841. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1842. if (Message.Msg == WM_LBUTTONDOWN)
  1843. {
  1844. StaticText->SetFocus();
  1845. // in case the action takes long, make sure focus is shown immediately
  1846. UpdateWindow(StaticText->Handle);
  1847. Clicked = true;
  1848. Message.Result = 1;
  1849. }
  1850. else if (Message.Msg == WM_RBUTTONDOWN)
  1851. {
  1852. StaticText->SetFocus();
  1853. Message.Result = 1;
  1854. }
  1855. else if (Message.Msg == WM_CHAR)
  1856. {
  1857. if (reinterpret_cast<TWMChar &>(Message).CharCode == L' ')
  1858. {
  1859. Clicked = true;
  1860. Message.Result = 1;
  1861. }
  1862. else
  1863. {
  1864. ControlWndProc(StaticText)(Message);
  1865. }
  1866. }
  1867. else if (Message.Msg == CM_DIALOGCHAR)
  1868. {
  1869. if (StaticText->CanFocus() && StaticText->ShowAccelChar &&
  1870. IsAccel(reinterpret_cast<TCMDialogChar &>(Message).CharCode, StaticText->Caption))
  1871. {
  1872. StaticText->SetFocus();
  1873. // in case the action takes long, make sure focus is shown immediately
  1874. UpdateWindow(StaticText->Handle);
  1875. Clicked = true;
  1876. Message.Result = 1;
  1877. }
  1878. else
  1879. {
  1880. ControlWndProc(StaticText)(Message);
  1881. }
  1882. }
  1883. else
  1884. {
  1885. ControlWndProc(StaticText)(Message);
  1886. }
  1887. if (Message.Msg == WM_PAINT)
  1888. {
  1889. TRect R;
  1890. TCanvas * Canvas;
  1891. FocusableLabelCanvas(StaticText, &Canvas, R);
  1892. try
  1893. {
  1894. if (StaticText->Focused())
  1895. {
  1896. Canvas->DrawFocusRect(R);
  1897. }
  1898. else if ((StaticText->Font->Color != LinkColor) && // LinkActionLabel and LinkLabel
  1899. !EndsStr(LinkAppLabelMark, StaticText->Caption)) // LinkAppLabel
  1900. {
  1901. Canvas->Pen->Style = psDot;
  1902. Canvas->Brush->Style = bsClear;
  1903. if (!StaticText->Enabled)
  1904. {
  1905. Canvas->Pen->Color = clBtnHighlight;
  1906. Canvas->MoveTo(R.Left + 1 + 1, R.Bottom);
  1907. Canvas->LineTo(R.Right + 1, R.Bottom);
  1908. Canvas->Pen->Color = clGrayText;
  1909. }
  1910. Canvas->MoveTo(R.Left, R.Bottom - 1);
  1911. Canvas->LineTo(R.Right, R.Bottom - 1);
  1912. }
  1913. }
  1914. __finally
  1915. {
  1916. delete Canvas;
  1917. }
  1918. }
  1919. else if ((Message.Msg == WM_SETFOCUS) || (Message.Msg == WM_KILLFOCUS) ||
  1920. (Message.Msg == CM_ENABLEDCHANGED))
  1921. {
  1922. StaticText->Invalidate();
  1923. }
  1924. }
  1925. //---------------------------------------------------------------------------
  1926. static THintWindow * PersistentHintWindow = NULL;
  1927. static TControl * PersistentHintControl = NULL;
  1928. //---------------------------------------------------------------------------
  1929. void __fastcall CancelPersistentHint()
  1930. {
  1931. if (PersistentHintWindow != NULL)
  1932. {
  1933. PersistentHintControl = NULL;
  1934. SAFE_DESTROY(PersistentHintWindow);
  1935. }
  1936. }
  1937. //---------------------------------------------------------------------------
  1938. void __fastcall ShowPersistentHint(TControl * Control, TPoint HintPos)
  1939. {
  1940. CancelPersistentHint();
  1941. THintInfo HintInfo;
  1942. HintInfo.HintControl = Control;
  1943. HintInfo.HintPos = HintPos;
  1944. HintInfo.HintMaxWidth = GetParentForm(Control)->Monitor->Width;
  1945. HintInfo.HintColor = Application->HintColor;
  1946. HintInfo.HintStr = GetShortHint(Control->Hint);
  1947. HintInfo.HintData = NULL;
  1948. bool CanShow = true;
  1949. if (Application->OnShowHint != NULL)
  1950. {
  1951. Application->OnShowHint(HintInfo.HintStr, CanShow, HintInfo);
  1952. }
  1953. if (CanShow)
  1954. {
  1955. PersistentHintControl = Control;
  1956. PersistentHintWindow = new TScreenTipHintWindow(Application);
  1957. PersistentHintWindow->BiDiMode = Control->BiDiMode;
  1958. PersistentHintWindow->Color = HintInfo.HintColor;
  1959. TRect HintWinRect;
  1960. if (HintInfo.HintMaxWidth < Control->Width)
  1961. {
  1962. HintInfo.HintMaxWidth = Control->Width;
  1963. }
  1964. HintWinRect = PersistentHintWindow->CalcHintRect(
  1965. HintInfo.HintMaxWidth, HintInfo.HintStr, HintInfo.HintData);
  1966. OffsetRect(HintWinRect, HintInfo.HintPos.x, HintInfo.HintPos.y);
  1967. // TODO: right align window placement for UseRightToLeftAlignment, see Forms.pas
  1968. PersistentHintWindow->ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
  1969. }
  1970. }
  1971. //---------------------------------------------------------------------------
  1972. static void __fastcall HintLabelWindowProc(void * Data, TMessage & Message)
  1973. {
  1974. bool Clicked = false;
  1975. bool Cancel = false;
  1976. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1977. if (Message.Msg == CM_HINTSHOW)
  1978. {
  1979. TCMHintShow & HintShow = reinterpret_cast<TCMHintShow &>(Message);
  1980. if (PersistentHintControl == StaticText)
  1981. {
  1982. // do not allow standard hint when persistent is already shown
  1983. HintShow.Result = 1;
  1984. }
  1985. else
  1986. {
  1987. HintShow.HintInfo->HideTimeout = 100000; // never
  1988. }
  1989. }
  1990. else if (Message.Msg == CN_KEYDOWN)
  1991. {
  1992. if ((reinterpret_cast<TWMKey &>(Message).CharCode == VK_ESCAPE) &&
  1993. (PersistentHintControl == StaticText))
  1994. {
  1995. CancelPersistentHint();
  1996. StaticText->Invalidate();
  1997. Message.Result = 1;
  1998. }
  1999. else
  2000. {
  2001. FocusableLabelWindowProc(Data, Message, Clicked);
  2002. }
  2003. }
  2004. else
  2005. {
  2006. FocusableLabelWindowProc(Data, Message, Clicked);
  2007. }
  2008. if (Message.Msg == CM_CANCELMODE)
  2009. {
  2010. TCMCancelMode & CancelMessage = (TCMCancelMode&)Message;
  2011. if ((CancelMessage.Sender != StaticText) &&
  2012. (CancelMessage.Sender != PersistentHintWindow))
  2013. {
  2014. Cancel = true;
  2015. }
  2016. }
  2017. if ((Message.Msg == WM_DESTROY) || (Message.Msg == WM_KILLFOCUS))
  2018. {
  2019. Cancel = true;
  2020. }
  2021. if (Cancel && (PersistentHintControl == StaticText))
  2022. {
  2023. CancelPersistentHint();
  2024. }
  2025. if (Clicked && (PersistentHintControl != StaticText))
  2026. {
  2027. TRect R;
  2028. TPoint HintPos;
  2029. FocusableLabelCanvas(StaticText, NULL, R);
  2030. HintPos.y = R.Bottom - R.Top;
  2031. HintPos.x = R.Left;
  2032. ShowPersistentHint(StaticText, StaticText->ClientToScreen(HintPos));
  2033. }
  2034. }
  2035. //---------------------------------------------------------------------------
  2036. void __fastcall HintLabel(TStaticText * StaticText, UnicodeString Hint)
  2037. {
  2038. // Currently all are right-justified, when other alignment is used,
  2039. // test respective branches in FocusableLabelCanvas.
  2040. DebugAssert(StaticText->Alignment == taRightJustify);
  2041. // With right-justify, it has to be off. We may not notice on original
  2042. // English version, results will differ with translations only
  2043. DebugAssert(!StaticText->AutoSize);
  2044. StaticText->ParentFont = true;
  2045. if (!Hint.IsEmpty())
  2046. {
  2047. StaticText->Hint = Hint;
  2048. }
  2049. StaticText->ShowHint = true;
  2050. StaticText->Cursor = crHandPoint;
  2051. TWndMethod WindowProc;
  2052. ((TMethod*)&WindowProc)->Data = StaticText;
  2053. ((TMethod*)&WindowProc)->Code = HintLabelWindowProc;
  2054. StaticText->WindowProc = WindowProc;
  2055. }
  2056. //---------------------------------------------------------------------------
  2057. static void __fastcall ComboBoxFixWindowProc(void * Data, TMessage & Message)
  2058. {
  2059. // it is TCustomComboxBox, but the properties are published only by TComboBox
  2060. TComboBox * ComboBox = static_cast<TComboBox *>(Data);
  2061. if (Message.Msg == WM_SIZE)
  2062. {
  2063. UnicodeString Text = ComboBox->Text;
  2064. try
  2065. {
  2066. ControlWndProc(ComboBox)(Message);
  2067. }
  2068. __finally
  2069. {
  2070. // workaround for bug in combo box, that causes it to change text to any
  2071. // item from drop down list which starts with current text,
  2072. // after control is resized (unless the text is in drop down list as well)
  2073. ComboBox->Text = Text;
  2074. // hide selection, which is wrongly shown when form is resized, even when the box has not focus
  2075. if (!ComboBox->Focused())
  2076. {
  2077. ComboBox->SelLength = 0;
  2078. }
  2079. }
  2080. }
  2081. else
  2082. {
  2083. ControlWndProc(ComboBox)(Message);
  2084. }
  2085. }
  2086. //---------------------------------------------------------------------------
  2087. void __fastcall FixComboBoxResizeBug(TCustomComboBox * ComboBox)
  2088. {
  2089. TWndMethod WindowProc;
  2090. ((TMethod*)&WindowProc)->Data = ComboBox;
  2091. ((TMethod*)&WindowProc)->Code = ComboBoxFixWindowProc;
  2092. ComboBox->WindowProc = WindowProc;
  2093. }
  2094. //---------------------------------------------------------------------------
  2095. static void __fastcall LinkLabelClick(TStaticText * StaticText)
  2096. {
  2097. if (StaticText->OnClick != NULL)
  2098. {
  2099. StaticText->OnClick(StaticText);
  2100. }
  2101. else
  2102. {
  2103. OpenBrowser(StaticText->Caption);
  2104. }
  2105. }
  2106. //---------------------------------------------------------------------------
  2107. static void __fastcall LinkLabelWindowProc(void * Data, TMessage & Message)
  2108. {
  2109. bool Clicked = false;
  2110. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  2111. if (Message.Msg == WM_CONTEXTMENU)
  2112. {
  2113. TWMContextMenu & ContextMenu = reinterpret_cast<TWMContextMenu &>(Message);
  2114. if ((ContextMenu.Pos.x < 0) && (ContextMenu.Pos.y < 0))
  2115. {
  2116. TRect R;
  2117. FocusableLabelCanvas(StaticText, NULL, R);
  2118. TPoint P = StaticText->ClientToScreen(TPoint(R.Left, R.Bottom));
  2119. ContextMenu.Pos.x = static_cast<short>(P.x);
  2120. ContextMenu.Pos.y = static_cast<short>(P.y);
  2121. }
  2122. }
  2123. else if (Message.Msg == WM_KEYDOWN)
  2124. {
  2125. TWMKey & Key = reinterpret_cast<TWMKey &>(Message);
  2126. if ((GetKeyState(VK_CONTROL) < 0) && (Key.CharCode == L'C'))
  2127. {
  2128. TInstantOperationVisualizer Visualizer;
  2129. CopyToClipboard(StaticText->Caption);
  2130. Message.Result = 1;
  2131. }
  2132. else
  2133. {
  2134. FocusableLabelWindowProc(Data, Message, Clicked);
  2135. }
  2136. }
  2137. FocusableLabelWindowProc(Data, Message, Clicked);
  2138. if (Message.Msg == WM_DESTROY)
  2139. {
  2140. delete StaticText->PopupMenu;
  2141. DebugAssert(StaticText->PopupMenu == NULL);
  2142. }
  2143. if (Clicked)
  2144. {
  2145. LinkLabelClick(StaticText);
  2146. }
  2147. }
  2148. //---------------------------------------------------------------------------
  2149. static void __fastcall LinkLabelContextMenuClick(void * Data, TObject * Sender)
  2150. {
  2151. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  2152. TMenuItem * MenuItem = dynamic_cast<TMenuItem *>(Sender);
  2153. DebugAssert(MenuItem != NULL);
  2154. if (MenuItem->Tag == 0)
  2155. {
  2156. LinkLabelClick(StaticText);
  2157. }
  2158. else
  2159. {
  2160. TInstantOperationVisualizer Visualizer;
  2161. CopyToClipboard(StaticText->Caption);
  2162. }
  2163. }
  2164. //---------------------------------------------------------------------------
  2165. static void __fastcall DoLinkLabel(TStaticText * StaticText)
  2166. {
  2167. StaticText->Transparent = false;
  2168. StaticText->ParentFont = true;
  2169. StaticText->Cursor = crHandPoint;
  2170. TWndMethod WindowProc;
  2171. ((TMethod*)&WindowProc)->Data = StaticText;
  2172. ((TMethod*)&WindowProc)->Code = LinkLabelWindowProc;
  2173. StaticText->WindowProc = WindowProc;
  2174. }
  2175. //---------------------------------------------------------------------------
  2176. void __fastcall LinkLabel(TStaticText * StaticText, UnicodeString Url,
  2177. TNotifyEvent OnEnter)
  2178. {
  2179. DoLinkLabel(StaticText);
  2180. StaticText->Font->Style = StaticText->Font->Style << fsUnderline;
  2181. reinterpret_cast<TButton*>(StaticText)->OnEnter = OnEnter;
  2182. if (!Url.IsEmpty())
  2183. {
  2184. StaticText->Caption = Url;
  2185. }
  2186. bool IsUrl = IsHttpOrHttpsUrl(StaticText->Caption);
  2187. if (IsUrl)
  2188. {
  2189. DebugAssert(StaticText->PopupMenu == NULL);
  2190. StaticText->PopupMenu = new TPopupMenu(StaticText);
  2191. try
  2192. {
  2193. TNotifyEvent ContextMenuOnClick;
  2194. ((TMethod*)&ContextMenuOnClick)->Data = StaticText;
  2195. ((TMethod*)&ContextMenuOnClick)->Code = LinkLabelContextMenuClick;
  2196. TMenuItem * Item;
  2197. Item = new TMenuItem(StaticText->PopupMenu);
  2198. Item->Caption = LoadStr(URL_LINK_OPEN);
  2199. Item->Tag = 0;
  2200. Item->ShortCut = ShortCut(L' ', TShiftState());
  2201. Item->OnClick = ContextMenuOnClick;
  2202. StaticText->PopupMenu->Items->Add(Item);
  2203. Item = new TMenuItem(StaticText->PopupMenu);
  2204. Item->Caption = LoadStr(EDIT_COPY);
  2205. Item->Tag = 1;
  2206. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  2207. Item->OnClick = ContextMenuOnClick;
  2208. StaticText->PopupMenu->Items->Add(Item);
  2209. }
  2210. catch(...)
  2211. {
  2212. delete StaticText->PopupMenu;
  2213. DebugAssert(StaticText->PopupMenu == NULL);
  2214. throw;
  2215. }
  2216. }
  2217. StaticText->Font->Color = LinkColor;
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. void __fastcall LinkActionLabel(TStaticText * StaticText)
  2221. {
  2222. DoLinkLabel(StaticText);
  2223. StaticText->Font->Color = LinkColor;
  2224. }
  2225. //---------------------------------------------------------------------------
  2226. void __fastcall LinkAppLabel(TStaticText * StaticText)
  2227. {
  2228. DoLinkLabel(StaticText);
  2229. StaticText->Caption = StaticText->Caption + LinkAppLabelMark;
  2230. }
  2231. //---------------------------------------------------------------------------
  2232. static void __fastcall HotTrackLabelMouseEnter(void * /*Data*/, TObject * Sender)
  2233. {
  2234. reinterpret_cast<TLabel *>(Sender)->Font->Color = clBlue;
  2235. }
  2236. //---------------------------------------------------------------------------
  2237. static void __fastcall HotTrackLabelMouseLeave(void * /*Data*/, TObject * Sender)
  2238. {
  2239. reinterpret_cast<TLabel *>(Sender)->ParentFont = true;
  2240. }
  2241. //---------------------------------------------------------------------------
  2242. void __fastcall HotTrackLabel(TLabel * Label)
  2243. {
  2244. DebugAssert(Label->OnMouseEnter == NULL);
  2245. DebugAssert(Label->OnMouseLeave == NULL);
  2246. Label->OnMouseEnter = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseEnter);
  2247. Label->OnMouseLeave = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseLeave);
  2248. }
  2249. //---------------------------------------------------------------------------
  2250. void __fastcall SetLabelHintPopup(TLabel * Label, const UnicodeString & Hint)
  2251. {
  2252. Label->Caption = Hint;
  2253. Label->Hint = Hint;
  2254. TRect Rect(0, 0, Label->Width, 0);
  2255. TScreenTipHintWindow::CalcHintTextRect(Label, Label->Canvas, Rect, Label->Caption);
  2256. Label->ShowHint = (Rect.Bottom > Label->Height);
  2257. }
  2258. //---------------------------------------------------------------------------
  2259. bool __fastcall HasLabelHintPopup(TControl * Control, const UnicodeString & HintStr)
  2260. {
  2261. TLabel * HintLabel = dynamic_cast<TLabel *>(Control);
  2262. return (HintLabel != NULL) && (GetShortHint(HintLabel->Caption) == HintStr);
  2263. }
  2264. //---------------------------------------------------------------------------
  2265. Forms::TMonitor * __fastcall FormMonitor(TCustomForm * Form)
  2266. {
  2267. Forms::TMonitor * Result;
  2268. if ((Application->MainForm != NULL) && (Application->MainForm != Form))
  2269. {
  2270. Result = Application->MainForm->Monitor;
  2271. }
  2272. else if (LastMonitor != NULL)
  2273. {
  2274. Result = LastMonitor;
  2275. }
  2276. else
  2277. {
  2278. int i = 0;
  2279. while ((i < Screen->MonitorCount) && !Screen->Monitors[i]->Primary)
  2280. {
  2281. i++;
  2282. }
  2283. DebugAssert(Screen->Monitors[i]->Primary);
  2284. Result = Screen->Monitors[i];
  2285. }
  2286. return Result;
  2287. }
  2288. //---------------------------------------------------------------------------
  2289. int __fastcall GetLastMonitor()
  2290. {
  2291. if (LastMonitor != NULL)
  2292. {
  2293. return LastMonitor->MonitorNum;
  2294. }
  2295. else
  2296. {
  2297. return -1;
  2298. }
  2299. }
  2300. //---------------------------------------------------------------------------
  2301. void __fastcall SetLastMonitor(int MonitorNum)
  2302. {
  2303. if ((MonitorNum >= 0) && (MonitorNum < Screen->MonitorCount))
  2304. {
  2305. LastMonitor = Screen->Monitors[MonitorNum];
  2306. }
  2307. else
  2308. {
  2309. LastMonitor = NULL;
  2310. }
  2311. }
  2312. //---------------------------------------------------------------------------
  2313. TForm * __fastcall _SafeFormCreate(TMetaClass * FormClass, TComponent * Owner)
  2314. {
  2315. TForm * Form;
  2316. if (Owner == NULL)
  2317. {
  2318. Owner = GetFormOwner();
  2319. }
  2320. // If there is no main form yet, make this one main.
  2321. // This:
  2322. // - Makes other forms (dialogs invoked from this one),
  2323. // be placed on the same monitor (otherwise all new forms get placed
  2324. // on primary monitor)
  2325. // - Triggers MainForm-specific code in DoFormWindowProc.
  2326. // - Shows button on taskbar
  2327. if (Application->MainForm == NULL)
  2328. {
  2329. Application->CreateForm(FormClass, &Form);
  2330. DebugAssert(Application->MainForm == Form);
  2331. }
  2332. else
  2333. {
  2334. Form = dynamic_cast<TForm *>(Construct(FormClass, Owner));
  2335. DebugAssert(Form != NULL);
  2336. }
  2337. return Form;
  2338. }
  2339. //---------------------------------------------------------------------------
  2340. bool __fastcall SupportsSplitButton()
  2341. {
  2342. return (Win32MajorVersion >= 6);
  2343. }
  2344. //---------------------------------------------------------------------------
  2345. static TButton * __fastcall FindStandardButton(TWinControl * Control, bool Default)
  2346. {
  2347. TButton * Result = NULL;
  2348. int Index = 0;
  2349. while ((Result == NULL) && (Index < Control->ControlCount))
  2350. {
  2351. TControl * ChildControl = Control->Controls[Index];
  2352. TButton * Button = dynamic_cast<TButton *>(ChildControl);
  2353. if ((Button != NULL) && (Default ? Button->Default : Button->Cancel))
  2354. {
  2355. Result = Button;
  2356. }
  2357. else
  2358. {
  2359. TWinControl * WinControl = dynamic_cast<TWinControl *>(ChildControl);
  2360. if (WinControl != NULL)
  2361. {
  2362. Result = FindStandardButton(WinControl, Default);
  2363. }
  2364. }
  2365. Index++;
  2366. }
  2367. return Result;
  2368. }
  2369. //---------------------------------------------------------------------------
  2370. TModalResult __fastcall DefaultResult(TCustomForm * Form, TButton * DefaultButton)
  2371. {
  2372. // The point of this is to avoid hardcoding mrOk when checking dialog results.
  2373. // Previously we used != mrCancel instead, as mrCancel is more reliable,
  2374. // being automatically used for Esc/X buttons (and hence kind of forced to be used
  2375. // for Cancel buttons). But that failed to be reliable in the end, for
  2376. // ModalResult being mrNone, when Windows session is being logged off.
  2377. // We interpreted mrNone as OK, causing lots of troubles.
  2378. TModalResult Result = mrNone;
  2379. TButton * Button = FindStandardButton(Form, true);
  2380. if (DebugAlwaysTrue(Button != NULL))
  2381. {
  2382. Result = Button->ModalResult;
  2383. }
  2384. if (Result == mrNone)
  2385. {
  2386. DebugAssert((DefaultButton != NULL) && (DefaultButton->ModalResult != mrNone));
  2387. Result = DefaultButton->ModalResult;
  2388. }
  2389. else
  2390. {
  2391. // If default button fallback was provided,
  2392. // make sure it is the default button we actually detected
  2393. DebugAssert((DefaultButton == NULL) || (Button == DefaultButton));
  2394. }
  2395. return Result;
  2396. }
  2397. //---------------------------------------------------------------------------
  2398. void __fastcall DefaultButton(TButton * Button, bool Default)
  2399. {
  2400. // default property setter does not have guard for "the same value"
  2401. if (Button->Default != Default)
  2402. {
  2403. Button->Default = Default;
  2404. }
  2405. }
  2406. //---------------------------------------------------------------------------
  2407. void __fastcall MemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift)
  2408. {
  2409. // Sender can be Form or Memo itself
  2410. TControl * Control = dynamic_cast<TControl *>(Sender);
  2411. if (DebugAlwaysTrue(Control != NULL))
  2412. {
  2413. TCustomForm * Form = GetParentForm(Control);
  2414. // Particularly when WantReturns is true,
  2415. // memo swallows also Esc, so we have to handle it ourselves.
  2416. // See also ReadOnlyControl.
  2417. if ((Key == VK_ESCAPE) && Shift.Empty())
  2418. {
  2419. Form->ModalResult = mrCancel;
  2420. Key = 0;
  2421. }
  2422. else if ((Key == VK_RETURN) && Shift.Contains(ssCtrl))
  2423. {
  2424. Form->ModalResult = DefaultResult(Form);
  2425. Key = 0;
  2426. }
  2427. }
  2428. }
  2429. //---------------------------------------------------------------------------
  2430. class TIconOwnerComponent : public TComponent
  2431. {
  2432. public:
  2433. __fastcall TIconOwnerComponent(TIcon * Icon) :
  2434. TComponent(NULL),
  2435. FIcon(Icon)
  2436. {
  2437. }
  2438. private:
  2439. std::unique_ptr<TIcon> FIcon;
  2440. };
  2441. //---------------------------------------------------------------------------
  2442. static void __fastcall FixFormIcon(TForm * Form, int Size, int WidthMetric, int HeightMetric)
  2443. {
  2444. // Whole this code is to call ReadIcon from Vcl.Graphics.pas with correct size
  2445. // Clone the icon data (whole .ico file content, that is originally loaded from .dfm)
  2446. // to a new TIcon that does not have a size fixed yet (size cannot be changed after handle is allocated)
  2447. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  2448. Form->Icon->SaveToStream(Stream.get());
  2449. std::unique_ptr<TIcon> Icon(new TIcon());
  2450. Stream->Position = 0;
  2451. Icon->LoadFromStream(Stream.get());
  2452. // Set desired size
  2453. int Width = GetSystemMetricsForControl(Form, WidthMetric);
  2454. int Height = GetSystemMetricsForControl(Form, HeightMetric);
  2455. Icon->SetSize(Width, Height);
  2456. // This calls TIcon::RequireHandle that retrieves the best icon for given size
  2457. LPARAM LParam = reinterpret_cast<LPARAM>(Icon->Handle);
  2458. SendMessage(Form->Handle, WM_SETICON, Size, LParam);
  2459. // Make sure the icon is released
  2460. TIconOwnerComponent * IconOwnerComponent = new TIconOwnerComponent(Icon.release());
  2461. IconOwnerComponent->Name = TIconOwnerComponent::QualifiedClassName() + IntToStr(Size);
  2462. Form->InsertComponent(IconOwnerComponent);
  2463. }
  2464. //---------------------------------------------------------------------------
  2465. void __fastcall FixFormIcons(TForm * Form)
  2466. {
  2467. // VCL sets only ICON_BIG (so small icon is scaled down by OS from big icon),
  2468. // and it uses a random (first?) size from the resource,
  2469. // not the best size.
  2470. FixFormIcon(Form, ICON_SMALL, SM_CXSMICON, SM_CYSMICON);
  2471. FixFormIcon(Form, ICON_BIG, SM_CXICON, SM_CYICON);
  2472. // We rely on VCL not calling WM_SETICON ever after
  2473. // (what it would do, if CreateWnd is called again).
  2474. // That would overwrite the ICON_BIG.
  2475. // We might be able to make sure it uses a correct size by calling
  2476. // TIcon.ReleaseHandle and setting a correct size.
  2477. }
  2478. //---------------------------------------------------------------------------
  2479. class TDesktopFontManager : public TComponent
  2480. {
  2481. public:
  2482. TDesktopFontManager();
  2483. virtual __fastcall ~TDesktopFontManager();
  2484. void AddControl(TControl * Control);
  2485. void Update();
  2486. protected:
  2487. virtual void __fastcall Notification(TComponent * AComponent, TOperation Operation);
  2488. private:
  2489. HWND FWindowHandle;
  2490. typedef std::set<TControl *> TControlSet;
  2491. TControlSet FControls;
  2492. void __fastcall WndProc(TMessage & Message);
  2493. void UpdateControl(TControl * Control);
  2494. };
  2495. //---------------------------------------------------------------------------
  2496. TDesktopFontManager::TDesktopFontManager() : TComponent(NULL)
  2497. {
  2498. // Alternative is using Application->HookMainWindow
  2499. FWindowHandle = AllocateHWnd(WndProc);
  2500. }
  2501. //---------------------------------------------------------------------------
  2502. __fastcall TDesktopFontManager::~TDesktopFontManager()
  2503. {
  2504. DeallocateHWnd(FWindowHandle);
  2505. }
  2506. //---------------------------------------------------------------------------
  2507. void __fastcall TDesktopFontManager::Notification(TComponent * AComponent, TOperation Operation)
  2508. {
  2509. if (DebugAlwaysTrue(Operation == opRemove))
  2510. {
  2511. TControl * Control = DebugNotNull(dynamic_cast<TControl *>(AComponent));
  2512. FControls.erase(Control);
  2513. }
  2514. TComponent::Notification(AComponent, Operation);
  2515. }
  2516. //---------------------------------------------------------------------------
  2517. void TDesktopFontManager::UpdateControl(TControl * Control)
  2518. {
  2519. class TPublicControl : public TControl
  2520. {
  2521. public:
  2522. __property Font;
  2523. };
  2524. TPublicControl * PublicControl = reinterpret_cast<TPublicControl *>(Control);
  2525. std::unique_ptr<TFont> DesktopFont(new TFont());
  2526. if (WinConfiguration->PanelFont.FontName.IsEmpty())
  2527. {
  2528. int PixelsPerInch = GetControlPixelsPerInch(Control);
  2529. TLogFont LogFont;
  2530. if (DebugAlwaysTrue(SystemParametersInfoForPixelsPerInch(SPI_GETICONTITLELOGFONT, sizeof(LogFont), &LogFont, 0, PixelsPerInch)))
  2531. {
  2532. DesktopFont->Handle = CreateFontIndirect(&LogFont);
  2533. }
  2534. }
  2535. else
  2536. {
  2537. TWinConfiguration::RestoreFont(WinConfiguration->PanelFont, DesktopFont.get());
  2538. DesktopFont->Height = ScaleByPixelsPerInchFromSystem(DesktopFont->Height, Control);
  2539. DesktopFont->PixelsPerInch = PublicControl->Font->PixelsPerInch;
  2540. }
  2541. // Neither CreateFontIndirect nor RestoreFont set color, so we should should have the default set by TFont constructor here.
  2542. DebugAssert(DesktopFont->Color == clWindowText);
  2543. if (!SameFont(DesktopFont.get(), PublicControl->Font) ||
  2544. (DesktopFont->PixelsPerInch != PublicControl->Font->PixelsPerInch))
  2545. {
  2546. // Preserve color (particularly white color of file panel font in dark mode)
  2547. DesktopFont->Color = PublicControl->Font->Color;
  2548. PublicControl->Font->Assign(DesktopFont.get());
  2549. }
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. void TDesktopFontManager::AddControl(TControl * Control)
  2553. {
  2554. FControls.insert(Control);
  2555. Control->FreeNotification(this);
  2556. UpdateControl(Control);
  2557. }
  2558. //---------------------------------------------------------------------------
  2559. void TDesktopFontManager::Update()
  2560. {
  2561. TControlSet::iterator I = FControls.begin();
  2562. while (I != FControls.end())
  2563. {
  2564. UpdateControl(*I);
  2565. I++;
  2566. }
  2567. }
  2568. //---------------------------------------------------------------------------
  2569. void __fastcall TDesktopFontManager::WndProc(TMessage & Message)
  2570. {
  2571. if (Message.Msg == WM_WININICHANGE)
  2572. {
  2573. Update();
  2574. }
  2575. Message.Result = DefWindowProc(FWindowHandle, Message.Msg, Message.WParam, Message.LParam);
  2576. }
  2577. //---------------------------------------------------------------------------
  2578. std::unique_ptr<TDesktopFontManager> DesktopFontManager(new TDesktopFontManager());
  2579. //---------------------------------------------------------------------------
  2580. void __fastcall UseDesktopFont(TControl * Control)
  2581. {
  2582. TCustomStatusBar * StatusBar = dynamic_cast<TCustomStatusBar *>(Control);
  2583. if (StatusBar != NULL)
  2584. {
  2585. // prevent syncing to system font
  2586. StatusBar->UseSystemFont = false;
  2587. }
  2588. DesktopFontManager->AddControl(Control);
  2589. }
  2590. //---------------------------------------------------------------------------
  2591. void __fastcall UpdateDesktopFont()
  2592. {
  2593. DesktopFontManager->Update();
  2594. }
  2595. //---------------------------------------------------------------------------
  2596. TShiftState __fastcall AllKeyShiftStates()
  2597. {
  2598. return TShiftState() << ssShift << ssAlt << ssCtrl;
  2599. }
  2600. //---------------------------------------------------------------------------
  2601. static bool __fastcall FormActivationHook(void * Data, TMessage & Message)
  2602. {
  2603. bool Result = false;
  2604. // Some dialogs, when application is restored from minimization,
  2605. // do not get activated. So we do it explicitly here.
  2606. // We cannot do this from TApplication::OnActivate because
  2607. // TApplication.WndProc resets focus to the last active window afterwards.
  2608. // So we override CM_ACTIVATE implementation here completelly.
  2609. if (Message.Msg == CM_ACTIVATE)
  2610. {
  2611. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  2612. if (Screen->FocusedForm == Form)
  2613. {
  2614. ::SetFocus(Form->Handle);
  2615. // VCLCOPY
  2616. if (Application->OnActivate != NULL)
  2617. {
  2618. Application->OnActivate(Application);
  2619. }
  2620. Result = true;
  2621. }
  2622. }
  2623. return Result;
  2624. }
  2625. //---------------------------------------------------------------------------
  2626. void __fastcall HookFormActivation(TCustomForm * Form)
  2627. {
  2628. Application->HookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2629. }
  2630. //---------------------------------------------------------------------------
  2631. void __fastcall UnhookFormActivation(TCustomForm * Form)
  2632. {
  2633. Application->UnhookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2634. }
  2635. //---------------------------------------------------------------------------
  2636. void __fastcall ShowFormNoActivate(TForm * Form)
  2637. {
  2638. TPublicForm * PublicForm = static_cast<TPublicForm *>(Form);
  2639. // This is same as SendToBack, except for added SWP_NOACTIVATE (VCLCOPY)
  2640. SetWindowPos(PublicForm->WindowHandle, HWND_BOTTOM, 0, 0, 0, 0,
  2641. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  2642. // This replaces TCustomForm::CMShowingChanged()
  2643. // which calls ShowWindow(Handle, SW_SHOWNORMAL).
  2644. ShowWindow(Form->Handle, SW_SHOWNOACTIVATE);
  2645. // - so we have to call DoShow explicitly.
  2646. PublicForm->DoShow();
  2647. // - also we skip applying TForm::Position (VCLCOPY)
  2648. if (DebugAlwaysTrue(Form->Position == poOwnerFormCenter))
  2649. {
  2650. TCustomForm * CenterForm = Application->MainForm;
  2651. TCustomForm * OwnerForm = dynamic_cast<TCustomForm *>(Form->Owner);
  2652. if (OwnerForm != NULL)
  2653. {
  2654. CenterForm = OwnerForm;
  2655. }
  2656. int X, Y;
  2657. if ((CenterForm != NULL) && (CenterForm != Form))
  2658. {
  2659. TRect Bounds = CenterForm->BoundsRect;
  2660. X = ((Bounds.Width() - Form->Width) / 2) + CenterForm->Left;
  2661. Y = ((Bounds.Height() - Form->Height) / 2) + CenterForm->Top;
  2662. }
  2663. else
  2664. {
  2665. X = (Screen->Width - Form->Width) / 2;
  2666. Y = (Screen->Height - Form->Height) / 2;
  2667. }
  2668. if (X < Screen->DesktopLeft)
  2669. {
  2670. X = Screen->DesktopLeft;
  2671. }
  2672. if (Y < Screen->DesktopTop)
  2673. {
  2674. Y = Screen->DesktopTop;
  2675. }
  2676. Form->SetBounds(X, Y, Form->Width, Form->Height);
  2677. // We cannot call SetWindowToMonitor().
  2678. // We cannot set FPosition = poDesigned, so workarea-checking code
  2679. // in DoFormWindowProc is not triggered
  2680. // If application is restored, dialog is not activated, do it manually.
  2681. // Wait for application to be activated to activate ourself.
  2682. HookFormActivation(Form);
  2683. }
  2684. }
  2685. //---------------------------------------------------------------------------
  2686. TPanel * __fastcall CreateBlankPanel(TComponent * Owner)
  2687. {
  2688. TPanel * Panel = new TPanel(Owner);
  2689. Panel->BevelOuter = bvNone;
  2690. Panel->BevelInner = bvNone; // default
  2691. Panel->BevelKind = bkNone;
  2692. return Panel;
  2693. }
  2694. //---------------------------------------------------------------------------
  2695. bool IsButtonBeingClicked(TButtonControl * Button)
  2696. {
  2697. class TPublicButtonControl : public TButtonControl
  2698. {
  2699. public:
  2700. __property ClicksDisabled;
  2701. };
  2702. TPublicButtonControl * PublicButton = reinterpret_cast<TPublicButtonControl *>(Button);
  2703. // HACK ClicksDisabled is set in TButtonControl.WndProc while changing focus as response to WM_LBUTTONDOWN.
  2704. return PublicButton->ClicksDisabled;
  2705. }
  2706. //---------------------------------------------------------------------------
  2707. // When using this in OnExit handers, it's still possible that the user does not actually click the
  2708. // CanceButton (for example, when the button is released out of the button).
  2709. // Then the validation is bypassed. Consequently, all dialogs that uses this must still
  2710. // gracefully handle submission with non-validated data.
  2711. bool IsCancelButtonBeingClicked(TControl * Control)
  2712. {
  2713. TCustomForm * Form = GetParentForm(Control);
  2714. TButtonControl * CancelButton = FindStandardButton(Form, false);
  2715. // Find dialog has no Cancel button
  2716. return (CancelButton != NULL) && IsButtonBeingClicked(CancelButton);
  2717. }
  2718. //---------------------------------------------------------------------------
  2719. TCanvas * CreateControlCanvas(TControl * Control)
  2720. {
  2721. std::unique_ptr<TControlCanvas> Canvas(new TControlCanvas());
  2722. Canvas->Control = Control;
  2723. TPublicControl * PublicControl = static_cast<TPublicControl *>(Control);
  2724. Canvas->Font = PublicControl->Font;
  2725. return Canvas.release();
  2726. }
  2727. //---------------------------------------------------------------------------
  2728. void AutoSizeButton(TButton * Button)
  2729. {
  2730. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(Button));
  2731. int MinWidth = Canvas->TextWidth(Button->Caption) + ScaleByTextHeight(Button, (2 * 8));
  2732. if (Button->Width < MinWidth)
  2733. {
  2734. if (Button->Anchors.Contains(akRight))
  2735. {
  2736. Button->Left = Button->Left - (MinWidth - Button->Width);
  2737. }
  2738. Button->Width = MinWidth;
  2739. }
  2740. }
  2741. //---------------------------------------------------------------------------
  2742. void GiveTBItemPriority(TTBCustomItem * Item)
  2743. {
  2744. DebugAssert(Item->GetTopComponent() != NULL);
  2745. TTBCustomToolbar * ToolbarComponent = dynamic_cast<TTBCustomToolbar *>(Item->GetTopComponent());
  2746. if ((ToolbarComponent != NULL) &&
  2747. // Only for top-level buttons on custom command toolbar, not for submenus of the custom commands menu in the main menu
  2748. (Item->Parent == ToolbarComponent->Items))
  2749. {
  2750. TTBItemViewer * Viewer = ToolbarComponent->View->Find(Item);
  2751. ToolbarComponent->View->GivePriority(Viewer);
  2752. }
  2753. }