Rights.cpp 17 KB

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