Tweak themes for more color consistency.
[ntk.git] / src / Fl_Theme.cxx
blob13d2da22fd87106c047c5225ca00fce80ddd55c3
2 /*******************************************************************************/
3 /* Copyright (C) 2012 Jonathan Moore Liles */
4 /* */
5 /* This program is free software; you can redistribute it and/or modify it */
6 /* under the terms of the GNU General Public License as published by the */
7 /* Free Software Foundation; either version 2 of the License, or (at your */
8 /* option) any later version. */
9 /* */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */
13 /* more details. */
14 /* */
15 /* You should have received a copy of the GNU General Public License along */
16 /* with This program; see the file COPYING. If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /*******************************************************************************/
20 #include "FL/Fl_Theme.H"
21 #include <FL/Fl.H>
22 #include <FL/Fl_Window.H>
23 #include <FL/Fl_Preferences.H>
25 Fl_Theme *Fl_Theme::first;
26 Fl_Theme *Fl_Theme::_current;
27 Fl_Color_Scheme *Fl_Color_Scheme::first;
28 Fl_Color_Scheme *Fl_Color_Scheme::_current;
30 int Fl_Theme::total;
31 int Fl_Color_Scheme::total;
33 void
34 Fl_Theme::add ( Fl_Theme *t )
36 t->next = first;
37 first = t;
38 total++;
41 Fl_Theme **
42 Fl_Theme::get ( void )
44 Fl_Theme **r = (Fl_Theme**) malloc( sizeof( Fl_Theme* ) * ( total + 1 ) );
46 int i = 0;
47 for ( Fl_Theme *t = first; t; t = t->next, i++ )
48 r[i] = t;
50 r[i] = 0;
52 return r;
56 static
57 Fl_Preferences *prefs ( void )
59 char path[512];
61 snprintf( path, sizeof(path), "%s/.config/ntk/", getenv("HOME" ) );
63 Fl_Preferences *p = new Fl_Preferences( path, "ntk", "theme" );
65 return p;
68 static void conf_set ( const char *key, const char *value )
70 Fl_Preferences *p = prefs();
72 p->set( key, value );
74 delete p;
77 static void conf_set ( const char *key, Fl_Color value )
79 Fl_Preferences *p = prefs();
81 p->set( key, (int)value );
83 delete p;
86 static const char *conf_get ( const char *key, const char *def )
88 static char buf[256];
90 Fl_Preferences *p = prefs();
92 p->get( key, buf, def, sizeof( buf ) );
94 delete p;
96 return buf;
99 static
100 Fl_Color
101 conf_get_color ( const char *key, Fl_Color def )
103 Fl_Preferences *p = prefs();
105 int c;
107 p->get( key, c, def );
109 delete p;
111 return (Fl_Color)c;
114 /* sets the configured default */
116 Fl_Theme::load_default ( void )
118 const char *name = conf_get( "theme", "cairo" );
120 int rv = set( name );
122 Fl_Color_Scheme::set( "Dark" );
124 uchar r, g, b;
126 Fl::get_color( conf_get_color( "background", FL_BACKGROUND_COLOR ), r, g, b );
127 Fl::background( r, g, b );
128 Fl::get_color( conf_get_color( "background2", FL_BACKGROUND2_COLOR ), r, g, b );
129 Fl::background2( r, g, b );
130 Fl::get_color( conf_get_color( "foreground", FL_FOREGROUND_COLOR ), r, g, b );
131 Fl::foreground( r, g, b );
133 return rv;
136 void
137 Fl_Theme::save ( void )
139 conf_set( "theme", Fl_Theme::_current->name() );
143 Fl_Theme::set ( const char *name )
145 for ( Fl_Theme *t = first; t; t = t->next )
146 if ( !strcasecmp( t->name(), name ) )
148 /* reset boxtypes */
149 Fl::reload_scheme();
151 t->_init_func();
152 Fl_Theme::_current = t;
154 refresh();
156 return 1;
159 return 0;
162 void
163 Fl_Theme::refresh ( void )
165 for ( Fl_Window *w = Fl::first_window(); w; w = Fl::next_window( w ) )
166 w->redraw();
169 void
170 Fl_Color_Scheme::add ( Fl_Color_Scheme *t )
172 t->next = first;
173 first = t;
174 total++;
177 Fl_Color_Scheme **
178 Fl_Color_Scheme::get ( void )
180 Fl_Color_Scheme **r = (Fl_Color_Scheme**) malloc( sizeof( Fl_Color_Scheme* ) * ( total + 1 ) );
182 int i = 0;
183 for ( Fl_Color_Scheme *t = first; t; t = t->next, i++ )
184 r[i] = t;
186 r[i] = 0;
188 return r;
191 void
192 Fl_Color_Scheme::save ( void )
194 conf_set( "color_scheme", Fl_Color_Scheme::_current->name() );
195 conf_set( "background", Fl::get_color( FL_BACKGROUND_COLOR ) );
196 conf_set( "foreground", Fl::get_color( FL_FOREGROUND_COLOR ) );
197 conf_set( "background2", Fl::get_color( FL_BACKGROUND2_COLOR ) );
200 void
201 Fl_Color_Scheme::refresh ( void )
203 for ( Fl_Window *w = Fl::first_window(); w; w = Fl::next_window( w ) )
204 w->redraw();
208 Fl_Color_Scheme::set ( const char *name )
210 for ( Fl_Color_Scheme *t = first; t; t = t->next )
211 if ( !strcasecmp( t->name(), name ) )
213 uchar r, g, b;
215 Fl::get_color( t->_bg, r, g, b );
216 Fl::background( r, g, b );
217 Fl::get_color( t->_bg2, r, g, b );
218 Fl::background2( r, g, b );
219 Fl::get_color( t->_fg, r, g, b );
220 Fl::foreground( r, g, b );
221 /* Fl::get_color( t->_sel, r, g, b ); */
222 /* Fl::selection( r, g, b ); */
224 Fl_Color_Scheme::_current = t;
226 refresh();
228 return 1;
231 return 0;