2 * Copyright 2002-2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * DarkWyrm, darkwyrm@earthlink.net
7 * Rene Gollent, rene@gollent.com
8 * John Scipione, jscipione@gmail.com
9 * Joseph Groover <looncraz@looncraz.net>
19 #include <Directory.h>
22 #include <LayoutBuilder.h>
24 #include <Messenger.h>
26 #include <SpaceLayoutItem.h>
28 #include "APRWindow.h"
30 #include "ColorPreview.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
);
50 fDecorMenu
= new BMenu("Window Style");
51 int32 decorCount
= fDecorUtil
->CountDecorators();
52 DecorInfo
* decor
= NULL
;
54 for (int32 i
= 0; i
< decorCount
; i
++) {
55 decor
= fDecorUtil
->GetDecorator(i
);
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());
67 marked
->SetMarked(true);
69 marked
= fDecorMenu
->FindItem("Default");
71 marked
->SetMarked(true);
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
,
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
)
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
));
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
);
131 APRView::MessageReceived(BMessage
*msg
)
133 if (msg
->WasDropped()) {
134 rgb_color
* color
= NULL
;
137 if (msg
->FindData("RGBColor", (type_code
)'RGBC', (const void**)&color
,
139 _SetCurrentColor(*color
);
141 Window()->PostMessage(kMsgUpdate
);
148 // Received from the color fPicker when its color changes
149 rgb_color color
= fPicker
->ValueAsColor();
150 _SetCurrentColor(color
);
152 Window()->PostMessage(kMsgUpdate
);
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());
165 fWhich
= item
->ColorWhich();
166 rgb_color color
= ui_color(fWhich
);
167 _SetCurrentColor(color
);
172 BView::MessageReceived(msg
);
179 APRView::LoadSettings()
181 get_default_colors(&fDefaultColors
);
182 get_current_colors(&fCurrentColors
);
183 fPrevColors
= fCurrentColors
;
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
);
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
);
222 APRView::IsDefaultable()
224 return !fDefaultColors
.HasSameData(fCurrentColors
);
229 APRView::IsRevertable()
231 return !fPrevColors
.HasSameData(fCurrentColors
);
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
);
244 item
->SetColor(color
);
245 fAttrList
->InvalidateItem(currentIndex
);
248 fPicker
->SetValue(color
);
249 fColorPreview
->SetColor(color
);
250 fColorPreview
->Invalidate();
255 APRView::_SetUIColors(const BMessage
& colors
)
257 set_ui_colors(&colors
);
258 fCurrentColors
= colors
;
263 APRView::_UpdatePreviews(const BMessage
& colors
)
266 for (int32 i
= color_description_count() - 1; i
>= 0; i
--) {
267 ColorWhichItem
* item
= static_cast<ColorWhichItem
*>(fAttrList
->ItemAt(i
));
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
);