Rights.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. //---------------------------------------------------------------------------
  2. #include <FormsPCH.h>
  3. #pragma hdrstop
  4. #include "Rights.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GrayedCheckBox"
  8. #pragma link "PngImageList"
  9. #pragma resource "*.dfm"
  10. //---------------------------------------------------------------------------
  11. __fastcall TRightsFrame::TRightsFrame(TComponent* Owner)
  12. : TFrame(Owner)
  13. {
  14. FAddXToDirectoriesSuffix = L"+x";
  15. FOnChange = NULL;
  16. FAllowAddXToDirectories = true;
  17. FPopup = false;
  18. FPopupParent = NULL;
  19. FDefaultButton = NULL;
  20. FCancelButton = NULL;
  21. // to avoid duplication of reference in forms that uses the frame
  22. PopupMenu = RightsPopup;
  23. FPopingContextMenu = false;
  24. FInitialized = false;
  25. FAcl = false;
  26. #define COPY_HINT(R) \
  27. Checks[TRights::rrGroup ## R]->Hint = Checks[TRights::rrUser ## R]->Hint; \
  28. Checks[TRights::rrOther ## R]->Hint = Checks[TRights::rrUser ## R]->Hint;
  29. COPY_HINT(Read);
  30. COPY_HINT(Write);
  31. COPY_HINT(Exec);
  32. #undef COPY_HINT
  33. }
  34. //---------------------------------------------------------------------------
  35. __fastcall TRightsFrame::~TRightsFrame()
  36. {
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TRightsFrame::SetStates(TRights::TRight Right, TRights::TState value)
  40. {
  41. DebugAssert(((value == TRights::rsNo) || (value == TRights::rsYes) || (value == TRights::rsUndef)));
  42. TCheckBox * CheckBox = Checks[Right];
  43. if (CheckBox != NULL)
  44. {
  45. switch (value) {
  46. case TRights::rsNo: CheckBox->State = cbUnchecked; break;
  47. case TRights::rsYes: CheckBox->State = cbChecked; break;
  48. case TRights::rsUndef: CheckBox->State = cbGrayed; break;
  49. }
  50. }
  51. }
  52. //---------------------------------------------------------------------------
  53. TRights::TState __fastcall TRightsFrame::GetStates(TRights::TRight Right)
  54. {
  55. TCheckBox * CheckBox = Checks[Right];
  56. if (CheckBox != NULL)
  57. {
  58. switch (CheckBox->State) {
  59. case cbUnchecked: return TRights::rsNo;
  60. case cbChecked: return TRights::rsYes;
  61. case cbGrayed:
  62. default: return TRights::rsUndef;
  63. }
  64. }
  65. else
  66. {
  67. return TRights::rsNo;
  68. }
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall TRightsFrame::SetRights(const TRights & value)
  72. {
  73. bool Changed = (AllowUndef != value.AllowUndef);
  74. AllowUndef = true; // temporarily
  75. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  76. {
  77. TRights::TRight R = static_cast<TRights::TRight>(Right);
  78. if (States[R] != value.RightUndef[R])
  79. {
  80. States[R] = value.RightUndef[R];
  81. Changed = true;
  82. }
  83. }
  84. AllowUndef = value.AllowUndef;
  85. if (Changed || !FInitialized)
  86. {
  87. UpdateControls();
  88. }
  89. }
  90. //---------------------------------------------------------------------------
  91. TRights __fastcall TRightsFrame::GetRights()
  92. {
  93. TRights Result;
  94. Result.AllowUndef = AllowUndef;
  95. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  96. {
  97. Result.RightUndef[static_cast<TRights::TRight>(Right)] =
  98. States[static_cast<TRights::TRight>(Right)];
  99. }
  100. return Result;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TRightsFrame::SetAllowUndef(bool value)
  104. {
  105. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  106. {
  107. TCheckBox * CheckBox = Checks[static_cast<TRights::TRight>(Right)];
  108. if (CheckBox != NULL)
  109. {
  110. CheckBox->AllowGrayed = value;
  111. }
  112. }
  113. }
  114. //---------------------------------------------------------------------------
  115. bool __fastcall TRightsFrame::GetAllowUndef()
  116. {
  117. bool Result = false, First = true;
  118. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  119. {
  120. TCheckBox * Check = Checks[static_cast<TRights::TRight>(Right)];
  121. if (Check != NULL)
  122. {
  123. if (First)
  124. {
  125. Result = Check->AllowGrayed;
  126. First = false;
  127. }
  128. else if (Result != Check->AllowGrayed)
  129. {
  130. DebugFail();
  131. }
  132. }
  133. }
  134. return Result;
  135. }
  136. //---------------------------------------------------------------------------
  137. TCheckBox * __fastcall TRightsFrame::GetChecks(TRights::TRight Right)
  138. {
  139. for (int Index = 0; Index < ControlCount; Index++)
  140. {
  141. if (Controls[Index]->InheritsFrom(__classid(TCheckBox)) &&
  142. (Controls[Index]->Tag == TRights::RightToFlag(Right)))
  143. {
  144. return ((TCheckBox *)Controls[Index]);
  145. }
  146. }
  147. return NULL;
  148. }
  149. //---------------------------------------------------------------------------
  150. void __fastcall TRightsFrame::SetAddXToDirectories(bool value)
  151. {
  152. DirectoriesXCheck->Checked = value;
  153. }
  154. //---------------------------------------------------------------------------
  155. bool __fastcall TRightsFrame::GetAddXToDirectories()
  156. {
  157. return DirectoriesXCheck->Checked;
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TRightsFrame::ControlChange(TObject * /*Sender*/)
  161. {
  162. UpdateControls();
  163. }
  164. //---------------------------------------------------------------------------
  165. bool __fastcall TRightsFrame::DirectoriesXEffective()
  166. {
  167. return !((Rights.NumberSet & TRights::rfExec) == TRights::rfExec);
  168. }
  169. //---------------------------------------------------------------------------
  170. void __fastcall TRightsFrame::UpdateControls()
  171. {
  172. if (!OctalEdit->Focused())
  173. {
  174. UpdateOctalEdit();
  175. }
  176. Color = (FPopup ? clWindow : clBtnFace);
  177. DirectoriesXCheck->Visible = AllowAddXToDirectories;
  178. EnableControl(DirectoriesXCheck,
  179. Enabled && DirectoriesXEffective());
  180. FInitialized = true;
  181. DoChange();
  182. }
  183. //---------------------------------------------------------------------------
  184. void TRightsFrame::CycleRights(TRights::TRightGroup RightGroup)
  185. {
  186. int Last = FAcl ? TRights::rlLastAcl : TRights::rlLastNormal;
  187. TRights::TState State = TRights::TState(); // shut up
  188. bool Any = false;
  189. bool Same = true;
  190. for (int RightLevelI = TRights::rlFirst; RightLevelI <= Last; RightLevelI++)
  191. {
  192. TRights::TRightLevel RightLevel = static_cast<TRights::TRightLevel>(RightLevelI);
  193. TRights::TRight Right = TRights::CalculateRight(RightGroup, RightLevel);
  194. if (Checks[Right]->Visible)
  195. {
  196. TRights::TState RightState = States[Right];
  197. if (!Any)
  198. {
  199. State = RightState;
  200. Any = true;
  201. }
  202. else if (State != RightState)
  203. {
  204. Same = false;
  205. break;
  206. }
  207. }
  208. }
  209. if (DebugAlwaysTrue(Any))
  210. {
  211. if (!Same)
  212. {
  213. State = TRights::rsYes;
  214. }
  215. else
  216. {
  217. switch (State)
  218. {
  219. case TRights::rsYes:
  220. State = TRights::rsNo;
  221. break;
  222. case TRights::rsNo:
  223. State = AllowUndef ? TRights::rsUndef : TRights::rsYes;
  224. break;
  225. case TRights::rsUndef:
  226. State = TRights::rsYes;
  227. break;
  228. }
  229. }
  230. for (int RightLevelI = TRights::rlFirst; RightLevelI <= Last; RightLevelI++)
  231. {
  232. TRights::TRightLevel RightLevel = static_cast<TRights::TRightLevel>(RightLevelI);
  233. TRights::TRight Right = TRights::CalculateRight(RightGroup, RightLevel);
  234. States[Right] = State;
  235. }
  236. UpdateControls();
  237. }
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall TRightsFrame::RightsButtonsClick(TObject * Sender)
  241. {
  242. CycleRights(static_cast<TRights::TRightGroup>(dynamic_cast<TComponent *>(Sender)->Tag - 1));
  243. }
  244. //---------------------------------------------------------------------------
  245. void __fastcall TRightsFrame::SetAllowAddXToDirectories(bool value)
  246. {
  247. if ((FAllowAddXToDirectories != value) || !FInitialized)
  248. {
  249. FAllowAddXToDirectories = value;
  250. UpdateControls();
  251. }
  252. }
  253. //---------------------------------------------------------------------------
  254. void __fastcall TRightsFrame::DoChange()
  255. {
  256. if (FOnChange)
  257. {
  258. FOnChange(this);
  259. }
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TRightsFrame::SetEnabled(bool Value)
  263. {
  264. TFrame::SetEnabled(Value);
  265. UpdateControls();
  266. }
  267. //---------------------------------------------------------------------------
  268. void __fastcall TRightsFrame::ForceUpdate()
  269. {
  270. UpdateOctalEdit();
  271. }
  272. //---------------------------------------------------------------------------
  273. bool __fastcall TRightsFrame::HasFocus()
  274. {
  275. return
  276. (Focused() ||
  277. ((Screen->ActiveControl != NULL) && (Screen->ActiveControl->Parent == this))) ||
  278. ((PopupParent != NULL) && PopupParent->Focused());
  279. }
  280. //---------------------------------------------------------------------------
  281. void __fastcall TRightsFrame::RightsActionsExecute(TBasicAction * Action,
  282. bool & Handled)
  283. {
  284. // prevent shortcuts to be evaluated when frame does not have a focus
  285. if (HasFocus())
  286. {
  287. bool Changed = true;
  288. TRights R = Rights;
  289. R.Number = TRights::rfNo;
  290. Handled = true;
  291. if (Action == NoRightsAction)
  292. {
  293. R = TRights::rfNo;
  294. }
  295. else if (Action == DefaultRightsAction)
  296. {
  297. R = TRights::rfDefault;
  298. }
  299. else if (Action == AllRightsAction)
  300. {
  301. R = TRights::rfAll;
  302. }
  303. else if (Action == LeaveRightsAsIsAction)
  304. {
  305. R.AllUndef();
  306. }
  307. else if (Action == CopyTextAction)
  308. {
  309. TInstantOperationVisualizer Visualizer;
  310. CopyToClipboard(Text);
  311. Changed = false;
  312. }
  313. else if (Action == CopyOctalAction)
  314. {
  315. TRights R = Rights;
  316. DebugAssert(!R.IsUndef);
  317. if (!R.IsUndef)
  318. {
  319. TInstantOperationVisualizer Visualizer;
  320. CopyToClipboard(R.Octal);
  321. }
  322. Changed = false;
  323. }
  324. else if (Action == PasteAction)
  325. {
  326. UnicodeString S;
  327. if (TextFromClipboard(S, true))
  328. {
  329. Text = S;
  330. }
  331. // trigger on change event, even if no change actually occurred to
  332. // allow parent form to visualize feedback of an action
  333. DoChange();
  334. // Update octal edit in Rights frame even if it has focus
  335. ForceUpdate();
  336. Changed = false;
  337. }
  338. else
  339. {
  340. Handled = false;
  341. }
  342. if (Changed)
  343. {
  344. Rights = R;
  345. ForceUpdate();
  346. DoChange();
  347. }
  348. }
  349. }
  350. //---------------------------------------------------------------------------
  351. void __fastcall TRightsFrame::RightsActionsUpdate(TBasicAction *Action,
  352. bool &Handled)
  353. {
  354. TRights R = Rights;
  355. Handled = true;
  356. if (Action == NoRightsAction)
  357. {
  358. // explicitly enable as action gets disabled, if its shortcut is pressed,
  359. // while the form does not have a focus and OnExecute handler denies the execution
  360. NoRightsAction->Enabled = !FAcl;
  361. NoRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfNo);
  362. }
  363. else if (Action == DefaultRightsAction)
  364. {
  365. DefaultRightsAction->Enabled = !FAcl;
  366. DefaultRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfDefault);
  367. }
  368. else if (Action == AllRightsAction)
  369. {
  370. AllRightsAction->Enabled = !FAcl;
  371. AllRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfAll);
  372. }
  373. else if (Action == LeaveRightsAsIsAction)
  374. {
  375. LeaveRightsAsIsAction->Enabled = !FAcl;
  376. LeaveRightsAsIsAction->Visible = R.AllowUndef;
  377. LeaveRightsAsIsAction->Checked = (R.NumberSet == TRights::rfNo) &&
  378. (R.NumberUnset == TRights::rfNo);
  379. }
  380. else if (Action == CopyTextAction)
  381. {
  382. CopyTextAction->Enabled = !FAcl && !R.IsUndef;
  383. }
  384. else if (Action == CopyOctalAction)
  385. {
  386. CopyOctalAction->Enabled = !FAcl && !R.IsUndef;
  387. }
  388. else if (Action == PasteAction)
  389. {
  390. PasteAction->Enabled = !FAcl && IsFormatInClipboard(CF_TEXT);
  391. }
  392. else
  393. {
  394. Handled = false;
  395. }
  396. }
  397. //---------------------------------------------------------------------------
  398. void __fastcall TRightsFrame::CreateParams(TCreateParams & Params)
  399. {
  400. TFrame::CreateParams(Params);
  401. if (FPopup)
  402. {
  403. Params.Style |= WS_BORDER;
  404. }
  405. }
  406. //---------------------------------------------------------------------------
  407. void __fastcall TRightsFrame::CreateWnd()
  408. {
  409. if (FPopup)
  410. {
  411. Width += 2 * GetSystemMetricsForControl(Parent, SM_CXBORDER);
  412. Height += 2 * GetSystemMetricsForControl(Parent, SM_CYBORDER);
  413. }
  414. TFrame::CreateWnd();
  415. UpdateButtons();
  416. }
  417. //---------------------------------------------------------------------------
  418. void __fastcall TRightsFrame::SetPopup(bool value)
  419. {
  420. if (Popup != value)
  421. {
  422. FPopup = value;
  423. Visible = !FPopup;
  424. CloseButton->Visible = value;
  425. CloseButton->Cancel = value;
  426. CloseButton->Default = value;
  427. }
  428. }
  429. //---------------------------------------------------------------------------
  430. void __fastcall TRightsFrame::DoCloseUp()
  431. {
  432. Hide();
  433. if (FDefaultButton != NULL)
  434. {
  435. FDefaultButton->Default = true;
  436. FDefaultButton = NULL;
  437. }
  438. if (FCancelButton != NULL)
  439. {
  440. FCancelButton->Cancel = true;
  441. FCancelButton = NULL;
  442. }
  443. }
  444. //---------------------------------------------------------------------------
  445. void __fastcall TRightsFrame::DoExit()
  446. {
  447. // this is bit hack to make frame hide when ComboEdit button is pressed while
  448. // from is poped-up already.
  449. if (FPopup && !IsAncestor(Screen->ActiveControl, FPopupParent))
  450. {
  451. DoCloseUp();
  452. }
  453. TFrame::DoExit();
  454. }
  455. //---------------------------------------------------------------------------
  456. bool __fastcall TRightsFrame::IsAncestor(TControl * Control, TControl * Ancestor)
  457. {
  458. while ((Control != NULL) &&
  459. (Control->Parent != Ancestor))
  460. {
  461. Control = Control->Parent;
  462. }
  463. return (Control != NULL);
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall TRightsFrame::WMContextMenu(TWMContextMenu & Message)
  467. {
  468. FPopingContextMenu = true;
  469. try
  470. {
  471. TFrame::Dispatch(&Message);
  472. }
  473. __finally
  474. {
  475. FPopingContextMenu = false;
  476. }
  477. }
  478. //---------------------------------------------------------------------------
  479. void __fastcall TRightsFrame::CMDialogKey(TCMDialogKey & Message)
  480. {
  481. if (FPopup && Visible &&
  482. ((Message.CharCode == VK_RETURN) ||
  483. (Message.CharCode == VK_ESCAPE)) &&
  484. KeyDataToShiftState(Message.KeyData).Empty())
  485. {
  486. CloseUp();
  487. Message.Result = 1;
  488. }
  489. else
  490. {
  491. TFrame::Dispatch(&Message);
  492. }
  493. if (Message.CharCode == VK_MENU)
  494. {
  495. UpdateButtons();
  496. }
  497. }
  498. //---------------------------------------------------------------------------
  499. bool TRightsFrame::IsButtonAccel(TCMDialogChar & Message, TSpeedButton * Button, TWinControl * FocusControl)
  500. {
  501. bool Result = IsAccel(Message.CharCode, Button->Caption) && Button->Visible && Button->Enabled;
  502. if (Result)
  503. {
  504. Perform(WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR, UISF_HIDEFOCUS), 0);
  505. FocusControl->SetFocus();
  506. }
  507. return Result;
  508. }
  509. //---------------------------------------------------------------------------
  510. void TRightsFrame::CMDialogChar(TCMDialogChar & Message)
  511. {
  512. if (IsButtonAccel(Message, OwnerButton, OwnerReadCheck) ||
  513. IsButtonAccel(Message, GroupButton, GroupReadCheck) ||
  514. IsButtonAccel(Message, OthersButton, OthersReadCheck))
  515. {
  516. Message.Result = 1;
  517. }
  518. else
  519. {
  520. TFrame::Dispatch(&Message);
  521. }
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall TRightsFrame::CMCancelMode(TCMCancelMode & Message)
  525. {
  526. if (FPopup && Visible && !FPopingContextMenu &&
  527. ((Message.Sender == NULL) ||
  528. (!IsAncestor(Message.Sender, this) &&
  529. !IsAncestor(Message.Sender, FPopupParent) &&
  530. (Message.Sender != this))))
  531. {
  532. CloseUp();
  533. }
  534. TFrame::Dispatch(&Message);
  535. }
  536. //---------------------------------------------------------------------------
  537. void TRightsFrame::CMDPIChanged(TMessage & Message)
  538. {
  539. TFrame::Dispatch(&Message);
  540. UpdateButtons();
  541. }
  542. //---------------------------------------------------------------------------
  543. void TRightsFrame::WMUpdateUIState(TMessage & Message)
  544. {
  545. TFrame::Dispatch(&Message);
  546. UpdateButtons();
  547. }
  548. //---------------------------------------------------------------------------
  549. void TRightsFrame::UpdateButton(TSpeedButton * Button, UnicodeString & Caption)
  550. {
  551. if (Caption.IsEmpty())
  552. {
  553. Caption = Button->Caption;
  554. DebugAssert(Caption.Pos(L"&") >= 0);
  555. }
  556. int Padding = ScaleByTextHeight(Button, 4);
  557. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(Button));
  558. UnicodeString ACaption = Caption;
  559. UnicodeString StrippedCaption = StripHotkey(ACaption);
  560. if ((SendMessage(Handle, WM_QUERYUISTATE, 0, 0) & UISF_HIDEACCEL) != 0)
  561. {
  562. ACaption = StrippedCaption;
  563. }
  564. Button->Caption = ACaption;
  565. Button->Width = Padding + Canvas->TextWidth(StrippedCaption) + Padding;
  566. Button->Left = OctalLabel->Left - Padding;
  567. }
  568. //---------------------------------------------------------------------------
  569. void TRightsFrame::UpdateButtons()
  570. {
  571. UpdateButton(OwnerButton, FOwnerCaption);
  572. UpdateButton(GroupButton, FGroupCaption);
  573. UpdateButton(OthersButton, FOthersCaption);
  574. }
  575. //---------------------------------------------------------------------------
  576. void __fastcall TRightsFrame::Dispatch(void * Message)
  577. {
  578. TMessage & AMessage = *static_cast<TMessage *>(Message);
  579. switch (AMessage.Msg)
  580. {
  581. case CM_CANCELMODE:
  582. CMCancelMode(*(TCMCancelMode *)Message);
  583. break;
  584. case CM_DIALOGKEY:
  585. CMDialogKey(*(TCMDialogKey *)Message);
  586. break;
  587. case CM_DIALOGCHAR:
  588. CMDialogChar(*(TCMDialogChar *)Message);
  589. break;
  590. case WM_CONTEXTMENU:
  591. WMContextMenu(*(TWMContextMenu *)Message);
  592. break;
  593. case CM_DPICHANGED:
  594. CMDPIChanged(AMessage);
  595. break;
  596. case WM_UPDATEUISTATE:
  597. WMUpdateUIState(AMessage);
  598. break;
  599. default:
  600. TFrame::Dispatch(Message);
  601. break;
  602. }
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TRightsFrame::DropDown()
  606. {
  607. TCustomForm * Form = GetParentForm(this);
  608. // Due to lack of better idea, we clear "default" and "cancel" flags of respective
  609. // form buttons to prevent them to handle ESC/ENTER keys.
  610. // Could use FindStandardButton.
  611. for (int Index = 0; Index < Form->ControlCount; Index++)
  612. {
  613. TButton * Button = dynamic_cast<TButton *>(Form->Controls[Index]);
  614. if (Button != NULL)
  615. {
  616. if (Button->Default)
  617. {
  618. DebugAssert(FDefaultButton == NULL);
  619. FDefaultButton = Button;
  620. Button->Default = false;
  621. }
  622. if (Button->Cancel)
  623. {
  624. DebugAssert(FCancelButton == NULL);
  625. FCancelButton = Button;
  626. Button->Cancel = false;
  627. }
  628. }
  629. }
  630. TPoint Origin(PopupParent->Left, PopupParent->Top + PopupParent->Height);
  631. Origin = Parent->ScreenToClient(PopupParent->Parent->ClientToScreen(Origin));
  632. if (Origin.x + Width > Parent->ClientWidth)
  633. {
  634. Origin.x += PopupParent->Width - Width;
  635. }
  636. Left = Origin.x;
  637. Top = Origin.y;
  638. Show();
  639. SetFocus();
  640. }
  641. //---------------------------------------------------------------------------
  642. void __fastcall TRightsFrame::CloseUp()
  643. {
  644. DebugAssert(FPopup);
  645. DebugAssert(Visible);
  646. if (!Focused())
  647. {
  648. // this can happen only if called from on-click handler of drop down button
  649. DoCloseUp();
  650. }
  651. FPopupParent->SetFocus();
  652. }
  653. //---------------------------------------------------------------------------
  654. UnicodeString __fastcall TRightsFrame::GetText()
  655. {
  656. UnicodeString Result = Rights.Text;
  657. if (AllowAddXToDirectories && DirectoriesXEffective() && AddXToDirectories)
  658. {
  659. Result = FORMAT(L"%s (%s)", (Result, FAddXToDirectoriesSuffix));
  660. }
  661. return Result;
  662. }
  663. //---------------------------------------------------------------------------
  664. void __fastcall TRightsFrame::SetText(UnicodeString value)
  665. {
  666. if (Text != value)
  667. {
  668. UnicodeString RightsStr = value;
  669. int P = RightsStr.LowerCase().Pos(FAddXToDirectoriesSuffix);
  670. bool AAddXToDirectories = (P > 0);
  671. if (AAddXToDirectories)
  672. {
  673. RightsStr.Delete(P, FAddXToDirectoriesSuffix.Length());
  674. }
  675. RightsStr = DeleteChar(DeleteChar(RightsStr, L'('), L')').Trim();
  676. TRights R = Rights;
  677. if (((RightsStr.Length() == 3) || (RightsStr.Length() == 4)) &&
  678. IsNumber(RightsStr))
  679. {
  680. R.Octal = RightsStr;
  681. }
  682. else
  683. {
  684. R.Text = RightsStr;
  685. }
  686. Rights = R;
  687. AddXToDirectories = AAddXToDirectories;
  688. }
  689. }
  690. //---------------------------------------------------------------------------
  691. void __fastcall TRightsFrame::RightsPopupPopup(TObject * /*Sender*/)
  692. {
  693. if (!HasFocus())
  694. {
  695. SetFocus();
  696. }
  697. }
  698. //---------------------------------------------------------------------------
  699. void __fastcall TRightsFrame::FrameContextPopup(TObject * Sender,
  700. TPoint & MousePos, bool & Handled)
  701. {
  702. if (!FAcl)
  703. {
  704. SelectScaledImageList(RightsImages);
  705. MenuPopup(Sender, MousePos, Handled);
  706. }
  707. else
  708. {
  709. Handled = true;
  710. }
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall TRightsFrame::UpdateByOctal()
  714. {
  715. if (!OctalEdit->Text.IsEmpty())
  716. {
  717. TRights R = Rights;
  718. R.Octal = OctalEdit->Text;
  719. Rights = R;
  720. }
  721. UpdateControls();
  722. OctalEdit->Modified = false;
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TRightsFrame::UpdateOctalEdit()
  726. {
  727. TRights R = Rights;
  728. OctalEdit->Text = R.IsUndef ? UnicodeString() : R.Octal;
  729. OctalEdit->Modified = false;
  730. OctalEdit->SelectAll();
  731. }
  732. //---------------------------------------------------------------------------
  733. void __fastcall TRightsFrame::OctalEditChange(TObject *)
  734. {
  735. if (OctalEdit->Modified && OctalEdit->Text.Length() >= 3)
  736. {
  737. try
  738. {
  739. UpdateByOctal();
  740. }
  741. catch(...)
  742. {
  743. OctalEdit->Modified = true;
  744. }
  745. }
  746. }
  747. //---------------------------------------------------------------------------
  748. void __fastcall TRightsFrame::OctalEditExit(TObject *)
  749. {
  750. if ((!Visible && DebugAlwaysTrue(Popup)) || // Popup assert: should happen only if popup is closed by esc key
  751. IsCancelButtonBeingClicked(this) || // CloseButton
  752. ((FCancelButton != NULL) && IsButtonBeingClicked(FCancelButton)))
  753. {
  754. // cancel changes
  755. ForceUpdate();
  756. }
  757. else if (OctalEdit->Modified)
  758. {
  759. // Now the text in OctalEdit is almost necessarily invalid, otherwise
  760. // OctalEditChange would have already cleared Modified flag
  761. try
  762. {
  763. UpdateByOctal();
  764. }
  765. catch(...)
  766. {
  767. OctalEdit->SelectAll();
  768. OctalEdit->SetFocus();
  769. throw;
  770. }
  771. }
  772. else
  773. {
  774. UpdateControls();
  775. }
  776. }
  777. //---------------------------------------------------------------------------
  778. void __fastcall TRightsFrame::CloseButtonClick(TObject *)
  779. {
  780. CloseUp();
  781. }
  782. //---------------------------------------------------------------------------
  783. void TRightsFrame::DisplayAsAcl(TRights::TRight ReadRight, TRights::TRight WriteRight, TRights::TRight ExecRight, TRights::TRight SpecialRight)
  784. {
  785. TCheckBox * ReadCheck = Checks[ReadRight];
  786. TCheckBox * WriteCheck = Checks[WriteRight];
  787. TCheckBox * ReadAclCheck = Checks[ExecRight];
  788. TCheckBox * WriteAclCheck = Checks[SpecialRight];
  789. WriteCheck->Visible = false;
  790. int ReadAclLeft = (3*ReadCheck->Left + 2*WriteAclCheck->Left) / 5;
  791. int Shift = ReadAclLeft - ReadAclCheck->Left;
  792. ReadAclCheck->Left = ReadAclLeft;
  793. ReadAclCheck->Width = ReadAclCheck->Width - Shift;
  794. ReadAclCheck->Caption = L"R ACL";
  795. ReadAclCheck->Hint = LoadStr(PROPERTIES_S3_R_ACL_HINT);
  796. WriteAclCheck->Caption = L"W ACL";
  797. WriteAclCheck->Hint = LoadStr(PROPERTIES_S3_W_ACL_HINT);
  798. WriteAclCheck->ShowHint = true;
  799. FGroupCaption = LoadStr(PROPERTIES_S3_USERS);
  800. FOthersCaption = LoadStr(PROPERTIES_S3_EVERYONE);
  801. UpdateButtons();
  802. }
  803. //---------------------------------------------------------------------------
  804. void TRightsFrame::DisplayAsAcl()
  805. {
  806. AllowAddXToDirectories = false;
  807. OctalLabel->Visible = false;
  808. OctalEdit->Visible = false;
  809. DisplayAsAcl(TRights::rrUserRead, TRights::rrUserWrite, TRights::rrUserExec, TRights::rrUserIDExec);
  810. DisplayAsAcl(TRights::rrGroupRead, TRights::rrGroupWrite, TRights::rrGroupExec, TRights::rrGroupIDExec);
  811. DisplayAsAcl(TRights::rrOtherRead, TRights::rrOtherWrite, TRights::rrOtherExec, TRights::rrStickyBit);
  812. FAcl = true;
  813. }