Subversion Repositories Kolibri OS

Rev

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

  1. #ifndef _HEADER_BUTTONS_H
  2. #define _HEADER_BUTTONS_H
  3.  
  4. #ifndef __MENUET__
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <limits.h>
  8. #endif
  9. #include "gr-draw.h"
  10.  
  11. struct TXButton
  12. {
  13.   TXButton(int n = -1, int w = 100, int h = 20) : num(n), w(w), h(h), drw(1) {}
  14.   TXButton(const TXButton &b) : w(b.w), h(b.h) {}
  15.  
  16.   TXButton &operator=(const TXButton &b) {w = b.w; h = b.h; num = b.num; return *this;}
  17.  
  18.   virtual void Draw(TGraphDraw *drw, int x0, int y0,
  19.       unsigned long dcolor, unsigned long lcolor, unsigned long tcolor, int ins = 0);
  20.   virtual int ButtonPnt(int xp, int yp, int &n, int kind = 0);
  21.  
  22.   int num, w, h, drw;
  23. };
  24.  
  25. void TXButton::Draw(TGraphDraw *drw, int x0, int y0,
  26.          unsigned long dcolor, unsigned long /*lcolor*/,
  27.          unsigned long /*tcolor*/, int /*ins*/)
  28. {
  29.   if (!drw || !drw->IsDraw()) return;
  30.   drw->SetColor(dcolor);
  31.   drw->DrawLine(x0, y0, x0 + w, y0);
  32.   drw->DrawLine(x0, y0, x0, y0 + h);
  33.   drw->DrawLine(x0 + w, y0, x0 + w, y0 + h);
  34.   drw->DrawLine(x0, y0 + h, x0 + w, y0 + h);
  35. }
  36.  
  37. int TXButton::ButtonPnt(int xp, int yp, int &n, int /*kind*/)
  38. {
  39.   n = num;
  40.   return (xp >= 0 && yp >= 0 && xp <= w && yp <= h) ? 1 : INT_MIN;
  41. }
  42.  
  43.  
  44. struct TextButton : public TXButton
  45. {
  46.   enum {thick_def = 3};
  47.  
  48.   TextButton(int n = -1, int w = 100, int h = 20, char *text = 0, int thick = thick_def)
  49.                : TXButton(n, w, h), text(text), thick(thick) {CheckDefW();}
  50.   TextButton(const TXButton &b, char *text = 0, int thick = thick_def)
  51.                : TXButton(b), text(text), thick(thick) {CheckDefW();}
  52.   TextButton(const TextButton &b) : TXButton(b), text(b.text), thick(b.thick) {}
  53.  
  54.   TextButton &operator=(const TextButton &b);
  55.  
  56.   virtual void Draw(TGraphDraw *drw, int x0, int y0,
  57.       unsigned long dcolor, unsigned long lcolor, unsigned long tcolor, int ins = 0);
  58.   void CheckDefW();
  59.  
  60.   int thick;
  61.   char *text;
  62. };
  63.  
  64. inline TextButton &TextButton::operator=(const TextButton &b)
  65. {
  66.   text = b.text;
  67.   TXButton::operator=(b);
  68.   return *this;
  69. }
  70.  
  71. void TextButton::Draw(TGraphDraw *drw, int x0, int y0,
  72.            unsigned long dcolor, unsigned long lcolor, unsigned long tcolor, int ins)
  73. {
  74.   if (!drw || !drw->IsDraw()) return;
  75.   int i;
  76.   if (thick <= -1 && thick >= -5)
  77.   {
  78.     drw->SetColor(dcolor);
  79.     for (i = 0; i < -thick; i++)
  80.     {
  81.       drw->DrawLine(x0 + i, y0 + i, x0 + w - i, y0 + i);
  82.       drw->DrawLine(x0 + i, y0 + i, x0 + i, y0 + h - i);
  83.       drw->DrawLine(x0 + w - i, y0 + i, x0 + w - i, y0 + h - i);
  84.       drw->DrawLine(x0 + i, y0 + h - i, x0 + w - i, y0 + h - i);
  85.     }
  86.   }
  87.   else if (thick > 0)
  88.   {
  89.     for (i = 0; i < thick; i++)
  90.     {
  91.       drw->SetColor(ins ? dcolor : lcolor);
  92.       drw->DrawLine(x0 + i, y0 + i, x0 + w - i, y0 + i);
  93.       drw->DrawLine(x0 + i, y0 + i, x0 + i, y0 + h - i);
  94.       drw->SetColor(ins ? lcolor : dcolor);
  95.       drw->DrawLine(x0 + w - i, y0 + i, x0 + w - i, y0 + h - i);
  96.       drw->DrawLine(x0 + i, y0 + h - i, x0 + w - i, y0 + h - i);
  97.     }
  98.   }
  99.   else TXButton::Draw(drw, x0, y0, dcolor, lcolor, tcolor, ins);
  100.   if (text)
  101.   {
  102.     drw->SetColor(tcolor);
  103.     drw->DrawText(x0 + 7, y0 + (h - drw->GetTextH(text)) / 2, text);
  104.   }
  105. }
  106.  
  107. inline void TextButton::CheckDefW()
  108. {
  109.   if (w < 0 && text) w = 15 + strlen(text) * 8;
  110. }
  111.  
  112. class TXButtonArray
  113. {
  114. public:
  115.   TXButtonArray(int d = 10) : button(0), nbutton(0), mbutton(0),
  116.                               delt(d), ins(-1), k_ins(0) {}
  117.   ~TXButtonArray() {Clear();}
  118.  
  119.   void Clear();
  120.   void Add(TXButton *bt);
  121.   void Add(int n = -1, int w = 100, int h = 20, char *str = 0,
  122.            int thick = TextButton::thick_def);
  123.   TXButton *operator[](int i) {return button[i];}
  124.   int GetNButton() const {return nbutton;}
  125.   int GetInsert() const {return ins;}
  126.   TXButton *GetButton(int n);
  127.  
  128.   int GetDelt() const {return delt;}
  129.   void SetDelt(int d) {delt = d;}
  130.  
  131.   int GetParam(int w, int p) const;
  132.   int GetNumStr(int w) const {return GetParam(w, 0);}
  133.   int GetHeight(int w) const {return GetParam(w, 1);}
  134.   int GetFWidth() const;
  135.  
  136.   void Draw(TGraphDraw *drw, int x0, int y0, int w);
  137.   void Draw(TGraphDraw *drw, int x0, int y0, int w,
  138.      unsigned long dcolor, unsigned long lcolor, unsigned long tcolor, int insd = 1);
  139.   int ButtonPnt(int xp, int yp, int w, int &n, int kind = 0);
  140. protected:
  141.   void Extend(int n = -1);
  142.  
  143.   TXButton **button;
  144.   int nbutton, mbutton;
  145.   int delt;
  146.   int ins, k_ins;
  147. };
  148.  
  149. void TXButtonArray::Clear()
  150. {
  151.   int i;
  152.   if (button)
  153.   {
  154.     for (i = 0; i < nbutton; i++) if (button[i]) delete button[i];
  155.     delete[] button;
  156.   }
  157.   button = 0; nbutton = 0; mbutton = 0;
  158. }
  159.  
  160. void TXButtonArray::Extend(int n)
  161. {
  162.   if (n < 0) n = nbutton + 1;
  163.   if (mbutton < n)
  164.   {
  165.     typedef TXButton *TXButtonPnt;
  166.     TXButton **b_old = button;
  167.     int m_old = mbutton;
  168.     mbutton = n * 2;
  169.     button = new TXButtonPnt[mbutton];
  170.     for (int i = 0; i < mbutton; i++) button[i] = 0;
  171.     if (b_old)
  172.     {
  173.       for (int i = 0; i < m_old; i++) button[i] = b_old[i];
  174.       delete[] b_old;
  175.     }
  176.   }
  177. }
  178.  
  179. void TXButtonArray::Add(TXButton *bt)
  180. {
  181.   Extend();
  182.   if (button[nbutton]) delete[] button[nbutton];
  183.   button[nbutton] = bt;
  184.   nbutton++;
  185. }
  186.  
  187. void TXButtonArray::Add(int n, int w, int h, char *str, int thick)
  188. {
  189.   if (thick < 0 && !str) Add(new TXButton(n, w, h));
  190.   else Add(new TextButton(n, w, h, str, thick));
  191. }
  192.  
  193. TXButton *TXButtonArray::GetButton(int n)
  194. {
  195.   int i;
  196.   for (i = 0; i < nbutton; i++)
  197.   {
  198.     if (button[i] && button[i]->num == n) return button[i];
  199.   }
  200.   return 0;
  201. }
  202.  
  203. int TXButtonArray::GetFWidth() const
  204. {
  205.   int i, x = 0;
  206.   for (i = 0; i < nbutton; i++)
  207.   {
  208.     if (button[i] && button[i]->drw > 0) x += button[i]->w + delt;
  209.   }
  210.   if (x != 0) x -= delt;
  211.   return x;
  212. }
  213.  
  214. int TXButtonArray::GetParam(int w, int p) const
  215. {
  216.   int i, k = 0;
  217.   int x = 0, y = 0, ym = 0;
  218.   for (i = 0; i < nbutton; i++) if (button[i] && button[i]->drw > 0)
  219.   {
  220.     int xx = x + button[i]->w + delt;
  221.     if (x != 0 && xx > w + delt)
  222.     {
  223.       k++; y += ym + delt; ym = 0;
  224.       x = 0; xx = x + button[i]->w + delt;
  225.     }
  226.     x = xx;
  227.     if (ym < button[i]->h) ym = button[i]->h;
  228.   }
  229.   if (x != 0) {k++; y += ym;}
  230.   else if (y != 0) y -= delt;
  231.   if (p == 0) return k;
  232.   else if (p == 1) return y;
  233.   else return -1;
  234. }
  235.  
  236. void TXButtonArray::Draw(TGraphDraw *drw, int x0, int y0, int w)
  237. {
  238.   const unsigned short power_gray = 49152U;
  239.   unsigned long black = drw->GetBlackColor();
  240.   unsigned long grey = drw->CreateColor(power_gray, power_gray, power_gray);
  241.   Draw(drw, x0, y0, w, black, grey, black, 1);
  242.   drw->FreeColor(grey);
  243. }
  244.  
  245. void TXButtonArray::Draw(TGraphDraw *drw, int x0, int y0, int w,
  246.         unsigned long dcolor, unsigned long lcolor, unsigned long tcolor, int insd)
  247. {
  248.   int i;
  249.   int x = 0, y = 0, ym = 0;
  250.   if (!insd && ins >= 0) k_ins = 2;
  251.   for (i = 0; i < nbutton; i++) if (button[i] && button[i]->drw > 0)
  252.   {
  253.     int xx = x + button[i]->w + delt;
  254.     if (x != 0 && xx > w + delt)
  255.     {
  256.       y += ym + delt; ym = 0;
  257.       x = 0; xx = x + button[i]->w + delt;
  258.     }
  259.     button[i]->Draw(drw, x0 + x, y0 + y,
  260.                     dcolor, lcolor, tcolor, k_ins == 1 && ins == i);
  261.     x = xx;
  262.     if (ym < button[i]->h) ym = button[i]->h;
  263.   }
  264. }
  265.  
  266. int TXButtonArray::ButtonPnt(int xp, int yp, int w, int &n, int kind)
  267. {
  268.   int i;
  269.   int x = 0, y = 0, ym = 0;
  270.   if (ins < 0 && (kind == 2 || kind == 3)) return INT_MIN;
  271.   for (i = 0; i < nbutton; i++) if (button[i] && button[i]->drw > 0)
  272.   {
  273.     int xx = x + button[i]->w + delt;
  274.     if (x != 0 && xx > w + delt)
  275.     {
  276.       y += ym + delt; ym = 0;
  277.       x = 0; xx = x + button[i]->w + delt;
  278.     }
  279.     if (ins < 0 || i == ins)
  280.     {
  281.       int wh = button[i]->ButtonPnt(xp - x, yp - y, n, kind);
  282.       if (n == -1) n = i;
  283.       if (i == ins)
  284.       {
  285.         if (kind == 1) return wh;
  286.         else if (kind == 2)
  287.         {
  288.           if (wh == INT_MIN || n < 0)
  289.           {
  290.             if (k_ins != 2) {k_ins = 2; n = -10; return 1000;}
  291.           }
  292.           else if (k_ins != 1) {k_ins = 1; return 1000;}
  293.           return wh;
  294.         }
  295.         else if (kind == 3)
  296.         {
  297.           if (wh == INT_MIN)
  298.           {
  299.             n = -10;
  300.             if (k_ins == 1) wh = 1000;
  301.           }
  302.           k_ins = 0; ins = -1;
  303.           return wh;
  304.         }
  305.         else return wh;
  306.       }
  307.       else if (wh != INT_MIN)
  308.       {
  309.         if (kind == 1) {ins = i; k_ins = 1; return wh;}
  310.         else if (kind == 2 || kind == 3) return INT_MIN;
  311.         else return wh;
  312.       }
  313.     }
  314.     x = xx;
  315.     if (ym < button[i]->h) ym = button[i]->h;
  316.   }
  317.   n = -10;
  318.   return INT_MIN;
  319. }
  320.  
  321. struct TMultiButton : public TXButton
  322. {
  323.   TMultiButton(int n = -1, int w = 100, int h = 20, int d = 2)
  324.                          : TXButton(n, w, h), a(d) {}
  325.  
  326.   virtual void Draw(TGraphDraw *drw, int x0, int y0, unsigned long dcolor,
  327.                     unsigned long lcolor, unsigned long tcolor, int ins = 0);
  328.   virtual int ButtonPnt(int xp, int yp, int &n, int kind = 0);
  329.  
  330.   void SetDefW();
  331.  
  332.   TXButtonArray a;
  333. };
  334.  
  335. void TMultiButton::Draw(TGraphDraw *drw, int x0, int y0, unsigned long dcolor,
  336.                         unsigned long lcolor, unsigned long tcolor, int ins)
  337. {
  338.   a.Draw(drw, x0, y0, w, dcolor, lcolor, tcolor, ins);
  339. }
  340.  
  341. int TMultiButton::ButtonPnt(int xp, int yp, int &n, int kind)
  342. {
  343.   if (a.GetInsert() < 0 && (xp < 0 || yp < 0 || xp > w || yp > h)) return INT_MIN;
  344.   return a.ButtonPnt(xp, yp, w, n, kind);
  345. }
  346.  
  347. void TMultiButton::SetDefW()
  348. {
  349.   w = a.GetFWidth();
  350.   if (w < 0) w = 0;
  351.   h = a.GetHeight(w);
  352. }
  353.  
  354. #endif  //_HEADER_BUTTONS_H
  355.  
  356.