fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

fl_show_colormap.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: fl_show_colormap.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 // Colormap color selection dialog for the Fast Light Tool Kit (FLTK).
00005 //
00006 // Copyright 1998-2010 by Bill Spitzak and others.
00007 //
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU Library General Public
00010 // License as published by the Free Software Foundation; either
00011 // version 2 of the License, or (at your option) any later version.
00012 //
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 // Library General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU Library General Public
00019 // License along with this library; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021 // USA.
00022 //
00023 // Please report all bugs and problems on the following page:
00024 //
00025 //     http://www.fltk.org/str.php
00026 //
00027 
00028 #include <FL/Fl.H>
00029 #include <FL/Fl_Single_Window.H>
00030 #include <FL/fl_draw.H>
00031 #include <FL/fl_show_colormap.H>
00032 #include <config.h>
00033 
00034 #define BOXSIZE 14
00035 #define BORDER 4
00036 
00041 class ColorMenu : public Fl_Window {
00042   Fl_Color initial;
00043   Fl_Color which, previous;
00044   int done;
00045   void drawbox(Fl_Color);
00046   void draw();
00047   int handle(int);
00048 public:
00049   ColorMenu(Fl_Color oldcol);
00050   Fl_Color run();
00051 };
00052 
00053 ColorMenu::ColorMenu(Fl_Color oldcol) :
00054   Fl_Window(BOXSIZE*8+1+2*BORDER, BOXSIZE*32+1+2*BORDER) {
00055   clear_border();
00056   set_modal();
00057   initial = which = oldcol;
00058 }
00059 
00060 void ColorMenu::drawbox(Fl_Color c) {
00061   if (c < 0 || c > 255) return;
00062   int X = (c%8)*BOXSIZE+BORDER;
00063   int Y = (c/8)*BOXSIZE+BORDER;
00064 #if BORDER_WIDTH < 3
00065   if (c == which) fl_draw_box(FL_DOWN_BOX, X+1, Y+1, BOXSIZE-1, BOXSIZE-1, c);
00066   else fl_draw_box(FL_BORDER_BOX, X, Y, BOXSIZE+1, BOXSIZE+1, c);
00067 #else
00068   fl_draw_box(c == which ? FL_DOWN_BOX : FL_BORDER_BOX,
00069               X, Y, BOXSIZE+1, BOXSIZE+1, c);
00070 #endif
00071 }
00072 
00073 void ColorMenu::draw() {
00074   if (damage() != FL_DAMAGE_CHILD) {
00075     fl_draw_box(FL_UP_BOX,0,0,w(),h(),color());
00076     for (int c = 0; c < 256; c++) drawbox((Fl_Color)c);
00077   } else {
00078     drawbox(previous);
00079     drawbox(which);
00080   }
00081   previous = which;
00082 }
00083 
00084 int ColorMenu::handle(int e) {
00085   Fl_Color c = which;
00086   switch (e) {
00087   case FL_PUSH:
00088   case FL_DRAG: {
00089     int X = (Fl::event_x_root() - x() - BORDER);
00090     if (X >= 0) X = X/BOXSIZE;
00091     int Y = (Fl::event_y_root() - y() - BORDER);
00092     if (Y >= 0) Y = Y/BOXSIZE;
00093     if (X >= 0 && X < 8 && Y >= 0 && Y < 32)
00094       c = 8*Y + X;
00095     else
00096       c = initial;
00097     } break;
00098   case FL_RELEASE:
00099     done = 1;
00100     return 1;
00101   case FL_KEYBOARD:
00102     switch (Fl::event_key()) {
00103     case FL_Up: if (c > 7) c -= 8; break;
00104     case FL_Down: if (c < 256-8) c += 8; break;
00105     case FL_Left: if (c > 0) c--; break;
00106     case FL_Right: if (c < 255) c++; break;
00107     case FL_Escape: which = initial; done = 1; return 1;
00108     case FL_KP_Enter:
00109     case FL_Enter: done = 1; return 1;
00110     default: return 0;
00111     }
00112     break;
00113   default:
00114     return 0;
00115   }
00116   if (c != which) {
00117     which = (Fl_Color)c; damage(FL_DAMAGE_CHILD);
00118     int bx = (c%8)*BOXSIZE+BORDER;
00119     int by = (c/8)*BOXSIZE+BORDER;
00120     int px = x();
00121     int py = y();
00122     int scr_x, scr_y, scr_w, scr_h;
00123     Fl::screen_xywh(scr_x, scr_y, scr_w, scr_h);
00124     if (px < scr_x) px = scr_x;
00125     if (px+bx+BOXSIZE+BORDER >= scr_x+scr_w) px = scr_x+scr_w-bx-BOXSIZE-BORDER;
00126     if (py < scr_y) py = scr_y;
00127     if (py+by+BOXSIZE+BORDER >= scr_y+scr_h) py = scr_y+scr_h-by-BOXSIZE-BORDER;
00128     if (px+bx < BORDER) px = BORDER-bx;
00129     if (py+by < BORDER) py = BORDER-by;
00130     position(px,py);
00131   }
00132   return 1;
00133 }
00134 
00135 extern char fl_override_redirect; // hack for menus
00136 
00137 #ifdef _MSC_VER
00138 #pragma optimize("a",off) // needed to get the done check to work
00139 #endif
00140 Fl_Color ColorMenu::run() {
00141   if (which < 0 || which > 255) {
00142     position(Fl::event_x_root()-w()/2, Fl::event_y_root()-y()/2);
00143   } else {
00144     position(Fl::event_x_root()-(initial%8)*BOXSIZE-BOXSIZE/2-BORDER,
00145              Fl::event_y_root()-(initial/8)*BOXSIZE-BOXSIZE/2-BORDER);
00146   }
00147   show();
00148   Fl::grab(*this);
00149   done = 0;
00150   while (!done) Fl::wait();
00151   Fl::grab(0);
00152   return which;
00153 }
00154 
00155 Fl_Color fl_show_colormap(Fl_Color oldcol) {
00156   ColorMenu m(oldcol);
00157   return m.run();
00158 }
00159 
00160 //
00161 // End of "$Id: fl_show_colormap.cxx 7903 2010-11-28 21:06:39Z matt $".
00162 //