Rights.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Rights.h"
  5. #include <Common.h>
  6. #include <VCLCommon.h>
  7. #include <Tools.h>
  8. #include <GUITools.h>
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma link "GrayedCheckBox"
  12. #pragma link "PngImageList"
  13. #ifndef NO_RESOURCES
  14. #pragma resource "*.dfm"
  15. #endif
  16. //---------------------------------------------------------------------------
  17. __fastcall TRightsFrame::TRightsFrame(TComponent* Owner)
  18. : TFrame(Owner)
  19. {
  20. FAddXToDirectoriesSuffix = L"+x";
  21. FOnChange = NULL;
  22. FAllowAddXToDirectories = true;
  23. FPopup = false;
  24. FPopupParent = NULL;
  25. FDefaultButton = NULL;
  26. FCancelButton = NULL;
  27. // to avoid duplication of reference in forms that uses the frame
  28. PopupMenu = RightsPopup;
  29. FPopingContextMenu = false;
  30. FInitialized = false;
  31. #define COPY_HINT(R) \
  32. Checks[TRights::rrGroup ## R]->Hint = Checks[TRights::rrUser ## R]->Hint; \
  33. Checks[TRights::rrOther ## R]->Hint = Checks[TRights::rrUser ## R]->Hint;
  34. COPY_HINT(Read);
  35. COPY_HINT(Write);
  36. COPY_HINT(Exec);
  37. #undef COPY_HINT
  38. UpgradeSpeedButton(OwnerButton);
  39. UpgradeSpeedButton(GroupButton);
  40. UpgradeSpeedButton(OthersButton);
  41. }
  42. //---------------------------------------------------------------------------
  43. __fastcall TRightsFrame::~TRightsFrame()
  44. {
  45. }
  46. //---------------------------------------------------------------------------
  47. void __fastcall TRightsFrame::SetStates(TRights::TRight Right, TRights::TState value)
  48. {
  49. assert(((value == TRights::rsNo) || (value == TRights::rsYes) || (value == TRights::rsUndef)));
  50. TCheckBox * CheckBox = Checks[Right];
  51. if (CheckBox != NULL)
  52. {
  53. switch (value) {
  54. case TRights::rsNo: CheckBox->State = cbUnchecked; break;
  55. case TRights::rsYes: CheckBox->State = cbChecked; break;
  56. case TRights::rsUndef: CheckBox->State = cbGrayed; break;
  57. }
  58. }
  59. }
  60. //---------------------------------------------------------------------------
  61. TRights::TState __fastcall TRightsFrame::GetStates(TRights::TRight Right)
  62. {
  63. TCheckBox * CheckBox = Checks[Right];
  64. if (CheckBox != NULL)
  65. {
  66. switch (CheckBox->State) {
  67. case cbUnchecked: return TRights::rsNo;
  68. case cbChecked: return TRights::rsYes;
  69. case cbGrayed:
  70. default: return TRights::rsUndef;
  71. }
  72. }
  73. else
  74. {
  75. return TRights::rsNo;
  76. }
  77. }
  78. //---------------------------------------------------------------------------
  79. void __fastcall TRightsFrame::SetRights(const TRights & value)
  80. {
  81. bool Changed = (AllowUndef != value.AllowUndef);
  82. AllowUndef = true; // temporarily
  83. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  84. {
  85. TRights::TRight R = static_cast<TRights::TRight>(Right);
  86. if (States[R] != value.RightUndef[R])
  87. {
  88. States[R] = value.RightUndef[R];
  89. Changed = true;
  90. }
  91. }
  92. AllowUndef = value.AllowUndef;
  93. if (Changed || !FInitialized)
  94. {
  95. UpdateControls();
  96. }
  97. }
  98. //---------------------------------------------------------------------------
  99. TRights __fastcall TRightsFrame::GetRights()
  100. {
  101. TRights Result;
  102. Result.AllowUndef = AllowUndef;
  103. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  104. {
  105. Result.RightUndef[static_cast<TRights::TRight>(Right)] =
  106. States[static_cast<TRights::TRight>(Right)];
  107. }
  108. return Result;
  109. }
  110. //---------------------------------------------------------------------------
  111. void __fastcall TRightsFrame::SetAllowUndef(bool value)
  112. {
  113. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  114. {
  115. TCheckBox * CheckBox = Checks[static_cast<TRights::TRight>(Right)];
  116. if (CheckBox != NULL)
  117. {
  118. CheckBox->AllowGrayed = value;
  119. }
  120. }
  121. }
  122. //---------------------------------------------------------------------------
  123. bool __fastcall TRightsFrame::GetAllowUndef()
  124. {
  125. bool Result = false, First = true;
  126. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  127. {
  128. TCheckBox * Check = Checks[static_cast<TRights::TRight>(Right)];
  129. if (Check != NULL)
  130. {
  131. if (First)
  132. {
  133. Result = Check->AllowGrayed;
  134. First = false;
  135. }
  136. else if (Result != Check->AllowGrayed)
  137. {
  138. assert(false);
  139. }
  140. }
  141. }
  142. return Result;
  143. }
  144. //---------------------------------------------------------------------------
  145. TCheckBox * __fastcall TRightsFrame::GetChecks(TRights::TRight Right)
  146. {
  147. for (int Index = 0; Index < ControlCount; Index++)
  148. {
  149. if (Controls[Index]->InheritsFrom(__classid(TCheckBox)) &&
  150. (Controls[Index]->Tag == TRights::RightToFlag(Right)))
  151. {
  152. return ((TCheckBox *)Controls[Index]);
  153. }
  154. }
  155. return NULL;
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TRightsFrame::SetAddXToDirectories(bool value)
  159. {
  160. DirectoriesXCheck->Checked = value;
  161. }
  162. //---------------------------------------------------------------------------
  163. bool __fastcall TRightsFrame::GetAddXToDirectories()
  164. {
  165. return DirectoriesXCheck->Checked;
  166. }
  167. //---------------------------------------------------------------------------
  168. void __fastcall TRightsFrame::ControlChange(TObject * /*Sender*/)
  169. {
  170. UpdateControls();
  171. }
  172. //---------------------------------------------------------------------------
  173. bool __fastcall TRightsFrame::DirectoriesXEffective()
  174. {
  175. return !((Rights.NumberSet & TRights::rfExec) == TRights::rfExec);
  176. }
  177. //---------------------------------------------------------------------------
  178. void __fastcall TRightsFrame::UpdateControls()
  179. {
  180. Color = (FPopup ? clWindow : clBtnFace);
  181. DirectoriesXCheck->Visible = AllowAddXToDirectories;
  182. EnableControl(DirectoriesXCheck,
  183. Enabled && DirectoriesXEffective());
  184. FInitialized = true;
  185. DoChange();
  186. }
  187. //---------------------------------------------------------------------------
  188. void __fastcall TRightsFrame::CycleRights(int Group)
  189. {
  190. TRights::TState State;
  191. bool Same = true;
  192. for (int Right = 0; Right < 3; Right++)
  193. {
  194. TRights::TState CState = States[static_cast<TRights::TRight>(
  195. TRights::rrUserRead + Right + ((Group - 1) * 3))];
  196. if (Right == 0) State = CState;
  197. else
  198. if (State != CState) Same = False;
  199. }
  200. if (!Same)
  201. {
  202. State = TRights::rsYes;
  203. }
  204. else
  205. {
  206. switch (State) {
  207. case TRights::rsYes:
  208. State = TRights::rsNo;
  209. break;
  210. case TRights::rsNo:
  211. State = AllowUndef ? TRights::rsUndef : TRights::rsYes;
  212. break;
  213. case TRights::rsUndef:
  214. State = TRights::rsYes;
  215. break;
  216. }
  217. }
  218. for (int Right = 0; Right < 3; Right++)
  219. {
  220. States[static_cast<TRights::TRight>(
  221. TRights::rrUserRead + Right + ((Group - 1) * 3))] = State;
  222. }
  223. UpdateControls();
  224. }
  225. //---------------------------------------------------------------------------
  226. void __fastcall TRightsFrame::RightsButtonsClick(TObject *Sender)
  227. {
  228. CycleRights(((TComponent*)Sender)->Tag);
  229. }
  230. //---------------------------------------------------------------------------
  231. void __fastcall TRightsFrame::SetAllowAddXToDirectories(bool value)
  232. {
  233. if ((FAllowAddXToDirectories != value) || !FInitialized)
  234. {
  235. FAllowAddXToDirectories = value;
  236. UpdateControls();
  237. }
  238. }
  239. //---------------------------------------------------------------------------
  240. void __fastcall TRightsFrame::DoChange()
  241. {
  242. if (FOnChange)
  243. {
  244. FOnChange(this);
  245. }
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TRightsFrame::SetEnabled(bool Value)
  249. {
  250. TFrame::SetEnabled(Value);
  251. UpdateControls();
  252. }
  253. //---------------------------------------------------------------------------
  254. void __fastcall TRightsFrame::ForceUpdate()
  255. {
  256. }
  257. //---------------------------------------------------------------------------
  258. bool __fastcall TRightsFrame::HasFocus()
  259. {
  260. return
  261. (Focused() ||
  262. ((Screen->ActiveControl != NULL) && (Screen->ActiveControl->Parent == this))) ||
  263. ((PopupParent != NULL) && PopupParent->Focused());
  264. }
  265. //---------------------------------------------------------------------------
  266. void __fastcall TRightsFrame::RightsActionsExecute(TBasicAction * Action,
  267. bool & Handled)
  268. {
  269. // prevent shortcuts to be avaluated when frame does not have a focus
  270. if (HasFocus())
  271. {
  272. bool Changed = true;
  273. TRights R = Rights;
  274. R.Number = TRights::rfNo;
  275. Handled = true;
  276. if (Action == NoRightsAction)
  277. {
  278. R = TRights::rfNo;
  279. }
  280. else if (Action == DefaultRightsAction)
  281. {
  282. R = TRights::rfDefault;
  283. }
  284. else if (Action == AllRightsAction)
  285. {
  286. R = TRights::rfAll;
  287. }
  288. else if (Action == LeaveRightsAsIsAction)
  289. {
  290. R.AllUndef();
  291. }
  292. else if (Action == CopyTextAction)
  293. {
  294. TInstantOperationVisualizer Visualizer;
  295. CopyToClipboard(Text);
  296. Changed = false;
  297. }
  298. else if (Action == CopyOctalAction)
  299. {
  300. TRights R = Rights;
  301. assert(!R.IsUndef);
  302. if (!R.IsUndef)
  303. {
  304. TInstantOperationVisualizer Visualizer;
  305. CopyToClipboard(R.Octal);
  306. }
  307. Changed = false;
  308. }
  309. else if (Action == PasteAction)
  310. {
  311. UnicodeString S;
  312. if (TextFromClipboard(S))
  313. {
  314. Text = S;
  315. }
  316. // trigger on change event, even if no change actually occured to
  317. // allow parent form to visualize feedback of an action
  318. DoChange();
  319. Changed = false;
  320. }
  321. else
  322. {
  323. Handled = false;
  324. }
  325. if (Changed)
  326. {
  327. Rights = R;
  328. ForceUpdate();
  329. DoChange();
  330. }
  331. }
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall TRightsFrame::RightsActionsUpdate(TBasicAction *Action,
  335. bool &Handled)
  336. {
  337. TRights R = Rights;
  338. Handled = true;
  339. if (Action == NoRightsAction)
  340. {
  341. // explicitly enable as action gets disabled, if its shortcut is pressed,
  342. // while the form does not have a focus and OnExecute handler denies the execution
  343. NoRightsAction->Enabled = true;
  344. NoRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfNo);
  345. }
  346. else if (Action == DefaultRightsAction)
  347. {
  348. DefaultRightsAction->Enabled = true;
  349. DefaultRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfDefault);
  350. }
  351. else if (Action == AllRightsAction)
  352. {
  353. AllRightsAction->Enabled = true;
  354. AllRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfAll);
  355. }
  356. else if (Action == LeaveRightsAsIsAction)
  357. {
  358. LeaveRightsAsIsAction->Enabled = true;
  359. LeaveRightsAsIsAction->Visible = R.AllowUndef;
  360. LeaveRightsAsIsAction->Checked = (R.NumberSet == TRights::rfNo) &&
  361. (R.NumberUnset == TRights::rfNo);
  362. }
  363. else if (Action == CopyTextAction)
  364. {
  365. CopyTextAction->Enabled = !R.IsUndef;
  366. }
  367. else if (Action == CopyOctalAction)
  368. {
  369. CopyOctalAction->Enabled = !R.IsUndef;
  370. }
  371. else if (Action == PasteAction)
  372. {
  373. PasteAction->Enabled = IsFormatInClipboard(CF_TEXT);
  374. }
  375. else
  376. {
  377. Handled = false;
  378. }
  379. }
  380. //---------------------------------------------------------------------------
  381. void __fastcall TRightsFrame::CreateParams(TCreateParams & Params)
  382. {
  383. TFrame::CreateParams(Params);
  384. if (FPopup)
  385. {
  386. Params.Style |= WS_BORDER;
  387. }
  388. }
  389. //---------------------------------------------------------------------------
  390. void __fastcall TRightsFrame::CreateWnd()
  391. {
  392. if (FPopup)
  393. {
  394. Width += 2 * GetSystemMetrics(SM_CXBORDER);
  395. Height += 2 * GetSystemMetrics(SM_CYBORDER);
  396. }
  397. TFrame::CreateWnd();
  398. }
  399. //---------------------------------------------------------------------------
  400. void __fastcall TRightsFrame::SetPopup(bool value)
  401. {
  402. if (Popup != value)
  403. {
  404. FPopup = value;
  405. Visible = !FPopup;
  406. }
  407. }
  408. //---------------------------------------------------------------------------
  409. void __fastcall TRightsFrame::DoCloseUp()
  410. {
  411. Hide();
  412. if (FDefaultButton != NULL)
  413. {
  414. FDefaultButton->Default = true;
  415. FDefaultButton = NULL;
  416. }
  417. if (FCancelButton != NULL)
  418. {
  419. FCancelButton->Cancel = true;
  420. FCancelButton = NULL;
  421. }
  422. }
  423. //---------------------------------------------------------------------------
  424. void __fastcall TRightsFrame::DoExit()
  425. {
  426. // this is bit hack to make frame hide when ComboEdit button is pressed while
  427. // from is poped-up already.
  428. if (FPopup && !IsAncestor(Screen->ActiveControl, FPopupParent))
  429. {
  430. DoCloseUp();
  431. }
  432. TFrame::DoExit();
  433. }
  434. //---------------------------------------------------------------------------
  435. bool __fastcall TRightsFrame::IsAncestor(TControl * Control, TControl * Ancestor)
  436. {
  437. while ((Control != NULL) &&
  438. (Control->Parent != Ancestor))
  439. {
  440. Control = Control->Parent;
  441. }
  442. return (Control != NULL);
  443. }
  444. //---------------------------------------------------------------------------
  445. void __fastcall TRightsFrame::WMContextMenu(TWMContextMenu & Message)
  446. {
  447. FPopingContextMenu = true;
  448. try
  449. {
  450. TFrame::Dispatch(&Message);
  451. }
  452. __finally
  453. {
  454. FPopingContextMenu = false;
  455. }
  456. }
  457. //---------------------------------------------------------------------------
  458. void __fastcall TRightsFrame::CMDialogKey(TCMDialogKey & Message)
  459. {
  460. if (FPopup && Visible &&
  461. ((Message.CharCode == VK_RETURN) ||
  462. (Message.CharCode == VK_ESCAPE)) &&
  463. KeyDataToShiftState(Message.KeyData).Empty())
  464. {
  465. CloseUp();
  466. Message.Result = 1;
  467. }
  468. else
  469. {
  470. TFrame::Dispatch(&Message);
  471. }
  472. }
  473. //---------------------------------------------------------------------------
  474. void __fastcall TRightsFrame::CMCancelMode(TCMCancelMode & Message)
  475. {
  476. if (FPopup && Visible && !FPopingContextMenu &&
  477. ((Message.Sender == NULL) ||
  478. (!IsAncestor(Message.Sender, this) &&
  479. !IsAncestor(Message.Sender, FPopupParent) &&
  480. (Message.Sender != this))))
  481. {
  482. CloseUp();
  483. }
  484. TFrame::Dispatch(&Message);
  485. }
  486. //---------------------------------------------------------------------------
  487. void __fastcall TRightsFrame::Dispatch(void * Message)
  488. {
  489. TMessage & AMessage = *static_cast<TMessage *>(Message);
  490. switch (AMessage.Msg)
  491. {
  492. case CM_CANCELMODE:
  493. CMCancelMode(*(TCMCancelMode *)Message);
  494. break;
  495. case CM_DIALOGKEY:
  496. CMDialogKey(*(TCMDialogKey *)Message);
  497. break;
  498. case WM_CONTEXTMENU:
  499. WMContextMenu(*(TWMContextMenu *)Message);
  500. break;
  501. default:
  502. TFrame::Dispatch(Message);
  503. break;
  504. }
  505. }
  506. //---------------------------------------------------------------------------
  507. void __fastcall TRightsFrame::DropDown()
  508. {
  509. TCustomForm * Form = GetParentForm(this);
  510. // due to lack of better idea, we clear "default" and "cancel" flags of respective
  511. // form buttons to prevent them to handle ESC/ENTER keys.
  512. for (int Index = 0; Index < Form->ControlCount; Index++)
  513. {
  514. TButton * Button = dynamic_cast<TButton *>(Form->Controls[Index]);
  515. if (Button != NULL)
  516. {
  517. if (Button->Default)
  518. {
  519. assert(FDefaultButton == NULL);
  520. FDefaultButton = Button;
  521. Button->Default = false;
  522. }
  523. if (Button->Cancel)
  524. {
  525. assert(FCancelButton == NULL);
  526. FCancelButton = Button;
  527. Button->Cancel = false;
  528. }
  529. }
  530. }
  531. TPoint Origin(PopupParent->Left, PopupParent->Top + PopupParent->Height);
  532. Origin = Parent->ScreenToClient(PopupParent->Parent->ClientToScreen(Origin));
  533. if (Origin.x + Width > Parent->ClientWidth)
  534. {
  535. Origin.x += PopupParent->Width - Width;
  536. }
  537. Left = Origin.x;
  538. Top = Origin.y;
  539. Show();
  540. SetFocus();
  541. }
  542. //---------------------------------------------------------------------------
  543. void __fastcall TRightsFrame::CloseUp()
  544. {
  545. assert(FPopup);
  546. assert(Visible);
  547. if (!Focused())
  548. {
  549. // this can happen only if called from on-click handler of drop down button
  550. DoCloseUp();
  551. }
  552. FPopupParent->SetFocus();
  553. }
  554. //---------------------------------------------------------------------------
  555. UnicodeString __fastcall TRightsFrame::GetText()
  556. {
  557. UnicodeString Result = Rights.Text;
  558. if (AllowAddXToDirectories && DirectoriesXEffective() && AddXToDirectories)
  559. {
  560. Result = FORMAT(L"%s (%s)", (Result, FAddXToDirectoriesSuffix));
  561. }
  562. return Result;
  563. }
  564. //---------------------------------------------------------------------------
  565. void __fastcall TRightsFrame::SetText(UnicodeString value)
  566. {
  567. if (Text != value)
  568. {
  569. UnicodeString RightsStr = value;
  570. int P = RightsStr.LowerCase().Pos(FAddXToDirectoriesSuffix);
  571. bool AAddXToDirectories = (P > 0);
  572. if (AAddXToDirectories)
  573. {
  574. RightsStr.Delete(P, FAddXToDirectoriesSuffix.Length());
  575. }
  576. RightsStr = DeleteChar(DeleteChar(RightsStr, L'('), L')').Trim();
  577. TRights R = Rights;
  578. if (((RightsStr.Length() == 3) || (RightsStr.Length() == 4)) &&
  579. IsNumber(RightsStr))
  580. {
  581. R.Octal = RightsStr;
  582. }
  583. else
  584. {
  585. R.Text = RightsStr;
  586. }
  587. Rights = R;
  588. AddXToDirectories = AAddXToDirectories;
  589. }
  590. }
  591. //---------------------------------------------------------------------------
  592. void __fastcall TRightsFrame::RightsPopupPopup(TObject * /*Sender*/)
  593. {
  594. if (!HasFocus())
  595. {
  596. SetFocus();
  597. }
  598. }
  599. //---------------------------------------------------------------------------
  600. void __fastcall TRightsFrame::FrameContextPopup(TObject * Sender,
  601. TPoint & MousePos, bool & Handled)
  602. {
  603. MenuPopup(Sender, MousePos, Handled);
  604. }
  605. //---------------------------------------------------------------------------