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