xref: /haiku/src/apps/terminal/PrefWindow.cpp (revision 6c514d3e3bfc9c33f9f4a2edd0b593d734046c8f)
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 
268989d8a5SStephan Aßmus #undef TR_CONTEXT
278989d8a5SStephan Aßmus #define TR_CONTEXT "Terminal PrefWindow"
28f8cc2a74SStefano Ceccherini 
29a5ec0eebSStefano Ceccherini PrefWindow::PrefWindow(const BMessenger &messenger)
30*6c514d3eSMatt Madia 	: BWindow(BRect(0, 0, 375, 185), B_TRANSLATE("Terminal settings"),
31f8cc2a74SStefano Ceccherini 		B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
32c1ca410bSStefano Ceccherini 		B_NOT_RESIZABLE|B_NOT_ZOOMABLE|B_AUTO_UPDATE_SIZE_LIMITS),
3356e415ddSStefano Ceccherini 	fPreviousPref(new PrefHandler(PrefHandler::Default())),
34f8cc2a74SStefano Ceccherini 	fSavePanel(NULL),
35f8cc2a74SStefano Ceccherini 	fDirty(false),
36a5ec0eebSStefano Ceccherini 	fTerminalMessenger(messenger)
37f8cc2a74SStefano Ceccherini {
38c1ca410bSStefano Ceccherini 	BLayoutBuilder::Group<>(this, B_VERTICAL)
3964fd88f5SStefano Ceccherini 		.AddGroup(B_VERTICAL)
40c1ca410bSStefano Ceccherini 		.SetInsets(10, 10, 10, 10)
41*6c514d3eSMatt Madia 			.Add(new AppearancePrefView(B_TRANSLATE("Appearance"),
42*6c514d3eSMatt Madia 				fTerminalMessenger))
43c1ca410bSStefano Ceccherini 			.AddGroup(B_HORIZONTAL)
44c1ca410bSStefano Ceccherini 				.Add(fSaveAsFileButton = new BButton("savebutton",
45*6c514d3eSMatt Madia 					B_TRANSLATE("Save to file" B_UTF8_ELLIPSIS),
46c1ca410bSStefano Ceccherini 					new BMessage(MSG_SAVEAS_PRESSED), B_WILL_DRAW))
47c1ca410bSStefano Ceccherini 				.AddGlue()
48c1ca410bSStefano Ceccherini 				.Add(fRevertButton = new BButton("revertbutton",
49*6c514d3eSMatt Madia 					B_TRANSLATE("Cancel"), new BMessage(MSG_REVERT_PRESSED),
50c1ca410bSStefano Ceccherini 					B_WILL_DRAW))
51*6c514d3eSMatt Madia 				.Add(fSaveButton = new BButton("okbutton", B_TRANSLATE("OK"),
52c1ca410bSStefano Ceccherini 					new BMessage(MSG_SAVE_PRESSED), B_WILL_DRAW))
53c1ca410bSStefano Ceccherini 			.End()
54c1ca410bSStefano Ceccherini 		.End();
55f8cc2a74SStefano Ceccherini 
56f8cc2a74SStefano Ceccherini 	fSaveButton->MakeDefault(true);
57f8cc2a74SStefano Ceccherini 
58c4eae2c1SStefano Ceccherini 	AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
59c4eae2c1SStefano Ceccherini 	AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
60f8cc2a74SStefano Ceccherini 
61c1ca410bSStefano Ceccherini 	CenterOnScreen();
62f8cc2a74SStefano Ceccherini 	Show();
63f8cc2a74SStefano Ceccherini }
64f8cc2a74SStefano Ceccherini 
65f8cc2a74SStefano Ceccherini 
667e99a484SStefano Ceccherini PrefWindow::~PrefWindow()
67f8cc2a74SStefano Ceccherini {
68f8cc2a74SStefano Ceccherini }
69f8cc2a74SStefano Ceccherini 
70f8cc2a74SStefano Ceccherini 
71f8cc2a74SStefano Ceccherini void
727e99a484SStefano Ceccherini PrefWindow::Quit()
73f8cc2a74SStefano Ceccherini {
74a5ec0eebSStefano Ceccherini 	fTerminalMessenger.SendMessage(MSG_PREF_CLOSED);
7556e415ddSStefano Ceccherini 	delete fPreviousPref;
76f8cc2a74SStefano Ceccherini 	delete fSavePanel;
77f8cc2a74SStefano Ceccherini 	BWindow::Quit();
78f8cc2a74SStefano Ceccherini }
79f8cc2a74SStefano Ceccherini 
80f8cc2a74SStefano Ceccherini 
81f8cc2a74SStefano Ceccherini bool
827e99a484SStefano Ceccherini PrefWindow::QuitRequested()
83f8cc2a74SStefano Ceccherini {
84f8cc2a74SStefano Ceccherini 	if (!fDirty)
85f8cc2a74SStefano Ceccherini 		return true;
86f8cc2a74SStefano Ceccherini 
87*6c514d3eSMatt Madia 	BAlert *alert = new BAlert("",
88*6c514d3eSMatt Madia 		B_TRANSLATE("Save changes to this settings panel?"),
89*6c514d3eSMatt Madia 		B_TRANSLATE("Cancel"), B_TRANSLATE("Don't save"), B_TRANSLATE("Save"),
90f8cc2a74SStefano Ceccherini 		B_WIDTH_AS_USUAL, B_OFFSET_SPACING,
91f8cc2a74SStefano Ceccherini 		B_WARNING_ALERT);
92f8cc2a74SStefano Ceccherini 	alert->SetShortcut(0, B_ESCAPE);
93f8cc2a74SStefano Ceccherini 	alert->SetShortcut(1, 'd');
94f8cc2a74SStefano Ceccherini 	alert->SetShortcut(2, 's');
95f8cc2a74SStefano Ceccherini 
96f8cc2a74SStefano Ceccherini 	int32 index = alert->Go();
97f8cc2a74SStefano Ceccherini 	if (index == 0)
98f8cc2a74SStefano Ceccherini 		return false;
99f8cc2a74SStefano Ceccherini 
100f8cc2a74SStefano Ceccherini 	if (index == 2)
101f8cc2a74SStefano Ceccherini 		_Save();
102f03034d7SStefano Ceccherini 	else
103f03034d7SStefano Ceccherini 		_Revert();
104f8cc2a74SStefano Ceccherini 
105f8cc2a74SStefano Ceccherini 	return true;
106f8cc2a74SStefano Ceccherini }
107f8cc2a74SStefano Ceccherini 
108f8cc2a74SStefano Ceccherini 
109f8cc2a74SStefano Ceccherini void
1107e99a484SStefano Ceccherini PrefWindow::_SaveAs()
111f8cc2a74SStefano Ceccherini {
11206a28b54SIngo Weinhold 	if (!fSavePanel) {
11306a28b54SIngo Weinhold 		BMessenger messenger(this);
11406a28b54SIngo Weinhold 		fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger);
11506a28b54SIngo Weinhold 	}
116f8cc2a74SStefano Ceccherini 
117f8cc2a74SStefano Ceccherini 	fSavePanel->Show();
118f8cc2a74SStefano Ceccherini }
119f8cc2a74SStefano Ceccherini 
120f8cc2a74SStefano Ceccherini 
121f8cc2a74SStefano Ceccherini void
1227e99a484SStefano Ceccherini PrefWindow::_SaveRequested(BMessage *msg)
123f8cc2a74SStefano Ceccherini {
124f8cc2a74SStefano Ceccherini 	entry_ref dirref;
125f8cc2a74SStefano Ceccherini 	const char *filename;
126f8cc2a74SStefano Ceccherini 
127f8cc2a74SStefano Ceccherini 	msg->FindRef("directory", &dirref);
128f8cc2a74SStefano Ceccherini 	msg->FindString("name", &filename);
129f8cc2a74SStefano Ceccherini 
130f8cc2a74SStefano Ceccherini 	BDirectory dir(&dirref);
131f8cc2a74SStefano Ceccherini 	BPath path(&dir, filename);
132f8cc2a74SStefano Ceccherini 
133dc25fe81SStefano Ceccherini 	PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE, TERM_SIGNATURE);
134f8cc2a74SStefano Ceccherini }
135f8cc2a74SStefano Ceccherini 
136f8cc2a74SStefano Ceccherini 
137f8cc2a74SStefano Ceccherini void
1387e99a484SStefano Ceccherini PrefWindow::_Save()
139f8cc2a74SStefano Ceccherini {
14056e415ddSStefano Ceccherini 	delete fPreviousPref;
14156e415ddSStefano Ceccherini 	fPreviousPref = new PrefHandler(PrefHandler::Default());
142f8cc2a74SStefano Ceccherini 
143411d718eSStefano Ceccherini 	PrefHandler::Default()->SaveDefaultAsText();
144f8cc2a74SStefano Ceccherini 	fDirty = false;
145f8cc2a74SStefano Ceccherini }
146f8cc2a74SStefano Ceccherini 
147f8cc2a74SStefano Ceccherini 
148f8cc2a74SStefano Ceccherini void
1497e99a484SStefano Ceccherini PrefWindow::_Revert()
150f8cc2a74SStefano Ceccherini {
15156e415ddSStefano Ceccherini 	if (fDirty) {
15256e415ddSStefano Ceccherini 		PrefHandler::SetDefault(new PrefHandler(fPreviousPref));
153f8cc2a74SStefano Ceccherini 
154a5ec0eebSStefano Ceccherini 		fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED);
155a5ec0eebSStefano Ceccherini 		fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED);
156f8cc2a74SStefano Ceccherini 
157f8cc2a74SStefano Ceccherini 		fDirty = false;
158f8cc2a74SStefano Ceccherini 	}
15956e415ddSStefano Ceccherini }
160f8cc2a74SStefano Ceccherini 
161f8cc2a74SStefano Ceccherini 
162f8cc2a74SStefano Ceccherini void
1637e99a484SStefano Ceccherini PrefWindow::MessageReceived(BMessage *msg)
164f8cc2a74SStefano Ceccherini {
165f8cc2a74SStefano Ceccherini 	switch (msg->what) {
166f8cc2a74SStefano Ceccherini 		case MSG_SAVE_PRESSED:
167f8cc2a74SStefano Ceccherini 			_Save();
168f8cc2a74SStefano Ceccherini 			PostMessage(B_QUIT_REQUESTED);
169f8cc2a74SStefano Ceccherini 			break;
170f8cc2a74SStefano Ceccherini 
171f8cc2a74SStefano Ceccherini 		case MSG_SAVEAS_PRESSED:
172f8cc2a74SStefano Ceccherini 			_SaveAs();
173f8cc2a74SStefano Ceccherini 			break;
174f8cc2a74SStefano Ceccherini 
175f8cc2a74SStefano Ceccherini 		case MSG_REVERT_PRESSED:
176f8cc2a74SStefano Ceccherini 			_Revert();
177f8cc2a74SStefano Ceccherini 			PostMessage(B_QUIT_REQUESTED);
178f8cc2a74SStefano Ceccherini 			break;
179f8cc2a74SStefano Ceccherini 
180f8cc2a74SStefano Ceccherini 		case MSG_PREF_MODIFIED:
181f8cc2a74SStefano Ceccherini 			fDirty = true;
182f8cc2a74SStefano Ceccherini 			break;
183f8cc2a74SStefano Ceccherini 
184f8cc2a74SStefano Ceccherini 		case B_SAVE_REQUESTED:
185f8cc2a74SStefano Ceccherini 			_SaveRequested(msg);
186f8cc2a74SStefano Ceccherini 			break;
187f8cc2a74SStefano Ceccherini 
188f8cc2a74SStefano Ceccherini 		default:
189f8cc2a74SStefano Ceccherini 			BWindow::MessageReceived(msg);
190f8cc2a74SStefano Ceccherini 			break;
191f8cc2a74SStefano Ceccherini 	}
192f8cc2a74SStefano Ceccherini }
193