19105df34Sshatty/*
27974d3dcSStephan Aßmus * Copyright 2007-2009, Haiku, Inc. All rights reserved.
33834597bSStefano Ceccherini * Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net
46c514d3eSMatt Madia * Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai.
53834597bSStefano Ceccherini * All rights reserved. Distributed under the terms of the MIT license.
69105df34Sshatty */
7723de034SAxel Dörfler
8723de034SAxel Dörfler
93e837bf6SStefano Ceccherini#include "AppearPrefView.h"
103e837bf6SStefano Ceccherini#include "PrefHandler.h"
11f8cc2a74SStefano Ceccherini#include "PrefWindow.h"
123e837bf6SStefano Ceccherini#include "TermConst.h"
133e837bf6SStefano Ceccherini
143e837bf6SStefano Ceccherini#include <Alert.h>
159105df34Sshatty#include <Box.h>
169105df34Sshatty#include <Button.h>
178989d8a5SStephan Aßmus#include <Catalog.h>
189105df34Sshatty#include <FilePanel.h>
19c1ca410bSStefano Ceccherini#include <GroupLayoutBuilder.h>
20c1ca410bSStefano Ceccherini#include <LayoutBuilder.h>
218989d8a5SStephan Aßmus#include <Locale.h>
223e837bf6SStefano Ceccherini#include <Path.h>
239105df34Sshatty
243e837bf6SStefano Ceccherini#include <stdio.h>
259105df34Sshatty
26546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
27546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "Terminal PrefWindow"
289105df34Sshatty
29004b2edbSStefano CeccheriniPrefWindow::PrefWindow(const BMessenger& messenger)
3071302e7cSJonas Sundström	:
3171302e7cSJonas Sundström	BWindow(BRect(0, 0, 375, 185), "Terminal settings",
32723de034SAxel Dörfler		B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
3371302e7cSJonas Sundström		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
3456e415ddSStefano Ceccherini	fPreviousPref(new PrefHandler(PrefHandler::Default())),
353e837bf6SStefano Ceccherini	fSavePanel(NULL),
363e837bf6SStefano Ceccherini	fDirty(false),
37a5ec0eebSStefano Ceccherini	fTerminalMessenger(messenger)
389105df34Sshatty{
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)
46d0ac6099SHumdinger		.SetInsets(B_USE_WINDOW_SPACING)
47cca6cae6SAdrien Destugues			.Add(fAppearanceView = new AppearancePrefView(
48cca6cae6SAdrien Destugues				B_TRANSLATE("Appearance"), fTerminalMessenger))
49c1ca410bSStefano Ceccherini			.AddGroup(B_HORIZONTAL)
507f0d2c98SJanus				.Add(fDefaultsButton = new BButton("defaultsbutton",
517f0d2c98SJanus					B_TRANSLATE("Defaults"), new BMessage(MSG_DEFAULTS_PRESSED),
527f0d2c98SJanus					B_WILL_DRAW))
53c1ca410bSStefano Ceccherini				.Add(fRevertButton = new BButton("revertbutton",
54cca6cae6SAdrien Destugues					B_TRANSLATE("Revert"), new BMessage(MSG_REVERT_PRESSED),
55c1ca410bSStefano Ceccherini					B_WILL_DRAW))
567f0d2c98SJanus				.AddGlue()
577f0d2c98SJanus				.Add(fSaveAsFileButton = new BButton("savebutton",
587f0d2c98SJanus					B_TRANSLATE("Save to file" B_UTF8_ELLIPSIS),
597f0d2c98SJanus					new BMessage(MSG_SAVEAS_PRESSED), B_WILL_DRAW))
60c1ca410bSStefano Ceccherini			.End()
61c1ca410bSStefano Ceccherini		.End();
626c514d3eSMatt Madia
63defb8292SAdrien Destugues	fRevertButton->SetEnabled(fDirty);
646c514d3eSMatt Madia
65c4eae2c1SStefano Ceccherini	AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
66c4eae2c1SStefano Ceccherini	AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
67723de034SAxel Dörfler
68c1ca410bSStefano Ceccherini	CenterOnScreen();
69ec87043eSDarkWyrm	Show();
709105df34Sshatty}
719105df34Sshatty
72723de034SAxel Dörfler
737e99a484SStefano CeccheriniPrefWindow::~PrefWindow()
749105df34Sshatty{
759105df34Sshatty}
769105df34Sshatty
77723de034SAxel Dörfler
789105df34Sshattyvoid
797e99a484SStefano CeccheriniPrefWindow::Quit()
809105df34Sshatty{
81a5ec0eebSStefano Ceccherini	fTerminalMessenger.SendMessage(MSG_PREF_CLOSED);
8256e415ddSStefano Ceccherini	delete fPreviousPref;
83ec87043eSDarkWyrm	delete fSavePanel;
84ec87043eSDarkWyrm	BWindow::Quit();
859105df34Sshatty}
86ec87043eSDarkWyrm
87723de034SAxel Dörfler
889105df34Sshattybool
897e99a484SStefano CeccheriniPrefWindow::QuitRequested()
909105df34Sshatty{
91defb8292SAdrien Destugues	if (fDirty)
923834597bSStefano Ceccherini		_Save();
93723de034SAxel Dörfler
94ec87043eSDarkWyrm	return true;
959105df34Sshatty}
96ec87043eSDarkWyrm
97723de034SAxel Dörfler
989105df34Sshattyvoid
997e99a484SStefano CeccheriniPrefWindow::_SaveAs()
1009105df34Sshatty{
10106a28b54SIngo Weinhold	if (!fSavePanel) {
10206a28b54SIngo Weinhold		BMessenger messenger(this);
10306a28b54SIngo Weinhold		fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger);
10406a28b54SIngo Weinhold	}
1056c514d3eSMatt Madia
106ec87043eSDarkWyrm	fSavePanel->Show();
1079105df34Sshatty}
108ec87043eSDarkWyrm
109723de034SAxel Dörfler
1109105df34Sshattyvoid
1117e99a484SStefano CeccheriniPrefWindow::_SaveRequested(BMessage *msg)
1129105df34Sshatty{
113ec87043eSDarkWyrm	entry_ref dirref;
114004b2edbSStefano Ceccherini	const char* filename;
115662f5284SAxel Dörfler
116ec87043eSDarkWyrm	msg->FindRef("directory", &dirref);
117ec87043eSDarkWyrm	msg->FindString("name", &filename);
118662f5284SAxel Dörfler
119ec87043eSDarkWyrm	BDirectory dir(&dirref);
120ec87043eSDarkWyrm	BPath path(&dir, filename);
121662f5284SAxel Dörfler
122defb8292SAdrien Destugues	PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE,
123defb8292SAdrien Destugues		TERM_SIGNATURE);
1249105df34Sshatty}
125ec87043eSDarkWyrm
126662f5284SAxel Dörfler
1279105df34Sshattyvoid
1287e99a484SStefano CeccheriniPrefWindow::_Save()
1299105df34Sshatty{
13056e415ddSStefano Ceccherini	delete fPreviousPref;
13156e415ddSStefano Ceccherini	fPreviousPref = new PrefHandler(PrefHandler::Default());
132662f5284SAxel Dörfler
133411d718eSStefano Ceccherini	PrefHandler::Default()->SaveDefaultAsText();
134411d718eSStefano Ceccherini	fDirty = false;
1359105df34Sshatty}
136ec87043eSDarkWyrm
137662f5284SAxel Dörfler
1389105df34Sshattyvoid
1397e99a484SStefano CeccheriniPrefWindow::_Revert()
1409105df34Sshatty{
14156e415ddSStefano Ceccherini	if (fDirty) {
14256e415ddSStefano Ceccherini		PrefHandler::SetDefault(new PrefHandler(fPreviousPref));
143662f5284SAxel Dörfler
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);
148662f5284SAxel Dörfler
149cca6cae6SAdrien Destugues		fAppearanceView->Revert();
150cca6cae6SAdrien Destugues
15156e415ddSStefano Ceccherini		fDirty = false;
152defb8292SAdrien Destugues		fRevertButton->SetEnabled(fDirty);
15356e415ddSStefano Ceccherini	}
1549105df34Sshatty}
155ec87043eSDarkWyrm
156723de034SAxel Dörfler
1579105df34Sshattyvoid
158004b2edbSStefano CeccheriniPrefWindow::MessageReceived(BMessage* msg)
1599105df34Sshatty{
160723de034SAxel Dörfler	switch (msg->what) {
161ec87043eSDarkWyrm		case MSG_SAVEAS_PRESSED:
1623834597bSStefano Ceccherini			_SaveAs();
163ec87043eSDarkWyrm			break;
1643834597bSStefano Ceccherini
165ec87043eSDarkWyrm		case MSG_REVERT_PRESSED:
1663834597bSStefano Ceccherini			_Revert();
167ec87043eSDarkWyrm			break;
1683834597bSStefano Ceccherini
169defb8292SAdrien Destugues		case MSG_DEFAULTS_PRESSED:
170defb8292SAdrien Destugues			PrefHandler::SetDefault(new PrefHandler(false));
171defb8292SAdrien Destugues			fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED);
172defb8292SAdrien Destugues			fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED);
173defb8292SAdrien Destugues			fTerminalMessenger.SendMessage(MSG_TAB_TITLE_SETTING_CHANGED);
174defb8292SAdrien Destugues			fTerminalMessenger.SendMessage(MSG_WINDOW_TITLE_SETTING_CHANGED);
175defb8292SAdrien Destugues			fAppearanceView->Revert();
176defb8292SAdrien Destugues			// fallthrough
177defb8292SAdrien Destugues
178ec87043eSDarkWyrm		case MSG_PREF_MODIFIED:
179ec87043eSDarkWyrm			fDirty = true;
180defb8292SAdrien Destugues			fRevertButton->SetEnabled(fDirty);
181ec87043eSDarkWyrm			break;
1823834597bSStefano Ceccherini
183ec87043eSDarkWyrm		case B_SAVE_REQUESTED:
1843834597bSStefano Ceccherini			_SaveRequested(msg);
185ec87043eSDarkWyrm			break;
1863834597bSStefano Ceccherini
187ec87043eSDarkWyrm		default:
188ec87043eSDarkWyrm			BWindow::MessageReceived(msg);
1893834597bSStefano Ceccherini			break;
190ec87043eSDarkWyrm	}
1919105df34Sshatty}
192