Subversion Repositories Kolibri OS

Rev

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

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