1 /* 2 * Copyright 2007-2009, Haiku, Inc. All rights reserved. 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 "TermConst.h" 13 14 #include <Alert.h> 15 #include <Box.h> 16 #include <Button.h> 17 #include <FilePanel.h> 18 #include <GroupLayoutBuilder.h> 19 #include <LayoutBuilder.h> 20 #include <Path.h> 21 22 #include <stdio.h> 23 24 25 PrefWindow::PrefWindow(const BMessenger &messenger) 26 : BWindow(BRect(0, 0, 375, 185), "Terminal settings", 27 B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 28 B_NOT_RESIZABLE|B_NOT_ZOOMABLE|B_AUTO_UPDATE_SIZE_LIMITS), 29 fPreviousPref(new PrefHandler(PrefHandler::Default())), 30 fSavePanel(NULL), 31 fDirty(false), 32 fTerminalMessenger(messenger) 33 { 34 BLayoutBuilder::Group<>(this, B_VERTICAL) 35 .AddGroup(B_VERTICAL) 36 .SetInsets(10, 10, 10, 10) 37 .Add(new AppearancePrefView("Appearance", fTerminalMessenger)) 38 .AddGroup(B_HORIZONTAL) 39 .Add(fSaveAsFileButton = new BButton("savebutton", 40 "Save to file" B_UTF8_ELLIPSIS, 41 new BMessage(MSG_SAVEAS_PRESSED), B_WILL_DRAW)) 42 .AddGlue() 43 .Add(fRevertButton = new BButton("revertbutton", 44 "Cancel", new BMessage(MSG_REVERT_PRESSED), 45 B_WILL_DRAW)) 46 .Add(fSaveButton = new BButton("okbutton", "OK", 47 new BMessage(MSG_SAVE_PRESSED), B_WILL_DRAW)) 48 .End() 49 .End(); 50 51 fSaveButton->MakeDefault(true); 52 53 AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 54 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 55 56 CenterOnScreen(); 57 Show(); 58 } 59 60 61 PrefWindow::~PrefWindow() 62 { 63 } 64 65 66 void 67 PrefWindow::Quit() 68 { 69 fTerminalMessenger.SendMessage(MSG_PREF_CLOSED); 70 delete fPreviousPref; 71 delete fSavePanel; 72 BWindow::Quit(); 73 } 74 75 76 bool 77 PrefWindow::QuitRequested() 78 { 79 if (!fDirty) 80 return true; 81 82 BAlert *alert = new BAlert("", "Save changes to this settings panel?", 83 "Cancel", "Don't save", "Save", 84 B_WIDTH_AS_USUAL, B_OFFSET_SPACING, 85 B_WARNING_ALERT); 86 alert->SetShortcut(0, B_ESCAPE); 87 alert->SetShortcut(1, 'd'); 88 alert->SetShortcut(2, 's'); 89 90 int32 index = alert->Go(); 91 if (index == 0) 92 return false; 93 94 if (index == 2) 95 _Save(); 96 else 97 _Revert(); 98 99 return true; 100 } 101 102 103 void 104 PrefWindow::_SaveAs() 105 { 106 if (!fSavePanel) { 107 BMessenger messenger(this); 108 fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger); 109 } 110 111 fSavePanel->Show(); 112 } 113 114 115 void 116 PrefWindow::_SaveRequested(BMessage *msg) 117 { 118 entry_ref dirref; 119 const char *filename; 120 121 msg->FindRef("directory", &dirref); 122 msg->FindString("name", &filename); 123 124 BDirectory dir(&dirref); 125 BPath path(&dir, filename); 126 127 PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE, TERM_SIGNATURE); 128 } 129 130 131 void 132 PrefWindow::_Save() 133 { 134 delete fPreviousPref; 135 fPreviousPref = new PrefHandler(PrefHandler::Default()); 136 137 PrefHandler::Default()->SaveDefaultAsText(); 138 fDirty = false; 139 } 140 141 142 void 143 PrefWindow::_Revert() 144 { 145 if (fDirty) { 146 PrefHandler::SetDefault(new PrefHandler(fPreviousPref)); 147 148 fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED); 149 fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED); 150 151 fDirty = false; 152 } 153 } 154 155 156 void 157 PrefWindow::MessageReceived(BMessage *msg) 158 { 159 switch (msg->what) { 160 case MSG_SAVE_PRESSED: 161 _Save(); 162 PostMessage(B_QUIT_REQUESTED); 163 break; 164 165 case MSG_SAVEAS_PRESSED: 166 _SaveAs(); 167 break; 168 169 case MSG_REVERT_PRESSED: 170 _Revert(); 171 PostMessage(B_QUIT_REQUESTED); 172 break; 173 174 case MSG_PREF_MODIFIED: 175 fDirty = true; 176 break; 177 178 case B_SAVE_REQUESTED: 179 _SaveRequested(msg); 180 break; 181 182 default: 183 BWindow::MessageReceived(msg); 184 break; 185 } 186 } 187