xref: /haiku/src/preferences/appearance/APRView.cpp (revision 683cbefe9ec156fe9587b9a64a5e1b666a21654d)
1 //------------------------------------------------------------------------------
2 //	Copyright (c) 2001-2002, OpenBeOS
3 //
4 //	Permission is hereby granted, free of charge, to any person obtaining a
5 //	copy of this software and associated documentation files (the "Software"),
6 //	to deal in the Software without restriction, including without limitation
7 //	the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 //	and/or sell copies of the Software, and to permit persons to whom the
9 //	Software is furnished to do so, subject to the following conditions:
10 //
11 //	The above copyright notice and this permission notice shall be included in
12 //	all copies or substantial portions of the Software.
13 //
14 //	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 //	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 //	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 //	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 //	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 //	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 //	DEALINGS IN THE SOFTWARE.
21 //
22 //	File Name:		APRView.cpp
23 //	Author:			DarkWyrm <bpmagic@columbus.rr.com>
24 //	Description:	color handler for the app
25 //
26 //	Handles all the grunt work. Color settings are stored in a BMessage to
27 //	allow for easy transition to and from disk. The messy details are in the
28 //	hard-coded strings to translate the enumerated color_which defines to
29 //	strings and such, but even those should be easy to maintain.
30 //------------------------------------------------------------------------------
31 #include <OS.h>
32 #include <Directory.h>
33 #include <Alert.h>
34 #include <storage/Path.h>
35 #include <Entry.h>
36 #include <File.h>
37 #include <stdio.h>
38 
39 #include <PortLink.h>
40 #include <InterfaceDefs.h>
41 #include <ServerProtocol.h>
42 
43 #include "APRView.h"
44 #include "defs.h"
45 #include "ColorWell.h"
46 #include <ColorUtils.h>
47 #include "ColorWhichItem.h"
48 #include "ColorSet.h"
49 
50 #include "ServerConfig.h"
51 
52 //#define DEBUG_COLORSET
53 
54 #ifdef DEBUG_COLORSET
55 #define STRACE(a) printf a
56 #else
57 #define STRACE(A) /* nothing */
58 #endif
59 
60 #define COLOR_DROPPED 'cldp'
61 
62 // Declarations for private functions in InterfaceDefs.cpp
63 namespace BPrivate
64 {
65 	void get_system_colors(ColorSet *colors);
66 	void set_system_colors(const ColorSet &colors);
67 }
68 
69 APRView::APRView(const BRect &frame, const char *name, int32 resize, int32 flags)
70 	:BView(frame,name,resize,flags)
71 {
72 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
73 
74 	// Set up list of color attributes
75 	BRect rect(10,10,200,150);
76 	attrlist=new BListView(rect,"AttributeList");
77 
78 	scrollview=new BScrollView("ScrollView",attrlist, B_FOLLOW_LEFT |
79 		B_FOLLOW_TOP, 0, false, true);
80 	AddChild(scrollview);
81 	scrollview->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
82 
83 	attrlist->SetSelectionMessage(new BMessage(ATTRIBUTE_CHOSEN));
84 
85 	attrlist->AddItem(new ColorWhichItem(B_PANEL_BACKGROUND_COLOR));
86 	attrlist->AddItem(new ColorWhichItem((color_which)B_PANEL_TEXT_COLOR));
87 	attrlist->AddItem(new ColorWhichItem((color_which)B_DOCUMENT_BACKGROUND_COLOR));
88 	attrlist->AddItem(new ColorWhichItem((color_which)B_DOCUMENT_TEXT_COLOR));
89 	attrlist->AddItem(new ColorWhichItem((color_which)B_CONTROL_BACKGROUND_COLOR));
90 	attrlist->AddItem(new ColorWhichItem((color_which)B_CONTROL_TEXT_COLOR));
91 	attrlist->AddItem(new ColorWhichItem((color_which)B_CONTROL_BORDER_COLOR));
92 	attrlist->AddItem(new ColorWhichItem((color_which)B_CONTROL_HIGHLIGHT_COLOR));
93 	attrlist->AddItem(new ColorWhichItem((color_which)B_NAVIGATION_BASE_COLOR));
94 	attrlist->AddItem(new ColorWhichItem((color_which)B_NAVIGATION_PULSE_COLOR));
95 	attrlist->AddItem(new ColorWhichItem((color_which)B_SHINE_COLOR));
96 	attrlist->AddItem(new ColorWhichItem((color_which)B_SHADOW_COLOR));
97 	attrlist->AddItem(new ColorWhichItem(B_MENU_BACKGROUND_COLOR));
98 	attrlist->AddItem(new ColorWhichItem((color_which)B_MENU_SELECTED_BACKGROUND_COLOR));
99 	attrlist->AddItem(new ColorWhichItem(B_MENU_ITEM_TEXT_COLOR));
100 	attrlist->AddItem(new ColorWhichItem(B_MENU_SELECTED_ITEM_TEXT_COLOR));
101 	attrlist->AddItem(new ColorWhichItem((color_which)B_MENU_SELECTED_BORDER_COLOR));
102 	attrlist->AddItem(new ColorWhichItem((color_which)B_TOOLTIP_BACKGROUND_COLOR));
103 
104 	attrlist->AddItem(new ColorWhichItem((color_which)B_SUCCESS_COLOR));
105 	attrlist->AddItem(new ColorWhichItem((color_which)B_FAILURE_COLOR));
106 	attrlist->AddItem(new ColorWhichItem(B_WINDOW_TAB_COLOR));
107 	attrlist->AddItem(new ColorWhichItem((color_which)B_WINDOW_TAB_TEXT_COLOR));
108 	attrlist->AddItem(new ColorWhichItem((color_which)B_INACTIVE_WINDOW_TAB_COLOR));
109 	attrlist->AddItem(new ColorWhichItem((color_which)B_INACTIVE_WINDOW_TAB_TEXT_COLOR));
110 
111 
112 	BRect wellrect(0,0,50,50);
113 	wellrect.OffsetTo( rect.right + 42, rect.top +
114 					(scrollview->Bounds().Height() - wellrect.Height())/2 );
115 
116 	colorwell=new ColorWell(wellrect,new BMessage(COLOR_DROPPED),true);
117 	AddChild(colorwell);
118 
119 	// Center the list and color well
120 
121 	rect = scrollview->Frame();
122 	rect.right = wellrect.right;
123 	rect.OffsetTo( (Bounds().Width()-rect.Width())/2,rect.top);
124 
125 	scrollview->MoveTo(rect.left, rect.top);
126 	colorwell->MoveTo(rect.right - colorwell->Bounds().Width(), colorwell->Frame().top);
127 
128 	picker=new BColorControl(BPoint(10,scrollview->Frame().bottom+20),B_CELLS_32x8,5.0,"Picker",
129 		new BMessage(UPDATE_COLOR));
130 	AddChild(picker);
131 
132 	picker->MoveTo( (Bounds().Width() - picker->Frame().Width())/2,
133 			scrollview->Frame().bottom + 20 );
134 
135 	BRect cvrect;
136 
137 	cvrect=picker->Frame();
138 	cvrect.right=cvrect.left+60;
139 	cvrect.bottom=cvrect.top+30;
140 
141 	// 40 is a 10 pixel space between buttons and other objects,
142 	// such as the edge or another button
143 	cvrect.OffsetTo( (Bounds().Width()- ((cvrect.Width()*3) + 40))/2,
144 					picker->Frame().bottom+10);
145 
146 	defaults=new BButton(cvrect,"DefaultsButton","Defaults",
147 						new BMessage(DEFAULT_SETTINGS),
148 						B_FOLLOW_LEFT |B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
149 	AddChild(defaults);
150 
151 	cvrect.OffsetBy(70,0);
152 	revert=new BButton(cvrect,"RevertButton","Revert",
153 						new BMessage(REVERT_SETTINGS),
154 						B_FOLLOW_LEFT |B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
155 	AddChild(revert);
156 	revert->SetEnabled(false);
157 
158 	cvrect.OffsetBy(70,0);
159 	apply=new BButton(cvrect,"ApplyButton","Apply", new BMessage(APPLY_SETTINGS),
160 						B_FOLLOW_LEFT |B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE);
161 	AddChild(apply);
162 	apply->SetEnabled(false);
163 
164 	BEntry entry(COLOR_SET_DIR);
165 	entry_ref ref;
166 	entry.GetRef(&ref);
167 
168 	attribute=B_PANEL_BACKGROUND_COLOR;
169 	attrstring="Panel Background";
170 
171 	LoadSettings();
172 
173 	// Save values for when we need to revert
174 	prevset=currentset;
175 }
176 
177 void APRView::AttachedToWindow(void)
178 {
179 	picker->SetTarget(this);
180 	attrlist->Select(0);
181 	attrlist->SetTarget(this);
182 	apply->SetTarget(this);
183 	defaults->SetTarget(this);
184 	revert->SetTarget(this);
185 	colorwell->SetTarget(this);
186 
187 	picker->SetValue(currentset.StringToColor(attrstring.String()));
188 }
189 
190 void APRView::MessageReceived(BMessage *msg)
191 {
192 	if(msg->WasDropped())
193 	{
194 		rgb_color *col;
195 		ssize_t size;
196 		if(msg->FindData("RGBColor",(type_code)'RGBC',(const void**)&col,&size)==B_OK)
197 		{
198 			picker->SetValue(*col);
199 			colorwell->SetColor(*col);
200 			colorwell->Invalidate();
201 		}
202 	}
203 
204 	switch(msg->what)
205 	{
206 		case UPDATE_COLOR:
207 		{
208 			// Received from the color picker when its color changes
209 
210 			rgb_color col=picker->ValueAsColor();
211 
212 			colorwell->SetColor(col);
213 			colorwell->Invalidate();
214 
215 			// Update current attribute in the settings
216 			currentset.SetColor(attrstring.String(),col);
217 
218 			apply->SetEnabled(true);
219 			revert->SetEnabled(true);
220 
221 			break;
222 		}
223 		case ATTRIBUTE_CHOSEN:
224 		{
225 			// Received when the user chooses a GUI attribute from the list
226 
227 			ColorWhichItem *whichitem = (ColorWhichItem*)attrlist->ItemAt( attrlist->CurrentSelection() );
228 
229 			if(!whichitem)
230 				break;
231 
232 			attrstring=whichitem->Text();
233 			UpdateControlsFromAttr(whichitem->Text());
234 			break;
235 		}
236 		case APPLY_SETTINGS:
237 		{
238 			apply->SetEnabled(false);
239 			BPrivate::set_system_colors(currentset);
240 
241 			BString path(SERVER_SETTINGS_DIR);
242 			path += COLOR_SETTINGS_NAME;
243 
244 			SaveColorSet( path.String(), currentset);
245 			break;
246 		}
247 		case REVERT_SETTINGS:
248 		{
249 			currentset=prevset;
250 			UpdateControlsFromAttr(attrstring.String());
251 
252 			// Save settings on disk if 'Apply' has been used
253 			if(!apply->IsEnabled())
254 			{
255 				BString path(SERVER_SETTINGS_DIR);
256 				path += COLOR_SETTINGS_NAME;
257 				SaveColorSet( path.String(), currentset);
258 			}
259 			apply->SetEnabled(false);
260 			revert->SetEnabled(false);
261 
262 			break;
263 		}
264 		case DEFAULT_SETTINGS:
265 		{
266 			currentset.SetToDefaults();
267 			apply->SetEnabled(true);
268 
269 			UpdateControlsFromAttr(attrstring.String());
270 			break;
271 		}
272 		default:
273 			BView::MessageReceived(msg);
274 			break;
275 	}
276 }
277 
278 void APRView::LoadSettings(void)
279 {
280 	// Load the current GUI color settings from disk. This is done instead of
281 	// getting them from the server at this point for testing purposes.
282 
283 	// Query the server for the current settings
284 	//BPrivate::get_system_colors(&currentset);
285 
286 	// TODO: remove this and enable the get_system_colors() call
287 	LoadColorSet("/boot/home/config/settings/app_server/system_colors",&currentset);
288 
289 	UpdateControlsFromAttr(attrstring.String());
290 }
291 
292 void APRView::UpdateControlsFromAttr(const char *string)
293 {
294 	if(!string)
295 		return;
296 	STRACE(("Update color for %s\n",string));
297 
298 	picker->SetValue(currentset.StringToColor(string));
299 	colorwell->SetColor(picker->ValueAsColor());
300 	colorwell->Invalidate();
301 }
302