Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1805 yogev_ezra 1
#ifndef _Windows
2
# error Can't include this file without windows
3
#endif
4
 
5
#include "gr-draw.h"
6
 
7
#ifndef _WIN_GRAPHIC_DRAW_H
8
#define _WIN_GRAPHIC_DRAW_H
9
 
10
#include "winabout.h"
11
#include "keysym.h"
12
#include 
13
#include 
14
#include 
15
#include 
16
#include 
17
#include 
18
#include 
19
 
20
#define main	OwlMain
21
 
22
class TWinGraphDraw : public TGraphDraw
23
{
24
public:
25
  TWinGraphDraw(const char *s = 0);
26
  ~TWinGraphDraw();
27
public:
28
  static const int CM_ABOUT;
29
  static unsigned long GetKeySym(unsigned int key);
30
protected:
31
  class TDrwWindow : public TWindow
32
  {
33
  public:
34
    TDrwWindow(TWinGraphDraw *drw) : TWindow(), win_draw(drw)
35
    {
36
      Init((TWindow*)NULL, 0, 0);
37
    }
38
 
39
    TColor GetBkgndColor() const {return BkgndColor;}
40
  protected:
41
    void SetupWindow();
42
    bool CanClose();
43
    void Paint(TDC &dc, bool erase, TRect &rect);
44
    void EvSize(uint sizeType, TSize &size) {Invalidate();}
45
    void EvLButtonDblClk(uint modKeys, TPoint& point) {EvLButtonDown(modKeys, point);}
46
    void EvLButtonDown(uint modKeys, TPoint& point);
47
    void EvLButtonUp(uint modKeys, TPoint& point);
48
    void EvRButtonDblClk(uint modKeys, TPoint& point) {EvRButtonDown(modKeys, point);}
49
    void EvRButtonDown(uint modKeys, TPoint& point);
50
    void EvRButtonUp(uint modKeys, TPoint& point);
51
    void EvMButtonDblClk(uint modKeys, TPoint& point) {EvMButtonDown(modKeys, point);}
52
    void EvMButtonDown(uint modKeys, TPoint& point);
53
    void EvMButtonUp(uint modKeys, TPoint& point);
54
    void EvMouseMove(uint modKeys, TPoint& point);
55
    void EvKeyDown(uint key, uint repeatCount, uint flags);
56
    void EvKeyUp(uint key, uint repeatCount, uint flags);
57
  public:
58
    TWinGraphDraw *win_draw;
59
 
60
    DECLARE_RESPONSE_TABLE(TDrwWindow);
61
  };
62
 
63
  class TDrwFrameWindow : public TFrameWindow
64
  {
65
  public:
66
    TDrwFrameWindow(TWindow *parent, TDrwWindow *client);
67
  protected:
68
    virtual void SetupWindow();
69
    void EvSysCommand(uint cmdType, TPoint& point);
70
  public:
71
    TWinGraphDraw *win_draw;
72
 
73
    DECLARE_RESPONSE_TABLE(TDrwFrameWindow);
74
  };
75
 
76
  class TDrwApplication : public TApplication
77
  {
78
  public:
79
    TDrwApplication(TWinGraphDraw *drw, int w = 0, int h = 0)
80
                  : def_w(w), def_h(h), win_draw(drw) {}
81
 
82
    void InitMainWindow()
83
    {
84
      win_draw->win = new TDrwWindow(win_draw);
85
      SetMainWindow(new TDrwFrameWindow(0, win_draw->win));
86
      GetMainWindow()->SetIcon(this, "WinDrawIcon");
87
      GetMainWindow()->SetIconSm(this, "WinDrawIcon");
88
      if (def_w > 0 && def_h > 0)
89
      {
90
        GetMainWindow()->Attr.W = def_w + 2*GetSystemMetrics(SM_CXFRAME);
91
        GetMainWindow()->Attr.H = def_h +
92
           GetSystemMetrics(SM_CYCAPTION) + 2*GetSystemMetrics(SM_CYFRAME);
93
      }
94
    }
95
  public:
96
    int def_w, def_h;
97
    TWinGraphDraw *win_draw;
98
  };
99
 
100
  friend class TDrwWindow;
101
  friend class TDrwApplication;
102
protected:
103
  TDrwApplication app;
104
  TDrwWindow *win;
105
  TDC *dc;
106
  int kind_dc;
107
  TColor bgcolor;
108
  int evmask, quit;
109
public:
110
  unsigned long GetBgColor();
111
  void SetBgColor(unsigned long c);
112
  void SetTitle(const char *s);
113
 
114
  int GetStatus() {return win ? 1 : 0;}
115
  int Init() {return 0;}
116
  int Run(int evmask = 0, int w = INT_MIN, int h = INT_MIN);
117
 
118
  void GetSize(int &w, int &h);
119
  int OpenDraw();
120
  int IsDraw() {return win && dc;}
121
  void CloseDraw();
122
 
123
  int SetColor(unsigned long c);
124
  int DrawLine(int x0, int y0, int x1, int y1);
125
  int DrawText(int x0, int y0, char *text);
126
  int DrawClear();
127
  int GetTextH(const char *s);
128
  int GetTextW(const char *s);
129
  void Quit(int q = 1);
130
};
131
 
132
const int TWinGraphDraw::CM_ABOUT = 7128;
133
 
134
unsigned long TWinGraphDraw::GetKeySym(uint key)
135
{
136
  switch(key)
137
  {
138
    case 37:  return XK_Left;
139
    case 39:  return XK_Right;
140
    case 38:  return XK_Up;
141
    case 40:  return XK_Down;
142
    case 13:  return XK_Return;
143
    case 32:  return XK_space;
144
    case 27:  return XK_Escape;
145
    case 188: return XK_comma;
146
    case 190: return XK_period;
147
    case 189: return XK_minus;
148
    case 187: return XK_equal;
149
    case 46:  return XK_Delete;
150
    case 191: return XK_slash;
151
    case 112: return XK_F1;
152
    case 113: return XK_F2;
153
    case 114: return XK_F3;
154
    case 115: return XK_F4;
155
    case 116: return XK_F5;
156
    case 117: return XK_F6;
157
    case 118: return XK_F7;
158
    case 119: return XK_F8;
159
    case 120: return XK_F9;
160
    case 121: return XK_F10;
161
    case 122: return XK_F11;
162
    case 123: return XK_F12;
163
    case 65:  return XK_a;
164
    case 66:  return XK_b;
165
    case 67:  return XK_c;
166
    case 68:  return XK_d;
167
    case 69:  return XK_e;
168
    case 70:  return XK_f;
169
    case 71:  return XK_g;
170
    case 72:  return XK_h;
171
    case 73:  return XK_i;
172
    case 74:  return XK_j;
173
    case 75:  return XK_k;
174
    case 76:  return XK_l;
175
    case 77:  return XK_m;
176
    case 78:  return XK_n;
177
    case 79:  return XK_o;
178
    case 80:  return XK_p;
179
    case 81:  return XK_q;
180
    case 82:  return XK_r;
181
    case 83:  return XK_s;
182
    case 84:  return XK_t;
183
    case 85:  return XK_u;
184
    case 86:  return XK_v;
185
    case 87:  return XK_w;
186
    case 88:  return XK_x;
187
    case 89:  return XK_y;
188
    case 90:  return XK_z;
189
    default:  return XK_VoidSymbol;
190
  }
191
}
192
 
193
TWinGraphDraw::TWinGraphDraw(const char *s)
194
             : TGraphDraw(s), app(this), dc(0), kind_dc(0)
195
{
196
  bgcolor = GetWhiteColor();
197
}
198
 
199
TWinGraphDraw::~TWinGraphDraw()
200
{
201
  CloseDraw();
202
  dc = 0; kind_dc = 0; win = 0;
203
}
204
 
205
unsigned long TWinGraphDraw::GetBgColor()
206
{
207
  if (win) bgcolor = win->GetBkgndColor();
208
  return bgcolor.GetValue();
209
}
210
 
211
void TWinGraphDraw::SetBgColor(unsigned long c)
212
{
213
  bgcolor.SetValue(c);
214
  if (win) win->SetBkgndColor(bgcolor);
215
}
216
 
217
void TWinGraphDraw::SetTitle(const char *s)
218
{
219
  TGraphDraw::SetTitle(s);
220
  if (win) win->SetCaption(s);
221
}
222
 
223
int TWinGraphDraw::Run(int evmask, int w, int h)
224
{
225
  if (!evfunc) return -2;
226
  this->evmask = evmask;
227
  app.def_w = w; app.def_h = h;
228
  quit = 0;
229
  app.Run();
230
  return quit;
231
}
232
 
233
void TWinGraphDraw::GetSize(int &w, int &h)
234
{
235
  if (win)
236
  {
237
    TRect rect;
238
    win->GetWindowRect(rect);
239
    w = rect.Width(); h = rect.Height();
240
  }
241
  else TGraphDraw::GetSize(w, h);
242
}
243
 
244
int TWinGraphDraw::OpenDraw()
245
{
246
  if (!win) return 0;
247
  if (!dc) {dc = new TWindowDC(*win); kind_dc = 1;}
248
  return 1;
249
}
250
 
251
void TWinGraphDraw::CloseDraw()
252
{
253
  if (dc && kind_dc == 1) delete dc;
254
  if (kind_dc <= 2) {dc = 0; kind_dc = 0;}
255
}
256
 
257
int TWinGraphDraw::SetColor(unsigned long c)
258
{
259
  if (!win || !dc) return 0;
260
  TColor color(c);
261
  dc->SelectObject((TPen)color);
262
  dc->SetTextColor(color);
263
  return 1;
264
}
265
 
266
int TWinGraphDraw::DrawLine(int x0, int y0, int x1, int y1)
267
{
268
  if (!win || !dc) return 0;
269
  dc->MoveTo(x0, y0);
270
  dc->LineTo(x1, y1);
271
  return 1;
272
}
273
 
274
int TWinGraphDraw::DrawText(int x0, int y0, char *text)
275
{
276
  if (!win || !dc) return 0;
277
  dc->TextOut(x0, y0, text);
278
  return 1;
279
}
280
 
281
int TWinGraphDraw::DrawClear()
282
{
283
  if (!win || !dc) return 0;
284
  dc->FillRect(TRect(TPoint(0, 0), win->GetWindowRect().Size()), bgcolor);
285
  return 1;
286
}
287
 
288
int TWinGraphDraw::GetTextH(const char *s)
289
{
290
  if (win)
291
  {
292
    TFont fnt = win->GetWindowFont();
293
    return fnt.GetHeight();
294
  }
295
  else return TGraphDraw::GetTextH(s);
296
}
297
 
298
int TWinGraphDraw::GetTextW(const char *s)
299
{
300
  if (win)
301
  {
302
    TFont fnt = win->GetWindowFont();
303
    return fnt.GetMaxWidth() * strlen(s);
304
  }
305
  else return TGraphDraw::GetTextW(s);
306
}
307
 
308
void TWinGraphDraw::Quit(int q)
309
{
310
  quit = q;
311
  if (quit <= 0) quit = 0;
312
  else if (win) win->PostMessage(WM_CLOSE);
313
}
314
 
315
DEFINE_RESPONSE_TABLE1(TWinGraphDraw::TDrwFrameWindow, TFrameWindow)
316
  EV_WM_SYSCOMMAND,
317
END_RESPONSE_TABLE;
318
 
319
TWinGraphDraw::TDrwFrameWindow::TDrwFrameWindow(TWindow *parent, TDrwWindow *client)
320
             : TFrameWindow(parent, client->win_draw->GetTitle(), client)
321
{
322
  win_draw = client->win_draw;
323
}
324
 
325
void TWinGraphDraw::TDrwFrameWindow::SetupWindow()
326
{
327
  TFrameWindow::SetupWindow();
328
  if (win_draw->GetAboutInfo() > 0)
329
  {
330
    TMenu menu = GetSystemMenu(false);
331
    menu.AppendMenu(MF_SEPARATOR);
332
    menu.AppendMenu(MF_STRING, CM_ABOUT, "&About");
333
  }
334
}
335
 
336
void TWinGraphDraw::TDrwFrameWindow::EvSysCommand(uint cmdType, TPoint& point)
337
{
338
  if (cmdType == CM_ABOUT)
339
  {
340
    char dia_name[50] = "IDD_ABOUT";
341
    int dia_num = win_draw->GetAboutInfo();
342
    if (dia_num > 0)
343
    {
344
      itoa(dia_num, dia_name + strlen(dia_name), 10);
345
      TAboutDialog(this, dia_name).Execute();
346
    }
347
  }
348
  else TFrameWindow::EvSysCommand(cmdType, point);
349
}
350
 
351
DEFINE_RESPONSE_TABLE1(TWinGraphDraw::TDrwWindow, TWindow)
352
  EV_WM_MOUSEMOVE,
353
  EV_WM_LBUTTONDBLCLK,
354
  EV_WM_LBUTTONDOWN,
355
  EV_WM_LBUTTONUP,
356
  EV_WM_RBUTTONDBLCLK,
357
  EV_WM_RBUTTONDOWN,
358
  EV_WM_RBUTTONUP,
359
  EV_WM_MBUTTONDBLCLK,
360
  EV_WM_MBUTTONDOWN,
361
  EV_WM_MBUTTONUP,
362
  EV_WM_SIZE,
363
  EV_WM_KEYDOWN,
364
  EV_WM_KEYUP,
365
END_RESPONSE_TABLE;
366
 
367
void TWinGraphDraw::TDrwWindow::SetupWindow()
368
{
369
  TWindow::SetupWindow();
370
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0) return;
371
  event ev;
372
  ev.type = event::start;
373
  ev.any.drw = win_draw;
374
  win_draw->evfunc(ev);
375
  win_draw->CloseDraw();
376
}
377
 
378
bool TWinGraphDraw::TDrwWindow::CanClose()
379
{
380
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 1) return true;
381
  event ev;
382
  ev.type = event::close;
383
  ev.any.drw = win_draw;
384
  win_draw->quit = win_draw->evfunc(ev);
385
  if (win_draw->quit <= 0) win_draw->quit = 0;
386
  win_draw->CloseDraw();
387
  return win_draw->quit != 0;
388
}
389
 
390
void TWinGraphDraw::TDrwWindow::Paint(TDC &dc, bool, TRect&)
391
{
392
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0) return;
393
  win_draw->CloseDraw();
394
  win_draw->dc = &dc; win_draw->kind_dc = 2;
395
  event ev;
396
  ev.type = event::draw;
397
  ev.any.drw = win_draw;
398
  win_draw->evfunc(ev);
399
  win_draw->CloseDraw();
400
}
401
 
402
void TWinGraphDraw::TDrwWindow::EvLButtonDown(uint /*modKeys*/, TPoint& point)
403
{
404
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
405
      !(win_draw->evmask & button_down_mask)) return;
406
  event ev;
407
  ev.type = event::button_down;
408
  ev.button.drw = win_draw;
409
  ev.button.n = 1;
410
  ev.button.x = point.x;
411
  ev.button.y = point.y;
412
  int r = win_draw->evfunc(ev);
413
  if (r >= 0 && (r & ret_setcapture)) SetCapture();
414
  win_draw->CloseDraw();
415
}
416
 
417
void TWinGraphDraw::TDrwWindow::EvLButtonUp(uint /*modKeys*/, TPoint& point)
418
{
419
  ReleaseCapture();
420
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
421
      !(win_draw->evmask & button_up_mask)) return;
422
  event ev;
423
  ev.type = event::button_up;
424
  ev.button.drw = win_draw;
425
  ev.button.n = 1;
426
  ev.button.x = point.x;
427
  ev.button.y = point.y;
428
  win_draw->evfunc(ev);
429
  win_draw->CloseDraw();
430
}
431
 
432
void TWinGraphDraw::TDrwWindow::EvRButtonDown(uint /*modKeys*/, TPoint& point)
433
{
434
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
435
      !(win_draw->evmask & button_down_mask)) return;
436
  event ev;
437
  ev.type = event::button_down;
438
  ev.button.drw = win_draw;
439
  ev.button.n = 2;
440
  ev.button.x = point.x;
441
  ev.button.y = point.y;
442
  int r = win_draw->evfunc(ev);
443
  if (r >= 0 && (r & ret_setcapture)) SetCapture();
444
  win_draw->CloseDraw();
445
}
446
 
447
void TWinGraphDraw::TDrwWindow::EvRButtonUp(uint /*modKeys*/, TPoint& point)
448
{
449
  ReleaseCapture();
450
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
451
      !(win_draw->evmask & button_up_mask)) return;
452
  event ev;
453
  ev.type = event::button_up;
454
  ev.button.drw = win_draw;
455
  ev.button.n = 2;
456
  ev.button.x = point.x;
457
  ev.button.y = point.y;
458
  win_draw->evfunc(ev);
459
  win_draw->CloseDraw();
460
}
461
 
462
void TWinGraphDraw::TDrwWindow::EvMButtonDown(uint /*modKeys*/, TPoint& point)
463
{
464
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
465
      !(win_draw->evmask & button_down_mask)) return;
466
  event ev;
467
  ev.type = event::button_down;
468
  ev.button.drw = win_draw;
469
  ev.button.n = 3;
470
  ev.button.x = point.x;
471
  ev.button.y = point.y;
472
  int r = win_draw->evfunc(ev);
473
  if (r >= 0 && (r & ret_setcapture)) SetCapture();
474
  win_draw->CloseDraw();
475
}
476
 
477
void TWinGraphDraw::TDrwWindow::EvMButtonUp(uint /*modKeys*/, TPoint& point)
478
{
479
  ReleaseCapture();
480
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
481
      !(win_draw->evmask & button_up_mask)) return;
482
  event ev;
483
  ev.type = event::button_up;
484
  ev.button.drw = win_draw;
485
  ev.button.n = 3;
486
  ev.button.x = point.x;
487
  ev.button.y = point.y;
488
  win_draw->evfunc(ev);
489
  win_draw->CloseDraw();
490
}
491
 
492
void TWinGraphDraw::TDrwWindow::EvMouseMove(uint /*modKeys*/, TPoint& point)
493
{
494
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
495
      !(win_draw->evmask & (mouse_move_mask | mouse_drag_mask))) return;
496
  event ev;
497
  ev.type = event::mouse_move;
498
  ev.button.drw = win_draw;
499
  ev.button.n = -1;
500
  ev.button.x = point.x;
501
  ev.button.y = point.y;
502
  win_draw->evfunc(ev);
503
  win_draw->CloseDraw();
504
}
505
 
506
void TWinGraphDraw::TDrwWindow::EvKeyDown(uint key, uint /*repeatCount*/, uint /*flags*/)
507
{
508
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
509
      !(win_draw->evmask & key_down_mask)) return;
510
  event ev;
511
  ev.type = event::key_down;
512
  ev.key.drw = win_draw;
513
  ev.key.k = GetKeySym(key);
514
  win_draw->evfunc(ev);
515
  win_draw->CloseDraw();
516
}
517
 
518
void TWinGraphDraw::TDrwWindow::EvKeyUp(uint key, uint /*repeatCount*/, uint /*flags*/)
519
{
520
  if (!win_draw || !win_draw->evfunc || win_draw->quit > 0 ||
521
      !(win_draw->evmask & key_up_mask)) return;
522
  event ev;
523
  ev.type = event::key_up;
524
  ev.key.drw = win_draw;
525
  ev.key.k = GetKeySym(key);
526
  win_draw->evfunc(ev);
527
  win_draw->CloseDraw();
528
}
529
 
530
#endif  //_WIN_GRAPHIC_DRAW_H