btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / tracker / TrackerSettingsWindow.cpp
blobeb72193fc89ba4116f39c54b1f3c3652bb7c74e0
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
36 #include <Catalog.h>
37 #include <ControlLook.h>
38 #include <InterfaceDefs.h>
39 #include <LayoutBuilder.h>
40 #include <Locale.h>
41 #include <ScrollView.h>
43 #include "TrackerSettings.h"
44 #include "TrackerSettingsWindow.h"
47 namespace BPrivate {
49 class SettingsItem : public BStringItem {
50 public:
51 SettingsItem(const char* label, SettingsView* view);
53 void DrawItem(BView* owner, BRect rect, bool drawEverything);
55 SettingsView* View();
57 private:
58 SettingsView* fSettingsView;
61 } // namespace BPrivate
64 const uint32 kSettingsViewChanged = 'Svch';
65 const uint32 kDefaultsButtonPressed = 'Apbp';
66 const uint32 kRevertButtonPressed = 'Rebp';
69 #undef B_TRANSLATION_CONTEXT
70 #define B_TRANSLATION_CONTEXT "TrackerSettingsWindow"
73 // #pragma mark - TrackerSettingsWindow
76 TrackerSettingsWindow::TrackerSettingsWindow()
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)
83 fSettingsTypeListView = new BListView("List View",
84 B_SINGLE_SELECTION_LIST);
86 BScrollView* scrollView = new BScrollView("scrollview",
87 fSettingsTypeListView, B_FRAME_EVENTS | B_WILL_DRAW, false, true);
89 fDefaultsButton = new BButton("Defaults", B_TRANSLATE("Defaults"),
90 new BMessage(kDefaultsButtonPressed));
91 fDefaultsButton->SetEnabled(false);
93 fRevertButton = new BButton("Revert", B_TRANSLATE("Revert"),
94 new BMessage(kRevertButtonPressed));
95 fRevertButton->SetEnabled(false);
97 fSettingsContainerBox = new BBox("SettingsContainerBox");
99 // const float spacing = be_control_look->DefaultItemSpacing();
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();
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);
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));
133 fSettingsTypeListView->SetSelectionMessage(
134 new BMessage(kSettingsViewChanged));
135 fSettingsTypeListView->Select(0);
139 bool
140 TrackerSettingsWindow::QuitRequested()
142 if (IsHidden())
143 return true;
145 Hide();
146 return false;
150 void
151 TrackerSettingsWindow::MessageReceived(BMessage* message)
153 switch (message->what) {
154 case kSettingsContentsModified:
155 _HandleChangedContents();
156 break;
158 case kDefaultsButtonPressed:
159 _HandlePressedDefaultsButton();
160 break;
162 case kRevertButtonPressed:
163 _HandlePressedRevertButton();
164 break;
166 case kSettingsViewChanged:
167 _HandleChangedSettingsView();
168 break;
170 default:
171 _inherited::MessageReceived(message);
172 break;
177 void
178 TrackerSettingsWindow::Show()
180 if (Lock()) {
181 int32 itemCount = fSettingsTypeListView->CountItems();
183 for (int32 i = 0; i < itemCount; i++) {
184 _ViewAt(i)->RecordRevertSettings();
185 _ViewAt(i)->ShowCurrentSettings();
188 fSettingsTypeListView->Invalidate();
190 _UpdateButtons();
192 Unlock();
195 if (IsHidden()) {
196 // move to current workspace
197 SetWorkspaces(B_CURRENT_WORKSPACE);
200 _inherited::Show();
204 void
205 TrackerSettingsWindow::ShowPage(SettingsPage page)
207 fSettingsTypeListView->Select(page);
211 SettingsView*
212 TrackerSettingsWindow::_ViewAt(int32 i)
214 if (!Lock())
215 return NULL;
217 SettingsItem* item = dynamic_cast<SettingsItem*>(
218 fSettingsTypeListView->ItemAt(i));
220 Unlock();
222 return item != NULL ? item->View() : NULL;
226 void
227 TrackerSettingsWindow::_HandleChangedContents()
229 fSettingsTypeListView->Invalidate();
230 _UpdateButtons();
232 TrackerSettings().SaveSettings(false);
236 void
237 TrackerSettingsWindow::_UpdateButtons()
239 int32 itemCount = fSettingsTypeListView->CountItems();
241 bool defaultable = false;
242 bool revertable = false;
244 for (int32 i = 0; i < itemCount; i++) {
245 defaultable |= _ViewAt(i)->IsDefaultable();
246 revertable |= _ViewAt(i)->IsRevertable();
249 fDefaultsButton->SetEnabled(defaultable);
250 fRevertButton->SetEnabled(revertable);
254 void
255 TrackerSettingsWindow::_HandlePressedDefaultsButton()
257 int32 itemCount = fSettingsTypeListView->CountItems();
259 for (int32 i = 0; i < itemCount; i++) {
260 if (_ViewAt(i)->IsDefaultable())
261 _ViewAt(i)->SetDefaults();
264 _HandleChangedContents();
268 void
269 TrackerSettingsWindow::_HandlePressedRevertButton()
271 int32 itemCount = fSettingsTypeListView->CountItems();
273 for (int32 i = 0; i < itemCount; i++) {
274 if (_ViewAt(i)->IsRevertable())
275 _ViewAt(i)->Revert();
278 _HandleChangedContents();
282 void
283 TrackerSettingsWindow::_HandleChangedSettingsView()
285 int32 currentSelection = fSettingsTypeListView->CurrentSelection();
286 if (currentSelection < 0)
287 return;
289 BView* oldView = fSettingsContainerBox->ChildAt(0);
291 if (oldView != NULL)
292 oldView->RemoveSelf();
294 SettingsItem* selectedItem = dynamic_cast<SettingsItem*>(
295 fSettingsTypeListView->ItemAt(currentSelection));
296 if (selectedItem != NULL) {
297 fSettingsContainerBox->SetLabel(selectedItem->Text());
299 BView* view = selectedItem->View();
300 float tint = B_NO_TINT;
301 view->SetViewUIColor(fSettingsContainerBox->ViewUIColor(&tint), tint);
302 view->Hide();
303 fSettingsContainerBox->AddChild(view);
305 view->Show();
310 // #pragma mark - SettingsItem
313 SettingsItem::SettingsItem(const char* label, SettingsView* view)
315 BStringItem(label),
316 fSettingsView(view)
321 void
322 SettingsItem::DrawItem(BView* owner, BRect rect, bool drawEverything)
324 if (fSettingsView) {
325 bool isRevertable = fSettingsView->IsRevertable();
326 bool isSelected = IsSelected();
328 if (isSelected || drawEverything) {
329 rgb_color color;
330 if (isSelected)
331 color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
332 else
333 color = owner->ViewColor();
335 owner->SetHighColor(color);
336 owner->SetLowColor(color);
337 owner->FillRect(rect);
340 if (isRevertable)
341 owner->SetFont(be_bold_font);
342 else
343 owner->SetFont(be_plain_font);
345 if (isSelected)
346 owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
347 else
348 owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
350 font_height fheight;
351 owner->GetFontHeight(&fheight);
353 owner->DrawString(Text(),
354 BPoint(rect.left + be_control_look->DefaultLabelSpacing(),
355 rect.top + fheight.ascent + 2 + floorf(fheight.leading / 2)));
357 owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
358 owner->SetLowColor(owner->ViewColor());
363 SettingsView*
364 SettingsItem::View()
366 return fSettingsView;