xref: /haiku/src/preferences/appearance/APRView.cpp (revision 6aff37d1c79e20748c683ae224bd629f88a5b0be)
1 /*
2  * Copyright 2002-2015 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		DarkWyrm, darkwyrm@earthlink.net
7  *		Rene Gollent, rene@gollent.com
8  *		John Scipione, jscipione@gmail.com
9  *		Joseph Groover <looncraz@looncraz.net>
10  */
11 
12 
13 #include "APRView.h"
14 
15 #include <stdio.h>
16 
17 #include <Alert.h>
18 #include <Catalog.h>
19 #include <Directory.h>
20 #include <Entry.h>
21 #include <File.h>
22 #include <LayoutBuilder.h>
23 #include <Locale.h>
24 #include <Messenger.h>
25 #include <Path.h>
26 #include <SpaceLayoutItem.h>
27 
28 #include "APRWindow.h"
29 #include "defs.h"
30 #include "ColorPreview.h"
31 #include "Colors.h"
32 #include "ColorWhichListView.h"
33 #include "ColorWhichItem.h"
34 
35 
36 #undef B_TRANSLATION_CONTEXT
37 #define B_TRANSLATION_CONTEXT "Colors tab"
38 
39 #define COLOR_DROPPED 'cldp'
40 #define DECORATOR_CHANGED 'dcch'
41 
42 
43 APRView::APRView(const char* name)
44 	:
45 	BView(name, B_WILL_DRAW)
46 {
47 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
48 
49 #if 0
50 	fDecorMenu = new BMenu("Window Style");
51 	int32 decorCount = fDecorUtil->CountDecorators();
52 	DecorInfo* decor = NULL;
53 	if (decorCount > 1) {
54 		for (int32 i = 0; i < decorCount; i++) {
55 			decor = fDecorUtil->GetDecorator(i);
56 			if (!decor)
57 				continue;
58 			fDecorMenu->AddItem(new BMenuItem(decor->Name().String(),
59 				new BMessage(DECORATOR_CHANGED)));
60 		}
61 
62 		BMenuField* field = new BMenuField("Window Style", fDecorMenu);
63 		// TODO: use this menu field.
64 	}
65 	BMenuItem* marked = fDecorMenu->ItemAt(fDecorUtil->IndexOfCurrentDecorator());
66 	if (marked)
67 		marked->SetMarked(true);
68 	else {
69 		marked = fDecorMenu->FindItem("Default");
70 		if (marked)
71 			marked->SetMarked(true);
72 	}
73 #endif
74 
75 	LoadSettings();
76 
77 	// Set up list of color attributes
78 	fAttrList = new ColorWhichListView("AttributeList");
79 
80 	fScrollView = new BScrollView("ScrollView", fAttrList, 0, false, true);
81 	fScrollView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
82 
83 	int32 count = color_description_count();
84 	for (int32 i = 0; i < count; i++) {
85 		const ColorDescription& description = *get_color_description(i);
86 		const char* text = B_TRANSLATE_NOCOLLECT(description.text);
87 		color_which which = description.which;
88 		fAttrList->AddItem(new ColorWhichItem(text, which, ui_color(which)));
89 	}
90 
91 	BRect wellrect(0, 0, 50, 50);
92 	fColorPreview = new ColorPreview(wellrect, new BMessage(COLOR_DROPPED), 0);
93 	fColorPreview->SetExplicitAlignment(BAlignment(B_ALIGN_HORIZONTAL_CENTER,
94 		B_ALIGN_BOTTOM));
95 
96 	fPicker = new BColorControl(B_ORIGIN, B_CELLS_32x8, 8.0,
97 		"picker", new BMessage(UPDATE_COLOR));
98 
99 	BLayoutBuilder::Group<>(this, B_VERTICAL)
100 		.Add(fScrollView, 10.0)
101 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
102 			.Add(fColorPreview)
103 			.Add(fPicker)
104 			.End()
105 		.SetInsets(B_USE_WINDOW_SPACING);
106 
107 	fColorPreview->Parent()->SetExplicitMaxSize(
108 		BSize(B_SIZE_UNSET, fPicker->Bounds().Height()));
109 	fAttrList->SetSelectionMessage(new BMessage(ATTRIBUTE_CHOSEN));
110 }
111 
112 
113 APRView::~APRView()
114 {
115 }
116 
117 
118 void
119 APRView::AttachedToWindow()
120 {
121 	fPicker->SetTarget(this);
122 	fAttrList->SetTarget(this);
123 	fColorPreview->SetTarget(this);
124 
125 	fAttrList->Select(0);
126 	SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
127 }
128 
129 
130 void
131 APRView::MessageReceived(BMessage *msg)
132 {
133 	if (msg->WasDropped()) {
134 		rgb_color* color = NULL;
135 		ssize_t size = 0;
136 
137 		if (msg->FindData("RGBColor", (type_code)'RGBC', (const void**)&color,
138 				&size) == B_OK) {
139 			_SetCurrentColor(*color);
140 
141 			Window()->PostMessage(kMsgUpdate);
142 		}
143 	}
144 
145 	switch (msg->what) {
146 		case UPDATE_COLOR:
147 		{
148 			// Received from the color fPicker when its color changes
149 			rgb_color color = fPicker->ValueAsColor();
150 			_SetCurrentColor(color);
151 
152 			Window()->PostMessage(kMsgUpdate);
153 			break;
154 		}
155 
156 		case ATTRIBUTE_CHOSEN:
157 		{
158 			// Received when the user chooses a GUI fAttribute from the list
159 
160 			ColorWhichItem* item = (ColorWhichItem*)
161 				fAttrList->ItemAt(fAttrList->CurrentSelection());
162 			if (item == NULL)
163 				break;
164 
165 			fWhich = item->ColorWhich();
166 			rgb_color color = ui_color(fWhich);
167 			_SetCurrentColor(color);
168 			break;
169 		}
170 
171 		default:
172 			BView::MessageReceived(msg);
173 			break;
174 	}
175 }
176 
177 
178 void
179 APRView::LoadSettings()
180 {
181 	get_default_colors(&fDefaultColors);
182 	get_current_colors(&fCurrentColors);
183 	fPrevColors = fCurrentColors;
184 }
185 
186 
187 void
188 APRView::SetDefaults()
189 {
190 	_SetUIColors(fDefaultColors);
191 	_UpdatePreviews(fDefaultColors);
192 
193 	// Use a default color that stands out to show errors clearly
194 	rgb_color color = fDefaultColors.GetColor(ui_color_name(fWhich),
195 		make_color(255, 0, 255));
196 
197 	fPicker->SetValue(color);
198 	fColorPreview->SetColor(color);
199 	fColorPreview->Invalidate();
200 
201 	Window()->PostMessage(kMsgUpdate);
202 }
203 
204 
205 void
206 APRView::Revert()
207 {
208 	_SetUIColors(fPrevColors);
209 	_UpdatePreviews(fPrevColors);
210 
211 	rgb_color color = fPrevColors.GetColor(ui_color_name(fWhich),
212 		make_color(255, 0, 255));
213 	fPicker->SetValue(color);
214 	fColorPreview->SetColor(color);
215 	fColorPreview->Invalidate();
216 
217 	Window()->PostMessage(kMsgUpdate);
218 }
219 
220 
221 bool
222 APRView::IsDefaultable()
223 {
224 	return !fDefaultColors.HasSameData(fCurrentColors);
225 }
226 
227 
228 bool
229 APRView::IsRevertable()
230 {
231 	return !fPrevColors.HasSameData(fCurrentColors);
232 }
233 
234 
235 void
236 APRView::_SetCurrentColor(rgb_color color)
237 {
238 	set_ui_color(fWhich, color);
239 	fCurrentColors.SetColor(ui_color_name(fWhich), color);
240 
241 	int32 currentIndex = fAttrList->CurrentSelection();
242 	ColorWhichItem* item = (ColorWhichItem*)fAttrList->ItemAt(currentIndex);
243 	if (item != NULL) {
244 		item->SetColor(color);
245 		fAttrList->InvalidateItem(currentIndex);
246 	}
247 
248 	fPicker->SetValue(color);
249 	fColorPreview->SetColor(color);
250 	fColorPreview->Invalidate();
251 }
252 
253 
254 void
255 APRView::_SetUIColors(const BMessage& colors)
256 {
257 	set_ui_colors(&colors);
258 	fCurrentColors = colors;
259 }
260 
261 
262 void
263 APRView::_UpdatePreviews(const BMessage& colors)
264 {
265 	rgb_color color;
266 	for (int32 i = color_description_count() - 1; i >= 0; i--) {
267 		ColorWhichItem* item = static_cast<ColorWhichItem*>(fAttrList->ItemAt(i));
268 		if (item == NULL)
269 			continue;
270 
271 		color = colors.GetColor(ui_color_name(get_color_description(i)->which),
272 			make_color(255, 0, 255));
273 
274 		item->SetColor(color);
275 		fAttrList->InvalidateItem(i);
276 	}
277 }
278