Rights.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Rights.h"
  5. #include <Common.h>
  6. #include <VCLCommon.h>
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "GrayedCheckBox"
  10. #pragma resource "*.dfm"
  11. //---------------------------------------------------------------------------
  12. __fastcall TRightsFrame::TRightsFrame(TComponent* Owner)
  13. : TFrame(Owner)
  14. {
  15. FOnChange = NULL;
  16. FAllowAddXToDirectories = true;
  17. // to avoid duplication of reference in forms that uses the frame
  18. PopupMenu = RightsPopup;
  19. }
  20. //---------------------------------------------------------------------------
  21. __fastcall TRightsFrame::~TRightsFrame()
  22. {
  23. }
  24. //---------------------------------------------------------------------------
  25. void __fastcall TRightsFrame::SetStates(TRights::TRight Right, TRights::TState value)
  26. {
  27. assert(((value == TRights::rsNo) || (value == TRights::rsYes) || (value == TRights::rsUndef)));
  28. TCheckBox * CheckBox = Checks[Right];
  29. if (CheckBox != NULL)
  30. {
  31. switch (value) {
  32. case TRights::rsNo: CheckBox->State = cbUnchecked; break;
  33. case TRights::rsYes: CheckBox->State = cbChecked; break;
  34. case TRights::rsUndef: CheckBox->State = cbGrayed; break;
  35. }
  36. }
  37. }
  38. //---------------------------------------------------------------------------
  39. TRights::TState __fastcall TRightsFrame::GetStates(TRights::TRight Right)
  40. {
  41. TCheckBox * CheckBox = Checks[Right];
  42. if (CheckBox != NULL)
  43. {
  44. switch (CheckBox->State) {
  45. case cbUnchecked: return TRights::rsNo;
  46. case cbChecked: return TRights::rsYes;
  47. case cbGrayed:
  48. default: return TRights::rsUndef;
  49. }
  50. }
  51. else
  52. {
  53. return TRights::rsNo;
  54. }
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TRightsFrame::SetRights(const TRights & value)
  58. {
  59. AllowUndef = true; // temporarily
  60. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  61. {
  62. States[static_cast<TRights::TRight>(Right)] =
  63. value.RightUndef[static_cast<TRights::TRight>(Right)];
  64. }
  65. AllowUndef = value.AllowUndef;
  66. UpdateControls();
  67. }
  68. //---------------------------------------------------------------------------
  69. TRights __fastcall TRightsFrame::GetRights()
  70. {
  71. TRights Result;
  72. Result.AllowUndef = AllowUndef;
  73. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  74. {
  75. Result.RightUndef[static_cast<TRights::TRight>(Right)] =
  76. States[static_cast<TRights::TRight>(Right)];
  77. }
  78. return Result;
  79. }
  80. //---------------------------------------------------------------------------
  81. void __fastcall TRightsFrame::SetAllowUndef(bool value)
  82. {
  83. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  84. {
  85. TCheckBox * CheckBox = Checks[static_cast<TRights::TRight>(Right)];
  86. if (CheckBox != NULL)
  87. {
  88. CheckBox->AllowGrayed = value;
  89. }
  90. }
  91. }
  92. //---------------------------------------------------------------------------
  93. bool __fastcall TRightsFrame::GetAllowUndef()
  94. {
  95. bool Result = false, First = true;
  96. for (int Right = TRights::rrFirst; Right <= TRights::rrLast; Right++)
  97. {
  98. TCheckBox * Check = Checks[static_cast<TRights::TRight>(Right)];
  99. if (Check != NULL)
  100. {
  101. if (First)
  102. {
  103. Result = Check->AllowGrayed;
  104. First = false;
  105. }
  106. else if (Result != Check->AllowGrayed)
  107. {
  108. assert(false);
  109. }
  110. }
  111. }
  112. return Result;
  113. }
  114. //---------------------------------------------------------------------------
  115. TCheckBox * __fastcall TRightsFrame::GetChecks(TRights::TRight Right)
  116. {
  117. for (int Index = 0; Index < ControlCount; Index++)
  118. {
  119. if (Controls[Index]->InheritsFrom(__classid(TCheckBox)) &&
  120. (Controls[Index]->Tag == TRights::RightToFlag(Right)))
  121. {
  122. return ((TCheckBox *)Controls[Index]);
  123. }
  124. }
  125. return NULL;
  126. }
  127. //---------------------------------------------------------------------------
  128. void __fastcall TRightsFrame::SetAddXToDirectories(bool value)
  129. {
  130. DirectoriesXCheck->Checked = value;
  131. }
  132. //---------------------------------------------------------------------------
  133. bool __fastcall TRightsFrame::GetAddXToDirectories()
  134. {
  135. return DirectoriesXCheck->Checked;
  136. }
  137. //---------------------------------------------------------------------------
  138. void __fastcall TRightsFrame::ControlChange(TObject * /*Sender*/)
  139. {
  140. UpdateControls();
  141. }
  142. //---------------------------------------------------------------------------
  143. void __fastcall TRightsFrame::UpdateControls()
  144. {
  145. DirectoriesXCheck->Visible = AllowAddXToDirectories;
  146. EnableControl(DirectoriesXCheck,
  147. Enabled && !((Rights.NumberSet & TRights::rfExec) == TRights::rfExec));
  148. DoChange();
  149. }
  150. //---------------------------------------------------------------------------
  151. void __fastcall TRightsFrame::CycleRights(int Group)
  152. {
  153. TRights::TState State;
  154. bool Same = true;
  155. for (int Right = 0; Right < 3; Right++)
  156. {
  157. TRights::TState CState = States[static_cast<TRights::TRight>(
  158. TRights::rrUserRead + Right + ((Group - 1) * 3))];
  159. if (Right == 0) State = CState;
  160. else
  161. if (State != CState) Same = False;
  162. }
  163. if (!Same)
  164. {
  165. State = TRights::rsYes;
  166. }
  167. else
  168. {
  169. switch (State) {
  170. case TRights::rsYes:
  171. State = TRights::rsNo;
  172. break;
  173. case TRights::rsNo:
  174. State = AllowUndef ? TRights::rsUndef : TRights::rsYes;
  175. break;
  176. case TRights::rsUndef:
  177. State = TRights::rsYes;
  178. break;
  179. }
  180. }
  181. for (int Right = 0; Right < 3; Right++)
  182. {
  183. States[static_cast<TRights::TRight>(
  184. TRights::rrUserRead + Right + ((Group - 1) * 3))] = State;
  185. }
  186. UpdateControls();
  187. }
  188. //---------------------------------------------------------------------------
  189. void __fastcall TRightsFrame::RightsButtonsClick(TObject *Sender)
  190. {
  191. CycleRights(((TComponent*)Sender)->Tag);
  192. }
  193. //---------------------------------------------------------------------------
  194. void __fastcall TRightsFrame::SetAllowAddXToDirectories(bool value)
  195. {
  196. if (FAllowAddXToDirectories != value)
  197. {
  198. FAllowAddXToDirectories = value;
  199. UpdateControls();
  200. }
  201. }
  202. //---------------------------------------------------------------------------
  203. void __fastcall TRightsFrame::DoChange()
  204. {
  205. if (FOnChange)
  206. {
  207. FOnChange(this);
  208. }
  209. }
  210. //---------------------------------------------------------------------------
  211. void __fastcall TRightsFrame::SetEnabled(bool Value)
  212. {
  213. TFrame::SetEnabled(Value);
  214. UpdateControls();
  215. }
  216. //---------------------------------------------------------------------------
  217. void __fastcall TRightsFrame::ForceUpdate()
  218. {
  219. }
  220. //---------------------------------------------------------------------------
  221. void __fastcall TRightsFrame::RightsActionsExecute(TBasicAction * Action,
  222. bool & Handled)
  223. {
  224. TRights R = Rights;
  225. R.Number = TRights::rfNo;
  226. Handled = true;
  227. if (Action == NoRightsAction)
  228. {
  229. R = TRights::rfNo;
  230. }
  231. else if (Action == DefaultRightsAction)
  232. {
  233. R = TRights::rfDefault;
  234. }
  235. else if (Action == AllRightsAction)
  236. {
  237. R = TRights::rfAll;
  238. }
  239. else if (Action == LeaveRightsAsIsAction)
  240. {
  241. R.AllUndef();
  242. }
  243. else
  244. {
  245. Handled = false;
  246. }
  247. Rights = R;
  248. ForceUpdate();
  249. }
  250. //---------------------------------------------------------------------------
  251. void __fastcall TRightsFrame::RightsActionsUpdate(TBasicAction *Action,
  252. bool &Handled)
  253. {
  254. TRights R = Rights;
  255. Handled = true;
  256. if (Action == NoRightsAction)
  257. {
  258. NoRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfNo);
  259. }
  260. else if (Action == DefaultRightsAction)
  261. {
  262. DefaultRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfDefault);
  263. }
  264. else if (Action == AllRightsAction)
  265. {
  266. AllRightsAction->Checked = !R.IsUndef && (R.NumberSet == TRights::rfAll);
  267. }
  268. else if (Action == LeaveRightsAsIsAction)
  269. {
  270. LeaveRightsAsIsAction->Enabled = R.AllowUndef;
  271. LeaveRightsAsIsAction->Checked = (R.NumberSet == TRights::rfNo) &&
  272. (R.NumberUnset == TRights::rfNo);
  273. }
  274. else
  275. {
  276. Handled = false;
  277. }
  278. }
  279. //---------------------------------------------------------------------------