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