xref: /haiku/src/preferences/appearance/APRView.cpp (revision 0fe022fd0d6d600863555f5a556ab5a5273d20e0)
1 /*
2  * Copyright 2002-2013 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  */
10 
11 
12 #include "APRView.h"
13 
14 #include <stdio.h>
15 
16 #include <Alert.h>
17 #include <Catalog.h>
18 #include <Directory.h>
19 #include <Entry.h>
20 #include <File.h>
21 #include <LayoutBuilder.h>
22 #include <Locale.h>
23 #include <Messenger.h>
24 #include <Path.h>
25 #include <SpaceLayoutItem.h>
26 
27 #include "APRWindow.h"
28 #include "defs.h"
29 #include "ColorPreview.h"
30 #include "ColorSet.h"
31 #include "ColorWhichItem.h"
32 
33 
34 #undef B_TRANSLATION_CONTEXT
35 #define B_TRANSLATION_CONTEXT "Colors tab"
36 
37 #define COLOR_DROPPED 'cldp'
38 #define DECORATOR_CHANGED 'dcch'
39 
40 
41 APRView::APRView(const char* name)
42 	:
43 	BView(name, B_WILL_DRAW),
44 	fDefaultSet(ColorSet::DefaultColorSet())
45 {
46 	SetViewColor(ui_color(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->SetViewColor(ui_color(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,
88 			fCurrentSet.GetColor(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 }
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 = fCurrentSet.GetColor(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 	int32 count = color_description_count();
181 	for (int32 i = 0; i < count; i++) {
182 		color_which which = get_color_description(i)->which;
183 		fCurrentSet.SetColor(which, ui_color(which));
184 	}
185 
186 	fPrevSet = fCurrentSet;
187 }
188 
189 
190 void
191 APRView::SetDefaults()
192 {
193 	fCurrentSet = ColorSet::DefaultColorSet();
194 
195 	_UpdateAllColors();
196 
197 	rgb_color color = fCurrentSet.GetColor(fWhich);
198 	fPicker->SetValue(color);
199 	fColorPreview->SetColor(color);
200 	fColorPreview->Invalidate();
201 
202 	Window()->PostMessage(kMsgUpdate);
203 }
204 
205 
206 void
207 APRView::Revert()
208 {
209 	fCurrentSet = fPrevSet;
210 
211 	_UpdateAllColors();
212 
213 	rgb_color color = fCurrentSet.GetColor(fWhich);
214 	fPicker->SetValue(color);
215 	fColorPreview->SetColor(color);
216 	fColorPreview->Invalidate();
217 
218 	Window()->PostMessage(kMsgUpdate);
219 }
220 
221 
222 bool
223 APRView::IsDefaultable()
224 {
225 	for (int32 i = color_description_count() - 1; i >= 0; i--) {
226 		color_which which = get_color_description(i)->which;
227 		if (fCurrentSet.GetColor(which) != fDefaultSet.GetColor(which))
228 			return true;
229 	}
230 
231 	return false;
232 }
233 
234 
235 bool
236 APRView::IsRevertable()
237 {
238 	for (int32 i = color_description_count() - 1; i >= 0; i--) {
239 		color_which which = get_color_description(i)->which;
240 		if (fCurrentSet.GetColor(which) != fPrevSet.GetColor(which))
241 			return true;
242 	}
243 
244 	return false;
245 }
246 
247 
248 void
249 APRView::_SetCurrentColor(rgb_color color)
250 {
251 	fCurrentSet.SetColor(fWhich, color);
252 	set_ui_color(fWhich, color);
253 
254 	int32 currentIndex = fAttrList->CurrentSelection();
255 	ColorWhichItem* item = (ColorWhichItem*)fAttrList->ItemAt(currentIndex);
256 	if (item != NULL) {
257 		item->SetColor(color);
258 		fAttrList->InvalidateItem(currentIndex);
259 	}
260 
261 	fPicker->SetValue(color);
262 	fColorPreview->SetColor(color);
263 	fColorPreview->Invalidate();
264 }
265 
266 
267 void
268 APRView::_UpdateAllColors()
269 {
270 	for (int32 i = color_description_count() - 1; i >= 0; i--) {
271 		color_which which = get_color_description(i)->which;
272 		rgb_color color = fCurrentSet.GetColor(which);
273 		set_ui_color(which, color);
274 		static_cast<ColorWhichItem*>(fAttrList->ItemAt(i))->SetColor(color);
275 		fAttrList->InvalidateItem(i);
276 	}
277 }
278