Canvas.cpp (5674B)
1 #include <win/Canvas.h> 2 #include <win/Window.h> 3 #include <win/Event.h> 4 #include "Connection.h" 5 #include "../../srv/window/protocol.h" 6 7 Canvas::Canvas() 8 : fID(-1), 9 fWindow(0), 10 fInPaint(false), 11 fShowLevel(0) 12 { 13 } 14 15 Canvas::~Canvas() 16 { 17 } 18 19 void Canvas::DrawLine(long x1, long y1, long x2, long y2) 20 { 21 if (fWindow == 0) 22 return; 23 24 fWindow->fConnection->WriteInt8(OP_DRAW_LINE); 25 fWindow->fConnection->WriteInt32(fID); 26 fWindow->fConnection->WriteInt32(x1); 27 fWindow->fConnection->WriteInt32(y1); 28 fWindow->fConnection->WriteInt32(x2); 29 fWindow->fConnection->WriteInt32(y2); 30 fWindow->fConnection->EndCommand(); 31 } 32 33 void Canvas::FillRect(long x1, long y1, long x2, long y2) 34 { 35 if (fWindow == 0) 36 return; 37 38 fWindow->fConnection->WriteInt8(OP_FILL_RECT); 39 fWindow->fConnection->WriteInt32(fID); 40 fWindow->fConnection->WriteInt32(x1); 41 fWindow->fConnection->WriteInt32(y1); 42 fWindow->fConnection->WriteInt32(x2); 43 fWindow->fConnection->WriteInt32(y2); 44 fWindow->fConnection->EndCommand(); 45 } 46 47 void Canvas::SetPenColor(char c) 48 { 49 if (fWindow == 0) 50 return; 51 52 fWindow->fConnection->WriteInt8(OP_SET_PEN_COLOR); 53 fWindow->fConnection->WriteInt32(fID); 54 fWindow->fConnection->WriteInt8(c); 55 fWindow->fConnection->EndCommand(); 56 } 57 58 void Canvas::SetBackgroundColor(char c) 59 { 60 if (fWindow == 0) 61 return; 62 63 fWindow->fConnection->WriteInt8(OP_SET_BG_COLOR); 64 fWindow->fConnection->WriteInt32(fID); 65 fWindow->fConnection->WriteInt8(c); 66 fWindow->fConnection->EndCommand(); 67 } 68 69 void Canvas::DrawString(long x, long y, const char *str) 70 { 71 if (fWindow == 0) 72 return; 73 74 fWindow->fConnection->WriteInt8(OP_DRAW_STRING); 75 fWindow->fConnection->WriteInt32(fID); 76 fWindow->fConnection->WriteInt32(x); 77 fWindow->fConnection->WriteInt32(y); 78 while (*str) 79 fWindow->fConnection->WriteInt8(*str++); 80 81 fWindow->fConnection->WriteInt8(0); 82 fWindow->fConnection->EndCommand(); 83 } 84 85 void Canvas::CopyRect(long left, long top, long right, long bottom, 86 long newLeft, long newTop) 87 { 88 if (fWindow == 0) 89 return; 90 91 fWindow->fConnection->WriteInt8(OP_COPY_RECT); 92 fWindow->fConnection->WriteInt32(fID); 93 fWindow->fConnection->WriteInt32(left); 94 fWindow->fConnection->WriteInt32(top); 95 fWindow->fConnection->WriteInt32(right); 96 fWindow->fConnection->WriteInt32(bottom); 97 fWindow->fConnection->WriteInt32(newLeft); 98 fWindow->fConnection->WriteInt32(newTop); 99 fWindow->fConnection->EndCommand(); 100 } 101 102 103 void Canvas::Hide() 104 { 105 if (fWindow == 0) 106 return; 107 108 if (--fShowLevel == 0) { 109 fWindow->fConnection->WriteInt8(OP_HIDE_WINDOW); 110 fWindow->fConnection->WriteInt32(fID); 111 fWindow->fConnection->Flush(); 112 } 113 } 114 115 void Canvas::Show() 116 { 117 if (fWindow == 0) 118 return; 119 120 if (++fShowLevel == 1) { 121 fWindow->fConnection->WriteInt8(OP_SHOW_WINDOW); 122 fWindow->fConnection->WriteInt32(fID); 123 fWindow->fConnection->Flush(); 124 } 125 } 126 127 void Canvas::BeginPaint(long *out_left, long *out_top, long *out_right, long *out_bottom) 128 { 129 if (fInPaint) 130 return; 131 132 fInPaint = true; 133 fWindow->fConnection->WriteInt8(OP_BEGIN_PAINT); 134 fWindow->fConnection->WriteInt32(fID); 135 fWindow->fConnection->Flush(); 136 137 *out_left = fWindow->fConnection->ReadInt32(); 138 *out_top = fWindow->fConnection->ReadInt32(); 139 *out_right = fWindow->fConnection->ReadInt32(); 140 *out_bottom = fWindow->fConnection->ReadInt32(); 141 } 142 143 void Canvas::EndPaint() 144 { 145 if (!fInPaint) 146 return; 147 148 fInPaint = false; 149 fWindow->fConnection->WriteInt8(OP_END_PAINT); 150 fWindow->fConnection->WriteInt32(fID); 151 fWindow->fConnection->Flush(); 152 } 153 154 void Canvas::AddChild(Canvas *child, long left, long top, long right, long bottom) 155 { 156 if (fWindow == 0) 157 return; 158 159 fWindow->fConnection->WriteInt8(OP_CREATE_WINDOW); 160 fWindow->fConnection->WriteInt32(fID); // My child 161 fWindow->fConnection->WriteInt32(left); 162 fWindow->fConnection->WriteInt32(top); 163 fWindow->fConnection->WriteInt32(right); 164 fWindow->fConnection->WriteInt32(bottom); 165 fWindow->fConnection->WriteInt32(fWindow->fEventPort); 166 fWindow->fConnection->Flush(); 167 child->fID = fWindow->fConnection->ReadInt32(); 168 child->fWindow = fWindow; 169 child->fLeft = 0; 170 child->fTop = 0; 171 child->fRight = right - left; 172 child->fBottom = bottom - top; 173 174 // Stick window in the window's canvas list 175 child->fWinListNext = fWindow->fCanvasList; 176 child->fWinListPrev = &fWindow->fCanvasList; 177 if (fWindow->fCanvasList) 178 fWindow->fCanvasList->fWinListPrev = &child->fWinListNext; 179 180 fWindow->fCanvasList = child; 181 182 child->Show(); 183 } 184 185 void Canvas::RemoveChild(Canvas *child) 186 { 187 fWindow->fConnection->WriteInt8(OP_DESTROY_WINDOW); 188 fWindow->fConnection->WriteInt32(child->fID); 189 fWindow->fConnection->Flush(); 190 *child->fWinListPrev = child->fWinListNext; 191 } 192 193 void Canvas::HandleEvent(Event *event) 194 { 195 switch (event->what) { 196 case EVT_PAINT: { 197 long left, top, right, bottom; 198 BeginPaint(&left, &top, &right, &bottom); 199 Repaint(left, top, right, bottom); 200 EndPaint(); 201 break; 202 } 203 204 default: 205 EventReceived(event); 206 } 207 } 208 209 Window* Canvas::GetWindow() const 210 { 211 return fWindow; 212 } 213 214 void Canvas::GetBounds(long *left, long *top, long *right, long *bottom) 215 { 216 *left = fLeft; 217 *top = fTop; 218 *right = fRight; 219 *bottom = fBottom; 220 } 221 222 void Canvas::Invalidate(long left, long top, long right, long bottom) 223 { 224 fWindow->fConnection->WriteInt8(OP_INVALIDATE); 225 fWindow->fConnection->WriteInt32(fID); 226 fWindow->fConnection->WriteInt32(left); 227 fWindow->fConnection->WriteInt32(top); 228 fWindow->fConnection->WriteInt32(right); 229 fWindow->fConnection->WriteInt32(bottom); 230 fWindow->fConnection->Flush(); 231 } 232 233 void Canvas::Repaint(long left, long top, long right, long bottom) 234 { 235 } 236 237 void Canvas::EventReceived(const Event *) 238 { 239 } 240 241 void Canvas::LockMouseFocus() 242 { 243 fWindow->fConnection->WriteInt8(OP_LOCK_MOUSE_FOCUS); 244 fWindow->fConnection->Flush(); 245 } 246