1 /* 2 * Copyright 2002-2006, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * DarkWyrm (darkwyrm@earthlink.net) 7 */ 8 #include <OS.h> 9 #include <Directory.h> 10 #include <Alert.h> 11 #include <Messenger.h> 12 #include <storage/Path.h> 13 #include <Entry.h> 14 #include <File.h> 15 #include <stdio.h> 16 17 #include <InterfaceDefs.h> 18 19 #include "APRView.h" 20 #include "defs.h" 21 #include "ColorWell.h" 22 #include "ColorWhichItem.h" 23 #include "ColorSet.h" 24 25 //#define DEBUG_APRVIEW 26 27 #ifdef DEBUG_APRVIEW 28 #define STRACE(a) printf a 29 #else 30 #define STRACE(A) /* nothing */ 31 #endif 32 33 #define COLOR_DROPPED 'cldp' 34 #define DECORATOR_CHANGED 'dcch' 35 36 namespace BPrivate 37 { 38 int32 count_decorators(void); 39 status_t set_decorator(const int32 &index); 40 int32 get_decorator(void); 41 status_t get_decorator_name(const int32 &index, BString &name); 42 status_t get_decorator_preview(const int32 &index, BBitmap *bitmap); 43 } 44 45 APRView::APRView(const BRect &frame, const char *name, int32 resize, int32 flags) 46 : BView(frame,name,resize,flags), 47 fDecorMenu(NULL) 48 { 49 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 50 51 BRect rect(Bounds().InsetByCopy(10,10)); 52 53 #ifdef HAIKU_TARGET_PLATFORM_HAIKU 54 55 fDecorMenu = new BMenu("Window Style"); 56 int32 decorCount = BPrivate::count_decorators(); 57 if (decorCount > 1) { 58 for (int32 i = 0; i < decorCount; i++) { 59 BString name; 60 BPrivate::get_decorator_name(i, name); 61 if (name.CountChars() < 1) 62 continue; 63 fDecorMenu->AddItem(new BMenuItem(name.String(), 64 new BMessage(DECORATOR_CHANGED))); 65 } 66 67 BMenuField *field = new BMenuField(rect, "menufield", "Window Style", 68 fDecorMenu, B_FOLLOW_RIGHT | 69 B_FOLLOW_TOP); 70 AddChild(field); 71 field->SetDivider(be_plain_font->StringWidth("Window style: ") + 5); 72 field->ResizeToPreferred(); 73 field->MoveTo(Bounds().right - field->Bounds().Width(), 10); 74 rect = Bounds().InsetByCopy(10,10); 75 rect.OffsetTo(10, field->Frame().bottom + 10); 76 } 77 BMenuItem *marked = fDecorMenu->ItemAt(BPrivate::get_decorator()); 78 if (marked) 79 marked->SetMarked(true); 80 else 81 { 82 marked = fDecorMenu->FindItem("Default"); 83 if (marked) 84 marked->SetMarked(true); 85 } 86 87 #endif 88 89 // Set up list of color fAttributes 90 rect.right -= B_V_SCROLL_BAR_WIDTH; 91 rect.bottom = rect.top + 75; 92 fAttrList = new BListView(rect,"AttributeList", B_SINGLE_SELECTION_LIST, 93 B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); 94 95 fScrollView = new BScrollView("ScrollView",fAttrList, B_FOLLOW_LEFT_RIGHT | 96 B_FOLLOW_TOP, 0, false, true); 97 AddChild(fScrollView); 98 fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 99 100 fAttrList->SetSelectionMessage(new BMessage(ATTRIBUTE_CHOSEN)); 101 102 fAttrList->AddItem(new ColorWhichItem(B_PANEL_BACKGROUND_COLOR)); 103 fAttrList->AddItem(new ColorWhichItem((color_which)B_PANEL_TEXT_COLOR)); 104 fAttrList->AddItem(new ColorWhichItem((color_which)B_DOCUMENT_BACKGROUND_COLOR)); 105 fAttrList->AddItem(new ColorWhichItem((color_which)B_DOCUMENT_TEXT_COLOR)); 106 fAttrList->AddItem(new ColorWhichItem((color_which)B_CONTROL_BACKGROUND_COLOR)); 107 fAttrList->AddItem(new ColorWhichItem((color_which)B_CONTROL_TEXT_COLOR)); 108 fAttrList->AddItem(new ColorWhichItem((color_which)B_CONTROL_BORDER_COLOR)); 109 fAttrList->AddItem(new ColorWhichItem((color_which)B_CONTROL_HIGHLIGHT_COLOR)); 110 fAttrList->AddItem(new ColorWhichItem((color_which)B_NAVIGATION_BASE_COLOR)); 111 fAttrList->AddItem(new ColorWhichItem((color_which)B_NAVIGATION_PULSE_COLOR)); 112 fAttrList->AddItem(new ColorWhichItem((color_which)B_SHINE_COLOR)); 113 fAttrList->AddItem(new ColorWhichItem((color_which)B_SHADOW_COLOR)); 114 fAttrList->AddItem(new ColorWhichItem(B_MENU_BACKGROUND_COLOR)); 115 fAttrList->AddItem(new ColorWhichItem((color_which)B_MENU_SELECTED_BACKGROUND_COLOR)); 116 fAttrList->AddItem(new ColorWhichItem(B_MENU_ITEM_TEXT_COLOR)); 117 fAttrList->AddItem(new ColorWhichItem(B_MENU_SELECTED_ITEM_TEXT_COLOR)); 118 fAttrList->AddItem(new ColorWhichItem((color_which)B_MENU_SELECTED_BORDER_COLOR)); 119 fAttrList->AddItem(new ColorWhichItem((color_which)B_TOOLTIP_BACKGROUND_COLOR)); 120 121 fAttrList->AddItem(new ColorWhichItem((color_which)B_SUCCESS_COLOR)); 122 fAttrList->AddItem(new ColorWhichItem((color_which)B_FAILURE_COLOR)); 123 fAttrList->AddItem(new ColorWhichItem(B_WINDOW_TAB_COLOR)); 124 fAttrList->AddItem(new ColorWhichItem((color_which)B_WINDOW_TAB_TEXT_COLOR)); 125 fAttrList->AddItem(new ColorWhichItem((color_which)B_INACTIVE_WINDOW_TAB_COLOR)); 126 fAttrList->AddItem(new ColorWhichItem((color_which)B_INACTIVE_WINDOW_TAB_TEXT_COLOR)); 127 128 129 BRect wellrect(0,0,50,50); 130 wellrect.OffsetTo(rect.right + 30, rect.top + 131 (fScrollView->Bounds().Height() - wellrect.Height())/2 ); 132 133 fColorWell = new ColorWell(wellrect,new BMessage(COLOR_DROPPED),true); 134 AddChild(fColorWell); 135 136 // Center the list and color well 137 138 rect = fScrollView->Frame(); 139 rect.right = wellrect.right; 140 rect.OffsetTo((Bounds().Width()-rect.Width())/2,rect.top); 141 142 fPicker = new BColorControl(BPoint(10,fScrollView->Frame().bottom+20),B_CELLS_32x8,5.0,"fPicker", 143 new BMessage(UPDATE_COLOR)); 144 AddChild(fPicker); 145 146 fDefaults = new BButton(BRect(0,0,1,1),"DefaultsButton","Defaults", 147 new BMessage(DEFAULT_SETTINGS), 148 B_FOLLOW_LEFT |B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE); 149 fDefaults->ResizeToPreferred(); 150 fDefaults->SetEnabled(false); 151 fDefaults->MoveTo((fPicker->Frame().right-(fDefaults->Frame().Width()*2)-20)/2,fPicker->Frame().bottom+20); 152 AddChild(fDefaults); 153 154 155 BRect cvrect(fDefaults->Frame()); 156 cvrect.OffsetBy(cvrect.Width() + 20,0); 157 158 fRevert = new BButton(cvrect,"RevertButton","Revert", 159 new BMessage(REVERT_SETTINGS), 160 B_FOLLOW_LEFT |B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE); 161 fRevert->SetEnabled(false); 162 AddChild(fRevert); 163 164 } 165 166 APRView::~APRView(void) 167 { 168 ColorSet::SaveColorSet("/boot/home/config/settings/app_server/system_colors",fCurrentSet); 169 } 170 171 void 172 APRView::AttachedToWindow(void) 173 { 174 fPicker->SetTarget(this); 175 fAttrList->SetTarget(this); 176 fDefaults->SetTarget(this); 177 fRevert->SetTarget(this); 178 fColorWell->SetTarget(this); 179 180 fPicker->SetValue(fCurrentSet.StringToColor(fAttrString.String())); 181 182 if (fDecorMenu) 183 fDecorMenu->SetTargetForItems(BMessenger(this)); 184 185 Window()->ResizeTo(MAX(fPicker->Frame().right,fPicker->Frame().right) + 10, 186 fDefaults->Frame().bottom + 10); 187 LoadSettings(); 188 fAttrList->Select(0); 189 190 fDefaults->SetEnabled(fCurrentSet.IsDefaultable()); 191 } 192 193 void 194 APRView::MessageReceived(BMessage *msg) 195 { 196 if (msg->WasDropped()) { 197 rgb_color *col; 198 ssize_t size; 199 200 if (msg->FindData("RGBColor",(type_code)'RGBC',(const void**)&col,&size)==B_OK) { 201 fPicker->SetValue(*col); 202 fColorWell->SetColor(*col); 203 fColorWell->Invalidate(); 204 } 205 } 206 207 switch(msg->what) { 208 case DECORATOR_CHANGED: { 209 int32 index = fDecorMenu->IndexOf(fDecorMenu->FindMarked()); 210 #ifdef HAIKU_TARGET_PLATFORM_HAIKU 211 if (index >= 0) 212 BPrivate::set_decorator(index); 213 #endif 214 break; 215 } 216 case UPDATE_COLOR: { 217 // Received from the color fPicker when its color changes 218 219 rgb_color col=fPicker->ValueAsColor(); 220 221 fColorWell->SetColor(col); 222 fColorWell->Invalidate(); 223 224 // Update current fAttribute in the settings 225 fCurrentSet.SetColor(fAttrString.String(),col); 226 227 fDefaults->SetEnabled(fCurrentSet.IsDefaultable()); 228 fRevert->SetEnabled(true); 229 230 break; 231 } 232 case ATTRIBUTE_CHOSEN: { 233 // Received when the user chooses a GUI fAttribute from the list 234 235 ColorWhichItem *whichitem = (ColorWhichItem*) 236 fAttrList->ItemAt(fAttrList->CurrentSelection()); 237 238 if (!whichitem) 239 break; 240 241 fAttrString=whichitem->Text(); 242 UpdateControlsFromAttr(whichitem->Text()); 243 244 fDefaults->SetEnabled(fCurrentSet.IsDefaultable()); 245 break; 246 } 247 case REVERT_SETTINGS: { 248 fCurrentSet=fPrevSet; 249 UpdateControlsFromAttr(fAttrString.String()); 250 251 fRevert->SetEnabled(false); 252 253 break; 254 } 255 case DEFAULT_SETTINGS: { 256 fCurrentSet.SetToDefaults(); 257 fDefaults->SetEnabled(false); 258 259 UpdateControlsFromAttr(fAttrString.String()); 260 BMenuItem *item = fDecorMenu->FindItem("Default"); 261 if (item) 262 { 263 item->SetMarked(true); 264 #ifdef HAIKU_TARGET_PLATFORM_HAIKU 265 BPrivate::set_decorator(fDecorMenu->IndexOf(item)); 266 #endif 267 } 268 break; 269 } 270 default: 271 BView::MessageReceived(msg); 272 break; 273 } 274 } 275 276 void APRView::LoadSettings(void) 277 { 278 // Load the current GUI color settings from disk. This is done instead of 279 // getting them from the server at this point for testing purposes. 280 281 // Query the server for the current settings 282 //BPrivate::get_system_colors(&fCurrentSet); 283 284 // TODO: remove this and enable the get_system_colors() call 285 if (ColorSet::LoadColorSet("/boot/home/config/settings/app_server/system_colors",&fCurrentSet) != B_OK) { 286 fCurrentSet.SetToDefaults(); 287 ColorSet::SaveColorSet("/boot/home/config/settings/app_server/system_colors",fCurrentSet); 288 } 289 290 fPrevSet = fCurrentSet; 291 } 292 293 void APRView::UpdateControlsFromAttr(const char *string) 294 { 295 if (!string) 296 return; 297 STRACE(("Update color for %s\n",string)); 298 299 fPicker->SetValue(fCurrentSet.StringToColor(string)); 300 fColorWell->SetColor(fPicker->ValueAsColor()); 301 fColorWell->Invalidate(); 302 } 303