Back to index

texmacs  1.0.7.15
color_picker_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : color_picker_widget.cpp
00004 * DESCRIPTION: Widget for selecting colors or images
00005 * COPYRIGHT  : (C) 2010  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "Widkit/basic_widget.hpp"
00013 #include "Widkit/attribute_widget.hpp"
00014 #include "Widkit/layout.hpp"
00015 #include "file.hpp"
00016 #include "scheme.hpp"
00017 
00018 /******************************************************************************
00019 * Color button widget
00020 ******************************************************************************/
00021 
00022 wk_widget
00023 color_widget (tree col) {
00024   return glue_wk_widget (col, false, false, 32*PIXEL, 24*PIXEL);
00025 }
00026 
00027 class color_command_rep: public command_rep {
00028   command cmd;
00029   tree col;
00030 public:
00031   color_command_rep (command cmd2, tree col2): cmd (cmd2), col (col2) {}
00032   void apply () { cmd (list_object (object (col))); }
00033   tm_ostream& print (tm_ostream& out) {
00034     return out << "Apply (" << cmd << ", " << col << ")"; }
00035 };
00036 
00037 command
00038 color_command (command cmd, tree col) {
00039   return tm_new<color_command_rep> (cmd, col);
00040 }
00041 
00042 wk_widget
00043 color_button_widget (tree col, command cmd) {
00044   return command_button (color_widget (col),
00045                       color_command (cmd, col),
00046                       WIDGET_STYLE_BUTTON);
00047 }
00048 
00049 /******************************************************************************
00050 * Direct color picker
00051 ******************************************************************************/
00052 
00053 wk_widget
00054 tile_color_picker (array<tree> cols, command cmd, int columns) {
00055   int i, n= N(cols);
00056   array<wk_widget> ws (n);
00057   for (i=0; i<n; i++)
00058     ws[i]= color_button_widget (cols[i], cmd);
00059   return tile (ws, columns);
00060 }
00061 
00062 wk_widget
00063 direct_color_picker (command cmd, bool bg, array<tree> proposals) {
00064   (void) proposals;
00065   array<wk_widget> picker (bg? 5: 3);
00066 
00067   array<tree> cols1;
00068   cols1 << tree ("dark red") << tree ("dark magenta")
00069        << tree ("dark blue") << tree ("dark cyan")
00070        << tree ("dark green") << tree ("dark yellow")
00071        << tree ("dark orange") << tree ("dark brown")
00072        << tree ("red") << tree ("magenta")
00073        << tree ("blue") << tree ("cyan")
00074        << tree ("green") << tree ("yellow")
00075        << tree ("orange") << tree ("brown")
00076        << tree ("pastel red") << tree ("pastel magenta")
00077        << tree ("pastel blue") << tree ("pastel cyan")
00078        << tree ("pastel green") << tree ("pastel yellow")
00079        << tree ("pastel orange") << tree ("pastel brown");
00080   picker[0]= tile_color_picker (cols1, cmd, 8);
00081 
00082   picker[1]= glue_wk_widget (true, false, 0, 5*PIXEL);
00083 
00084   array<tree> cols2;
00085   cols2 << tree ("black") << tree ("darker grey")
00086        << tree ("dark grey") << tree ("light grey")
00087        << tree ("pastel grey")     << tree ("white");
00088   picker[2]= tile_color_picker (cols2, cmd, 8);
00089 
00090   if (bg) {
00091     picker[3]= glue_wk_widget (true, false, 0, 5*PIXEL);
00092 
00093     bool dir_problem;
00094     url pattern_dir= "$TEXMACS_PATH/misc/patterns";
00095     array<string> all= read_directory (pattern_dir, dir_problem);
00096     array<tree> cols3;
00097     for (int i=0; i<N(all); i++)
00098       if (ends (all[i], ".png")) {
00099        url image= resolve (relative (pattern_dir, all[i]));
00100        cols3 << tree (PATTERN, as_string (pattern_dir * all[i]), "", "");
00101       }
00102     picker[4]= tile_color_picker (cols3, cmd, 8);
00103   }
00104 
00105   return vertical_list (picker);
00106 }
00107 
00108 /******************************************************************************
00109 * exported routines
00110 ******************************************************************************/
00111 
00112 wk_widget
00113 color_picker_wk_widget (command cmd, bool bg, array<tree> proposals) {
00114   return direct_color_picker (cmd, bg, proposals);
00115 }