1 // ----------------------------------------------------------------------------
2 // colorbox.cxx
3 //
4 // Copyright (C) 2007-2008
5 //		Dave Freese, W1HKJ
6 //
7 // This file is part of fldigi.
8 //
9 // Fldigi is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // Fldigi is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with fldigi.  If not, see <http://www.gnu.org/licenses/>.
21 // ----------------------------------------------------------------------------
22 
23 #include <config.h>
24 
25 #include <string>
26 #include <FL/Fl_Color_Chooser.H>
27 
28 #include "gettext.h"
29 #include "colorbox.h"
30 #include "waterfall.h"
31 #include "confdialog.h"
32 #include "main.h"
33 #include "fl_digi.h"
34 #include "fileselect.h"
35 #include "debug.h"
36 
37 using namespace std;
38 
draw()39 void colorbox::draw() {
40 	int ypos = y() + 2;
41 	int xpos;
42 	int ht = h() - 4;
43 	int wd = w() - 4;
44 	draw_box();
45 	for(int i = 0; i < wd; i++){
46 		xpos = x() + 2 + i;
47 		int xc = i * 256 / wd;
48 		fl_rectf (xpos, ypos, 1, ht, mag2RGBI[xc].R, mag2RGBI[xc].G, mag2RGBI[xc].B);
49 	}
50 }
51 
setColorButtons()52 void setColorButtons()
53 {
54 	for (int i = 0; i < 9; i++) {
55 		btnColor[i]->color( fl_rgb_color( palette[i].R, palette[i].G, palette[i].B ) );
56 		btnColor[i]->redraw();
57 	}
58 }
59 
selectColor(int n)60 void selectColor(int n)
61 {
62 	uchar r, g, b;
63 	r = palette[n].R;
64 	g = palette[n].G;
65 	b = palette[n].B;
66 	fl_color_chooser("Spectrum", r, g, b);
67 
68 	palette[n].R = r;
69 	palette[n].G = g;
70 	palette[n].B = b;
71 
72 	btnColor[n]->color( fl_rgb_color( palette[n].R, palette[n].G, palette[n].B ) );
73 	btnColor[n]->redraw();
74 	wf->setcolors();
75 	WF_Palette->redraw();
76 }
77 
78 static string palfilename = "";
79 static string palLabelStr;
80 
loadPalette()81 void loadPalette()
82 {
83 	int r,g,b;
84 	FILE *clrfile = NULL;
85 	if (palfilename.size() == 0) {
86 		palfilename = PalettesDir;
87 		palfilename.append ("fldigi.pal");
88 	}
89     const char *p = FSEL::select(_("Open palette"), _("Fldigi palette\t*.pal"), palfilename.c_str());
90 	if (!p) return;
91 	if (!*p) return;
92 	if ((clrfile = fl_fopen(p, "r")) != NULL) {
93 		for (int i = 0; i < 9; i++) {
94 			if (fscanf(clrfile, "%d;%d;%d\n", &r, &g, &b) == EOF) {
95 				if (ferror(clrfile))
96 					LOG_PERROR("fscanf");
97 				else
98 					LOG_ERROR("unexpected EOF");
99 				fclose(clrfile);
100 				return;
101 			}
102 			palette[i].R = r;
103 			palette[i].G = g;
104 			palette[i].B = b;
105 		}
106    		fclose(clrfile);
107 		wf->setcolors();
108    		setColorButtons();
109 		palfilename = p;
110 		palLabelStr = p;
111 		size_t pos = palLabelStr.find_last_of('/');
112 		if (pos != string::npos) palLabelStr.erase(0, pos+1);
113 		palLabelStr = _("Palette: ") + palLabelStr;
114 		WF_Palette->label(palLabelStr.c_str());
115 		WF_Palette->redraw();
116 		progdefaults.PaletteName = palLabelStr;
117 	}
118 }
119 
savePalette()120 void savePalette()
121 {
122 	FILE *clrfile = NULL;
123 	if (palfilename.size() == 0) {
124 		palfilename = PalettesDir;
125 		palfilename.append ("fldigi.pal");
126 	}
127 	const char *p = FSEL::saveas(_("Save palette"), _("Fldigi palette\t*.pal"), palfilename.c_str());
128 	if (!p) return;
129 	if ((clrfile = fl_fopen(p, "w")) != NULL) {
130 		for (int i = 0; i < 9; i++) {
131 			fprintf(clrfile, "%3d;%3d;%3d\n", palette[i].R, palette[i].G, palette[i].B );
132 		}
133    		fclose(clrfile);
134 		palfilename = p;
135 		palLabelStr = p;
136 		size_t pos = palLabelStr.find_last_of('/');
137 		if (pos != string::npos) palLabelStr.erase(0, pos+1);
138 		palLabelStr = _("Palette: ") + palLabelStr;
139 		WF_Palette->label(palLabelStr.c_str());
140 		WF_Palette->redraw();
141 		progdefaults.PaletteName = palLabelStr;
142    	}
143 }
144