xref: /haiku/src/preferences/appearance/APRView.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
1 /*
2  * Copyright 2002-2009, Haiku. 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  */
9 
10 #include "APRView.h"
11 
12 #include <Alert.h>
13 #include <Catalog.h>
14 #include <Directory.h>
15 #include <Entry.h>
16 #include <File.h>
17 #include <GroupLayoutBuilder.h>
18 #include <Locale.h>
19 #include <Messenger.h>
20 #include <Path.h>
21 #include <SpaceLayoutItem.h>
22 
23 #include <stdio.h>
24 
25 #include "APRWindow.h"
26 #include "defs.h"
27 #include "ColorWell.h"
28 #include "ColorWhichItem.h"
29 #include "ColorSet.h"
30 
31 
32 #undef B_TRANSLATE_CONTEXT
33 #define B_TRANSLATE_CONTEXT "Colors tab"
34 
35 #define COLOR_DROPPED 'cldp'
36 #define DECORATOR_CHANGED 'dcch'
37 
38 namespace BPrivate
39 {
40 	int32 count_decorators(void);
41 	status_t set_decorator(const int32 &index);
42 	int32 get_decorator(void);
43 	status_t get_decorator_name(const int32 &index, BString &name);
44 	status_t get_decorator_preview(const int32 &index, BBitmap *bitmap);
45 }
46 
47 APRView::APRView(const char *name, uint32 flags)
48  :	BView(name, flags),
49  	fDefaultSet(ColorSet::DefaultColorSet()),
50  	fDecorMenu(NULL)
51 {
52 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
53 
54 #if 0
55 	fDecorMenu = new BMenu("Window Style");
56 	int32 decorCount = BPrivate::count_decorators();
57 	if (decorCount > 1) {
58 		for (int32 i = 0; i < decorCount; i++) {
59 			BString name;
60 			BPrivate::get_decorator_name(i, name);
61 			if (name.CountChars() < 1)
62 				continue;
63 			fDecorMenu->AddItem(new BMenuItem(name.String(),
64 				new BMessage(DECORATOR_CHANGED)));
65 		}
66 
67 		BMenuField *field = new BMenuField("Window Style", fDecorMenu);
68 		// TODO: use this menu field.
69 	}
70 	BMenuItem *marked = fDecorMenu->ItemAt(BPrivate::get_decorator());
71 	if (marked)
72 		marked->SetMarked(true);
73 	else {
74 		marked = fDecorMenu->FindItem("Default");
75 		if (marked)
76 			marked->SetMarked(true);
77 	}
78 #endif
79 
80 	// Set up list of color attributes
81 	fAttrList = new BListView("AttributeList", B_SINGLE_SELECTION_LIST);
82 
83 	fScrollView = new BScrollView("ScrollView", fAttrList, 0, false, true);
84 	fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
85 
86 	for (int32 i = 0; i < color_description_count(); i++) {
87 		const ColorDescription& description = *get_color_description(i);
88 		const char* text = B_TRANSLATE(description.text);
89 		color_which which = description.which;
90 		fAttrList->AddItem(new ColorWhichItem(text, which));
91 	}
92 
93 	BRect wellrect(0, 0, 50, 50);
94 	fColorWell = new ColorWell(wellrect, new BMessage(COLOR_DROPPED), 0);
95 	fColorWell->SetExplicitAlignment(BAlignment(B_ALIGN_HORIZONTAL_CENTER,
96 		B_ALIGN_BOTTOM));
97 
98 	fPicker = new BColorControl(B_ORIGIN, B_CELLS_32x8, 8.0,
99 		"picker", new BMessage(UPDATE_COLOR));
100 
101 	SetLayout(new BGroupLayout(B_VERTICAL));
102 
103 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
104 		.Add(fScrollView)
105 		.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
106 		.Add(BGroupLayoutBuilder(B_HORIZONTAL)
107 			.Add(fColorWell)
108 			.Add(BSpaceLayoutItem::CreateHorizontalStrut(5))
109 			.Add(fPicker)
110 		)
111 		.SetInsets(10, 10, 10, 10)
112 	);
113 
114 	fColorWell->Parent()->SetExplicitMaxSize(
115 		BSize(B_SIZE_UNSET, fPicker->Bounds().Height()));
116 	fAttrList->SetSelectionMessage(new BMessage(ATTRIBUTE_CHOSEN));
117 }
118 
119 
120 APRView::~APRView()
121 {
122 }
123 
124 
125 void
126 APRView::AttachedToWindow()
127 {
128 	fPicker->SetTarget(this);
129 	fAttrList->SetTarget(this);
130 	fColorWell->SetTarget(this);
131 
132 	if (fDecorMenu)
133 		fDecorMenu->SetTargetForItems(BMessenger(this));
134 
135 	LoadSettings();
136 	fAttrList->Select(0);
137 }
138 
139 
140 void
141 APRView::MessageReceived(BMessage *msg)
142 {
143 	if (msg->WasDropped()) {
144 		rgb_color *color;
145 		ssize_t size;
146 
147 		if (msg->FindData("RGBColor", (type_code)'RGBC', (const void**)&color,
148 				&size) == B_OK) {
149 			SetCurrentColor(*color);
150 		}
151 	}
152 
153 	switch (msg->what) {
154 		case DECORATOR_CHANGED:
155 		{
156 			int32 index = fDecorMenu->IndexOf(fDecorMenu->FindMarked());
157 			#ifdef HAIKU_TARGET_PLATFORM_HAIKU
158 			if (index >= 0)
159 				BPrivate::set_decorator(index);
160 			#endif
161 			break;
162 		}
163 		case UPDATE_COLOR:
164 		{
165 			// Received from the color fPicker when its color changes
166 			rgb_color color = fPicker->ValueAsColor();
167 			SetCurrentColor(color);
168 
169 			Window()->PostMessage(kMsgUpdate);
170 			break;
171 		}
172 		case ATTRIBUTE_CHOSEN:
173 		{
174 			// Received when the user chooses a GUI fAttribute from the list
175 
176 			ColorWhichItem *item = (ColorWhichItem*)
177 				fAttrList->ItemAt(fAttrList->CurrentSelection());
178 			if (item == NULL)
179 				break;
180 
181 			fWhich = item->ColorWhich();
182 			rgb_color color = fCurrentSet.GetColor(fWhich);
183 			SetCurrentColor(color);
184 
185 			Window()->PostMessage(kMsgUpdate);
186 			break;
187 		}
188 		case REVERT_SETTINGS:
189 		{
190 			fCurrentSet = fPrevSet;
191 
192 			UpdateControls();
193 			UpdateAllColors();
194 
195 			Window()->PostMessage(kMsgUpdate);
196 			break;
197 		}
198 		case DEFAULT_SETTINGS:
199 		{
200 			fCurrentSet = ColorSet::DefaultColorSet();
201 
202 			UpdateControls();
203 			UpdateAllColors();
204 
205 			if (fDecorMenu) {
206 				BMenuItem *item = fDecorMenu->FindItem("Default");
207 				if (item) {
208 					item->SetMarked(true);
209 					#ifdef HAIKU_TARGET_PLATFORM_HAIKU
210 					BPrivate::set_decorator(fDecorMenu->IndexOf(item));
211 					#endif
212 				}
213 			}
214 			Window()->PostMessage(kMsgUpdate);
215 			break;
216 		}
217 		default:
218 			BView::MessageReceived(msg);
219 			break;
220 	}
221 }
222 
223 
224 void
225 APRView::LoadSettings()
226 {
227 	for (int32 i = 0; i < color_description_count(); i++) {
228 		color_which which = get_color_description(i)->which;
229 		fCurrentSet.SetColor(which, ui_color(which));
230 	}
231 
232 	fPrevSet = fCurrentSet;
233 }
234 
235 
236 bool
237 APRView::IsDefaultable()
238 {
239 	return fCurrentSet != fDefaultSet;
240 }
241 
242 
243 void
244 APRView::UpdateAllColors()
245 {
246 	for (int32 i = 0; i < color_description_count(); i++) {
247 		color_which which = get_color_description(i)->which;
248 		rgb_color color = fCurrentSet.GetColor(which);
249 		set_ui_color(which, color);
250 	}
251 }
252 
253 
254 void
255 APRView::SetCurrentColor(rgb_color color)
256 {
257 	fCurrentSet.SetColor(fWhich, color);
258 	set_ui_color(fWhich, color);
259 	UpdateControls();
260 }
261 
262 
263 void
264 APRView::UpdateControls()
265 {
266 	rgb_color color = fCurrentSet.GetColor(fWhich);
267 	fPicker->SetValue(color);
268 	fColorWell->SetColor(color);
269 	fColorWell->Invalidate();
270 }
271