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