Use cairo to draw everything.
[ntk.git] / test / color_chooser.cxx
blob8d578e783f935ea90253cb0834f7c76286a9ff6b
1 //
2 // "$Id: color_chooser.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
4 // Color chooser test program for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2010 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.
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.
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.
23 // Please report all bugs and problems on the following page:
25 // http://www.fltk.org/str.php
28 #include <FL/Fl.H>
29 #include <FL/Fl_Window.H>
30 #include <FL/Fl_Box.H>
31 #include <FL/Fl_Button.H>
32 #include <FL/fl_show_colormap.H>
33 #include <FL/Fl_Color_Chooser.H>
34 #include <FL/Fl_Image.H>
35 #include <FL/x.H>
36 #include <FL/fl_draw.H>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #if !defined(WIN32) && !defined(__APPLE__)
41 #include "list_visuals.cxx"
42 #endif
44 int width = 75;
45 int height = 75;
46 uchar *image;
48 void make_image() {
49 image = new uchar[3*width*height];
50 uchar *p = image;
51 for (int y = 0; y < height; y++) {
52 double Y = double(y)/(height-1);
53 for (int x = 0; x < width; x++) {
54 double X = double(x)/(width-1);
55 *p++ = uchar(255*((1-X)*(1-Y))); // red in upper-left
56 *p++ = uchar(255*((1-X)*Y)); // green in lower-left
57 *p++ = uchar(255*(X*Y)); // blue in lower-right
62 class Pens : public Fl_Box {
63 void draw();
64 public:
65 Pens(int X, int Y, int W, int H, const char* L)
66 : Fl_Box(X,Y,W,H,L) {}
68 void Pens::draw() {
69 // use every color in the gray ramp:
70 for (int i = 0; i < 3*8; i++) {
71 fl_color((Fl_Color)(FL_GRAY_RAMP+i));
72 fl_line(x()+i, y(), x()+i, y()+h());
76 Fl_Color c = FL_GRAY;
77 #define fullcolor_cell (FL_FREE_COLOR)
79 void cb1(Fl_Widget *, void *v) {
80 c = fl_show_colormap(c);
81 Fl_Box* b = (Fl_Box*)v;
82 b->color(c);
83 b->parent()->redraw();
86 void cb2(Fl_Widget *, void *v) {
87 uchar r,g,b;
88 Fl::get_color(c,r,g,b);
89 if (!fl_color_chooser("New color:",r,g,b,3)) return;
90 c = fullcolor_cell;
91 Fl::set_color(fullcolor_cell,r,g,b);
92 Fl_Box* bx = (Fl_Box*)v;
93 bx->color(fullcolor_cell);
94 bx->parent()->redraw();
97 int main(int argc, char ** argv) {
98 Fl::set_color(fullcolor_cell,145,159,170);
99 Fl_Window window(400,400);
100 Fl_Box box(50,50,300,300);
101 box.box(FL_THIN_DOWN_BOX);
102 c = fullcolor_cell;
103 box.color(c);
104 Fl_Button b1(140,120,120,30,"fl_show_colormap");
105 b1.callback(cb1,&box);
106 Fl_Button b2(140,160,120,30,"fl_choose_color");
107 b2.callback(cb2,&box);
108 Fl_Box image_box(140,200,120,120,0);
109 make_image();
110 (new Fl_RGB_Image(image, width, height))->label(&image_box);
111 Fl_Box b(140,320,120,0,"Example of fl_draw_image()");
112 Pens p(80,200,3*8,120,"lines");
113 p.align(FL_ALIGN_TOP);
114 int i = 1;
115 if (!Fl::args(argc,argv,i) || i < argc-1) {
116 printf("usage: %s <switches> visual-number\n"
117 " - : default visual\n"
118 " r : call Fl::visual(FL_RGB)\n"
119 " c : call Fl::own_colormap()\n",argv[0]);
120 #if !defined(WIN32) && !defined(__APPLE__)
121 printf(" # : use this visual with an empty colormap:\n");
122 list_visuals();
123 #endif
124 puts(Fl::help);
125 exit(1);
127 if (i!=argc) {
128 if (argv[i][0] == 'r') {
129 if (!Fl::visual(FL_RGB)) printf("Fl::visual(FL_RGB) returned false.\n");
130 } else if (argv[i][0] == 'c') {
131 Fl::own_colormap();
132 } else if (argv[i][0] != '-') {
133 #if !defined(WIN32) && !defined(__APPLE__)
134 int visid = atoi(argv[i]);
135 fl_open_display();
136 XVisualInfo templt; int num;
137 templt.visualid = visid;
138 fl_visual = XGetVisualInfo(fl_display, VisualIDMask, &templt, &num);
139 if (!fl_visual) Fl::fatal("No visual with id %d",visid);
140 fl_colormap = XCreateColormap(fl_display, RootWindow(fl_display,fl_screen),
141 fl_visual->visual, AllocNone);
142 fl_xpixel(FL_BLACK); // make sure black is allocated
143 #else
144 Fl::fatal("Visual id's not supported on MSWindows or MacOS.");
145 #endif
148 window.show(argc,argv);
149 return Fl::run();
153 // End of "$Id: color_chooser.cxx 7903 2010-11-28 21:06:39Z matt $".