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