Subversion Repositories Kolibri OS

Rev

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

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