Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

htpal.cc

Go to the documentation of this file.
00001 /*
00002  *      HT Editor
00003  *      htpal.cc
00004  *
00005  *      Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
00006  *
00007  *      This program is free software; you can redistribute it and/or modify
00008  *      it under the terms of the GNU General Public License version 2 as
00009  *      published by the Free Software Foundation.
00010  *
00011  *      This program is distributed in the hope that it will be useful,
00012  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *      GNU General Public License for more details.
00015  *
00016  *      You should have received a copy of the GNU General Public License
00017  *      along with this program; if not, write to the Free Software
00018  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "global.h"
00022 #include "htatom.h"
00023 #include "htcurses.h"
00024 #include "htctrl.h"
00025 #include "htdialog.h"
00026 #include "htpal.h"
00027 #include "htreg.h"
00028 #include "htstring.h"
00029 #include "snprintf.h"
00030 #include "tools.h"
00031 
00032 // include indices
00033 #include "httag.h"
00034 #include "syntax.h"
00035 #include "out_ht.h"
00036 //
00037 
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 #define ATOM_PALETTE_ENTRY                      MAGICD("PAL\x00")
00042 #define ATOM_HT_CREATE_PALETTE_ENTRY    MAGICD("PAL\x10")
00043 
00044 struct pal_layout {
00045         int idx;
00046         char *name;
00047 };
00048 
00049 /* palette layout: tags */
00050 pal_layout pal_layout_tags[]={
00051 {palidx_tags_edit_tag_cursor_select,    "edit-tag cursor select"},
00052 {palidx_tags_edit_tag_cursor_edit,              "edit-tag cursor edit"},
00053 {palidx_tags_edit_tag_cursor_unfocused, "edit-tag cursor unfocused"},
00054 {palidx_tags_edit_tag_selected,         "edit-tag selected"},
00055 {palidx_tags_edit_tag_modified,         "edit-tag modified"},
00056 {palidx_tags_edit_tag,                          "edit-tag"},
00057 {palidx_tags_sel_tag_cursor_focused,    "sel-tag cursor focused"},
00058 {palidx_tags_sel_tag_cursor_unfocused,  "sel-tag cursor unfocused"},
00059 {palidx_tags_sel_tag,                           "sel-tag"},
00060 {0, NULL}
00061 };
00062 
00063 /* palette layout: generic */
00064 pal_layout pal_layout_generic[] = {
00065 {palidx_generic_body,                           "body"},
00066 
00067 {palidx_generic_text_focused,                   "text focused"},
00068 {palidx_generic_text_unfocused,         "text unfocused"},
00069 {palidx_generic_text_shortcut,          "text shortcut"},
00070 {palidx_generic_text_shortcut_selected, "text shortcut selected"},
00071 {palidx_generic_text_selected,          "text selected"},
00072 {palidx_generic_text_disabled,          "text disabled"},
00073 
00074 {palidx_generic_frame_focused,          "frame focused"},
00075 {palidx_generic_frame_unfocused,                "frame unfocused"},
00076 {palidx_generic_frame_move_resize,              "frame move-resize"},
00077 {palidx_generic_frame_killer,                   "frame killer"},
00078 
00079 {palidx_generic_scrollbar,                      "scrollbar"},
00080 
00081 {palidx_generic_input_focused,          "input focused"},
00082 {palidx_generic_input_unfocused,                "input unfocused"},
00083 {palidx_generic_input_selected,         "input selected"},
00084 {palidx_generic_input_clip,                     "input clip-chars"},
00085 
00086 {palidx_generic_button_focused,         "button focused"},
00087 {palidx_generic_button_unfocused,               "button unfocused"},
00088 {palidx_generic_button_shadow,          "button shadow"},
00089 {palidx_generic_button_shortcut,                "button shortcut"},
00090 
00091 {palidx_generic_list_focused_selected,  "list focused & selected"},
00092 {palidx_generic_list_focused_unselected, "list focused & unselected"},
00093 {palidx_generic_list_unfocused_selected, "list unfocused & selected"},
00094 {palidx_generic_list_unfocused_unselected, "list unfocused & unselected"},
00095 
00096 {palidx_generic_cluster_focused,                "cluster focused"},
00097 {palidx_generic_cluster_unfocused,              "cluster unfocused"},
00098 {palidx_generic_cluster_shortcut,               "cluster shortcut"},
00099 
00100 {0, NULL}
00101 };
00102 
00103 /* palette layout: syntax */
00104 pal_layout pal_layout_syntax[] = {
00105 {palidx_syntax_whitespace,                      "whitespace"},
00106 {palidx_syntax_comment,                         "comment"},
00107 {palidx_syntax_identifier,                      "identifier"},
00108 {palidx_syntax_reserved,                                "reserved"},
00109 {palidx_syntax_intnum,                          "integer number"},
00110 {palidx_syntax_floatnum,                                "float number"},
00111 {palidx_syntax_string,                          "string"},
00112 {palidx_syntax_char,                            "character"},
00113 {palidx_syntax_symbol,                          "symbol"},
00114 {palidx_syntax_preprocess,                      "preprocess"},
00115 {palidx_syntax_meta,                            "meta"}
00116 };
00117 
00118 /* palette layout: analyser */
00119 pal_layout pal_layout_analyser[] = {
00120 {palidx_analyser_default,                       "default"},
00121 {palidx_analyser_comment,                       "comment"},
00122 {palidx_analyser_label,                         "label"},
00123 {palidx_analyser_number,                                "number"},
00124 {palidx_analyser_string,                                "string"},
00125 {palidx_analyser_symbol,                                "symbol-character"},
00126 };
00127 
00128 /* all layouts */
00129 
00130 struct pal_class {
00131         pal_layout *layout;
00132         char *name;
00133 };
00134 
00135 pal_class pal_layouts[] =
00136 {
00137 {pal_layout_generic,    "generic"},
00138 {pal_layout_tags,               "tags"},
00139 {pal_layout_syntax,             "syntax"},
00140 {pal_layout_analyser,   "analyser"},
00141 {NULL, NULL}
00142 };
00143 
00144 /*
00145  *   reg/pal management
00146  */
00147 
00148 vcp getcolorv(palette *pal, UINT index)
00149 {
00150         if ((index<pal->size) && (pal->data)) return pal->data[index];
00151         return VCP(VC_WHITE, VC_RED);
00152 }
00153 
00154 pal_layout *find_pal_layout(pal_class *layouts, char *pal_class, int *lsize)
00155 {
00156         pal_layout *pl = NULL;
00157         while (layouts->layout && layouts->name) {
00158                 if (strcmp(layouts->name, pal_class) == 0) {
00159                         pl = layouts->layout;
00160                         break;
00161                 }
00162                 layouts++;
00163         }
00164         int s = 0;
00165         if (pl) {
00166                 pal_layout *p = pl;
00167                 while (p->name) {
00168                         p++;
00169                         s++;
00170                 }
00171         }
00172         if (pl) *lsize = s;
00173         return pl;
00174 }
00175 
00176 int find_pal_entry_idx(pal_layout *layout, const char *name)
00177 {
00178         while (layout->name) {
00179                 if (strcmp(layout->name, name) == 0) return layout->idx;
00180                 layout++;
00181         }
00182         return -1;
00183 }
00184 
00185 bool load_pal(char *pal_class, char *pal_flavour, palette *p)
00186 {
00187         if ((!pal_flavour) || (!pal_class)) return false;
00188         char dir[256];          /* secure */
00189         ht_snprintf(dir, sizeof dir, "%s/%s/%s", palettekey, pal_class, pal_flavour);
00190 
00191         int psize = 0;
00192         pal_layout *pl = find_pal_layout(pal_layouts, pal_class, &psize);
00193         if (!pl) return false;
00194         p->size = psize;
00195         p->data = (vcp*)malloc(sizeof *p->data * psize);
00196 
00197         for (int i=0; i < psize; i++) p->data[i] = VCP(VC_WHITE, VC_RED);
00198 
00199         ht_registry_node_type t;
00200         palette_entry *d;
00201         const char *n = NULL;
00202         ht_registry_node_type rnt_pal = registry->lookup_node_type(rnt_palette_name);
00203         while ((n = registry->enum_next((ht_registry_data **)&d, &t, dir, n))) {
00204                 if (t == rnt_pal) {
00205                         int idx = find_pal_entry_idx(pl, n);
00206                         if ((idx != -1) && (idx < psize)) {
00207                                 p->data[idx] = d->color;
00208                         }
00209                 }
00210         }
00211         return true;
00212 }
00213 
00214 /*
00215  *      CLASS palette_entry
00216  */
00217  
00218 palette_entry::palette_entry(UINT _idx, vcp _color)
00219 {
00220         idx=_idx;
00221         color=_color;
00222 }
00223 
00224 bool palette_entry::editdialog(const char *keyname)
00225 {
00226         bool r=0;
00227         bounds b;
00228         b.w=50;
00229         b.h=15;
00230         b.x=(screen->size.w-b.w)/2;
00231         b.y=(screen->size.h-b.h)/2;
00232         
00233         ht_dialog *d=new ht_dialog();
00234         d->init(&b, "edit palette entry", FS_TITLE | FS_KILLER);
00235         
00236         ht_color_block *fgc, *bgc;
00237         ht_label *l1, *l2;
00238         
00239         BOUNDS_ASSIGN(b, 2, 1, 16, 5);
00240         fgc=new ht_color_block();
00241         fgc->init(&b, VCP_FOREGROUND(color), cf_transparent | cf_light);
00242         d->insert(fgc);
00243 
00244         BOUNDS_ASSIGN(b, 2, 0, 16, 1);
00245         l1 = new ht_label();
00246         l1->init(&b, "~foreground", fgc);
00247         d->insert(l1);
00248         
00249         BOUNDS_ASSIGN(b, 20, 1, 16, 5);
00250         bgc=new ht_color_block();
00251         bgc->init(&b, VCP_BACKGROUND(color), cf_transparent | cf_light);
00252         d->insert(bgc);
00253         
00254         BOUNDS_ASSIGN(b, 20, 0, 16, 1);
00255         l2 = new ht_label();
00256         l2->init(&b, "~background", bgc);
00257         d->insert(l2);
00258 
00259         if (d->run(false)) {
00260                 ht_color_block_data fgd, bgd;
00261                 fgc->databuf_get(&fgd, sizeof fgd);
00262                 bgc->databuf_get(&bgd, sizeof bgd);
00263                 color=VCP(fgd.color, bgd.color);
00264                 r=1;
00265         }
00266         
00267         d->done();
00268         delete d;
00269         return r;
00270 }
00271 
00272 int palette_entry::load(ht_object_stream *f)
00273 {
00274         idx=f->getIntHex(4, 0);
00275         color=f->getIntHex(4, 0);
00276         return f->get_error();
00277 }
00278 
00279 OBJECT_ID palette_entry::object_id() const
00280 {
00281         return ATOM_PALETTE_ENTRY;
00282 }
00283 
00284 void palette_entry::store(ht_object_stream *f)
00285 {
00286         f->putIntHex(idx, 4, 0);
00287         f->putIntHex(color, 4, 0);
00288 }
00289 
00290 void palette_entry::strvalue(char *buf32bytes)
00291 {
00292         char *p=buf32bytes;
00293         char *text;
00294         int fg=VCP_FOREGROUND(color);
00295         int bg=VCP_BACKGROUND(color);
00296         if ((fg==VC_TRANSPARENT) && (bg==VC_TRANSPARENT)) {
00297                 text="transparent";
00298                 fg=VC_WHITE;
00299                 bg=VC_BLACK;
00300         } else if (fg==VC_TRANSPARENT) {
00301                 text="fgtrans";
00302                 fg=VC_WHITE;
00303                 if (bg==fg) fg=VC_BLACK;
00304         } else if (bg==VC_TRANSPARENT) {
00305                 text="bgtrans";
00306                 bg=VC_BLACK;
00307                 if (bg==fg) fg=VC_WHITE;
00308         } else {
00309                 text="normal";
00310         }
00311         p=tag_make_color(p, VCP(fg, bg));
00312         p+=sprintf(p, text);
00313         p=tag_make_default_color(p);
00314         *p=0;
00315 }
00316 
00317 ht_registry_data *create_empty_palette_entry()
00318 {
00319         return new palette_entry();
00320 }
00321 
00322 /*
00323  *      INIT
00324  */
00325 
00326 BUILDER(ATOM_PALETTE_ENTRY, palette_entry);
00327 
00328 bool init_pal()
00329 {
00330         REGISTER(ATOM_PALETTE_ENTRY, palette_entry);
00331         register_atom(ATOM_HT_CREATE_PALETTE_ENTRY, (void*)create_empty_palette_entry);
00332 
00333         return true;
00334 }
00335 
00336 /*
00337  *      DONE
00338  */
00339 
00340 void done_pal()
00341 {
00342         UNREGISTER(ATOM_PALETTE_ENTRY, palette_entry);
00343         unregister_atom(ATOM_HT_CREATE_PALETTE_ENTRY);
00344 }
00345 

Generated on Fri May 7 21:15:36 2004 by doxygen 1.3.5