1 /* 2 Open Tracker License 3 4 Terms and Conditions 5 6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved. 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy of 9 this software and associated documentation files (the "Software"), to deal in 10 the Software without restriction, including without limitation the rights to 11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 12 of the Software, and to permit persons to whom the Software is furnished to do 13 so, subject to the following conditions: 14 15 The above copyright notice and this permission notice applies to all licensees 16 and shall be included in all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 25 Except as contained in this notice, the name of Be Incorporated shall not be 26 used in advertising or otherwise to promote the sale, use or other dealings in 27 this Software without prior written authorization from Be Incorporated. 28 29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks 30 of Be Incorporated in the United States and other countries. Other brand product 31 names are registered trademarks or trademarks of their respective holders. 32 All rights reserved. 33 */ 34 35 36 #include <Catalog.h> 37 #include <ControlLook.h> 38 #include <InterfaceDefs.h> 39 #include <LayoutBuilder.h> 40 #include <Locale.h> 41 #include <ScrollView.h> 42 43 #include "TrackerSettings.h" 44 #include "TrackerSettingsWindow.h" 45 46 47 namespace BPrivate { 48 49 class SettingsItem : public BStringItem { 50 public: 51 SettingsItem(const char* label, SettingsView* view); 52 53 void DrawItem(BView* owner, BRect rect, bool drawEverything); 54 55 SettingsView* View(); 56 57 private: 58 SettingsView* fSettingsView; 59 }; 60 61 } // namespace BPrivate 62 63 64 const uint32 kSettingsViewChanged = 'Svch'; 65 const uint32 kDefaultsButtonPressed = 'Apbp'; 66 const uint32 kRevertButtonPressed = 'Rebp'; 67 68 69 #undef B_TRANSLATION_CONTEXT 70 #define B_TRANSLATION_CONTEXT "TrackerSettingsWindow" 71 72 73 // #pragma mark - TrackerSettingsWindow 74 75 76 TrackerSettingsWindow::TrackerSettingsWindow() 77 : 78 BWindow(BRect(80, 80, 450, 350), B_TRANSLATE("Tracker preferences"), 79 B_TITLED_WINDOW, B_NOT_MINIMIZABLE | B_NOT_RESIZABLE 80 | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE 81 | B_AUTO_UPDATE_SIZE_LIMITS) 82 { 83 fSettingsTypeListView = new BListView("List View", 84 B_SINGLE_SELECTION_LIST); 85 86 BScrollView* scrollView = new BScrollView("scrollview", 87 fSettingsTypeListView, B_FRAME_EVENTS | B_WILL_DRAW, false, true); 88 89 fDefaultsButton = new BButton("Defaults", B_TRANSLATE("Defaults"), 90 new BMessage(kDefaultsButtonPressed)); 91 fDefaultsButton->SetEnabled(false); 92 93 fRevertButton = new BButton("Revert", B_TRANSLATE("Revert"), 94 new BMessage(kRevertButtonPressed)); 95 fRevertButton->SetEnabled(false); 96 97 fSettingsContainerBox = new BBox("SettingsContainerBox"); 98 99 // const float spacing = be_control_look->DefaultItemSpacing(); 100 101 BLayoutBuilder::Group<>(this) 102 .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) 103 .Add(scrollView) 104 .AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING) 105 .Add(fSettingsContainerBox) 106 .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) 107 .Add(fDefaultsButton) 108 .Add(fRevertButton) 109 .AddGlue() 110 .End() 111 .End() 112 .SetInsets(B_USE_WINDOW_SPACING) 113 .End(); 114 115 fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Desktop"), 116 new DesktopSettingsView()), kDesktopSettings); 117 fSettingsTypeListView->AddItem(new SettingsItem(B_TRANSLATE("Windows"), 118 new WindowsSettingsView()), kWindowsSettings); 119 fSettingsTypeListView->AddItem(new SettingsItem( 120 B_TRANSLATE("Volume icons"), new SpaceBarSettingsView()), 121 kSpaceBarSettings); 122 fSettingsTypeListView->AddItem(new SettingsItem( 123 B_TRANSLATE("Disk mount"), new AutomountSettingsPanel()), 124 kAutomountSettings); 125 126 // constraint the listview width so that the longest item fits 127 float width = 0; 128 fSettingsTypeListView->GetPreferredSize(&width, NULL); 129 width += B_V_SCROLL_BAR_WIDTH; 130 fSettingsTypeListView->SetExplicitMinSize(BSize(width, 0)); 131 fSettingsTypeListView->SetExplicitMaxSize(BSize(width, B_SIZE_UNLIMITED)); 132 133 fSettingsTypeListView->SetSelectionMessage( 134 new BMessage(kSettingsViewChanged)); 135 fSettingsTypeListView->Select(0); 136 } 137 138 139 bool 140 TrackerSettingsWindow::QuitRequested() 141 { 142 if (IsHidden()) 143 return true; 144 145 Hide(); 146 return false; 147 } 148 149 150 void 151 TrackerSettingsWindow::MessageReceived(BMessage* message) 152 { 153 switch (message->what) { 154 case kSettingsContentsModified: 155 _HandleChangedContents(); 156 break; 157 158 case kDefaultsButtonPressed: 159 _HandlePressedDefaultsButton(); 160 break; 161 162 case kRevertButtonPressed: 163 _HandlePressedRevertButton(); 164 break; 165 166 case kSettingsViewChanged: 167 _HandleChangedSettingsView(); 168 break; 169 170 default: 171 _inherited::MessageReceived(message); 172 break; 173 } 174 } 175 176 177 void 178 TrackerSettingsWindow::Show() 179 { 180 if (Lock()) { 181 int32 itemCount = fSettingsTypeListView->CountItems(); 182 183 for (int32 i = 0; i < itemCount; i++) { 184 _ViewAt(i)->RecordRevertSettings(); 185 _ViewAt(i)->ShowCurrentSettings(); 186 } 187 188 fSettingsTypeListView->Invalidate(); 189 190 _UpdateButtons(); 191 192 Unlock(); 193 } 194 195 if (IsHidden()) { 196 // move to current workspace 197 SetWorkspaces(B_CURRENT_WORKSPACE); 198 } 199 200 _inherited::Show(); 201 } 202 203 204 void 205 TrackerSettingsWindow::ShowPage(SettingsPage page) 206 { 207 fSettingsTypeListView->Select(page); 208 } 209 210 211 SettingsView* 212 TrackerSettingsWindow::_ViewAt(int32 i) 213 { 214 if (!Lock()) 215 return NULL; 216 217 SettingsItem* item = dynamic_cast<SettingsItem*>( 218 fSettingsTypeListView->ItemAt(i)); 219 220 Unlock(); 221 222 return item != NULL ? item->View() : NULL; 223 } 224 225 226 void 227 TrackerSettingsWindow::_HandleChangedContents() 228 { 229 fSettingsTypeListView->Invalidate(); 230 _UpdateButtons(); 231 232 TrackerSettings().SaveSettings(false); 233 } 234 235 236 void 237 TrackerSettingsWindow::_UpdateButtons() 238 { 239 int32 itemCount = fSettingsTypeListView->CountItems(); 240 241 bool defaultable = false; 242 bool revertable = false; 243 244 for (int32 i = 0; i < itemCount; i++) { 245 defaultable |= _ViewAt(i)->IsDefaultable(); 246 revertable |= _ViewAt(i)->IsRevertable(); 247 } 248 249 fDefaultsButton->SetEnabled(defaultable); 250 fRevertButton->SetEnabled(revertable); 251 } 252 253 254 void 255 TrackerSettingsWindow::_HandlePressedDefaultsButton() 256 { 257 int32 itemCount = fSettingsTypeListView->CountItems(); 258 259 for (int32 i = 0; i < itemCount; i++) { 260 if (_ViewAt(i)->IsDefaultable()) 261 _ViewAt(i)->SetDefaults(); 262 } 263 264 _HandleChangedContents(); 265 } 266 267 268 void 269 TrackerSettingsWindow::_HandlePressedRevertButton() 270 { 271 int32 itemCount = fSettingsTypeListView->CountItems(); 272 273 for (int32 i = 0; i < itemCount; i++) { 274 if (_ViewAt(i)->IsRevertable()) 275 _ViewAt(i)->Revert(); 276 } 277 278 _HandleChangedContents(); 279 } 280 281 282 void 283 TrackerSettingsWindow::_HandleChangedSettingsView() 284 { 285 int32 currentSelection = fSettingsTypeListView->CurrentSelection(); 286 if (currentSelection < 0) 287 return; 288 289 BView* oldView = fSettingsContainerBox->ChildAt(0); 290 291 if (oldView != NULL) 292 oldView->RemoveSelf(); 293 294 SettingsItem* selectedItem = dynamic_cast<SettingsItem*>( 295 fSettingsTypeListView->ItemAt(currentSelection)); 296 if (selectedItem != NULL) { 297 fSettingsContainerBox->SetLabel(selectedItem->Text()); 298 299 BView* view = selectedItem->View(); 300 view->SetViewColor(fSettingsContainerBox->ViewColor()); 301 view->Hide(); 302 fSettingsContainerBox->AddChild(view); 303 304 view->Show(); 305 } 306 } 307 308 309 // #pragma mark - SettingsItem 310 311 312 SettingsItem::SettingsItem(const char* label, SettingsView* view) 313 : 314 BStringItem(label), 315 fSettingsView(view) 316 { 317 } 318 319 320 void 321 SettingsItem::DrawItem(BView* owner, BRect rect, bool drawEverything) 322 { 323 if (fSettingsView) { 324 bool isRevertable = fSettingsView->IsRevertable(); 325 bool isSelected = IsSelected(); 326 327 if (isSelected || drawEverything) { 328 rgb_color color; 329 if (isSelected) 330 color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR); 331 else 332 color = owner->ViewColor(); 333 334 owner->SetHighColor(color); 335 owner->SetLowColor(color); 336 owner->FillRect(rect); 337 } 338 339 if (isRevertable) 340 owner->SetFont(be_bold_font); 341 else 342 owner->SetFont(be_plain_font); 343 344 if (isSelected) 345 owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR)); 346 else 347 owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR)); 348 349 font_height fheight; 350 owner->GetFontHeight(&fheight); 351 352 owner->DrawString(Text(), 353 BPoint(rect.left + be_control_look->DefaultLabelSpacing(), 354 rect.top + fheight.ascent + 2 + floorf(fheight.leading / 2))); 355 356 owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR)); 357 owner->SetLowColor(owner->ViewColor()); 358 } 359 } 360 361 362 SettingsView* 363 SettingsItem::View() 364 { 365 return fSettingsView; 366 } 367