1f8cc2a74SStefano Ceccherini /* 27974d3dcSStephan Aßmus * Copyright 2007-2009, Haiku, Inc. All rights reserved. 3f8cc2a74SStefano Ceccherini * Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net 4f8cc2a74SStefano Ceccherini * Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai. 5f8cc2a74SStefano Ceccherini * All rights reserved. Distributed under the terms of the MIT license. 6f8cc2a74SStefano Ceccherini */ 7f8cc2a74SStefano Ceccherini 8f8cc2a74SStefano Ceccherini 9f8cc2a74SStefano Ceccherini #include "AppearPrefView.h" 10f8cc2a74SStefano Ceccherini #include "PrefHandler.h" 11f8cc2a74SStefano Ceccherini #include "PrefWindow.h" 12f8cc2a74SStefano Ceccherini #include "TermConst.h" 13f8cc2a74SStefano Ceccherini 14f8cc2a74SStefano Ceccherini #include <Alert.h> 15f8cc2a74SStefano Ceccherini #include <Box.h> 16f8cc2a74SStefano Ceccherini #include <Button.h> 178989d8a5SStephan Aßmus #include <Catalog.h> 18f8cc2a74SStefano Ceccherini #include <FilePanel.h> 19c1ca410bSStefano Ceccherini #include <GroupLayoutBuilder.h> 20c1ca410bSStefano Ceccherini #include <LayoutBuilder.h> 218989d8a5SStephan Aßmus #include <Locale.h> 22f8cc2a74SStefano Ceccherini #include <Path.h> 23f8cc2a74SStefano Ceccherini 24f8cc2a74SStefano Ceccherini #include <stdio.h> 25f8cc2a74SStefano Ceccherini 26546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT 27546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "Terminal PrefWindow" 28f8cc2a74SStefano Ceccherini 29a5ec0eebSStefano Ceccherini PrefWindow::PrefWindow(const BMessenger& messenger) 3071302e7cSJonas Sundström : 3171302e7cSJonas Sundström BWindow(BRect(0, 0, 375, 185), "Terminal settings", 32f8cc2a74SStefano Ceccherini B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 33c1ca410bSStefano Ceccherini B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS), 3456e415ddSStefano Ceccherini fPreviousPref(new PrefHandler(PrefHandler::Default())), 35f8cc2a74SStefano Ceccherini fSavePanel(NULL), 36f8cc2a74SStefano Ceccherini fDirty(false), 37a5ec0eebSStefano Ceccherini fTerminalMessenger(messenger) 38f8cc2a74SStefano Ceccherini { 39560ff447SJonas Sundström BString app = B_TRANSLATE_SYSTEM_NAME("Terminal"); 4071302e7cSJonas Sundström BString title = B_TRANSLATE_COMMENT("%app% settings", "window title"); 4171302e7cSJonas Sundström title.ReplaceFirst("%app%", app); 4271302e7cSJonas Sundström SetTitle(title); 4371302e7cSJonas Sundström 44c1ca410bSStefano Ceccherini BLayoutBuilder::Group<>(this, B_VERTICAL) 4564fd88f5SStefano Ceccherini .AddGroup(B_VERTICAL) 46c1ca410bSStefano Ceccherini .SetInsets(10, 10, 10, 10) 47cca6cae6SAdrien Destugues .Add(fAppearanceView = new AppearancePrefView( 48cca6cae6SAdrien Destugues B_TRANSLATE("Appearance"), fTerminalMessenger)) 49c1ca410bSStefano Ceccherini .AddGroup(B_HORIZONTAL) 50c1ca410bSStefano Ceccherini .Add(fSaveAsFileButton = new BButton("savebutton", 516c514d3eSMatt Madia B_TRANSLATE("Save to file" B_UTF8_ELLIPSIS), 52c1ca410bSStefano Ceccherini new BMessage(MSG_SAVEAS_PRESSED), B_WILL_DRAW)) 53c1ca410bSStefano Ceccherini .AddGlue() 54c1ca410bSStefano Ceccherini .Add(fRevertButton = new BButton("revertbutton", 55cca6cae6SAdrien Destugues B_TRANSLATE("Revert"), new BMessage(MSG_REVERT_PRESSED), 56c1ca410bSStefano Ceccherini B_WILL_DRAW)) 57*defb8292SAdrien Destugues .Add(fDefaultsButton = new BButton("okbutton", 58*defb8292SAdrien Destugues B_TRANSLATE("Defaults"), new BMessage(MSG_DEFAULTS_PRESSED), 59*defb8292SAdrien Destugues B_WILL_DRAW)) 60c1ca410bSStefano Ceccherini .End() 61c1ca410bSStefano Ceccherini .End(); 62f8cc2a74SStefano Ceccherini 63*defb8292SAdrien Destugues fRevertButton->SetEnabled(fDirty); 64f8cc2a74SStefano Ceccherini 65c4eae2c1SStefano Ceccherini AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 66c4eae2c1SStefano Ceccherini AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); 67f8cc2a74SStefano Ceccherini 68c1ca410bSStefano Ceccherini CenterOnScreen(); 69f8cc2a74SStefano Ceccherini Show(); 70f8cc2a74SStefano Ceccherini } 71f8cc2a74SStefano Ceccherini 72f8cc2a74SStefano Ceccherini 737e99a484SStefano Ceccherini PrefWindow::~PrefWindow() 74f8cc2a74SStefano Ceccherini { 75f8cc2a74SStefano Ceccherini } 76f8cc2a74SStefano Ceccherini 77f8cc2a74SStefano Ceccherini 78f8cc2a74SStefano Ceccherini void 797e99a484SStefano Ceccherini PrefWindow::Quit() 80f8cc2a74SStefano Ceccherini { 81a5ec0eebSStefano Ceccherini fTerminalMessenger.SendMessage(MSG_PREF_CLOSED); 8256e415ddSStefano Ceccherini delete fPreviousPref; 83f8cc2a74SStefano Ceccherini delete fSavePanel; 84f8cc2a74SStefano Ceccherini BWindow::Quit(); 85f8cc2a74SStefano Ceccherini } 86f8cc2a74SStefano Ceccherini 87f8cc2a74SStefano Ceccherini 88f8cc2a74SStefano Ceccherini bool 897e99a484SStefano Ceccherini PrefWindow::QuitRequested() 90f8cc2a74SStefano Ceccherini { 91*defb8292SAdrien Destugues if (fDirty) 92f8cc2a74SStefano Ceccherini _Save(); 93f8cc2a74SStefano Ceccherini 94f8cc2a74SStefano Ceccherini return true; 95f8cc2a74SStefano Ceccherini } 96f8cc2a74SStefano Ceccherini 97f8cc2a74SStefano Ceccherini 98f8cc2a74SStefano Ceccherini void 997e99a484SStefano Ceccherini PrefWindow::_SaveAs() 100f8cc2a74SStefano Ceccherini { 10106a28b54SIngo Weinhold if (!fSavePanel) { 10206a28b54SIngo Weinhold BMessenger messenger(this); 10306a28b54SIngo Weinhold fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger); 10406a28b54SIngo Weinhold } 105f8cc2a74SStefano Ceccherini 106f8cc2a74SStefano Ceccherini fSavePanel->Show(); 107f8cc2a74SStefano Ceccherini } 108f8cc2a74SStefano Ceccherini 109f8cc2a74SStefano Ceccherini 110f8cc2a74SStefano Ceccherini void 1117e99a484SStefano Ceccherini PrefWindow::_SaveRequested(BMessage *msg) 112f8cc2a74SStefano Ceccherini { 113f8cc2a74SStefano Ceccherini entry_ref dirref; 114f8cc2a74SStefano Ceccherini const char* filename; 115f8cc2a74SStefano Ceccherini 116f8cc2a74SStefano Ceccherini msg->FindRef("directory", &dirref); 117f8cc2a74SStefano Ceccherini msg->FindString("name", &filename); 118f8cc2a74SStefano Ceccherini 119f8cc2a74SStefano Ceccherini BDirectory dir(&dirref); 120f8cc2a74SStefano Ceccherini BPath path(&dir, filename); 121f8cc2a74SStefano Ceccherini 122*defb8292SAdrien Destugues PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE, 123*defb8292SAdrien Destugues TERM_SIGNATURE); 124f8cc2a74SStefano Ceccherini } 125f8cc2a74SStefano Ceccherini 126f8cc2a74SStefano Ceccherini 127f8cc2a74SStefano Ceccherini void 1287e99a484SStefano Ceccherini PrefWindow::_Save() 129f8cc2a74SStefano Ceccherini { 13056e415ddSStefano Ceccherini delete fPreviousPref; 13156e415ddSStefano Ceccherini fPreviousPref = new PrefHandler(PrefHandler::Default()); 132f8cc2a74SStefano Ceccherini 133411d718eSStefano Ceccherini PrefHandler::Default()->SaveDefaultAsText(); 134f8cc2a74SStefano Ceccherini fDirty = false; 135f8cc2a74SStefano Ceccherini } 136f8cc2a74SStefano Ceccherini 137f8cc2a74SStefano Ceccherini 138f8cc2a74SStefano Ceccherini void 1397e99a484SStefano Ceccherini PrefWindow::_Revert() 140f8cc2a74SStefano Ceccherini { 14156e415ddSStefano Ceccherini if (fDirty) { 14256e415ddSStefano Ceccherini PrefHandler::SetDefault(new PrefHandler(fPreviousPref)); 143f8cc2a74SStefano Ceccherini 144a5ec0eebSStefano Ceccherini fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED); 145a5ec0eebSStefano Ceccherini fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED); 146a949fa2aSIngo Weinhold fTerminalMessenger.SendMessage(MSG_TAB_TITLE_SETTING_CHANGED); 147a949fa2aSIngo Weinhold fTerminalMessenger.SendMessage(MSG_WINDOW_TITLE_SETTING_CHANGED); 148f8cc2a74SStefano Ceccherini 149cca6cae6SAdrien Destugues fAppearanceView->Revert(); 150cca6cae6SAdrien Destugues 151f8cc2a74SStefano Ceccherini fDirty = false; 152*defb8292SAdrien Destugues fRevertButton->SetEnabled(fDirty); 153f8cc2a74SStefano Ceccherini } 15456e415ddSStefano Ceccherini } 155f8cc2a74SStefano Ceccherini 156f8cc2a74SStefano Ceccherini 157f8cc2a74SStefano Ceccherini void 1587e99a484SStefano Ceccherini PrefWindow::MessageReceived(BMessage* msg) 159f8cc2a74SStefano Ceccherini { 160f8cc2a74SStefano Ceccherini switch (msg->what) { 161f8cc2a74SStefano Ceccherini case MSG_SAVEAS_PRESSED: 162f8cc2a74SStefano Ceccherini _SaveAs(); 163f8cc2a74SStefano Ceccherini break; 164f8cc2a74SStefano Ceccherini 165f8cc2a74SStefano Ceccherini case MSG_REVERT_PRESSED: 166f8cc2a74SStefano Ceccherini _Revert(); 167f8cc2a74SStefano Ceccherini break; 168f8cc2a74SStefano Ceccherini 169*defb8292SAdrien Destugues case MSG_DEFAULTS_PRESSED: 170*defb8292SAdrien Destugues PrefHandler::SetDefault(new PrefHandler(false)); 171*defb8292SAdrien Destugues fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED); 172*defb8292SAdrien Destugues fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED); 173*defb8292SAdrien Destugues fTerminalMessenger.SendMessage(MSG_TAB_TITLE_SETTING_CHANGED); 174*defb8292SAdrien Destugues fTerminalMessenger.SendMessage(MSG_WINDOW_TITLE_SETTING_CHANGED); 175*defb8292SAdrien Destugues fAppearanceView->Revert(); 176*defb8292SAdrien Destugues // fallthrough 177*defb8292SAdrien Destugues 178f8cc2a74SStefano Ceccherini case MSG_PREF_MODIFIED: 179f8cc2a74SStefano Ceccherini fDirty = true; 180*defb8292SAdrien Destugues fRevertButton->SetEnabled(fDirty); 181f8cc2a74SStefano Ceccherini break; 182f8cc2a74SStefano Ceccherini 183f8cc2a74SStefano Ceccherini case B_SAVE_REQUESTED: 184f8cc2a74SStefano Ceccherini _SaveRequested(msg); 185f8cc2a74SStefano Ceccherini break; 186f8cc2a74SStefano Ceccherini 187f8cc2a74SStefano Ceccherini default: 188f8cc2a74SStefano Ceccherini BWindow::MessageReceived(msg); 189f8cc2a74SStefano Ceccherini break; 190f8cc2a74SStefano Ceccherini } 191f8cc2a74SStefano Ceccherini } 192