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_TRANSLATION_CONTEXT 34 #define B_TRANSLATION_CONTEXT "Colors tab" 35 36 #define COLOR_DROPPED 'cldp' 37 #define DECORATOR_CHANGED 'dcch' 38 39 40 APRView::APRView(const char* name) 41 : 42 BView(name, B_WILL_DRAW), 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 LoadSettings(); 74 75 // Set up list of color attributes 76 fAttrList = new BListView("AttributeList", B_SINGLE_SELECTION_LIST); 77 78 fScrollView = new BScrollView("ScrollView", fAttrList, 0, false, true); 79 fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 80 81 for (int32 i = 0; i < color_description_count(); i++) { 82 const ColorDescription& description = *get_color_description(i); 83 const char* text = B_TRANSLATE_NOCOLLECT(description.text); 84 color_which which = description.which; 85 fAttrList->AddItem(new ColorWhichItem(text, which, 86 fCurrentSet.GetColor(which))); 87 } 88 89 BRect wellrect(0, 0, 50, 50); 90 fColorWell = new ColorWell(wellrect, new BMessage(COLOR_DROPPED), 0); 91 fColorWell->SetExplicitAlignment(BAlignment(B_ALIGN_HORIZONTAL_CENTER, 92 B_ALIGN_BOTTOM)); 93 94 fPicker = new BColorControl(B_ORIGIN, B_CELLS_32x8, 8.0, 95 "picker", new BMessage(UPDATE_COLOR)); 96 97 SetLayout(new BGroupLayout(B_VERTICAL)); 98 99 AddChild(BGroupLayoutBuilder(B_VERTICAL, 0) 100 .Add(fScrollView) 101 .Add(BSpaceLayoutItem::CreateVerticalStrut(5)) 102 .Add(BGroupLayoutBuilder(B_HORIZONTAL) 103 .Add(fColorWell) 104 .Add(BSpaceLayoutItem::CreateHorizontalStrut(5)) 105 .Add(fPicker) 106 ) 107 .SetInsets(10, 10, 10, 10) 108 ); 109 110 fColorWell->Parent()->SetExplicitMaxSize( 111 BSize(B_SIZE_UNSET, fPicker->Bounds().Height())); 112 fAttrList->SetSelectionMessage(new BMessage(ATTRIBUTE_CHOSEN)); 113 } 114 115 116 APRView::~APRView() 117 { 118 } 119 120 121 void 122 APRView::AttachedToWindow() 123 { 124 fPicker->SetTarget(this); 125 fAttrList->SetTarget(this); 126 fColorWell->SetTarget(this); 127 128 fAttrList->Select(0); 129 } 130 131 132 void 133 APRView::MessageReceived(BMessage *msg) 134 { 135 if (msg->WasDropped()) { 136 rgb_color *color; 137 ssize_t size; 138 139 if (msg->FindData("RGBColor", (type_code)'RGBC', (const void**)&color, 140 &size) == B_OK) { 141 _SetCurrentColor(*color); 142 } 143 } 144 145 switch (msg->what) { 146 case UPDATE_COLOR: 147 { 148 // Received from the color fPicker when its color changes 149 rgb_color color = fPicker->ValueAsColor(); 150 _SetCurrentColor(color); 151 152 Window()->PostMessage(kMsgUpdate); 153 break; 154 } 155 case ATTRIBUTE_CHOSEN: 156 { 157 // Received when the user chooses a GUI fAttribute from the list 158 159 ColorWhichItem *item = (ColorWhichItem*) 160 fAttrList->ItemAt(fAttrList->CurrentSelection()); 161 if (item == NULL) 162 break; 163 164 fWhich = item->ColorWhich(); 165 rgb_color color = fCurrentSet.GetColor(fWhich); 166 _SetCurrentColor(color); 167 168 Window()->PostMessage(kMsgUpdate); 169 break; 170 } 171 default: 172 BView::MessageReceived(msg); 173 break; 174 } 175 } 176 177 178 void 179 APRView::LoadSettings() 180 { 181 for (int32 i = 0; i < color_description_count(); i++) { 182 color_which which = get_color_description(i)->which; 183 fCurrentSet.SetColor(which, ui_color(which)); 184 } 185 186 fPrevSet = fCurrentSet; 187 } 188 189 190 void 191 APRView::SetDefaults() 192 { 193 fCurrentSet = ColorSet::DefaultColorSet(); 194 195 _UpdateControls(); 196 _UpdateAllColors(); 197 198 Window()->PostMessage(kMsgUpdate); 199 } 200 201 202 void 203 APRView::Revert() 204 { 205 fCurrentSet = fPrevSet; 206 207 _UpdateControls(); 208 _UpdateAllColors(); 209 210 Window()->PostMessage(kMsgUpdate); 211 } 212 213 214 bool 215 APRView::IsDefaultable() 216 { 217 for (int32 i = 0; i < color_description_count(); i++) { 218 color_which which = get_color_description(i)->which; 219 if (fCurrentSet.GetColor(which) != fDefaultSet.GetColor(which)) 220 return true; 221 } 222 return false; 223 } 224 225 226 bool 227 APRView::IsRevertable() 228 { 229 for (int32 i = 0; i < color_description_count(); i++) { 230 color_which which = get_color_description(i)->which; 231 if (fCurrentSet.GetColor(which) != fPrevSet.GetColor(which)) 232 return true; 233 } 234 return false; 235 } 236 237 238 void 239 APRView::_SetCurrentColor(rgb_color color) 240 { 241 fCurrentSet.SetColor(fWhich, color); 242 set_ui_color(fWhich, color); 243 _UpdateControls(); 244 } 245 246 247 void 248 APRView::_UpdateControls() 249 { 250 rgb_color color = fCurrentSet.GetColor(fWhich); 251 252 int32 currentIndex = fAttrList->CurrentSelection(); 253 ColorWhichItem *item = (ColorWhichItem*) fAttrList->ItemAt(currentIndex); 254 if (item != NULL) { 255 item->SetColor(color); 256 fAttrList->InvalidateItem(currentIndex); 257 } 258 259 fPicker->SetValue(color); 260 fColorWell->SetColor(color); 261 fColorWell->Invalidate(); 262 } 263 264 265 void 266 APRView::_UpdateAllColors() 267 { 268 for (int32 i = 0; i < color_description_count(); i++) { 269 color_which which = get_color_description(i)->which; 270 rgb_color color = fCurrentSet.GetColor(which); 271 set_ui_color(which, color); 272 } 273 } 274