1/* 2 * Copyright 2010, Haiku, Inc. All Rights Reserved. 3 * Copyright 2009, Pier Luigi Fiorini. 4 * Distributed under the terms of the MIT License. 5 * 6 * Authors: 7 * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com 8 */ 9 10#include <stdio.h> 11#include <stdlib.h> 12 13#include <Alert.h> 14#include <Catalog.h> 15#include <Directory.h> 16#include <Message.h> 17#include <FindDirectory.h> 18#include <GroupLayout.h> 19#include <GridLayoutBuilder.h> 20#include <SpaceLayoutItem.h> 21#include <TextControl.h> 22#include <Menu.h> 23#include <MenuItem.h> 24#include <MenuField.h> 25#include <Mime.h> 26#include <Node.h> 27#include <notification/Notifications.h> 28#include <Path.h> 29 30#include "DisplayView.h" 31#include "SettingsHost.h" 32 33 34#undef B_TRANSLATION_CONTEXT 35#define B_TRANSLATION_CONTEXT "DisplayView" 36 37 38DisplayView::DisplayView(SettingsHost* host) 39 : 40 SettingsPane("display", host) 41{ 42 // Window width 43 fWindowWidth = new BTextControl(B_TRANSLATE("Window width:"), NULL, 44 new BMessage(kSettingChanged)); 45 46 // Icon size 47 fIconSize = new BMenu("iconSize"); 48 fIconSize->AddItem(new BMenuItem(B_TRANSLATE("Mini icon"), 49 new BMessage(kSettingChanged))); 50 fIconSize->AddItem(new BMenuItem(B_TRANSLATE("Large icon"), 51 new BMessage(kSettingChanged))); 52 fIconSize->SetLabelFromMarked(true); 53 fIconSizeField = new BMenuField(B_TRANSLATE("Icon size:"), fIconSize); 54 55 // Load settings 56 Load(); 57 58 // Calculate inset 59 float inset = ceilf(be_plain_font->Size() * 0.7f); 60 61 SetLayout(new BGroupLayout(B_VERTICAL)); 62 AddChild(BGridLayoutBuilder(inset, inset) 63 .Add(fWindowWidth->CreateLabelLayoutItem(), 0, 0) 64 .Add(fWindowWidth->CreateTextViewLayoutItem(), 1, 0) 65 .Add(fIconSizeField->CreateLabelLayoutItem(), 0, 1) 66 .Add(fIconSizeField->CreateMenuBarLayoutItem(), 1, 1) 67 .Add(BSpaceLayoutItem::CreateGlue(), 0, 2, 2, 1) 68 .SetInsets(inset, inset, inset, inset) 69 ); 70} 71 72 73void 74DisplayView::AttachedToWindow() 75{ 76 fWindowWidth->SetTarget(this); 77 fIconSize->SetTargetForItems(this); 78} 79 80 81void 82DisplayView::MessageReceived(BMessage* msg) 83{ 84 switch (msg->what) { 85 case kSettingChanged: 86 SettingsPane::MessageReceived(msg); 87 break; 88 default: 89 BView::MessageReceived(msg); 90 } 91} 92 93 94status_t 95DisplayView::Load() 96{ 97 BPath path; 98 99 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) 100 return B_ERROR; 101 102 path.Append(kSettingsDirectory); 103 104 if (create_directory(path.Path(), 0755) != B_OK) { 105 BAlert* alert = new BAlert("", 106 B_TRANSLATE("There was a problem saving the preferences.\n" 107 "It's possible you don't have write access to the " 108 "settings directory."), B_TRANSLATE("OK"), NULL, NULL, 109 B_WIDTH_AS_USUAL, B_STOP_ALERT); 110 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 111 (void)alert->Go(); 112 } 113 114 path.Append(kDisplaySettings); 115 116 BFile file(path.Path(), B_READ_ONLY); 117 BMessage settings; 118 settings.Unflatten(&file); 119 120 char buffer[255]; 121 int32 setting; 122 BMenuItem* item = NULL; 123 124 float width; 125 if (settings.FindFloat(kWidthName, &width) != B_OK) 126 width = kDefaultWidth; 127 (void)sprintf(buffer, "%.2f", width); 128 fWindowWidth->SetText(buffer); 129 130 icon_size iconSize; 131 if (settings.FindInt32(kIconSizeName, &setting) != B_OK) 132 iconSize = kDefaultIconSize; 133 else 134 iconSize = (icon_size)setting; 135 if (iconSize == B_MINI_ICON) 136 item = fIconSize->ItemAt(0); 137 else 138 item = fIconSize->ItemAt(1); 139 if (item) 140 item->SetMarked(true); 141 142 return B_OK; 143} 144 145 146status_t 147DisplayView::Save() 148{ 149 BPath path; 150 151 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) 152 return B_ERROR; 153 154 path.Append(kSettingsDirectory); 155 path.Append(kDisplaySettings); 156 157 BMessage settings; 158 159 float width = atof(fWindowWidth->Text()); 160 settings.AddFloat(kWidthName, width); 161 162 icon_size iconSize = kDefaultIconSize; 163 switch (fIconSize->IndexOf(fIconSize->FindMarked())) { 164 case 0: 165 iconSize = B_MINI_ICON; 166 break; 167 default: 168 iconSize = B_LARGE_ICON; 169 } 170 settings.AddInt32(kIconSizeName, (int32)iconSize); 171 172 // Save settings file 173 BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 174 status_t ret = settings.Flatten(&file); 175 if (ret != B_OK) { 176 BAlert* alert = new BAlert("", 177 B_TRANSLATE("Can't save preferenes, you probably don't have " 178 "write access to the settings directory or the disk is full."), 179 B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT); 180 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 181 (void)alert->Go(); 182 return ret; 183 } 184 185 return B_OK; 186} 187 188 189status_t 190DisplayView::Revert() 191{ 192 return B_ERROR; 193} 194