home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / OWLSRC.PAK / BITMAPGA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.7 KB  |  148 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.8  $
  6. //
  7. // Implementation of class TBitmapGadget
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_BITMAPGA_H)
  11. # include <owl/bitmapga.h>
  12. #endif
  13. #if !defined(OWL_GADGETWI_H)
  14. # include <owl/gadgetwi.h>
  15. #endif
  16. #if !defined(OWL_CELARRAY_H)
  17. # include <owl/celarray.h>
  18. #endif
  19.  
  20. OWL_DIAGINFO;
  21. DIAG_DECLARE_GROUP(OwlGadget);  // diagnostic group for gadgets
  22.  
  23. //
  24. // Construct a bitmap gadget
  25. //
  26. TBitmapGadget::TBitmapGadget(TResId imageResIdOrIndex,
  27.                              int    id,
  28.                              TBorderStyle borderStyle,
  29.                              int    numImages,
  30.                              int    startImage,
  31.                              bool   sharedCels)
  32. :
  33.   TGadget(id, borderStyle),
  34.   NumImages(numImages),
  35.   CurrentImage(startImage),
  36.   ImageArray(0),            // Created on first call to GetDesiredSize
  37.   BitmapOrigin(0)
  38. {
  39.   if (sharedCels) {
  40.     ResId = 0;
  41.     ImageIndex = (int)(const char far*)imageResIdOrIndex;
  42.   }
  43.   else {
  44.     ResId = imageResIdOrIndex;
  45.     ImageIndex = 0;
  46.   }
  47.  
  48.   TRACEX(OwlGadget, OWL_CDLEVEL, "TBitmapGadget constructed @" << this);
  49. }
  50.  
  51. //
  52. // Destruct a bitmap gadget and free its resources
  53. //
  54. TBitmapGadget::~TBitmapGadget()
  55. {
  56.   delete ImageArray;
  57.   TRACEX(OwlGadget, OWL_CDLEVEL, "TBitmapGadget destructed @" << this);
  58. }
  59.  
  60. //
  61. // Handle a system color change by cleaning up & reloading & processing the
  62. // bitmap. Is also called to create the initial bitmap.
  63. //
  64. void
  65. TBitmapGadget::SysColorChange()
  66. {
  67.   delete ImageArray;
  68.  
  69.   if (ResId) {
  70.     TDib dib(*Window->GetModule(), ResId);
  71.     dib.MapUIColors(TDib::MapFace | TDib::MapText | TDib::MapShadow |
  72.       TDib::MapHighlight);
  73.     ImageArray = new TCelArray(dib, NumImages);
  74.   }
  75. }
  76.  
  77. //
  78. // Set the bounding rect for this button gadget. Also takes care of
  79. // re-centering the image
  80. //
  81. void
  82. TBitmapGadget::SetBounds(const TRect& boundRect)
  83. {
  84.   TRACEX(OwlGadget, 1, "TBitmapGadget::SetBounds() called @" << this);
  85.   TGadget::SetBounds(boundRect);
  86.  
  87.   TRect innerRect;
  88.   GetInnerRect(innerRect);
  89.  
  90.   TSize bitmapSize = ImageArray->CelSize();
  91.  
  92.   BitmapOrigin.x = innerRect.left + (innerRect.Width()-bitmapSize.cx)/2;
  93.   BitmapOrigin.y = innerRect.top + (innerRect.Height()-bitmapSize.cy)/2;
  94. }
  95.  
  96. //
  97. // Find out how big this bitmap gadget wants to be. Calculated using the base
  98. // size to get the borders, etc. plus the image size.
  99. //
  100. void
  101. TBitmapGadget::GetDesiredSize(TSize& size)
  102. {
  103.   TRACEX(OwlGadget, 1, "TBitmapGadget::GetDesiredSize() called @" << this);
  104.   TGadget::GetDesiredSize(size);
  105.  
  106.   if (!ImageArray)
  107.     SysColorChange();   // Get the initial bitmap
  108.  
  109.   size += (ImageArray ? ImageArray->CelSize() : Window->GetCelArray().CelSize());
  110. }
  111.  
  112. //
  113. // Choose the relative image to display. If immediate is true, this gadget is
  114. // repainted immediately
  115. //
  116. int
  117. TBitmapGadget::SelectImage(int imageNum, bool immediate)
  118. {
  119.   PRECONDITION(imageNum >=0 && imageNum < NumImages);
  120.  
  121.   uint oldImageNum = CurrentImage;
  122.  
  123.   if (imageNum != CurrentImage) {
  124.     CurrentImage = imageNum;
  125.     Invalidate(false);
  126.   }
  127.  
  128.   if (immediate)
  129.     Update();
  130.   return oldImageNum;
  131. }
  132.  
  133. //
  134. // Paint this bitmap gadget. Uses normal borders, plus draws the image centered
  135. //
  136. void
  137. TBitmapGadget::Paint(TDC& dc)
  138. {
  139.   PaintBorder(dc);
  140.  
  141.   TRect destRect;
  142.   GetInnerRect(destRect);
  143.  
  144.   TCelArray& imageArray = ImageArray ? *ImageArray : Window->GetCelArray();
  145.  
  146.   imageArray.BitBlt(ImageIndex+CurrentImage, dc, destRect.left, destRect.top);
  147. }
  148.