Subversion Repositories Kolibri OS

Rev

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