1 /* 2 * Copyright 2002-2013 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 */ 10 11 12 #include "APRView.h" 13 14 #include <stdio.h> 15 16 #include <Alert.h> 17 #include <Catalog.h> 18 #include <Directory.h> 19 #include <Entry.h> 20 #include <File.h> 21 #include <GroupLayoutBuilder.h> 22 #include <Locale.h> 23 #include <Messenger.h> 24 #include <Path.h> 25 #include <SpaceLayoutItem.h> 26 27 #include "APRWindow.h" 28 #include "defs.h" 29 #include "ColorPreview.h" 30 #include "ColorSet.h" 31 #include "ColorWhichItem.h" 32 33 34 #undef B_TRANSLATION_CONTEXT 35 #define B_TRANSLATION_CONTEXT "Colors tab" 36 37 #define COLOR_DROPPED 'cldp' 38 #define DECORATOR_CHANGED 'dcch' 39 40 41 APRView::APRView(const char* name) 42 : 43 BView(name, B_WILL_DRAW), 44 fDefaultSet(ColorSet::DefaultColorSet()) 45 { 46 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 47 48 #if 0 49 fDecorMenu = new BMenu("Window Style"); 50 int32 decorCount = fDecorUtil->CountDecorators(); 51 DecorInfo* decor = NULL; 52 if (decorCount > 1) { 53 for (int32 i = 0; i < decorCount; i++) { 54 decor = fDecorUtil->GetDecorator(i); 55 if (!decor) 56 continue; 57 fDecorMenu->AddItem(new BMenuItem(decor->Name().String(), 58 new BMessage(DECORATOR_CHANGED))); 59 } 60 61 BMenuField* field = new BMenuField("Window Style", fDecorMenu); 62 // TODO: use this menu field. 63 } 64 BMenuItem* marked = fDecorMenu->ItemAt(fDecorUtil->IndexOfCurrentDecorator()); 65 if (marked) 66 marked->SetMarked(true); 67 else { 68 marked = fDecorMenu->FindItem("Default"); 69 if (marked) 70 marked->SetMarked(true); 71 } 72 #endif 73 74 LoadSettings(); 75 76 // Set up list of color attributes 77 fAttrList = new BListView("AttributeList", B_SINGLE_SELECTION_LIST); 78 79 fScrollView = new BScrollView("ScrollView", fAttrList, 0, false, true); 80 fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 81 82 int32 count = color_description_count(); 83 for (int32 i = 0; i < count; i++) { 84 const ColorDescription& description = *get_color_description(i); 85 const char* text = B_TRANSLATE_NOCOLLECT(description.text); 86 color_which which = description.which; 87 fAttrList->AddItem(new ColorWhichItem(text, which, 88 fCurrentSet.GetColor(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 SetLayout(new BGroupLayout(B_VERTICAL)); 100 101 AddChild(BGroupLayoutBuilder(B_VERTICAL, 0) 102 .Add(fScrollView) 103 .Add(BSpaceLayoutItem::CreateVerticalStrut(5)) 104 .Add(BGroupLayoutBuilder(B_HORIZONTAL) 105 .Add(fColorPreview) 106 .Add(BSpaceLayoutItem::CreateHorizontalStrut(5)) 107 .Add(fPicker) 108 ) 109 .SetInsets(10, 10, 10, 10) 110 ); 111 112 fColorPreview->Parent()->SetExplicitMaxSize( 113 BSize(B_SIZE_UNSET, fPicker->Bounds().Height())); 114 fAttrList->SetSelectionMessage(new BMessage(ATTRIBUTE_CHOSEN)); 115 } 116 117 118 APRView::~APRView() 119 { 120 } 121 122 123 void 124 APRView::AttachedToWindow() 125 { 126 fPicker->SetTarget(this); 127 fAttrList->SetTarget(this); 128 fColorPreview->SetTarget(this); 129 130 fAttrList->Select(0); 131 } 132 133 134 void 135 APRView::MessageReceived(BMessage *msg) 136 { 137 if (msg->WasDropped()) { 138 rgb_color* color = NULL; 139 ssize_t size = 0; 140 141 if (msg->FindData("RGBColor", (type_code)'RGBC', (const void**)&color, 142 &size) == B_OK) { 143 _SetCurrentColor(*color); 144 } 145 } 146 147 switch (msg->what) { 148 case UPDATE_COLOR: 149 { 150 // Received from the color fPicker when its color changes 151 rgb_color color = fPicker->ValueAsColor(); 152 _SetCurrentColor(color); 153 154 Window()->PostMessage(kMsgUpdate); 155 break; 156 } 157 158 case ATTRIBUTE_CHOSEN: 159 { 160 // Received when the user chooses a GUI fAttribute from the list 161 162 ColorWhichItem* item = (ColorWhichItem*) 163 fAttrList->ItemAt(fAttrList->CurrentSelection()); 164 if (item == NULL) 165 break; 166 167 fWhich = item->ColorWhich(); 168 rgb_color color = fCurrentSet.GetColor(fWhich); 169 _SetCurrentColor(color); 170 171 Window()->PostMessage(kMsgUpdate); 172 break; 173 } 174 175 default: 176 BView::MessageReceived(msg); 177 break; 178 } 179 } 180 181 182 void 183 APRView::LoadSettings() 184 { 185 int32 count = color_description_count(); 186 for (int32 i = 0; i < count; i++) { 187 color_which which = get_color_description(i)->which; 188 fCurrentSet.SetColor(which, ui_color(which)); 189 } 190 191 fPrevSet = fCurrentSet; 192 } 193 194 195 void 196 APRView::SetDefaults() 197 { 198 fCurrentSet = ColorSet::DefaultColorSet(); 199 200 _UpdateAllColors(); 201 202 rgb_color color = fCurrentSet.GetColor(fWhich); 203 fPicker->SetValue(color); 204 fColorPreview->SetColor(color); 205 fColorPreview->Invalidate(); 206 207 Window()->PostMessage(kMsgUpdate); 208 } 209 210 211 void 212 APRView::Revert() 213 { 214 fCurrentSet = fPrevSet; 215 216 _UpdateAllColors(); 217 218 rgb_color color = fCurrentSet.GetColor(fWhich); 219 fPicker->SetValue(color); 220 fColorPreview->SetColor(color); 221 fColorPreview->Invalidate(); 222 223 Window()->PostMessage(kMsgUpdate); 224 } 225 226 227 bool 228 APRView::IsDefaultable() 229 { 230 for (int32 i = color_description_count() - 1; i >= 0; i--) { 231 color_which which = get_color_description(i)->which; 232 if (fCurrentSet.GetColor(which) != fDefaultSet.GetColor(which)) 233 return true; 234 } 235 236 return false; 237 } 238 239 240 bool 241 APRView::IsRevertable() 242 { 243 for (int32 i = color_description_count() - 1; i >= 0; i--) { 244 color_which which = get_color_description(i)->which; 245 if (fCurrentSet.GetColor(which) != fPrevSet.GetColor(which)) 246 return true; 247 } 248 249 return false; 250 } 251 252 253 void 254 APRView::_SetCurrentColor(rgb_color color) 255 { 256 fCurrentSet.SetColor(fWhich, color); 257 set_ui_color(fWhich, color); 258 259 int32 currentIndex = fAttrList->CurrentSelection(); 260 ColorWhichItem* item = (ColorWhichItem*)fAttrList->ItemAt(currentIndex); 261 if (item != NULL) { 262 item->SetColor(color); 263 fAttrList->InvalidateItem(currentIndex); 264 } 265 266 fPicker->SetValue(color); 267 fColorPreview->SetColor(color); 268 fColorPreview->Invalidate(); 269 } 270 271 272 void 273 APRView::_UpdateAllColors() 274 { 275 for (int32 i = color_description_count() - 1; i >= 0; i--) { 276 color_which which = get_color_description(i)->which; 277 rgb_color color = fCurrentSet.GetColor(which); 278 set_ui_color(which, color); 279 static_cast<ColorWhichItem*>(fAttrList->ItemAt(i))->SetColor(color); 280 fAttrList->InvalidateItem(i); 281 } 282 } 283