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