home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / Fl_Bitmap.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  4.1 KB  |  130 lines

  1. //
  2. // "$Id: Fl_Bitmap.cxx,v 1.5 1999/01/07 19:17:16 mike Exp $"
  3. //
  4. // Bitmap drawing routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. #include <FL/Fl.H>
  27. #include <FL/x.H>
  28. #include <FL/fl_draw.H>
  29. #include <FL/Fl_Widget.H>
  30. #include <FL/Fl_Menu_Item.H>
  31. #include <FL/Fl_Bitmap.H>
  32.  
  33. void Fl_Bitmap::draw(int XP, int YP, int WP, int HP, int cx, int cy) {
  34.   // account for current clip region (faster on Irix):
  35.   int X,Y,W,H; fl_clip_box(XP,YP,WP,HP,X,Y,W,H);
  36.   cx += X-XP; cy += Y-YP;
  37.   // clip the box down to the size of image, quit if empty:
  38.   if (cx < 0) {W += cx; X -= cx; cx = 0;}
  39.   if ((cx+W) > w) W = w-cx;
  40.   if (W <= 0) return;
  41.   if (cy < 0) {H += cy; Y -= cy; cy = 0;}
  42.   if ((cy+H) > h) H = h-cy;
  43.   if (H <= 0) return;
  44. #ifdef WIN32
  45.   if (!id) {
  46.     // we need to pad the lines out to words & swap the bits
  47.     // in each byte.
  48.     int w1 = (w+7)/8;
  49.     int w2 = ((w+15)/16)*2;
  50.     uchar* newarray = new uchar[w2*h];
  51.     const uchar* src = array;
  52.     uchar* dest = newarray;
  53.     static uchar reverse[16] =    /* Bit reversal lookup table */
  54.           { 0x00, 0x88, 0x44, 0xcc, 0x22, 0xaa, 0x66, 0xee,
  55.           0x11, 0x99, 0x55, 0xdd, 0x22, 0xbb, 0x77, 0xff };
  56.     for (int y=0; y < h; y++) {
  57.       for (int n = 0; n < w1; n++, src++)
  58.     *dest++ = (reverse[*src & 0x0f] & 0xf0) |
  59.               (reverse[(*src >> 4) & 0x0f] & 0x0f);
  60.       dest += w2-w1;
  61.     }
  62.     id = (ulong)CreateBitmap(w, h, 1, 1, newarray);
  63.     array = newarray; // keep the pointer so I can free it later
  64.   }
  65.   HDC tempdc = CreateCompatibleDC(fl_gc);
  66.   SelectObject(tempdc, (HGDIOBJ)id);
  67.   SelectObject(fl_gc, fl_brush());
  68.   // secret bitblt code found in old MSWindows reference manual:
  69.   BitBlt(fl_gc, X, Y, W, H, tempdc, cx, cy, 0xE20746L);
  70.   DeleteDC(tempdc);
  71. #else
  72.   if (!id) id = XCreateBitmapFromData(fl_display, fl_window,
  73.                       (const char*)array, (w+7)&-8, h);
  74.   XSetStipple(fl_display, fl_gc, id);
  75.   int ox = X-cx; if (ox < 0) ox += w;
  76.   int oy = Y-cy; if (oy < 0) oy += h;
  77.   XSetTSOrigin(fl_display, fl_gc, ox, oy);
  78.   XSetFillStyle(fl_display, fl_gc, FillStippled);
  79.   XFillRectangle(fl_display, fl_window, fl_gc, X, Y, W, H);
  80.   XSetFillStyle(fl_display, fl_gc, FillSolid);
  81. #endif
  82. }
  83.  
  84. Fl_Bitmap::~Fl_Bitmap() {
  85. #ifdef WIN32
  86.   if (id) {
  87.     DeleteObject((HGDIOBJ)id);
  88.     delete[] (uchar*)array;
  89.   }
  90. #else
  91.   if (id) fl_delete_offscreen((Fl_Offscreen)id);
  92. #endif
  93. }
  94.  
  95. static void bitmap_labeltype(
  96.     const Fl_Label* o, int x, int y, int w, int h, Fl_Align a)
  97. {
  98.   Fl_Bitmap* b = (Fl_Bitmap*)(o->value);
  99.   int cx;
  100.   if (a & FL_ALIGN_LEFT) cx = 0;
  101.   else if (a & FL_ALIGN_RIGHT) cx = b->w-w;
  102.   else cx = (b->w-w)/2;
  103.   int cy;
  104.   if (a & FL_ALIGN_TOP) cy = 0;
  105.   else if (a & FL_ALIGN_BOTTOM) cy = b->h-h;
  106.   else cy = (b->h-h)/2;
  107.   fl_color((Fl_Color)o->color);
  108.   b->draw(x,y,w,h,cx,cy);
  109. }
  110.  
  111. static void bitmap_measure(const Fl_Label* o, int& w, int& h) {
  112.   Fl_Bitmap* b = (Fl_Bitmap*)(o->value);
  113.   w = b->w;
  114.   h = b->h;
  115. }
  116.  
  117. void Fl_Bitmap::label(Fl_Widget* o) {
  118.   Fl::set_labeltype(_FL_BITMAP_LABEL, bitmap_labeltype, bitmap_measure);
  119.   o->label(_FL_BITMAP_LABEL, (const char*)this);
  120. }
  121.  
  122. void Fl_Bitmap::label(Fl_Menu_Item* o) {
  123.   Fl::set_labeltype(_FL_BITMAP_LABEL, bitmap_labeltype, bitmap_measure);
  124.   o->label(_FL_BITMAP_LABEL, (const char*)this);
  125. }
  126.  
  127. //
  128. // End of "$Id: Fl_Bitmap.cxx,v 1.5 1999/01/07 19:17:16 mike Exp $".
  129. //
  130.