1 /* 2 * Copyright 2007-2008, Haiku, Inc. 3 * Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net 4 * Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai. 5 * All rights reserved. Distributed under the terms of the MIT license. 6 */ 7 8 9 #include "AppearPrefView.h" 10 #include "PrefHandler.h" 11 #include "PrefWindow.h" 12 #include "PrefView.h" 13 #include "TermConst.h" 14 15 #include <Alert.h> 16 #include <Box.h> 17 #include <Button.h> 18 #include <FilePanel.h> 19 #include <Path.h> 20 #include <Screen.h> 21 22 #include <stdio.h> 23 24 25 PrefWindow::PrefWindow(BMessenger messenger) 26 : BWindow(_CenteredRect(BRect(0, 0, 350, 215)), "Terminal Preferences", 27 B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 28 B_NOT_RESIZABLE|B_NOT_ZOOMABLE), 29 fPreviousPref(new PrefHandler(PrefHandler::Default())), 30 fSavePanel(NULL), 31 fDirty(false), 32 fPrefDlgMessenger(messenger) 33 { 34 BView *top = new BView(Bounds(), "topview", B_FOLLOW_NONE, B_WILL_DRAW); 35 top->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 36 AddChild(top); 37 38 BRect rect = top->Bounds(); 39 rect.bottom *= .75; 40 AppearancePrefView *prefView 41 = new AppearancePrefView(rect, "Appearance", fPrefDlgMessenger); 42 top->AddChild(prefView); 43 44 fSaveAsFileButton = new BButton(BRect(0, 0, 1, 1), "savebutton", "Save to File" B_UTF8_ELLIPSIS, 45 new BMessage(MSG_SAVEAS_PRESSED), B_FOLLOW_TOP, B_WILL_DRAW); 46 fSaveAsFileButton->ResizeToPreferred(); 47 fSaveAsFileButton->MoveTo(5, top->Bounds().Height() - 5 - 48 fSaveAsFileButton->Bounds().Height()); 49 top->AddChild(fSaveAsFileButton); 50 51 fSaveButton = new BButton(BRect(0, 0, 1, 1), "okbutton", "OK", 52 new BMessage(MSG_SAVE_PRESSED), B_FOLLOW_TOP, B_WILL_DRAW); 53 fSaveButton->ResizeToPreferred(); 54 fSaveButton->MoveTo(top->Bounds().Width() - 5 - fSaveButton->Bounds().Width(), 55 top->Bounds().Height() - 5 - fSaveButton->Bounds().Height()); 56 fSaveButton->MakeDefault(true); 57 top->AddChild(fSaveButton); 58 59 fRevertButton = new BButton(BRect(0, 0, 1, 1), "revertbutton", 60 "Cancel", new BMessage(MSG_REVERT_PRESSED), B_FOLLOW_TOP, B_WILL_DRAW); 61 fRevertButton->ResizeToPreferred(); 62 fRevertButton->MoveTo(fSaveButton->Frame().left - 10 - 63 fRevertButton->Bounds().Width(), top->Bounds().Height() - 5 - 64 fRevertButton->Bounds().Height()); 65 top->AddChild(fRevertButton); 66 67 AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 68 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 69 70 Show(); 71 } 72 73 74 PrefWindow::~PrefWindow() 75 { 76 } 77 78 79 void 80 PrefWindow::Quit() 81 { 82 fPrefDlgMessenger.SendMessage(MSG_PREF_CLOSED); 83 delete fPreviousPref; 84 delete fSavePanel; 85 BWindow::Quit(); 86 } 87 88 89 bool 90 PrefWindow::QuitRequested() 91 { 92 if (!fDirty) 93 return true; 94 95 BAlert *alert = new BAlert("", "Save changes to this preference panel?", 96 "Cancel", "Don't Save", "Save", 97 B_WIDTH_AS_USUAL, B_OFFSET_SPACING, 98 B_WARNING_ALERT); 99 alert->SetShortcut(0, B_ESCAPE); 100 alert->SetShortcut(1, 'd'); 101 alert->SetShortcut(2, 's'); 102 103 int32 index = alert->Go(); 104 if (index == 0) 105 return false; 106 107 if (index == 2) 108 _Save(); 109 110 return true; 111 } 112 113 114 void 115 PrefWindow::_SaveAs() 116 { 117 if (!fSavePanel) { 118 BMessenger messenger(this); 119 fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger); 120 } 121 122 fSavePanel->Show(); 123 } 124 125 126 void 127 PrefWindow::_SaveRequested(BMessage *msg) 128 { 129 entry_ref dirref; 130 const char *filename; 131 132 msg->FindRef("directory", &dirref); 133 msg->FindString("name", &filename); 134 135 BDirectory dir(&dirref); 136 BPath path(&dir, filename); 137 138 PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE, TERM_SIGNATURE); 139 } 140 141 142 void 143 PrefWindow::_Save() 144 { 145 delete fPreviousPref; 146 fPreviousPref = new PrefHandler(PrefHandler::Default()); 147 148 BPath path; 149 if (PrefHandler::GetDefaultPath(path) == B_OK) { 150 PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE); 151 fDirty = false; 152 } 153 } 154 155 156 void 157 PrefWindow::_Revert() 158 { 159 if (fDirty) { 160 PrefHandler::SetDefault(new PrefHandler(fPreviousPref)); 161 162 fPrefDlgMessenger.SendMessage(MSG_HALF_FONT_CHANGED); 163 fPrefDlgMessenger.SendMessage(MSG_COLOR_CHANGED); 164 fPrefDlgMessenger.SendMessage(MSG_INPUT_METHOD_CHANGED); 165 166 fDirty = false; 167 } 168 } 169 170 171 void 172 PrefWindow::MessageReceived(BMessage *msg) 173 { 174 switch (msg->what) { 175 case MSG_SAVE_PRESSED: 176 _Save(); 177 PostMessage(B_QUIT_REQUESTED); 178 break; 179 180 case MSG_SAVEAS_PRESSED: 181 _SaveAs(); 182 break; 183 184 case MSG_REVERT_PRESSED: 185 _Revert(); 186 PostMessage(B_QUIT_REQUESTED); 187 break; 188 189 case MSG_PREF_MODIFIED: 190 fDirty = true; 191 break; 192 193 case B_SAVE_REQUESTED: 194 _SaveRequested(msg); 195 break; 196 197 default: 198 BWindow::MessageReceived(msg); 199 break; 200 } 201 } 202 203 204 BRect 205 PrefWindow::_CenteredRect(BRect rect) 206 { 207 BRect screenRect = BScreen().Frame(); 208 209 screenRect.InsetBy(10,10); 210 211 float x = screenRect.left + (screenRect.Width() - rect.Width()) / 2; 212 float y = screenRect.top + (screenRect.Height() - rect.Height()) / 3; 213 214 rect.OffsetTo(x, y); 215 216 return rect; 217 } 218