home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / BITMAPGA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  2.4 KB  |  99 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1993, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of class TBitmapGadget
  6. //----------------------------------------------------------------------------
  7. #include <owl/owlpch.h>
  8. #include <owl/bitmapga.h>
  9. #include <owl/gadgetwi.h>
  10. #include <owl/celarray.h>
  11.  
  12. TBitmapGadget::TBitmapGadget(TResId bmpResId,
  13.                              int id,
  14.                              TBorderStyle borderStyle,
  15.                              int numImages, int startImage)
  16. :
  17.   TGadget(id,borderStyle),
  18.   ResId(bmpResId),
  19.   NumImages(numImages)
  20. {
  21.   ImageArray = 0;
  22.   CurrentImage = startImage;
  23.   BitmapOrigin.x = BitmapOrigin.y = 0;
  24. }
  25.  
  26. TBitmapGadget::~TBitmapGadget()
  27. {
  28.   delete ImageArray;
  29. }
  30.  
  31. //
  32. // Handle a system color change by cleaning up & reloading & processing the
  33. // bitmap. Can also be called to create the initial bitmap.
  34. //
  35. void
  36. TBitmapGadget::SysColorChange()
  37. {
  38.   if (ImageArray)
  39.     delete ImageArray;
  40.   
  41.   TDib dib(*Window->GetModule(), ResId);
  42.   dib.MapUIColors(
  43.     TDib::MapFace | TDib::MapText | TDib::MapShadow | TDib::MapHighlight
  44.   );
  45.   ImageArray = new TCelArray(dib, NumImages);
  46. }
  47.  
  48. void
  49. TBitmapGadget::SetBounds(TRect& boundRect)
  50. {
  51.   TGadget::SetBounds(boundRect);
  52.   TRect  innerRect;
  53.   TSize  bitmapSize = ImageArray->CelSize();
  54.   GetInnerRect(innerRect);
  55.   
  56.   BitmapOrigin.x = innerRect.left + (innerRect.Width()-bitmapSize.cx)/2;
  57.   BitmapOrigin.y = innerRect.top + (innerRect.Height()-bitmapSize.cy)/2;
  58. }
  59.  
  60. void
  61. TBitmapGadget::GetDesiredSize(TSize& size)
  62. {
  63.   TGadget::GetDesiredSize(size);
  64.  
  65.   if (!ImageArray)
  66.     SysColorChange();   // Get the initial bitmap
  67.  
  68.   size += ImageArray->CelSize();
  69. }
  70.  
  71. int
  72. TBitmapGadget::SelectImage(int imageNum, bool immediate)
  73. {
  74.   uint oldImageNum = CurrentImage;
  75.   if (imageNum != CurrentImage) {
  76.     CurrentImage = imageNum;
  77.     Invalidate(false);
  78.   }
  79.   if (immediate)
  80.     Update();
  81.   return oldImageNum;
  82. }
  83.  
  84. void
  85. TBitmapGadget::Paint(TDC& dc)
  86. {
  87.   PaintBorder(dc);
  88.  
  89.   TMemoryDC imageDC;
  90.   imageDC.SelectObject(*ImageArray);
  91.  
  92.   TRect sourceRect = ImageArray->CelRect(CurrentImage);
  93.   TRect destRect;
  94.   GetInnerRect(destRect);
  95.   dc.BitBlt(destRect.left, destRect.top,
  96.             sourceRect.Width(), sourceRect.Height(),
  97.             imageDC, sourceRect.left, sourceRect.top);
  98. }
  99.