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

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.10  $
  6. //
  7. //  Implements TPictureWindow
  8. //----------------------------------------------------------------------------
  9. #include <owl/pch.h>
  10. #if !defined(OWL_WINDOW_H)
  11. # include <owl/window.h>
  12. #endif
  13. #if !defined(OWL_GDIOBJEC_H)
  14. # include <owl/gdiobjec.h>
  15. #endif
  16. #if !defined(OWL_PICTWIND_H)
  17. # include <owl/pictwind.h>
  18. #endif
  19. #if !defined(CLASSLIB_POINTER_H)
  20. # include <classlib/pointer.h>
  21. #endif
  22. #if !defined(WINSYS_SYSTEM_H)
  23. # include <winsys/system.h>
  24. #endif
  25.  
  26. OWL_DIAGINFO;
  27.  
  28. //
  29. // Constructor assumes ownership of the DIB passed in.
  30. //
  31. TPictureWindow::TPictureWindow(TWindow* parent, TDib* dib, TDisplayHow how,
  32.                                const char far* title, TModule* module)
  33. :
  34.   TWindow(parent, title, module), Dib(dib)
  35. {
  36.   SetHowToDisplay(how);
  37.   if (GetHowToDisplay() == Stretch) {
  38.     if (dib) {
  39.       Attr.W = dib->Width();
  40.       Attr.H = dib->Height();
  41.     }
  42.   }
  43. }
  44.  
  45. //
  46. // Destructor deletes the owned DIB.
  47. //
  48. TPictureWindow::~TPictureWindow()
  49. {
  50.   delete Dib;
  51. }
  52.  
  53.  
  54. //
  55. // Allow changing of the dib.
  56. // Return old dib.
  57. //
  58. TDib*
  59. TPictureWindow::SetDib(TDib* newDib)
  60. {
  61.   TDib* retDib = Dib;
  62.   Dib = newDib;
  63.   return retDib;
  64. }
  65.  
  66.  
  67. //
  68. // Paint the dib onto the window.
  69. //
  70. void
  71. TPictureWindow::Paint(TDC& dc, bool /*erase*/, TRect& /*rect*/)
  72. {
  73.   TPointer<TPalette> palette = 0;
  74.   bool hasPalette = ToBool(dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE);
  75.  
  76.   TDib* dib = GetDib();
  77.   if (dib) {
  78.     if (hasPalette) {
  79.       palette = new TPalette(*GetDib());
  80.       dc.SelectObject(*palette);
  81.       dc.RealizePalette();
  82.     }
  83.  
  84.     // figure out upper left corner of the client area
  85.     //
  86.     TRect clientRect(GetClientRect());
  87.     TPoint sourcePoint(0, 0);
  88.  
  89.     // adjust the upper left corner for centering picture
  90.     //
  91.     if (HowToDisplay == Center) {
  92.       // determine offsets
  93.       //
  94.       int offsetX = abs(dib->Width() - clientRect.Width()) / 2;
  95.       if (dib->Width() > clientRect.Width())
  96.         sourcePoint.x += offsetX;
  97.       else
  98.         clientRect.Offset(offsetX, 0);
  99.  
  100.       int offsetY = abs(dib->Height() - clientRect.Height()) / 2;
  101.       if (dib->Height() > clientRect.Height())
  102.         sourcePoint.y += offsetY;
  103.       else
  104.         clientRect.Offset(0, offsetY);
  105.     }
  106.  
  107.     // adjust the lower right corner
  108.     //
  109.     if (HowToDisplay != Stretch) {
  110.       clientRect.bottom = clientRect.top + dib->Height();
  111.       clientRect.right  = clientRect.left + dib->Width();
  112.  
  113.       // if the picture is larger than screen dimensions,
  114.       // adjust the upper left corner.
  115.       //
  116.       clientRect.top   -= sourcePoint.y;
  117.       clientRect.left  -= sourcePoint.x;
  118.     }
  119.  
  120.     // display the dib
  121.     //
  122.     switch (HowToDisplay) {
  123.       case UpperLeft:
  124.       case Center:
  125.         dc.SetDIBitsToDevice(clientRect, sourcePoint, *dib);
  126.         break;
  127.       case Stretch: {
  128.         TRect sourceRect(0, 0, dib->Width(), dib->Height());
  129.         dc.StretchDIBits(clientRect, sourceRect, *dib);
  130.         break;
  131.       }
  132.     } // switch HowToDisplay
  133.  
  134.     dc.RestoreObjects();
  135.   }
  136. }
  137.  
  138.  
  139. //
  140. // Change the formatting of the dib.
  141. //
  142. void
  143. TPictureWindow::SetHowToDisplay(TDisplayHow how)
  144. {
  145.   HowToDisplay = how;
  146.   if (IsWindow())
  147.     Invalidate(true);
  148. }
  149.  
  150.  
  151. //
  152. // Overridden from TWindow.
  153. // Return unique name to force GetWindowClass to be called.
  154. //
  155. char far*
  156. TPictureWindow::GetClassName()
  157. {
  158.   return "OWL_PictureWindow";
  159. }
  160.  
  161.  
  162. //
  163. // Overridden from TWindow.
  164. //
  165. void
  166. TPictureWindow::GetWindowClass(WNDCLASS& wndClass)
  167. {
  168.   TWindow::GetWindowClass(wndClass);
  169.   wndClass.style |= CS_HREDRAW | CS_VREDRAW;
  170. }
  171.  
  172.