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