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