Rights.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. DebugAssert(((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. DebugFail();
  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. DebugAssert(!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, true))
  313. {
  314. Text = S;
  315. }
  316. // trigger on change event, even if no change actually occurred to
  317. // allow parent form to visualize feedback of an action
  318. DoChange();
  319. // Update octal edit in RightsExt even if it has focus
  320. ForceUpdate();
  321. Changed = false;
  322. }
  323. else
  324. {
  325. Handled = false;
  326. }
  327. if (Changed)
  328. {
  329. Rights = R;
  330. ForceUpdate();
  331. DoChange();
  332. }
  333. }
  334. }
  335. //---------------------------------------------------------------------------
  336. void __fastcall TRightsFrame::RightsActionsUpdate(TBasicAction *Action,
  337. bool &Handled)
  338. {
  339. TRights R = Rights;
  340. Handled = true;
  341. if (Action == NoRightsAction)
  342. {
  343. // explicitly enable as action gets disabled, if its shortcut is pressed,
  344. // while the form does not have a focus and OnExecute handler denies the execution
  345. NoRightsAction->Enabled = true;
  346. NoRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfNo);
  347. }
  348. else if (Action == DefaultRightsAction)
  349. {
  350. DefaultRightsAction->Enabled = true;
  351. DefaultRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfDefault);
  352. }
  353. else if (Action == AllRightsAction)
  354. {
  355. AllRightsAction->Enabled = true;
  356. AllRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfAll);
  357. }
  358. else if (Action == LeaveRightsAsIsAction)
  359. {
  360. LeaveRightsAsIsAction->Enabled = true;
  361. LeaveRightsAsIsAction->Visible = R.AllowUndef;
  362. LeaveRightsAsIsAction->Checked = (R.NumberSet == TRights::rfNo) &&
  363. (R.NumberUnset == TRights::rfNo);
  364. }
  365. else if (Action == CopyTextAction)
  366. {
  367. CopyTextAction->Enabled = !R.IsUndef;
  368. }
  369. else if (Action == CopyOctalAction)
  370. {
  371. CopyOctalAction->Enabled = !R.IsUndef;
  372. }
  373. else if (Action == PasteAction)
  374. {
  375. PasteAction->Enabled = IsFormatInClipboard(CF_TEXT);
  376. }
  377. else
  378. {
  379. Handled = false;
  380. }
  381. }
  382. //---------------------------------------------------------------------------
  383. void __fastcall TRightsFrame::CreateParams(TCreateParams & Params)
  384. {
  385. TFrame::CreateParams(Params);
  386. if (FPopup)
  387. {
  388. Params.Style |= WS_BORDER;
  389. }
  390. }
  391. //---------------------------------------------------------------------------
  392. void __fastcall TRightsFrame::CreateWnd()
  393. {
  394. if (FPopup)
  395. {
  396. Width += 2 * GetSystemMetricsForControl(Parent, SM_CXBORDER);
  397. Height += 2 * GetSystemMetricsForControl(Parent, SM_CYBORDER);
  398. }
  399. TFrame::CreateWnd();
  400. }
  401. //---------------------------------------------------------------------------
  402. void __fastcall TRightsFrame::SetPopup(bool value)
  403. {
  404. if (Popup != value)
  405. {
  406. FPopup = value;
  407. Visible = !FPopup;
  408. }
  409. }
  410. //---------------------------------------------------------------------------
  411. void __fastcall TRightsFrame::DoCloseUp()
  412. {
  413. Hide();
  414. if (FDefaultButton != NULL)
  415. {
  416. FDefaultButton->Default = true;
  417. FDefaultButton = NULL;
  418. }
  419. if (FCancelButton != NULL)
  420. {
  421. FCancelButton->Cancel = true;
  422. FCancelButton = NULL;
  423. }
  424. }
  425. //---------------------------------------------------------------------------
  426. void __fastcall TRightsFrame::DoExit()
  427. {
  428. // this is bit hack to make frame hide when ComboEdit button is pressed while
  429. // from is poped-up already.
  430. if (FPopup && !IsAncestor(Screen->ActiveControl, FPopupParent))
  431. {
  432. DoCloseUp();
  433. }
  434. TFrame::DoExit();
  435. }
  436. //---------------------------------------------------------------------------
  437. bool __fastcall TRightsFrame::IsAncestor(TControl * Control, TControl * Ancestor)
  438. {
  439. while ((Control != NULL) &&
  440. (Control->Parent != Ancestor))
  441. {
  442. Control = Control->Parent;
  443. }
  444. return (Control != NULL);
  445. }
  446. //---------------------------------------------------------------------------
  447. void __fastcall TRightsFrame::WMContextMenu(TWMContextMenu & Message)
  448. {
  449. FPopingContextMenu = true;
  450. try
  451. {
  452. TFrame::Dispatch(&Message);
  453. }
  454. __finally
  455. {
  456. FPopingContextMenu = false;
  457. }
  458. }
  459. //---------------------------------------------------------------------------
  460. void __fastcall TRightsFrame::CMDialogKey(TCMDialogKey & Message)
  461. {
  462. if (FPopup && Visible &&
  463. ((Message.CharCode == VK_RETURN) ||
  464. (Message.CharCode == VK_ESCAPE)) &&
  465. KeyDataToShiftState(Message.KeyData).Empty())
  466. {
  467. CloseUp();
  468. Message.Result = 1;
  469. }
  470. else
  471. {
  472. TFrame::Dispatch(&Message);
  473. }
  474. }
  475. //---------------------------------------------------------------------------
  476. void __fastcall TRightsFrame::CMCancelMode(TCMCancelMode & Message)
  477. {
  478. if (FPopup && Visible && !FPopingContextMenu &&
  479. ((Message.Sender == NULL) ||
  480. (!IsAncestor(Message.Sender, this) &&
  481. !IsAncestor(Message.Sender, FPopupParent) &&
  482. (Message.Sender != this))))
  483. {
  484. CloseUp();
  485. }
  486. TFrame::Dispatch(&Message);
  487. }
  488. //---------------------------------------------------------------------------
  489. void __fastcall TRightsFrame::Dispatch(void * Message)
  490. {
  491. TMessage & AMessage = *static_cast<TMessage *>(Message);
  492. switch (AMessage.Msg)
  493. {
  494. case CM_CANCELMODE:
  495. CMCancelMode(*(TCMCancelMode *)Message);
  496. break;
  497. case CM_DIALOGKEY:
  498. CMDialogKey(*(TCMDialogKey *)Message);
  499. break;
  500. case WM_CONTEXTMENU:
  501. WMContextMenu(*(TWMContextMenu *)Message);
  502. break;
  503. default:
  504. TFrame::Dispatch(Message);
  505. break;
  506. }
  507. }
  508. //---------------------------------------------------------------------------
  509. void __fastcall TRightsFrame::DropDown()
  510. {
  511. TCustomForm * Form = GetParentForm(this);
  512. // due to lack of better idea, we clear "default" and "cancel" flags of respective
  513. // form buttons to prevent them to handle ESC/ENTER keys.
  514. for (int Index = 0; Index < Form->ControlCount; Index++)
  515. {
  516. TButton * Button = dynamic_cast<TButton *>(Form->Controls[Index]);
  517. if (Button != NULL)
  518. {
  519. if (Button->Default)
  520. {
  521. DebugAssert(FDefaultButton == NULL);
  522. FDefaultButton = Button;
  523. Button->Default = false;
  524. }
  525. if (Button->Cancel)
  526. {
  527. DebugAssert(FCancelButton == NULL);
  528. FCancelButton = Button;
  529. Button->Cancel = false;
  530. }
  531. }
  532. }
  533. TPoint Origin(PopupParent->Left, PopupParent->Top + PopupParent->Height);
  534. Origin = Parent->ScreenToClient(PopupParent->Parent->ClientToScreen(Origin));
  535. if (Origin.x + Width > Parent->ClientWidth)
  536. {
  537. Origin.x += PopupParent->Width - Width;
  538. }
  539. Left = Origin.x;
  540. Top = Origin.y;
  541. Show();
  542. SetFocus();
  543. }
  544. //---------------------------------------------------------------------------
  545. void __fastcall TRightsFrame::CloseUp()
  546. {
  547. DebugAssert(FPopup);
  548. DebugAssert(Visible);
  549. if (!Focused())
  550. {
  551. // this can happen only if called from on-click handler of drop down button
  552. DoCloseUp();
  553. }
  554. FPopupParent->SetFocus();
  555. }
  556. //---------------------------------------------------------------------------
  557. UnicodeString __fastcall TRightsFrame::GetText()
  558. {
  559. UnicodeString Result = Rights.Text;
  560. if (AllowAddXToDirectories && DirectoriesXEffective() && AddXToDirectories)
  561. {
  562. Result = FORMAT(L"%s (%s)", (Result, FAddXToDirectoriesSuffix));
  563. }
  564. return Result;
  565. }
  566. //---------------------------------------------------------------------------
  567. void __fastcall TRightsFrame::SetText(UnicodeString value)
  568. {
  569. if (Text != value)
  570. {
  571. UnicodeString RightsStr = value;
  572. int P = RightsStr.LowerCase().Pos(FAddXToDirectoriesSuffix);
  573. bool AAddXToDirectories = (P > 0);
  574. if (AAddXToDirectories)
  575. {
  576. RightsStr.Delete(P, FAddXToDirectoriesSuffix.Length());
  577. }
  578. RightsStr = DeleteChar(DeleteChar(RightsStr, L'('), L')').Trim();
  579. TRights R = Rights;
  580. if (((RightsStr.Length() == 3) || (RightsStr.Length() == 4)) &&
  581. IsNumber(RightsStr))
  582. {
  583. R.Octal = RightsStr;
  584. }
  585. else
  586. {
  587. R.Text = RightsStr;
  588. }
  589. Rights = R;
  590. AddXToDirectories = AAddXToDirectories;
  591. }
  592. }
  593. //---------------------------------------------------------------------------
  594. void __fastcall TRightsFrame::RightsPopupPopup(TObject * /*Sender*/)
  595. {
  596. if (!HasFocus())
  597. {
  598. SetFocus();
  599. }
  600. }
  601. //---------------------------------------------------------------------------
  602. void __fastcall TRightsFrame::FrameContextPopup(TObject * Sender,
  603. TPoint & MousePos, bool & Handled)
  604. {
  605. SelectScaledImageList(RightsImages);
  606. MenuPopup(Sender, MousePos, Handled);
  607. }
  608. //---------------------------------------------------------------------------