1 /* 2 * Copyright 2002-2015 Haiku, Inc. 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 * John Scipione, jscipione@gmail.com 9 * Joseph Groover <looncraz@looncraz.net> 10 */ 11 12 13 #include "APRView.h" 14 15 #include <stdio.h> 16 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> 27 28 #include "APRWindow.h" 29 #include "defs.h" 30 #include "ColorPreview.h" 31 #include "Colors.h" 32 #include "ColorWhichListView.h" 33 #include "ColorWhichItem.h" 34 35 36 #undef B_TRANSLATION_CONTEXT 37 #define B_TRANSLATION_CONTEXT "Colors tab" 38 39 #define COLOR_DROPPED 'cldp' 40 #define DECORATOR_CHANGED 'dcch' 41 42 43 APRView::APRView(const char* name) 44 : 45 BView(name, B_WILL_DRAW) 46 { 47 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 48 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))); 60 } 61 62 BMenuField* field = new BMenuField("Window Style", fDecorMenu); 63 // TODO: use this menu field. 64 } 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); 72 } 73 #endif 74 75 LoadSettings(); 76 77 // Set up list of color attributes 78 fAttrList = new ColorWhichListView("AttributeList"); 79 80 fScrollView = new BScrollView("ScrollView", fAttrList, 0, false, true); 81 fScrollView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 82 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))); 89 } 90 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)); 95 96 fPicker = new BColorControl(B_ORIGIN, B_CELLS_32x8, 8.0, 97 "picker", new BMessage(UPDATE_COLOR)); 98 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); 106 107 fColorPreview->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 fColorPreview->SetTarget(this); 124 125 fAttrList->Select(0); 126 SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 127 } 128 129 130 void 131 APRView::MessageReceived(BMessage *msg) 132 { 133 switch (msg->what) { 134 case SET_COLOR: 135 { 136 rgb_color* color; 137 ssize_t size; 138 color_which which; 139 140 if (msg->FindData(kRGBColor, B_RGB_COLOR_TYPE, 141 (const void**)&color, &size) == B_OK 142 && msg->FindUInt32(kWhich, (uint32*)&which) == B_OK) { 143 _SetColor(which, *color); 144 Window()->PostMessage(kMsgUpdate); 145 } 146 break; 147 } 148 149 case SET_CURRENT_COLOR: 150 { 151 rgb_color* color; 152 ssize_t size; 153 154 if (msg->FindData(kRGBColor, B_RGB_COLOR_TYPE, 155 (const void**)&color, &size) == B_OK) { 156 _SetCurrentColor(*color); 157 Window()->PostMessage(kMsgUpdate); 158 } 159 break; 160 } 161 162 case UPDATE_COLOR: 163 { 164 // Received from the color fPicker when its color changes 165 rgb_color color = fPicker->ValueAsColor(); 166 _SetCurrentColor(color); 167 168 Window()->PostMessage(kMsgUpdate); 169 break; 170 } 171 172 case ATTRIBUTE_CHOSEN: 173 { 174 // Received when the user chooses a GUI fAttribute from the list 175 176 ColorWhichItem* item = (ColorWhichItem*) 177 fAttrList->ItemAt(fAttrList->CurrentSelection()); 178 if (item == NULL) 179 break; 180 181 fWhich = item->ColorWhich(); 182 rgb_color color = ui_color(fWhich); 183 _SetCurrentColor(color); 184 break; 185 } 186 187 default: 188 BView::MessageReceived(msg); 189 break; 190 } 191 } 192 193 194 void 195 APRView::LoadSettings() 196 { 197 get_default_colors(&fDefaultColors); 198 get_current_colors(&fCurrentColors); 199 fPrevColors = fCurrentColors; 200 } 201 202 203 void 204 APRView::SetDefaults() 205 { 206 _SetUIColors(fDefaultColors); 207 _UpdatePreviews(fDefaultColors); 208 209 // Use a default color that stands out to show errors clearly 210 rgb_color color = fDefaultColors.GetColor(ui_color_name(fWhich), 211 make_color(255, 0, 255)); 212 213 fPicker->SetValue(color); 214 fColorPreview->SetColor(color); 215 fColorPreview->Invalidate(); 216 217 Window()->PostMessage(kMsgUpdate); 218 } 219 220 221 void 222 APRView::Revert() 223 { 224 _SetUIColors(fPrevColors); 225 _UpdatePreviews(fPrevColors); 226 227 rgb_color color = fPrevColors.GetColor(ui_color_name(fWhich), 228 make_color(255, 0, 255)); 229 fPicker->SetValue(color); 230 fColorPreview->SetColor(color); 231 fColorPreview->Invalidate(); 232 233 Window()->PostMessage(kMsgUpdate); 234 } 235 236 237 bool 238 APRView::IsDefaultable() 239 { 240 return !fDefaultColors.HasSameData(fCurrentColors); 241 } 242 243 244 bool 245 APRView::IsRevertable() 246 { 247 return !fPrevColors.HasSameData(fCurrentColors); 248 } 249 250 251 void 252 APRView::_SetColor(color_which which, rgb_color color) 253 { 254 set_ui_color(which, color); 255 fCurrentColors.SetColor(ui_color_name(which), color); 256 } 257 258 259 void 260 APRView::_SetCurrentColor(rgb_color color) 261 { 262 _SetColor(fWhich, color); 263 264 int32 currentIndex = fAttrList->CurrentSelection(); 265 ColorWhichItem* item = (ColorWhichItem*)fAttrList->ItemAt(currentIndex); 266 if (item != NULL) { 267 item->SetColor(color); 268 fAttrList->InvalidateItem(currentIndex); 269 } 270 271 fPicker->SetValue(color); 272 fColorPreview->SetColor(color); 273 fColorPreview->Invalidate(); 274 } 275 276 277 void 278 APRView::_SetUIColors(const BMessage& colors) 279 { 280 set_ui_colors(&colors); 281 fCurrentColors = colors; 282 } 283 284 285 void 286 APRView::_UpdatePreviews(const BMessage& colors) 287 { 288 rgb_color color; 289 for (int32 i = color_description_count() - 1; i >= 0; i--) { 290 ColorWhichItem* item = static_cast<ColorWhichItem*>(fAttrList->ItemAt(i)); 291 if (item == NULL) 292 continue; 293 294 color = colors.GetColor(ui_color_name(get_color_description(i)->which), 295 make_color(255, 0, 255)); 296 297 item->SetColor(color); 298 fAttrList->InvalidateItem(i); 299 } 300 } 301