xref: /haiku/src/preferences/appearance/APRView.cpp (revision e7c8829c5d8e5d34a2a1e111f1c06aceff256013)
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 			if (fDecorMenu) {
201 				BMenuItem *item = fDecorMenu->FindItem("Default");
202 				if (item) {
203 					item->SetMarked(true);
204 					#ifdef HAIKU_TARGET_PLATFORM_HAIKU
205 					BPrivate::set_decorator(fDecorMenu->IndexOf(item));
206 					#endif
207 				}
208 			}
209 			Window()->PostMessage(kMsgUpdate);
210 			break;
211 		}
212 		default:
213 			BView::MessageReceived(msg);
214 			break;
215 	}
216 }
217 
218 
219 void
220 APRView::LoadSettings()
221 {
222 	for (int32 i = 0; i < color_description_count(); i++) {
223 		color_which which = get_color_description(i)->which;
224 		fCurrentSet.SetColor(which, ui_color(which));
225 	}
226 
227 	fPrevSet = fCurrentSet;
228 }
229 
230 
231 bool
232 APRView::IsDefaultable()
233 {
234 	return fCurrentSet != fDefaultSet;
235 }
236 
237 
238 void
239 APRView::UpdateAllColors()
240 {
241 	for (int32 i = 0; i < color_description_count(); i++) {
242 		color_which which = get_color_description(i)->which;
243 		rgb_color color = fCurrentSet.GetColor(which);
244 		set_ui_color(which, color);
245 	}
246 }
247 
248 
249 void
250 APRView::SetCurrentColor(rgb_color color)
251 {
252 	fCurrentSet.SetColor(fWhich, color);
253 	set_ui_color(fWhich, color);
254 	UpdateControls();
255 }
256 
257 
258 void
259 APRView::UpdateControls()
260 {
261 	rgb_color color = fCurrentSet.GetColor(fWhich);
262 	fPicker->SetValue(color);
263 	fColorWell->SetColor(color);
264 	fColorWell->Invalidate();
265 }
266