tabs.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. unit tabs;
  2. interface
  3. uses
  4. objects,drivers,views;
  5. type
  6. PTabItem = ^TTabItem;
  7. TTabItem = record
  8. Next : PTabItem;
  9. View : PView;
  10. Dis : boolean;
  11. end;
  12. PTabDef = ^TTabDef;
  13. TTabDef = record
  14. Next : PTabDef;
  15. Name : PString;
  16. Items : PTabItem;
  17. DefItem : PView;
  18. ShortCut : char;
  19. end;
  20. PTab = ^TTab;
  21. TTab = object(TGroup)
  22. TabDefs : PTabDef;
  23. ActiveDef : integer;
  24. DefCount : word;
  25. constructor Init(var Bounds: TRect; ATabDef: PTabDef);
  26. function AtTab(Index: integer): PTabDef; virtual;
  27. procedure SelectTab(Index: integer); virtual;
  28. function TabCount: integer;
  29. function Valid(Command: Word): Boolean; virtual;
  30. procedure ChangeBounds(var Bounds: TRect); virtual;
  31. procedure HandleEvent(var Event: TEvent); virtual;
  32. function GetPalette: PPalette; virtual;
  33. procedure Draw; virtual;
  34. procedure SetData(var Rec);virtual;
  35. procedure GetData(var Rec);virtual;
  36. procedure SetState(AState: Word; Enable: Boolean); virtual;
  37. destructor Done; virtual;
  38. private
  39. InDraw: boolean;
  40. end;
  41. function NewTabItem(AView: PView; ANext: PTabItem): PTabItem;
  42. procedure DisposeTabItem(P: PTabItem);
  43. function NewTabDef(AName: string; ADefItem: PView; AItems: PTabItem; ANext: PTabDef): PTabDef;
  44. procedure DisposeTabDef(P: PTabDef);
  45. implementation
  46. uses
  47. FvCommon,dialogs;
  48. constructor TTab.Init(var Bounds: TRect; ATabDef: PTabDef);
  49. begin
  50. inherited Init(Bounds);
  51. Options:=Options or ofSelectable or ofFirstClick or ofPreProcess or ofPostProcess;
  52. GrowMode:=gfGrowHiX+gfGrowHiY+gfGrowRel;
  53. TabDefs:=ATabDef;
  54. ActiveDef:=-1;
  55. SelectTab(0);
  56. ReDraw;
  57. end;
  58. function TTab.TabCount: integer;
  59. var i: integer;
  60. P: PTabDef;
  61. begin
  62. I:=0; P:=TabDefs;
  63. while (P<>nil) do
  64. begin
  65. Inc(I);
  66. P:=P^.Next;
  67. end;
  68. TabCount:=I;
  69. end;
  70. function TTab.AtTab(Index: integer): PTabDef;
  71. var i: integer;
  72. P: PTabDef;
  73. begin
  74. i:=0; P:=TabDefs;
  75. while (I<Index) do
  76. begin
  77. if P=nil then RunError($AA);
  78. P:=P^.Next;
  79. Inc(i);
  80. end;
  81. AtTab:=P;
  82. end;
  83. procedure TTab.SelectTab(Index: integer);
  84. var P: PTabItem;
  85. V: PView;
  86. begin
  87. if ActiveDef<>Index then
  88. begin
  89. if Owner<>nil then Owner^.Lock;
  90. Lock;
  91. { --- Update --- }
  92. if TabDefs<>nil then
  93. begin
  94. DefCount:=1;
  95. while AtTab(DefCount-1)^.Next<>nil do Inc(DefCount);
  96. end
  97. else DefCount:=0;
  98. if ActiveDef<>-1 then
  99. begin
  100. P:=AtTab(ActiveDef)^.Items;
  101. while P<>nil do
  102. begin
  103. if P^.View<>nil then Delete(P^.View);
  104. P:=P^.Next;
  105. end;
  106. end;
  107. ActiveDef:=Index;
  108. P:=AtTab(ActiveDef)^.Items;
  109. while P<>nil do
  110. begin
  111. if P^.View<>nil then Insert(P^.View);
  112. P:=P^.Next;
  113. end;
  114. V:=AtTab(ActiveDef)^.DefItem;
  115. if V<>nil then V^.Select;
  116. ReDraw;
  117. { --- Update --- }
  118. UnLock;
  119. if Owner<>nil then Owner^.UnLock;
  120. DrawView;
  121. end;
  122. end;
  123. procedure TTab.ChangeBounds(var Bounds: TRect);
  124. var D: TPoint;
  125. procedure DoCalcChange(P: PView); {$ifndef FPC}far;{$endif}
  126. var
  127. R: TRect;
  128. begin
  129. if P^.Owner=nil then Exit; { it think this is a bug in TV }
  130. P^.CalcBounds(R, D);
  131. P^.ChangeBounds(R);
  132. end;
  133. var
  134. P: PTabItem;
  135. I: integer;
  136. begin
  137. D.X := Bounds.B.X - Bounds.A.X - Size.X;
  138. D.Y := Bounds.B.Y - Bounds.A.Y - Size.Y;
  139. inherited ChangeBounds(Bounds);
  140. for I:=0 to TabCount-1 do
  141. if I<>ActiveDef then
  142. begin
  143. P:=AtTab(I)^.Items;
  144. while P<>nil do
  145. begin
  146. if P^.View<>nil then DoCalcChange(P^.View);
  147. P:=P^.Next;
  148. end;
  149. end;
  150. end;
  151. procedure TTab.HandleEvent(var Event: TEvent);
  152. var Index : integer;
  153. I : integer;
  154. X : integer;
  155. Len : byte;
  156. P : TPoint;
  157. V : PView;
  158. CallOrig: boolean;
  159. LastV : PView;
  160. FirstV: PView;
  161. function FirstSelectable: PView;
  162. var
  163. FV : PView;
  164. begin
  165. FV := First;
  166. while (FV<>nil) and ((FV^.Options and ofSelectable)=0) and (FV<>Last) do
  167. FV:=FV^.Next;
  168. if FV<>nil then
  169. if (FV^.Options and ofSelectable)=0 then FV:=nil;
  170. FirstSelectable:=FV;
  171. end;
  172. function LastSelectable: PView;
  173. var
  174. LV : PView;
  175. begin
  176. LV := Last;
  177. while (LV<>nil) and ((LV^.Options and ofSelectable)=0) and (LV<>First) do
  178. LV:=LV^.Prev;
  179. if LV<>nil then
  180. if (LV^.Options and ofSelectable)=0 then LV:=nil;
  181. LastSelectable:=LV;
  182. end;
  183. begin
  184. if (Event.What and evMouseDown)<>0 then
  185. begin
  186. MakeLocal(Event.Where,P);
  187. if P.Y<3 then
  188. begin
  189. Index:=-1; X:=1;
  190. for i:=0 to DefCount-1 do
  191. begin
  192. Len:=CStrLen(AtTab(i)^.Name^);
  193. if (P.X>=X) and (P.X<=X+Len+1) then Index:=i;
  194. X:=X+Len+3;
  195. end;
  196. if Index<>-1 then
  197. SelectTab(Index);
  198. end;
  199. end;
  200. if Event.What=evKeyDown then
  201. begin
  202. Index:=-1;
  203. case Event.KeyCode of
  204. kbTab,kbShiftTab :
  205. if GetState(sfSelected) then
  206. begin
  207. if Current<>nil then
  208. begin
  209. LastV:=LastSelectable; FirstV:=FirstSelectable;
  210. if ((Current=LastV) or (Current=PLabel(LastV)^.Link)) and (Event.KeyCode=kbShiftTab) then
  211. begin
  212. if Owner<>nil then Owner^.SelectNext(true);
  213. end else
  214. if ((Current=FirstV) or (Current=PLabel(FirstV)^.Link)) and (Event.KeyCode=kbTab) then
  215. begin
  216. Lock;
  217. if Owner<>nil then Owner^.SelectNext(false);
  218. UnLock;
  219. end else
  220. SelectNext(Event.KeyCode=kbShiftTab);
  221. ClearEvent(Event);
  222. end;
  223. end;
  224. else
  225. for I:=0 to DefCount-1 do
  226. begin
  227. if Upcase(GetAltChar(Event.KeyCode))=AtTab(I)^.ShortCut
  228. then begin
  229. Index:=I;
  230. ClearEvent(Event);
  231. Break;
  232. end;
  233. end;
  234. end;
  235. if Index<>-1 then
  236. begin
  237. Select;
  238. SelectTab(Index);
  239. V:=AtTab(ActiveDef)^.DefItem;
  240. if V<>nil then V^.Focus;
  241. end;
  242. end;
  243. CallOrig:=true;
  244. if Event.What=evKeyDown then
  245. begin
  246. if ((Owner<>nil) and (Owner^.Phase=phPostProcess) and (GetAltChar(Event.KeyCode)<>#0)) or GetState(sfFocused)
  247. then
  248. else CallOrig:=false;
  249. end;
  250. if CallOrig then inherited HandleEvent(Event);
  251. end;
  252. function TTab.GetPalette: PPalette;
  253. begin
  254. GetPalette:=nil;
  255. end;
  256. procedure TTab.Draw;
  257. var B : TDrawBuffer;
  258. i : integer;
  259. C1,C2,C3,C : word;
  260. HeaderLen : integer;
  261. X,X2 : integer;
  262. Name : PString;
  263. ActiveKPos : integer;
  264. ActiveVPos : integer;
  265. FC : char;
  266. procedure SWriteBuf(X,Y,W,H: integer; var Buf);
  267. var i: integer;
  268. begin
  269. if Y+H>Size.Y then H:=Size.Y-Y;
  270. if X+W>Size.X then W:=Size.X-X;
  271. if Buffer=nil then WriteBuf(X,Y,W,H,Buf)
  272. else for i:=1 to H do
  273. Move(Buf,Buffer^[X+(Y+i-1)*Size.X],W*2);
  274. end;
  275. procedure ClearBuf;
  276. begin
  277. MoveChar(B,' ',C1,Size.X);
  278. end;
  279. begin
  280. if InDraw then Exit;
  281. InDraw:=true;
  282. { - Start of TGroup.Draw - }
  283. { if Buffer = nil then
  284. begin
  285. GetBuffer;
  286. end; }
  287. { - Start of TGroup.Draw - }
  288. C1:=GetColor(1);
  289. C2:=(GetColor(7) and $f0 or $08)+GetColor(9)*256;
  290. C3:=GetColor(8)+GetColor({9}8)*256;
  291. { Calculate the size of the headers }
  292. HeaderLen:=0;
  293. for i:=0 to DefCount-1 do
  294. HeaderLen:=HeaderLen+CStrLen(AtTab(i)^.Name^)+3;
  295. Dec(HeaderLen);
  296. if HeaderLen>Size.X-2 then HeaderLen:=Size.X-2;
  297. { --- 1. sor --- }
  298. ClearBuf; MoveChar(B[0],'³',C1,1); MoveChar(B[HeaderLen+1],'³',C1,1);
  299. X:=1;
  300. for i:=0 to DefCount-1 do
  301. begin
  302. Name:=AtTab(i)^.Name; X2:=CStrLen(Name^);
  303. if i=ActiveDef
  304. then begin
  305. ActiveKPos:=X-1;
  306. ActiveVPos:=X+X2+2;
  307. if GetState(sfFocused) then C:=C3 else C:=C2;
  308. end
  309. else C:=C2;
  310. MoveCStr(B[X],' '+Name^+' ',C); X:=X+X2+3;
  311. MoveChar(B[X-1],'³',C1,1);
  312. end;
  313. SWriteBuf(0,1,Size.X,1,B);
  314. { --- 0. sor --- }
  315. ClearBuf; MoveChar(B[0],'Ú',C1,1);
  316. X:=1;
  317. for i:=0 to DefCount-1 do
  318. begin
  319. if I<ActiveDef then FC:='Ú'
  320. else FC:='¿';
  321. X2:=CStrLen(AtTab(i)^.Name^)+2;
  322. MoveChar(B[X+X2],{'Â'}FC,C1,1);
  323. if i=DefCount-1 then X2:=X2+1;
  324. if X2>0 then
  325. MoveChar(B[X],'Ä',C1,X2);
  326. X:=X+X2+1;
  327. end;
  328. MoveChar(B[HeaderLen+1],'¿',C1,1);
  329. MoveChar(B[ActiveKPos],'Ú',C1,1); MoveChar(B[ActiveVPos],'¿',C1,1);
  330. SWriteBuf(0,0,Size.X,1,B);
  331. { --- 2. sor --- }
  332. MoveChar(B[1],'Ä',C1,Max(HeaderLen,0)); MoveChar(B[HeaderLen+2],'Ä',C1,Max(Size.X-HeaderLen-3,0));
  333. MoveChar(B[Size.X-1],'¿',C1,1);
  334. MoveChar(B[ActiveKPos],'Ù',C1,1);
  335. if ActiveDef=0 then MoveChar(B[0],'³',C1,1)
  336. else MoveChar(B[0],{'Ã'}'Ú',C1,1);
  337. MoveChar(B[HeaderLen+1],'Ä'{'Á'},C1,1); MoveChar(B[ActiveVPos],'À',C1,1);
  338. MoveChar(B[ActiveKPos+1],' ',C1,Max(ActiveVPos-ActiveKPos-1,0));
  339. SWriteBuf(0,2,Size.X,1,B);
  340. { --- marad‚k sor --- }
  341. ClearBuf; MoveChar(B[0],'³',C1,1); MoveChar(B[Size.X-1],'³',C1,1);
  342. SWriteBuf(0,3,Size.X,Size.Y-4,B);
  343. { --- Size.X . sor --- }
  344. MoveChar(B[0],'À',C1,1); MoveChar(B[1],'Ä',C1,Max(Size.X-2,0)); MoveChar(B[Size.X-1],'Ù',C1,1);
  345. SWriteBuf(0,Size.Y-1,Size.X,1,B);
  346. { - End of TGroup.Draw - }
  347. if Buffer <> nil then
  348. begin
  349. Lock;
  350. Redraw;
  351. UnLock;
  352. end;
  353. if Buffer <> nil then
  354. WriteBuf(0, 0, Size.X, Size.Y, Buffer^)
  355. else
  356. Redraw;
  357. { - End of TGroup.Draw - }
  358. InDraw:=false;
  359. end;
  360. function TTab.Valid(Command: Word): Boolean;
  361. var PT : PTabDef;
  362. PI : PTabItem;
  363. OK : boolean;
  364. begin
  365. OK:=true;
  366. PT:=TabDefs;
  367. while (PT<>nil) and (OK=true) do
  368. begin
  369. PI:=PT^.Items;
  370. while (PI<>nil) and (OK=true) do
  371. begin
  372. if PI^.View<>nil then OK:=OK and PI^.View^.Valid(Command);
  373. PI:=PI^.Next;
  374. end;
  375. PT:=PT^.Next;
  376. end;
  377. Valid:=OK;
  378. end;
  379. procedure TTab.SetData(var Rec);
  380. type
  381. Bytes = array[0..65534] of Byte;
  382. var
  383. I: Sw_Word;
  384. PT : PTabDef;
  385. PI : PTabItem;
  386. begin
  387. I := 0;
  388. PT:=TabDefs;
  389. while (PT<>nil) do
  390. begin
  391. PI:=PT^.Items;
  392. while (PI<>nil) do
  393. begin
  394. if PI^.View<>nil then
  395. begin
  396. PI^.View^.SetData(Bytes(Rec)[I]);
  397. Inc(I, PI^.View^.DataSize);
  398. end;
  399. PI:=PI^.Next;
  400. end;
  401. PT:=PT^.Next;
  402. end;
  403. end;
  404. procedure TTab.GetData(var Rec);
  405. type
  406. Bytes = array[0..65534] of Byte;
  407. var
  408. I: Sw_Word;
  409. PT : PTabDef;
  410. PI : PTabItem;
  411. begin
  412. I := 0;
  413. PT:=TabDefs;
  414. while (PT<>nil) do
  415. begin
  416. PI:=PT^.Items;
  417. while (PI<>nil) do
  418. begin
  419. if PI^.View<>nil then
  420. begin
  421. PI^.View^.GetData(Bytes(Rec)[I]);
  422. Inc(I, PI^.View^.DataSize);
  423. end;
  424. PI:=PI^.Next;
  425. end;
  426. PT:=PT^.Next;
  427. end;
  428. end;
  429. procedure TTab.SetState(AState: Word; Enable: Boolean);
  430. begin
  431. inherited SetState(AState,Enable);
  432. if (AState and sfFocused)<>0 then DrawView;
  433. end;
  434. destructor TTab.Done;
  435. var P,X: PTabDef;
  436. procedure DeleteViews(P: PView); {$ifndef FPC}far;{$endif}
  437. begin
  438. if P<>nil then Delete(P);
  439. end;
  440. begin
  441. ForEach(@DeleteViews);
  442. inherited Done;
  443. P:=TabDefs;
  444. while P<>nil do
  445. begin
  446. X:=P^.Next;
  447. DisposeTabDef(P);
  448. P:=X;
  449. end;
  450. end;
  451. function NewTabItem(AView: PView; ANext: PTabItem): PTabItem;
  452. var P: PTabItem;
  453. begin
  454. New(P); FillChar(P^,SizeOf(P^),0);
  455. P^.Next:=ANext; P^.View:=AView;
  456. NewTabItem:=P;
  457. end;
  458. procedure DisposeTabItem(P: PTabItem);
  459. begin
  460. if P<>nil then
  461. begin
  462. if P^.View<>nil then Dispose(P^.View, Done);
  463. Dispose(P);
  464. end;
  465. end;
  466. function NewTabDef(AName: string; ADefItem: PView; AItems: PTabItem; ANext: PTabDef): PTabDef;
  467. var P: PTabDef;
  468. x: byte;
  469. begin
  470. New(P);
  471. P^.Next:=ANext; P^.Name:=NewStr(AName); P^.Items:=AItems;
  472. x:=pos('~',AName);
  473. if (x<>0) and (x<length(AName)) then P^.ShortCut:=Upcase(AName[x+1])
  474. else P^.ShortCut:=#0;
  475. P^.DefItem:=ADefItem;
  476. NewTabDef:=P;
  477. end;
  478. procedure DisposeTabDef(P: PTabDef);
  479. var PI,X: PTabItem;
  480. begin
  481. DisposeStr(P^.Name);
  482. PI:=P^.Items;
  483. while PI<>nil do
  484. begin
  485. X:=PI^.Next;
  486. DisposeTabItem(PI);
  487. PI:=X;
  488. end;
  489. Dispose(P);
  490. end;
  491. end.