main.cpp (2709B)
1 #include <blt/os.h> 2 #include <blt/syscall.h> 3 #include <blt/qsem.h> 4 #include <win/Window.h> 5 #include <win/Canvas.h> 6 #include <win/Event.h> 7 #include <stdio.h> 8 #include <string.h> 9 #include <stdlib.h> 10 11 const int kCellWidth = 10; 12 13 class DrawingArea : public Canvas { 14 public: 15 DrawingArea(); 16 virtual void EventReceived(const Event*); 17 private: 18 int fLastX; 19 int fLastY; 20 bool fButtonDown; 21 }; 22 23 class PaletteArea : public Canvas { 24 public: 25 PaletteArea(DrawingArea *draw); 26 virtual void EventReceived(const Event*); 27 virtual void Repaint(long left, long top, long right, long bottom); 28 private: 29 DrawingArea *fDrawingArea; 30 int fCurrentColor; 31 }; 32 33 DrawingArea::DrawingArea() 34 : fButtonDown(false) 35 { 36 } 37 38 void DrawingArea::EventReceived(const Event *event) 39 { 40 switch (event->what) { 41 case EVT_MOUSE_DOWN: 42 LockMouseFocus(); 43 fButtonDown = true; 44 fLastX = event->x; 45 fLastY = event->y; 46 break; 47 48 case EVT_MOUSE_UP: 49 fButtonDown = false; 50 break; 51 52 case EVT_MOUSE_MOVED: 53 if (fButtonDown) { 54 printf("DrawLine(%d, %d, %d, %d)\n", fLastX, fLastY, event->x, event->y); 55 DrawLine(fLastX, fLastY, event->x, event->y); 56 fLastX = event->x; 57 fLastY = event->y; 58 GetWindow()->Flush(); 59 } 60 61 break; 62 63 default: 64 Canvas::EventReceived(event); 65 66 } 67 } 68 69 PaletteArea::PaletteArea(DrawingArea *draw) 70 : fDrawingArea(draw) 71 { 72 } 73 74 void PaletteArea::EventReceived(const Event *event) 75 { 76 switch (event->what) { 77 case EVT_MOUSE_DOWN: 78 printf("PaletteArea: EventReceived\n"); 79 fDrawingArea->SetPenColor(event->x / kCellWidth * 2); 80 Invalidate(fCurrentColor * kCellWidth, 0, fCurrentColor * kCellWidth + kCellWidth, 10); 81 fCurrentColor = event->x / kCellWidth; 82 Invalidate(fCurrentColor * kCellWidth, 0, fCurrentColor * kCellWidth + kCellWidth, 10); 83 break; 84 85 default: 86 Canvas::EventReceived(event); 87 } 88 } 89 90 void PaletteArea::Repaint(long left, long top, long right, long bottom) 91 { 92 int x = (left / kCellWidth) * kCellWidth; 93 while (x < right) { 94 SetPenColor(x / kCellWidth * 2); 95 FillRect(x, top, x + kCellWidth, bottom); 96 if (x / kCellWidth == fCurrentColor) { 97 SetPenColor(0); 98 DrawLine(x, top, x + kCellWidth, top); 99 DrawLine(x, bottom, x + kCellWidth, bottom); 100 DrawLine(x, top, x, bottom); 101 DrawLine(x + kCellWidth, top, x + kCellWidth, bottom); 102 } 103 104 x += kCellWidth; 105 } 106 } 107 108 int main() 109 { 110 Window *win = new Window(50, 30, 270, 180); 111 win->Lock(); 112 win->Show(); 113 Canvas *border = new Canvas; 114 win->AddChild(border, 0, 0, 250, 150); 115 border->SetBackgroundColor(10); 116 DrawingArea *draw = new DrawingArea; 117 PaletteArea *palette = new PaletteArea(draw); 118 border->AddChild(draw, 2, 2, 218, 139); 119 border->AddChild(palette, 2, 140, 218, 148); 120 draw->SetBackgroundColor(250); 121 win->Unlock(); 122 return 0; 123 }