logo
Main Page | Namespaces | Classes | Compounds | Files | Compound Members | Related

Canvas.cpp

00001 //
00002 //  SYNTOPIA. See http://Syntopia.sourceforge.net for details and documentation.        
00003 //
00004 //      Author of this file: Mikael Hvidtfeldt Christensen (mikaelc@users.sourceforge.net)
00005 //
00006 
00007 
00008 
00009 #include "Canvas.h"
00010 
00011 namespace GUIToolkit {
00012 
00013 MCanvas::MCanvas() {
00014         myPen = 0; oldPen = 0;
00015         myBrush = 0; oldBrush = 0;
00016 }
00017 
00018 MCanvas::~MCanvas() {
00019          // Free the windowsclass allocated in PutInWinForm
00020          // with RegisterClassEx                
00021          UnregisterClass(g_szClassName, GUIControl::GUIInstance->hInstance);
00022 }
00023 
00024 
00025 void MCanvas::PutInWinForm(WinForm * WF, int x0, int y0, int x1, int y1) {
00026         
00027         topWF = WF;
00028         _x0 = x0; _y0 = y0;
00029         _x1 = x1; _y1 = y1;
00030         ID = (HMENU)GUIControl::GUIInstance->Register(this);
00031 
00032         
00033         //Make a name for class.
00034         strcpy(g_szClassName,GUIControl::GUIInstance->getUniqueName().c_str());
00035         
00036         // This handles all the standard initialization of the window.  
00037         // Registering the Window Class
00038     wc.cbSize        = sizeof(WNDCLASSEX);                  // Size of Structure
00039     wc.style         = 0;       
00040     wc.lpfnWndProc   = GUIControl::WndProc; // WinAPI event Handler
00041     wc.cbClsExtra    = 0;
00042     wc.cbWndExtra    = 0;
00043     wc.hInstance     = GUIControl::GUIInstance->hInstance;
00044     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
00045     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
00046     wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); // (HBRUSH)(COLOR_WINDOW+1);
00047     wc.lpszMenuName  = NULL;
00048     wc.lpszClassName = g_szClassName;
00049     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
00050 
00051         // For dll compatibility
00052         wc.hIcon         = 0;
00053     wc.hCursor       = 0;
00054     wc.hIconSm       = 0;
00055 
00056     if(!RegisterClassEx(&wc))
00057     {
00058                 LPVOID lpMsgBuf;
00059                 if (!FormatMessage( 
00060                         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00061                         FORMAT_MESSAGE_FROM_SYSTEM | 
00062                         FORMAT_MESSAGE_IGNORE_INSERTS,
00063                         NULL,
00064                         GetLastError(),
00065                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00066                         (LPTSTR) &lpMsgBuf,
00067                         0,
00068                         NULL ))
00069                 {
00070                         // Handle the error.
00071                         return;
00072                 }
00073                 
00074                 // Process any inserts in lpMsgBuf.
00075                 // ...
00076                 
00077                 // Display the string.
00078                 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
00079                 
00080                 // Free the buffer.
00081                 LocalFree( lpMsgBuf );
00082         MessageBox(NULL, "Window Registration Failed33!", GUIControl::GUIInstance->getUniqueName().c_str(),
00083             MB_ICONEXCLAMATION | MB_OK);
00084         // TODO: We should THROW the error 
00085     }
00086 
00087     // Create the Window
00088 
00089         
00090         hLabel = CreateWindowEx(
00091         WS_EX_CLIENTEDGE,
00092         g_szClassName,
00093         "Funky",
00094         WS_CHILD | WS_EX_TRANSPARENT | WS_VISIBLE,
00095         x0,y0,x1,y1,
00096         WF->hwnd, ID,  GUIControl::GUIInstance->hInstance, NULL); //GetModuleHandle(NULL)
00097 
00098     if(hLabel == NULL)
00099     {
00100                 LPVOID lpMsgBuf;
00101                 if (!FormatMessage( 
00102                         FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00103                         FORMAT_MESSAGE_FROM_SYSTEM | 
00104                         FORMAT_MESSAGE_IGNORE_INSERTS,
00105                         NULL,
00106                         GetLastError(),
00107                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00108                         (LPTSTR) &lpMsgBuf,
00109                         0,
00110                         NULL ))
00111                 {
00112                         // Handle the error.
00113                         return;
00114                 }
00115                 
00116                 // Process any inserts in lpMsgBuf.
00117                 // ...
00118                 
00119                 // Display the string.
00120                 MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
00121                 
00122                 // Free the buffer.
00123                 LocalFree( lpMsgBuf );
00124         MessageBox(NULL, "Window Creation Failed44!", "Error!",
00125             MB_ICONEXCLAMATION | MB_OK);
00126         // TODO: We should THROW the error 
00127     }
00128 
00129         GUIControl::GUIInstance->RegisterManualDraw(hLabel,this);
00130         
00131                 PAINTSTRUCT ps;
00132 
00133         // We Need a handle to the window. BeginPaint will do for now.
00134         HDC hdc = BeginPaint(WF->hwnd, &ps);
00135 
00136         // We need a drawing context, and a bitmap for our back-buffer:
00137         OffScreenHdc = CreateCompatibleDC(hdc); 
00138     OffScreen = CreateCompatibleBitmap(hdc, x1, y1);
00139         
00140     // We must associate the Bitmap with the OffScrenHdc.
00141         // The Routine seems to return an 'old' buffer - do we need it?
00142         HBITMAP hbmOldBuffer = (HBITMAP) SelectObject(OffScreenHdc, OffScreen);
00143 
00144         // We havent done any paint, but end it anyways.
00145         EndPaint(WF->hwnd, &ps);
00146         clear();
00147 }
00148 
00149 void MCanvas::HandleEvents(event e) 
00150 {       
00151     RECT rcClient;                 // client area rectangle
00152     POINT ptClientUL;              // client upper left corner
00153     POINT ptClientLR;              // client lower right corner
00154 
00155 
00156 
00157         if (e.msg == WM_LBUTTONUP)
00158         {
00159                         //      ReleaseCapture();
00160                  ClipCursor(NULL);
00161         }
00162 
00163         if (e.msg == WM_LBUTTONDOWN)
00164         {
00165                         //SetCapture(hLabel);
00166 
00167             GetClientRect(hLabel, &rcClient);
00168             ptClientUL.x = rcClient.left;
00169             ptClientUL.y = rcClient.top;
00170 
00171             // Add one to the right and bottom sides, because the
00172             // coordinates retrieved by GetClientRect do not
00173             // include the far left and lowermost pixels.
00174 
00175             ptClientLR.x = rcClient.right + 1;
00176             ptClientLR.y = rcClient.bottom + 1;
00177             ClientToScreen(hLabel, &ptClientUL);
00178             ClientToScreen(hLabel, &ptClientLR);
00179 
00180             SetRect(&rcClient, ptClientUL.x, ptClientUL.y, ptClientLR.x, ptClientLR.y);
00181             ClipCursor(&rcClient);
00182         }
00183         
00184         e.EventSource = this;
00185         if (myEventHandler !=0) myEventHandler->HandleEvents(e);
00186 
00187 
00188         
00189 };
00190 
00191 
00192 void MCanvas::doDraw() {
00193         InvalidateRect(hLabel,0,FALSE);
00194 }
00195 
00196 void MCanvas::draw() {
00197         PAINTSTRUCT ps;                 
00198         HDC hdc = BeginPaint(hLabel, &ps);
00199         BitBlt(hdc, 0, 0, _x1, _y1, OffScreenHdc, 0, 0, SRCCOPY);
00200         EndPaint(hLabel, &ps);
00201 
00202 };
00203 
00204 void MCanvas::clear() {
00205         BitBlt(OffScreenHdc,0,0,_x1,_y1,OffScreenHdc,0,0,WHITENESS); 
00206 }
00207 
00208 void MCanvas::putPixel(int x,int y,int r, int g, int b)
00209 {
00210         SetPixel(OffScreenHdc, x, y, RGB(r,g,b));
00211 }
00212 
00213 void MCanvas::line(int x,int y,int x2, int y2)
00214 {
00215         MoveToEx(OffScreenHdc, x, y, 0);
00216         LineTo(OffScreenHdc, x2,y2);
00217 }
00218 
00219 void MCanvas::circle(int x,int y,int radius)
00220 {
00221         Ellipse(OffScreenHdc, x-radius, y-radius, x+radius, y+radius);  
00222 }
00223 
00224 void MCanvas::rectangle(int x,int y,int x2, int y2)
00225 {
00226         ::Rectangle(OffScreenHdc, x,y,x2,y2);   
00227 }
00228 
00229 
00230 void MCanvas::deleteColor()
00231 {   SelectObject(OffScreenHdc, oldPen);
00232     DeleteObject(myPen);
00233         myPen = 0;
00234 }
00235 
00236 void MCanvas::setColor(int r,int g,int b)
00237 {
00238         if (myPen != 0) {deleteColor();}
00239         myPen = CreatePen(PS_SOLID, 0, RGB(r, g, b));  
00240         oldPen = (HPEN) SelectObject(OffScreenHdc, myPen);
00241 }
00242 
00243 void MCanvas::setColor(COLORREF c)
00244 {
00245         if (myPen != 0) {deleteColor();}
00246         myPen = CreatePen(PS_SOLID, 0, c);  
00247         oldPen = (HPEN) SelectObject(OffScreenHdc, myPen);
00248 }
00249 
00250 
00251 void MCanvas::deleteBrush()
00252 {   SelectObject(OffScreenHdc, oldBrush);
00253     DeleteObject(myBrush);
00254         myBrush = 0;
00255 }
00256 
00257 void MCanvas::setBrush(int r,int g,int b)
00258 {
00259         if (myBrush != 0) {deleteBrush();}
00260         myBrush = CreateSolidBrush(RGB(r, g, b));  
00261         oldBrush = (HBRUSH) SelectObject(OffScreenHdc, myBrush);
00262 }
00263 
00264 void MCanvas::setBrush(COLORREF c)
00265 {
00266         if (myBrush != 0) {deleteBrush();}
00267         myBrush = CreateSolidBrush(c);  
00268         oldBrush = (HBRUSH) SelectObject(OffScreenHdc, myBrush);
00269 }
00270 
00271 void MCanvas::print(int x,int y,std::string s)
00272 {
00273 
00274     TextOut (OffScreenHdc, x, y, s.c_str(), s.length() );
00275         
00276 }
00277 
00278 void MCanvas::setBGColor(int r, int g, int b)
00279 {
00280         SetBkColor (OffScreenHdc, RGB(r,g,b));
00281 }
00282 
00283 void MCanvas::setBGColor(COLORREF c)
00284 {
00285         SetBkColor (OffScreenHdc, c);
00286 }
00287 
00288 void MCanvas::setTextColor(int r, int g, int b)
00289 {
00290         SetTextColor (OffScreenHdc, RGB(r,g,b));
00291 }
00292 
00293 void MCanvas::setTextColor(COLORREF c)
00294 {
00295         SetTextColor (OffScreenHdc, c);
00296 }
00297 
00298 }; // end of namespace

Syntopia Project. Visit the web page, or the SourceForge page.
Docs made by Doxygen. Email: Mikael Christensen