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