vfs: check userland buffers before reading them.
[haiku.git] / src / preferences / appearance / APRView.cpp
blob4a4a526e85da29680c6003e493d7396de237be4e
1 /*
2 * Copyright 2002-2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * DarkWyrm, darkwyrm@earthlink.net
7 * Rene Gollent, rene@gollent.com
8 * John Scipione, jscipione@gmail.com
9 * Joseph Groover <looncraz@looncraz.net>
13 #include "APRView.h"
15 #include <stdio.h>
17 #include <Alert.h>
18 #include <Catalog.h>
19 #include <Directory.h>
20 #include <Entry.h>
21 #include <File.h>
22 #include <LayoutBuilder.h>
23 #include <Locale.h>
24 #include <Messenger.h>
25 #include <Path.h>
26 #include <SpaceLayoutItem.h>
28 #include "APRWindow.h"
29 #include "defs.h"
30 #include "ColorPreview.h"
31 #include "Colors.h"
32 #include "ColorWhichListView.h"
33 #include "ColorWhichItem.h"
36 #undef B_TRANSLATION_CONTEXT
37 #define B_TRANSLATION_CONTEXT "Colors tab"
39 #define COLOR_DROPPED 'cldp'
40 #define DECORATOR_CHANGED 'dcch'
43 APRView::APRView(const char* name)
45 BView(name, B_WILL_DRAW)
47 SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
49 #if 0
50 fDecorMenu = new BMenu("Window Style");
51 int32 decorCount = fDecorUtil->CountDecorators();
52 DecorInfo* decor = NULL;
53 if (decorCount > 1) {
54 for (int32 i = 0; i < decorCount; i++) {
55 decor = fDecorUtil->GetDecorator(i);
56 if (!decor)
57 continue;
58 fDecorMenu->AddItem(new BMenuItem(decor->Name().String(),
59 new BMessage(DECORATOR_CHANGED)));
62 BMenuField* field = new BMenuField("Window Style", fDecorMenu);
63 // TODO: use this menu field.
65 BMenuItem* marked = fDecorMenu->ItemAt(fDecorUtil->IndexOfCurrentDecorator());
66 if (marked)
67 marked->SetMarked(true);
68 else {
69 marked = fDecorMenu->FindItem("Default");
70 if (marked)
71 marked->SetMarked(true);
73 #endif
75 LoadSettings();
77 // Set up list of color attributes
78 fAttrList = new ColorWhichListView("AttributeList");
80 fScrollView = new BScrollView("ScrollView", fAttrList, 0, false, true);
81 fScrollView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
83 int32 count = color_description_count();
84 for (int32 i = 0; i < count; i++) {
85 const ColorDescription& description = *get_color_description(i);
86 const char* text = B_TRANSLATE_NOCOLLECT(description.text);
87 color_which which = description.which;
88 fAttrList->AddItem(new ColorWhichItem(text, which, ui_color(which)));
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));
96 fPicker = new BColorControl(B_ORIGIN, B_CELLS_32x8, 8.0,
97 "picker", new BMessage(UPDATE_COLOR));
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);
107 fColorPreview->Parent()->SetExplicitMaxSize(
108 BSize(B_SIZE_UNSET, fPicker->Bounds().Height()));
109 fAttrList->SetSelectionMessage(new BMessage(ATTRIBUTE_CHOSEN));
113 APRView::~APRView()
118 void
119 APRView::AttachedToWindow()
121 fPicker->SetTarget(this);
122 fAttrList->SetTarget(this);
123 fColorPreview->SetTarget(this);
125 fAttrList->Select(0);
126 SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
130 void
131 APRView::MessageReceived(BMessage *msg)
133 if (msg->WasDropped()) {
134 rgb_color* color = NULL;
135 ssize_t size = 0;
137 if (msg->FindData("RGBColor", (type_code)'RGBC', (const void**)&color,
138 &size) == B_OK) {
139 _SetCurrentColor(*color);
141 Window()->PostMessage(kMsgUpdate);
145 switch (msg->what) {
146 case UPDATE_COLOR:
148 // Received from the color fPicker when its color changes
149 rgb_color color = fPicker->ValueAsColor();
150 _SetCurrentColor(color);
152 Window()->PostMessage(kMsgUpdate);
153 break;
156 case ATTRIBUTE_CHOSEN:
158 // Received when the user chooses a GUI fAttribute from the list
160 ColorWhichItem* item = (ColorWhichItem*)
161 fAttrList->ItemAt(fAttrList->CurrentSelection());
162 if (item == NULL)
163 break;
165 fWhich = item->ColorWhich();
166 rgb_color color = ui_color(fWhich);
167 _SetCurrentColor(color);
168 break;
171 default:
172 BView::MessageReceived(msg);
173 break;
178 void
179 APRView::LoadSettings()
181 get_default_colors(&fDefaultColors);
182 get_current_colors(&fCurrentColors);
183 fPrevColors = fCurrentColors;
187 void
188 APRView::SetDefaults()
190 _SetUIColors(fDefaultColors);
191 _UpdatePreviews(fDefaultColors);
193 // Use a default color that stands out to show errors clearly
194 rgb_color color = fDefaultColors.GetColor(ui_color_name(fWhich),
195 make_color(255, 0, 255));
197 fPicker->SetValue(color);
198 fColorPreview->SetColor(color);
199 fColorPreview->Invalidate();
201 Window()->PostMessage(kMsgUpdate);
205 void
206 APRView::Revert()
208 _SetUIColors(fPrevColors);
209 _UpdatePreviews(fPrevColors);
211 rgb_color color = fPrevColors.GetColor(ui_color_name(fWhich),
212 make_color(255, 0, 255));
213 fPicker->SetValue(color);
214 fColorPreview->SetColor(color);
215 fColorPreview->Invalidate();
217 Window()->PostMessage(kMsgUpdate);
221 bool
222 APRView::IsDefaultable()
224 return !fDefaultColors.HasSameData(fCurrentColors);
228 bool
229 APRView::IsRevertable()
231 return !fPrevColors.HasSameData(fCurrentColors);
235 void
236 APRView::_SetCurrentColor(rgb_color color)
238 set_ui_color(fWhich, color);
239 fCurrentColors.SetColor(ui_color_name(fWhich), color);
241 int32 currentIndex = fAttrList->CurrentSelection();
242 ColorWhichItem* item = (ColorWhichItem*)fAttrList->ItemAt(currentIndex);
243 if (item != NULL) {
244 item->SetColor(color);
245 fAttrList->InvalidateItem(currentIndex);
248 fPicker->SetValue(color);
249 fColorPreview->SetColor(color);
250 fColorPreview->Invalidate();
254 void
255 APRView::_SetUIColors(const BMessage& colors)
257 set_ui_colors(&colors);
258 fCurrentColors = colors;
262 void
263 APRView::_UpdatePreviews(const BMessage& colors)
265 rgb_color color;
266 for (int32 i = color_description_count() - 1; i >= 0; i--) {
267 ColorWhichItem* item = static_cast<ColorWhichItem*>(fAttrList->ItemAt(i));
268 if (item == NULL)
269 continue;
271 color = colors.GetColor(ui_color_name(get_color_description(i)->which),
272 make_color(255, 0, 255));
274 item->SetColor(color);
275 fAttrList->InvalidateItem(i);