Subversion Repositories Kolibri OS

Rev

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.     btnClose* = btnID + 100;
  28.     btnLeft* = btnID - 1;
  29.         btnRight* = btnID - 2;
  30.     tabHeight* = 22;
  31.     curTabHeight = 26;
  32.     scrWidth = 15;
  33.     btnCloseColor* = 0EF999FH;
  34.  
  35. TYPE
  36.  
  37.     tItem = POINTER TO RECORD (List.tItem)
  38.  
  39.         val: RW.tFileName;
  40.         modified: BOOLEAN
  41.  
  42.     END;
  43.  
  44.     tTabs* = POINTER TO RECORD
  45.  
  46.         strings: List.tList;
  47.         first, current: INTEGER;
  48.         width, height: INTEGER;
  49.         x, y, freeX: INTEGER
  50.  
  51.     END;
  52.  
  53.  
  54. PROCEDURE DblClicked* (t: tTabs; x, y: INTEGER): BOOLEAN;
  55.         RETURN (x > t.freeX) & U.between(t.y, y, t.y + t.height - 1)
  56. END DblClicked;
  57.  
  58.  
  59. PROCEDURE drawTab (t: tTabs; id, x, y, width, height: INTEGER; s: ARRAY OF CHAR; modified: BOOLEAN);
  60. CONST
  61.         btnCloseSize = 14;
  62. VAR
  63.     x2, y2, color, closeColor, closeForeColor, textColor: INTEGER;
  64.     left, top: INTEGER;
  65. BEGIN
  66.     IF id = t.current THEN
  67.         INC(height, curTabHeight - tabHeight);
  68.         DEC(y, curTabHeight - tabHeight)
  69.     END;
  70.     color := K.colors.work;
  71.     textColor := K.colors.work_text;
  72.     DEC(x); INC(width);
  73.     x2 := x + width - 1;
  74.     y2 := y + height - 1;
  75.  
  76.     K.DrawRect(x, y, width, height, color);
  77.     K.DrawLine(x, y, x2, y, K.colors.line);
  78.     K.DrawLine(x2, y, x2, y2, K.colors.line);
  79.  
  80.     top := y + 3;
  81.     IF id # t.current THEN
  82.         K.DrawLine(x2 - 1, y2, x, y2, K.colors.line);
  83.         closeColor := K.colors.button;
  84.         closeForeColor := K.colors.button_text
  85.     ELSE
  86.         INC(top, (curTabHeight - tabHeight) DIV 2);
  87.         closeColor := btnCloseColor;
  88.         closeForeColor := 0FFFFFFH
  89.     END;
  90.     K.DrawLine(x, y2, x, y, K.colors.line);
  91.  
  92.     K.DrawText866bk(x + K.fontWidth + K.fontWidth DIV 2, y + (height - K.fontHeight) DIV 2, textColor, color, s);
  93.     IF modified THEN
  94.         K.DrawText866bk(x + K.fontWidth DIV 2, y + (height - K.fontHeight) DIV 2, textColor, color, "*")
  95.     END;
  96.     K.CreateButton(id + ORD({30}) + btnID, x + 1, y - 1, width - 1, height - 1, 0, "");
  97.     left := x + width - btnCloseSize - 5;
  98.     K.CreateButton(id + btnClose, left, top, btnCloseSize, btnCloseSize,  closeColor, "");
  99.     K.DrawLine(left + 5, top + 4, left + btnCloseSize - 4, top + btnCloseSize - 5, closeForeColor);
  100.     K.DrawLine(left + 4, top + 4, left + btnCloseSize - 4, top + btnCloseSize - 4, closeForeColor);
  101.     K.DrawLine(left + 4, top + 5, left + btnCloseSize - 5, top + btnCloseSize - 4, closeForeColor);
  102.     K.DrawLine(left + 4, top + btnCloseSize - 4, left + btnCloseSize - 4, top + 4, closeForeColor);
  103.     K.DrawLine(left + 4, top + btnCloseSize - 5, left + btnCloseSize - 5, top + 4, closeForeColor);
  104.     K.DrawLine(left + 5, top + btnCloseSize - 4, left + btnCloseSize - 4, top + 5, closeForeColor);
  105. END drawTab;
  106.  
  107.  
  108. PROCEDURE tabWidth (tab: tItem): INTEGER;
  109.     RETURN (LENGTH(tab.val) + 5)*K.fontWidth
  110. END tabWidth;
  111.  
  112.  
  113. PROCEDURE Width (t: tTabs; pos, n: INTEGER): INTEGER;
  114. VAR
  115.     res, i: INTEGER;
  116.     item: List.tItem;
  117. BEGIN
  118.     res := 0;
  119.     i := pos;
  120.     item := List.getItem(t.strings, i);
  121.     WHILE (item # NIL) & (i <= n) DO
  122.         INC(res, tabWidth(item(tItem)));
  123.         item := item.next;
  124.         INC(i)
  125.     END
  126.     RETURN res
  127. END Width;
  128.  
  129.  
  130. PROCEDURE draw* (t: tTabs);
  131. VAR
  132.     x, y, xmax, n, width, i: INTEGER;
  133.     item: List.tItem;
  134.     scroll: BOOLEAN;
  135. BEGIN
  136.     y := t.y;
  137.     x := t.x;
  138.     K.DrawRect(x, y - (curTabHeight - tabHeight), t.width + (2*scrWidth + 2), t.height + (curTabHeight - tabHeight) - 1, K.colors.work);
  139.     IF Width(t, 0, t.strings.count - 1) > t.width THEN
  140.         INC(x, 2*scrWidth);
  141.         K.DeleteButton(btnLeft);
  142.         K.DeleteButton(btnRight);
  143.         K.CreateButton(btnLeft, t.x, y, scrWidth, t.height - 1, K.colors.button, "<");
  144.         K.CreateButton(btnRight, t.x + scrWidth, y, scrWidth, t.height - 1, K.colors.button, ">");
  145.         scroll := TRUE
  146.     ELSE
  147.         t.first := 0;
  148.         scroll := FALSE
  149.     END;
  150.     xmax := x + t.width - 1;
  151.  
  152.     n := t.strings.count - 1;
  153.     FOR i := 0 TO n DO
  154.         K.DeleteButton(i + btnID)
  155.     END;
  156.     WHILE (n >= 0) & (Width(t, n, t.strings.count - 1) <= t.width) DO
  157.         DEC(n)
  158.     END;
  159.     IF n < 0 THEN
  160.         n := 0
  161.     ELSE
  162.         INC(n)
  163.     END;
  164.     IF n < t.first THEN
  165.         t.first := n
  166.     END;
  167.  
  168.     K.DrawRect(x, y, t.width, t.height - 1, K.colors.work);
  169.     K.DrawLine(x, y + tabHeight - 1, x + t.width - 1 + 2*scrWidth*(1 - ORD(scroll)), y + tabHeight - 1, K.colors.line);
  170.     item := List.getItem(t.strings, t.first);
  171.     n := t.first;
  172.     WHILE (item # NIL) & (x <= xmax) DO
  173.         width := tabWidth(item(tItem));
  174.         IF x + width - 1 <= xmax THEN
  175.             drawTab(t, n, x + 1, y, width, t.height, item(tItem).val, item(tItem).modified);
  176.                 INC(n);
  177.                 INC(x, width);
  178.                 item := item.next
  179.         ELSE
  180.                 item := NIL
  181.         END
  182.     END;
  183.     t.freeX := x
  184. END draw;
  185.  
  186.  
  187. PROCEDURE add* (t: tTabs; s: ARRAY OF CHAR);
  188. VAR
  189.     item: tItem;
  190. BEGIN
  191.     NEW(item);
  192.     item.val := s;
  193.     item.modified := FALSE;
  194.     List.append(t.strings, item);
  195. END add;
  196.  
  197.  
  198. PROCEDURE modify* (t: tTabs; n: INTEGER; val: BOOLEAN);
  199. VAR
  200.     item: List.tItem;
  201. BEGIN
  202.         item := List.getItem(t.strings, n);
  203.         IF item(tItem).modified # val THEN
  204.                 item(tItem).modified := val;
  205.                 draw(t)
  206.         END
  207. END modify;
  208.  
  209.  
  210. PROCEDURE rename* (t: tTabs; n: INTEGER; s: ARRAY OF CHAR);
  211. VAR
  212.     item: List.tItem;
  213. BEGIN
  214.     item := List.getItem(t.strings, n);
  215.     item(tItem).val := s
  216. END rename;
  217.  
  218.  
  219. PROCEDURE delete* (t: tTabs; n: INTEGER);
  220. VAR
  221.     item: List.tItem;
  222. BEGIN
  223.     item := List.getItem(t.strings, n);
  224.     List.delete(t.strings, item);
  225.     DISPOSE(item)
  226. END delete;
  227.  
  228.  
  229. PROCEDURE scroll* (t: tTabs; btn: INTEGER);
  230. VAR
  231.     pos: INTEGER;
  232. BEGIN
  233.     pos := t.first + ORD(btn = btnRight) - ORD(btn = btnLeft);
  234.     IF pos < 0 THEN
  235.         pos := 0
  236.     ELSIF pos >= t.strings.count THEN
  237.         pos := t.strings.count - 1
  238.     END;
  239.     t.first := pos
  240. END scroll;
  241.  
  242.  
  243. PROCEDURE switch* (t: tTabs; n: INTEGER);
  244. BEGIN
  245.     IF (0 <= n) & (n < t.strings.count) THEN
  246.         t.current := n;
  247.         IF n < t.first THEN
  248.             t.first := 0
  249.         END;
  250.         WHILE Width(t, t.first, n) > t.width DO
  251.             INC(t.first)
  252.         END
  253.     END
  254. END switch;
  255.  
  256.  
  257. PROCEDURE setArea* (t: tTabs; x, y, width, height: INTEGER);
  258. BEGIN
  259.     t.x := x;
  260.     t.y := y;
  261.     t.width := width - 2*scrWidth;
  262.     t.height := height
  263. END setArea;
  264.  
  265.  
  266. PROCEDURE create* (): tTabs;
  267. VAR
  268.     res: tTabs;
  269. BEGIN
  270.     NEW(res);
  271.     res.strings := List.create(NIL);
  272.     res.current := 0;
  273.     res.first := 0
  274.     RETURN res
  275. END create;
  276.  
  277.  
  278. END Tabs.