Subversion Repositories Kolibri OS

Rev

Rev 9628 | Rev 9730 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     Copyright 2021, 2022 Anton Krotov
  3.  
  4.     This file is part of CEdit.
  5.  
  6.     CEdit is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     CEdit is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with CEdit. If not, see <http://www.gnu.org/licenses/>.
  18. *)
  19.  
  20. MODULE Tabs;
  21.  
  22. IMPORT List, K := KolibriOS, RW, U := Utils;
  23.  
  24. CONST
  25.  
  26.     btnID* = 100;
  27.     btnLeft* = btnID - 1;
  28.         btnRight* = btnID - 2;
  29.     tabHeight* = 22;
  30.     curTabHeight = 26;
  31.     scrWidth = 15;
  32.  
  33. TYPE
  34.  
  35.     tItem = POINTER TO RECORD (List.tItem)
  36.  
  37.         val: RW.tFileName;
  38.         modified: BOOLEAN
  39.  
  40.     END;
  41.  
  42.     tTabs* = POINTER TO RECORD
  43.  
  44.         strings: List.tList;
  45.         first, current: INTEGER;
  46.         width, height: INTEGER;
  47.         x, y, freeX: INTEGER
  48.  
  49.     END;
  50.  
  51.  
  52. PROCEDURE DblClicked* (t: tTabs; x, y: INTEGER): BOOLEAN;
  53.         RETURN (x > t.freeX) & U.between(t.y, y, t.y + t.height - 1)
  54. END DblClicked;
  55.  
  56.  
  57. PROCEDURE drawTab (t: tTabs; id, x, y, width, height: INTEGER; s: ARRAY OF CHAR; modified: BOOLEAN);
  58. VAR
  59.     x2, y2, color: INTEGER;
  60. BEGIN
  61.     IF id = t.current THEN
  62.         INC(height, curTabHeight - tabHeight);
  63.         DEC(y, curTabHeight - tabHeight);
  64.         color := K.colors.light
  65.     ELSE
  66.         color := K.colors.work
  67.     END;
  68.     DEC(x); INC(width);
  69.     x2 := x + width - 1;
  70.     y2 := y + height - 1;
  71.  
  72.     K.DrawRect(x, y, width, height, color);
  73.     K.DrawLine(x, y, x2, y, K.colors.line);
  74.     K.DrawLine(x2, y, x2, y2, K.colors.line);
  75.     IF id # t.current THEN
  76.         K.DrawLine(x2 - 1, y2, x, y2, K.colors.line);
  77.     END;
  78.     K.DrawLine(x, y2, x, y, K.colors.line);
  79.  
  80.     K.DrawText866bk(x + K.fontWidth + K.fontWidth DIV 2, y + (height - K.fontHeight) DIV 2, K.colors.work_text, color, s);
  81.     IF modified THEN
  82.         K.DrawText866bk(x + K.fontWidth DIV 2, y + (height - K.fontHeight) DIV 2, K.colors.work_text, color, "*")
  83.     END;
  84.     K.CreateButton(id + ORD({30}) + btnID, x + 1, y - 1, width - 1, height - 1, color, "");
  85. END drawTab;
  86.  
  87.  
  88. PROCEDURE tabWidth (tab: tItem): INTEGER;
  89.     RETURN (LENGTH(tab.val) + 3)*K.fontWidth
  90. END tabWidth;
  91.  
  92.  
  93. PROCEDURE Width (t: tTabs; pos, n: INTEGER): INTEGER;
  94. VAR
  95.     res, i: INTEGER;
  96.     item: List.tItem;
  97. BEGIN
  98.     res := 0;
  99.     i := pos;
  100.     item := List.getItem(t.strings, i);
  101.     WHILE (item # NIL) & (i <= n) DO
  102.         INC(res, tabWidth(item(tItem)));
  103.         item := item.next;
  104.         INC(i)
  105.     END
  106.     RETURN res
  107. END Width;
  108.  
  109.  
  110. PROCEDURE draw* (t: tTabs);
  111. VAR
  112.     x, y, xmax, n, width, i: INTEGER;
  113.     item: List.tItem;
  114.     scroll: BOOLEAN;
  115. BEGIN
  116.     y := t.y;
  117.     x := t.x;
  118.     K.DrawRect(x, y - (curTabHeight - tabHeight), t.width + (2*scrWidth + 2), t.height + (curTabHeight - tabHeight) - 1, K.colors.work);
  119.     IF Width(t, 0, t.strings.count - 1) > t.width THEN
  120.         INC(x, 2*scrWidth);
  121.         K.DeleteButton(btnLeft);
  122.         K.DeleteButton(btnRight);
  123.         K.CreateButton(btnLeft, t.x, y, scrWidth, t.height - 1, K.colors.button, "<");
  124.         K.CreateButton(btnRight, t.x + scrWidth, y, scrWidth, t.height - 1, K.colors.button, ">");
  125.         scroll := TRUE
  126.     ELSE
  127.         t.first := 0;
  128.         scroll := FALSE
  129.     END;
  130.     xmax := x + t.width - 1;
  131.  
  132.     n := t.strings.count - 1;
  133.     FOR i := 0 TO n DO
  134.         K.DeleteButton(i + btnID)
  135.     END;
  136.     WHILE (n >= 0) & (Width(t, n, t.strings.count - 1) <= t.width) DO
  137.         DEC(n)
  138.     END;
  139.     IF n < 0 THEN
  140.         n := 0
  141.     ELSE
  142.         INC(n)
  143.     END;
  144.     IF n < t.first THEN
  145.         t.first := n
  146.     END;
  147.  
  148.     K.DrawRect(x, y, t.width, t.height - 1, K.colors.work);
  149.     K.DrawLine(x, y + tabHeight - 1, x + t.width - 1 + 2*scrWidth*(1 - ORD(scroll)), y + tabHeight - 1, K.colors.line);
  150.     item := List.getItem(t.strings, t.first);
  151.     n := t.first;
  152.     WHILE (item # NIL) & (x <= xmax) DO
  153.         width := tabWidth(item(tItem));
  154.         IF x + width - 1 <= xmax THEN
  155.             drawTab(t, n, x + 1, y, width, t.height, item(tItem).val, item(tItem).modified);
  156.                 INC(n);
  157.                 INC(x, width);
  158.                 item := item.next
  159.         ELSE
  160.                 item := NIL
  161.         END
  162.     END;
  163.     t.freeX := x
  164. END draw;
  165.  
  166.  
  167. PROCEDURE add* (t: tTabs; s: ARRAY OF CHAR);
  168. VAR
  169.     item: tItem;
  170. BEGIN
  171.     NEW(item);
  172.     item.val := s;
  173.     item.modified := FALSE;
  174.     List.append(t.strings, item);
  175. END add;
  176.  
  177.  
  178. PROCEDURE modify* (t: tTabs; n: INTEGER; val: BOOLEAN);
  179. VAR
  180.     item: List.tItem;
  181. BEGIN
  182.         item := List.getItem(t.strings, n);
  183.         IF item(tItem).modified # val THEN
  184.                 item(tItem).modified := val;
  185.                 draw(t)
  186.         END
  187. END modify;
  188.  
  189.  
  190. PROCEDURE rename* (t: tTabs; n: INTEGER; s: ARRAY OF CHAR);
  191. VAR
  192.     item: List.tItem;
  193. BEGIN
  194.     item := List.getItem(t.strings, n);
  195.     item(tItem).val := s
  196. END rename;
  197.  
  198.  
  199. PROCEDURE delete* (t: tTabs; n: INTEGER);
  200. VAR
  201.     item: List.tItem;
  202. BEGIN
  203.     item := List.getItem(t.strings, n);
  204.     List.delete(t.strings, item);
  205.     DISPOSE(item)
  206. END delete;
  207.  
  208.  
  209. PROCEDURE scroll* (t: tTabs; btn: INTEGER);
  210. VAR
  211.     pos: INTEGER;
  212. BEGIN
  213.     pos := t.first + ORD(btn = btnRight) - ORD(btn = btnLeft);
  214.     IF pos < 0 THEN
  215.         pos := 0
  216.     ELSIF pos >= t.strings.count THEN
  217.         pos := t.strings.count - 1
  218.     END;
  219.     t.first := pos
  220. END scroll;
  221.  
  222.  
  223. PROCEDURE switch* (t: tTabs; n: INTEGER);
  224. BEGIN
  225.     IF (0 <= n) & (n < t.strings.count) THEN
  226.         t.current := n;
  227.         IF n < t.first THEN
  228.             t.first := 0
  229.         END;
  230.         WHILE Width(t, t.first, n) > t.width DO
  231.             INC(t.first)
  232.         END
  233.     END
  234. END switch;
  235.  
  236.  
  237. PROCEDURE setArea* (t: tTabs; x, y, width, height: INTEGER);
  238. BEGIN
  239.     t.x := x;
  240.     t.y := y;
  241.     t.width := width - 2*scrWidth;
  242.     t.height := height
  243. END setArea;
  244.  
  245.  
  246. PROCEDURE create* (): tTabs;
  247. VAR
  248.     res: tTabs;
  249. BEGIN
  250.     NEW(res);
  251.     res.strings := List.create(NIL);
  252.     res.current := 0;
  253.     res.first := 0
  254.     RETURN res
  255. END create;
  256.  
  257.  
  258. END Tabs.