Subversion Repositories Kolibri OS

Rev

Rev 1805 | Details | Compare with Previous | Last modification | View Log | RSS feed

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