home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / yacl-012.zip / ui / bitmap.cxx < prev    next >
C/C++ Source or Header  |  1995-04-04  |  7KB  |  255 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. /*
  8.  *
  9.  *          Copyright (C) 1994, M. A. Sridhar
  10.  *  
  11.  *
  12.  *     This software is Copyright M. A. Sridhar, 1994. You are free
  13.  *     to copy, modify or distribute this software  as you see fit,
  14.  *     and to use  it  for  any  purpose, provided   this copyright
  15.  *     notice and the following   disclaimer are included  with all
  16.  *     copies.
  17.  *
  18.  *                        DISCLAIMER
  19.  *
  20.  *     The author makes no warranties, either expressed or implied,
  21.  *     with respect  to  this  software, its  quality, performance,
  22.  *     merchantability, or fitness for any particular purpose. This
  23.  *     software is distributed  AS IS.  The  user of this  software
  24.  *     assumes all risks  as to its quality  and performance. In no
  25.  *     event shall the author be liable for any direct, indirect or
  26.  *     consequential damages, even if the  author has been  advised
  27.  *     as to the possibility of such damages.
  28.  *
  29.  */
  30.  
  31.  
  32. #if defined(__GNUC__)
  33. #pragma implementation
  34. #endif
  35.  
  36.  
  37. #if defined (__MS_WINDOWS__)
  38. #define OEMRESOURCE
  39. #include <windows.h>
  40.  
  41. #elif defined (__X_MOTIF__)
  42. #include <X11/Intrinsic.h>
  43. #endif
  44.  
  45.  
  46.  
  47. #include "base/string.h"
  48.  
  49. #include "ui/bitmap.h"
  50. #include "ui/cntroler.h"
  51. #include "ui/dsplsurf.h"
  52.  
  53.  
  54. UI_Bitmap::UI_Bitmap ()
  55. {
  56.     _handle = 0;
  57.     _width  = 0;
  58.     _height = 0;
  59. }
  60.  
  61.  
  62. #if defined (__MS_WINDOWS__)
  63. static struct {
  64.     char* name;
  65.     long  code;
  66. } BuiltIn [] = {
  67. "+OBM_CLOSE"   , OBM_CLOSE   ,
  68. "+OBM_UPARROW" , OBM_UPARROW ,
  69. "+OBM_DNARROW" , OBM_DNARROW ,
  70. "+OBM_RGARROW" , OBM_RGARROW ,
  71. "+OBM_LFARROW" , OBM_LFARROW ,
  72. "+OBM_REDUCE"  , OBM_REDUCE  ,
  73. "+OBM_ZOOM"    , OBM_ZOOM    ,
  74. "+OBM_RESTORE" , OBM_RESTORE ,
  75. "+OBM_REDUCED" , OBM_REDUCED ,
  76. "+OBM_ZOOMD"   , OBM_ZOOMD   ,
  77. "+OBM_RESTORED", OBM_RESTORED,
  78. "+OBM_UPARROWD", OBM_UPARROWD,
  79. "+OBM_DNARROWD", OBM_DNARROWD,
  80. "+OBM_RGARROWD", OBM_RGARROWD,
  81. "+OBM_LFARROWD", OBM_LFARROWD,
  82. "+OBM_MNARROW" , OBM_MNARROW ,
  83. "+OBM_COMBO"   , OBM_COMBO   ,
  84. "+OBM_UPARROWI", OBM_UPARROWI,
  85. "+OBM_DNARROWI", OBM_DNARROWI,
  86. 0, 0
  87. };
  88.  
  89. #endif
  90.  
  91. #if defined (__MS_WINDOWS__)
  92. UI_Bitmap::UI_Bitmap (const char* name)
  93. {
  94.     if (name && name[0] == '+') {
  95.         for (short i = 0; BuiltIn[i].name != 0; i++) {
  96.             if (CL_String (name) == BuiltIn[i].name)
  97.                 break;
  98.         }
  99.         _handle = LoadBitmap (NULL, (LPSTR) BuiltIn[i].code);
  100.     }
  101.     else
  102.         _handle = LoadBitmap (NULL, (LPSTR) name);
  103.     if (_handle) {
  104.         BITMAP  bm;
  105.         GetObject (_handle, sizeof (BITMAP), (LPSTR) &bm);
  106.         _bitsPerPixel = bm.bmBitsPixel;
  107.         _width        = bm.bmWidth;
  108.         _height       = bm.bmHeight;
  109.     }
  110.     else
  111.         _bitsPerPixel = _width = _height = 0;
  112. }
  113.  
  114. #endif
  115.  
  116.  
  117.  
  118. UI_Bitmap::~UI_Bitmap ()
  119. {
  120. #if defined (__MS_WINDOWS__)
  121.     if (_handle)
  122.         DeleteObject (_handle);
  123. #elif defined(__OS2__)
  124.     _handle = 0;
  125. #elif defined (__X_MOTIF__)
  126.     XDestroyImage (_handle);
  127. //     if (_handle) {
  128. //         Display *dpy = XtDisplay (_TheApplication->Controller().ShellWidget());
  129. //         XFreePixmap (dpy, _handle);
  130. //     }
  131. #endif
  132. }
  133.  
  134.  
  135.  
  136. // UI_Color& UI_Bitmap::operator[] (short i, short j)
  137. // {
  138. // }
  139.  
  140.  
  141.  
  142. bool UI_Bitmap::DrawOn (UI_DrawingSurface& s, const UI_Point& p) const
  143. {
  144.     s.DrawBitmap ((const UI_Bitmap&) *this, p);
  145.     return TRUE;
  146. }
  147.  
  148.  
  149.  
  150. void UI_Bitmap::CopyFrom (UI_DisplaySurface& s, const UI_Rectangle& r)
  151. {
  152. #if defined (__MS_WINDOWS__)
  153.     HDC srcdc = s.Handle();
  154.     if (!srcdc)
  155.         return;
  156.     HDC destdc = CreateCompatibleDC (srcdc);
  157.     if (!destdc)
  158.         return;
  159.     if (_handle)
  160.         DeleteObject (_handle);
  161.     long w = r.Width() ;
  162.     long h = r.Height();
  163.     _handle = CreateCompatibleBitmap (srcdc, w, h);
  164.     HBITMAP hOldBm = SelectObject (destdc, _handle);
  165.     SetMapMode (destdc, GetMapMode (srcdc));
  166.     BitBlt (destdc, 0, 0,  r.Width(), r.Height(), srcdc, r.Left(), r.Top(),
  167.             SRCCOPY);
  168.     _handle = SelectObject (destdc, hOldBm);
  169.     if (_handle) {
  170.         BITMAP  bm;
  171.         GetObject (_handle, sizeof (BITMAP), (LPSTR) &bm);
  172.         _bitsPerPixel = bm.bmBitsPixel;
  173.         _width        = bm.bmWidth;
  174.         _height       = bm.bmHeight;
  175.     }
  176.     else
  177.         _bitsPerPixel = _width = _height = 0;
  178.  
  179.     DeleteDC (destdc);
  180.  
  181. #elif defined(__OS2__)
  182.     HDC hdc = GpiQueryDevice ((HPS) s.Handle());
  183.     HAB hab = _TheApplication->Controller().AnchorBlockHandle();
  184.     HDC hdcMem = DevOpenDC (hab, OD_MEMORY, "*", 0L, NULL, hdc);
  185.     SIZEL sizel;
  186.     sizel.cx = sizel.cy = 0;
  187.     HPS hpsMem = GpiCreatePS (hab, hdcMem, &sizel, PU_ARBITRARY |
  188.                               GPIT_MICRO | GPIA_ASSOC);
  189.     long colorPlanes, colorBitCount;
  190.     DevQueryCaps (s.Handle(), CAPS_COLOR_PLANES, 1L, &colorPlanes);
  191.     DevQueryCaps (hdc, CAPS_COLOR_BITCOUNT, 1L, &colorBitCount);
  192.  
  193.     if (_handle != NULLHANDLE)
  194.         GpiDeleteBitmap (_handle);
  195.  
  196.     // Now create a bitmap.
  197.     BITMAPINFOHEADER bmp;
  198.     bmp.cbFix = sizeof(BITMAPINFOHEADER);
  199.     bmp.cx = (SHORT) r.Width();
  200.     bmp.cy = (SHORT) r.Height();
  201.     bmp.cPlanes   = (SHORT) colorPlanes;
  202.     bmp.cBitCount = (SHORT) colorBitCount;
  203.     _handle = GpiCreateBitmap (hpsMem, (PBITMAPINFOHEADER2) &bmp,
  204.                                0x0000, NULL, NULL);
  205.     GpiSetBitmap (hpsMem, _handle);
  206.  
  207.     GpiSetColor (hpsMem, CLR_TRUE);
  208.     GpiSetBackColor (hpsMem, CLR_FALSE);
  209.     GpiSetBackMix (hpsMem, BM_OVERPAINT);
  210.     
  211.     POINTL pt[4];
  212.     pt[0].x = pt[0].y = 0;
  213.     pt[1].x = r.Width()  - 1;
  214.     pt[1].y = r.Height() - 1;
  215.     pt[2].x = r.Left ();
  216.     pt[2].y = s.DrawingArea().Height() - r.Bottom() + 1;
  217.     pt[3].x = pt[2].x + r.Width()  - 1;
  218.     pt[3].y = pt[2].y + r.Height() - 1;
  219.     GpiBitBlt (hpsMem, (HPS) s.Handle(), 4, pt, ROP_SRCCOPY, BBO_AND);
  220.     _bitsPerPixel = colorPlanes * colorBitCount;
  221.     _width  = r.Width();
  222.     _height = r.Height();
  223.     GpiDestroyPS (hpsMem);
  224.     DevCloseDC   (hdcMem);
  225.     
  226. #elif defined (__X_MOTIF__)
  227.     Widget    w   = s.Client();
  228.     Display *dpy  = XtDisplay (w);
  229.     Window    win = XtWindow  (w);
  230.     _width  = r.Width();
  231.     _height = r.Height();
  232.     
  233. //     if (_handle)
  234. //         XFreePixmap (dpy, _handle);
  235. //    _handle = XCreatePixmap (dpy, DefaultRootWindow (dpy), _width, _height, 
  236. //                              DefaultDepthOfScreen (XtScreen (s.Client())));
  237.     
  238. //     XCopyArea (dpy, win, _handle, s.Handle(), r.Left(), r.Top(), _width,
  239. //                _height, 0, 0);
  240.     if (_handle)
  241.         XDestroyImage (_handle);
  242.     int format = DefaultDepth (dpy, DefaultScreen(dpy)) == 1 ? XYPixmap
  243.         : ZPixmap;
  244.     _handle = XGetImage (dpy, win, r.Left(), r.Top(), r.Width(), r.Height(),
  245.                          AllPlanes, format);
  246.     _bitsPerPixel = _handle->bits_per_pixel;
  247.     // Must fix the above to check for r being within client area
  248. #endif
  249. }
  250.  
  251.  
  252.  
  253.  
  254.  
  255.