Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

resource_painter.cpp

00001 // resource_painter.cpp
00002 
00003 // Copyright (C) 2004 Steven Weiß (steven11@gmx.de)
00004 //
00005 // Permission to copy, use, sell and distribute this software is granted
00006 // provided this copyright notice appears in all copies.
00007 // Permission to modify the code and to distribute modified code is granted
00008 // provided this copyright notice appears in all copies, and a notice
00009 // that the code was modified is included with the copyright notice.
00010 //
00011 // This software is provided "as is" without express or implied warranty,
00012 // and with no claim as to its suitability for any purpose.
00013 
00014 
00015 #include <background_wnd/painters/resource_painter.hpp>
00016 #include <crtdbg.h>
00017 
00018 
00019 namespace win32 { namespace gui {
00020 
00021 // ============================================================================================================= //
00022 // res_info
00023 
00024 namespace bg_wnd {
00025 
00031 res_info::res_info(unsigned nResource, unsigned uType, int cx, int cy, unsigned fuLoad, HINSTANCE hInst)
00032      : pchResource_(MAKEINTRESOURCE(nResource)), uType_(uType), cx_(cx), cy_(cy), fuLoad_(fuLoad), hInst_(hInst)
00033 {}
00034 
00035 res_info::res_info(const char* pchResource, unsigned uType, int cx, int cy, unsigned fuLoad, HINSTANCE hInst) 
00036      : pchResource_(pchResource), uType_(uType), cx_(cx), cy_(cy), fuLoad_(fuLoad), hInst_(hInst)
00037 {}
00038 
00039 }    // namespace bg_wnd
00040 
00041 // ============================================================================================================= //
00042 // resource_painter
00043 
00044 namespace detail {
00045 
00046 resource_painter_base::resource_painter_base(param0 info) : hResource_(NULL), uType_(0)
00047 {
00048      load_resource(info);
00049 }
00050      
00051 resource_painter_base::resource_painter_base(const resource_painter_base& p) : hResource_(NULL), uType_(0)
00052 {
00053      *this = p;               // call operator= ;-)
00054 }
00055 
00056 resource_painter_base::~resource_painter_base()
00057 {
00058      delete_resource();
00059 }
00060 
00061 const resource_painter_base& resource_painter_base::operator= (const resource_painter_base& p)
00062 {
00063      if (this != &p) {
00064           delete_resource();
00065           
00066           // FIX
00067           // before: switch(uType_) -> assignment was wrong because uType_ is reset in delete_resource()
00068           switch (p.uType_) {
00069                case IMAGE_CURSOR:  hResource_ = CopyCursor((HCURSOR) p.hResource_); break;
00070                case IMAGE_ICON:    hResource_ = CopyIcon((HICON) p.hResource_); break;
00071                case IMAGE_BITMAP:  
00072                     // create dc's
00073                     SIZE size   = p.resource_size();                  
00074                     HDC hSrcDC  = CreateCompatibleDC(NULL);
00075                     HDC hDestDC = CreateCompatibleDC(NULL);
00076                     
00077                     // blit                  
00078                     HBITMAP hOldBmp = (HBITMAP) SelectObject(hSrcDC, p.hResource_);
00079                     hResource_  = CreateCompatibleBitmap(hDestDC, size.cx, size.cy);
00080                     SelectObject(hDestDC, hResource_);
00081                     BitBlt(hDestDC, 0, 0, size.cx, size.cy, hSrcDC, 0, 0, SRCCOPY);
00082                     
00083                     // clean up
00084                     SelectObject(hSrcDC, hOldBmp);
00085                     SelectObject(hDestDC, hOldBmp);
00086                     DeleteDC(hSrcDC);
00087                     DeleteDC(hDestDC);
00088                     
00089                     break;
00090           }
00091           uType_ = p.uType_;
00092      }
00093      return *this;
00094 }
00095 
00096 void resource_painter_base::delete_resource()
00097 {
00098      switch (uType_) {
00099           case IMAGE_BITMAP: DeleteObject(hResource_); break;
00100           case IMAGE_CURSOR: DestroyCursor((HCURSOR) hResource_); break;
00101           case IMAGE_ICON:   DestroyIcon((HICON) hResource_); break;
00102           default: _ASSERTE(false); break;             // wrong type!
00103      }
00104      hResource_ = NULL;
00105      uType_ = 0;
00106 }
00107 
00108 void resource_painter_base::draw_resource(HDC hDC, int x, int y, int cx, int cy)
00109 {    
00110      switch (uType_) {        
00111           case IMAGE_BITMAP: { 
00112                HDC hTmp = CreateCompatibleDC(hDC);
00113                HGDIOBJ hOld = SelectObject(hTmp, (HGDIOBJ) hResource_);
00114                BitBlt(hDC, x, y, cx, cy, hTmp, 0, 0, SRCCOPY);
00115                SelectObject(hTmp, hOld);
00116                DeleteDC(hTmp);
00117                break;    
00118           }
00119 
00120           case IMAGE_CURSOR:            
00121           case IMAGE_ICON: {
00122                SIZE size = resource_size();            
00123                DrawIconEx(hDC, x, y, (HICON) hResource_, size.cx, size.cy, 0, NULL, DI_NORMAL);
00124                break;
00125           }
00126      }
00127 }
00128 
00130 void resource_painter_base::load_resource(const bg_wnd::res_info& info)
00131 {
00132      if (info.uType_ == IMAGE_BITMAP || info.uType_ == IMAGE_CURSOR || info.uType_ == IMAGE_ICON) { 
00133           delete_resource();       
00134           hResource_ = LoadImage(info.hInst_, info.pchResource_, info.uType_, info.cx_, info.cy_, info.fuLoad_);        
00135           uType_ = info.uType_;         
00136      }
00137      else {
00138           _ASSERTE(false);              // Type must be one of these: IMAGE_BITMAP, IMAGE_CURSOR or IMAGE_ICON
00139      }
00140 }
00141 
00143 SIZE resource_painter_base::resource_size() const
00144 {
00145      HBITMAP hBitmap = NULL;
00146      switch (uType_) {        
00147           case IMAGE_BITMAP: hBitmap = (HBITMAP) hResource_; break;        
00148 
00149           case IMAGE_CURSOR:            
00150           case IMAGE_ICON: 
00151                ICONINFO info = { 0 };                            
00152                GetIconInfo((HICON) hResource_, &info);
00153                hBitmap = info.hbmColor ? info.hbmColor : info.hbmMask; 
00154                break;
00155      }    
00156 
00157      SIZE size = { 0, 0 };
00158      BITMAP bm = { 0 };
00159      if (GetObject(hBitmap, sizeof (BITMAP), &bm) != 0) {
00160           size.cx = bm.bmWidth;
00161           size.cy = bm.bmHeight;
00162      }
00163      return size;
00164 }
00165 
00166 }    // namespace detail
00167 
00168 // ========================================================================================================= //
00169 
00176 resource_painter::resource_painter(param0 info) : Base(info)
00177 {}
00178 
00179 resource_painter* resource_painter::clone() const
00180 {
00181      return new resource_painter(*this);
00182 }
00183 
00184 void resource_painter::draw(HDC hDC, int cx, int cy)
00185 {
00186      draw_resource(hDC, 0, 0, cx, cy);
00187 }
00188 
00189 // ========================================================================================================= //
00190 
00197 tile_resource_painter::tile_resource_painter(param0 info) : Base(info)
00198 {
00199      tile_size_.cx = 0;
00200      tile_size_.cy = 0;
00201 }
00202 
00203 tile_resource_painter::tile_resource_painter(param0 info, param1 tile_size) : Base(info), tile_size_(tile_size)
00204 {}
00205 
00206 tile_resource_painter* tile_resource_painter::clone() const
00207 {
00208      return new tile_resource_painter(*this);
00209 }
00210 
00211 void tile_resource_painter::draw(HDC hDC, int cx, int cy)
00212 {
00213      SIZE size = tile_size_;
00214      if (tile_size_.cx == 0 && tile_size_.cy == 0)
00215           size = resource_size();
00216 
00217      _ASSERTE(size.cx != 0 && size.cy != 0);                // infinite loop if one is 0
00218      for (int x = 0; x < cx; x += size.cx) {
00219           for (int y = 0; y < cy; y += size.cy) {      
00220                draw_resource(hDC, x, y, cx - x, cy - y);              
00221           }         
00222      }
00223 }
00224 
00229 void tile_resource_painter::tile_size(SIZE tile_size)
00230 {    
00231      tile_size_ = tile_size;
00232 }
00233 
00235 SIZE tile_resource_painter::tile_size() const
00236 {
00237      return tile_size_;
00238 }
00239 
00240 
00241 } }  // namespace win32::gui

Generated on Mon Dec 27 15:30:11 2004 for background_wnd by  doxygen 1.3.9.1