xref: /haiku/src/apps/terminal/PrefWindow.cpp (revision a5ec0eeb43a14b09d418eed8da3d8767e63c3882)
1 /*
2  * Copyright 2007-2008, Haiku, Inc.
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 "PrefView.h"
13 #include "TermConst.h"
14 
15 #include <Alert.h>
16 #include <Box.h>
17 #include <Button.h>
18 #include <FilePanel.h>
19 #include <GroupLayoutBuilder.h>
20 #include <LayoutBuilder.h>
21 #include <Path.h>
22 
23 #include <stdio.h>
24 
25 
26 PrefWindow::PrefWindow(const BMessenger &messenger)
27 	: BWindow(BRect(0, 0, 375, 185), "Terminal Preferences",
28 		B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
29 		B_NOT_RESIZABLE|B_NOT_ZOOMABLE|B_AUTO_UPDATE_SIZE_LIMITS),
30 	fPreviousPref(new PrefHandler(PrefHandler::Default())),
31 	fSavePanel(NULL),
32 	fDirty(false),
33 	fTerminalMessenger(messenger)
34 {
35 	BLayoutBuilder::Group<>(this, B_VERTICAL)
36 		.AddGroup(B_VERTICAL, 1)
37 		.SetInsets(10, 10, 10, 10)
38 			.Add(new AppearancePrefView("Appearance", fTerminalMessenger))
39 			.AddGroup(B_HORIZONTAL)
40 				.Add(fSaveAsFileButton = new BButton("savebutton",
41 					"Save to File" B_UTF8_ELLIPSIS,
42 					new BMessage(MSG_SAVEAS_PRESSED), B_WILL_DRAW))
43 				.AddGlue()
44 				.Add(fRevertButton = new BButton("revertbutton",
45 					"Cancel", new BMessage(MSG_REVERT_PRESSED),
46 					B_WILL_DRAW))
47 				.Add(fSaveButton = new BButton("okbutton", "OK",
48 					new BMessage(MSG_SAVE_PRESSED), B_WILL_DRAW))
49 			.End()
50 		.End();
51 
52 
53 	fSaveButton->MakeDefault(true);
54 
55 	AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
56 	AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
57 
58 	CenterOnScreen();
59 	Show();
60 }
61 
62 
63 PrefWindow::~PrefWindow()
64 {
65 }
66 
67 
68 void
69 PrefWindow::Quit()
70 {
71 	fTerminalMessenger.SendMessage(MSG_PREF_CLOSED);
72 	delete fPreviousPref;
73 	delete fSavePanel;
74 	BWindow::Quit();
75 }
76 
77 
78 bool
79 PrefWindow::QuitRequested()
80 {
81 	if (!fDirty)
82 		return true;
83 
84 	BAlert *alert = new BAlert("", "Save changes to this preference panel?",
85 		"Cancel", "Don't Save", "Save",
86 		B_WIDTH_AS_USUAL, B_OFFSET_SPACING,
87 		B_WARNING_ALERT);
88 	alert->SetShortcut(0, B_ESCAPE);
89 	alert->SetShortcut(1, 'd');
90 	alert->SetShortcut(2, 's');
91 
92 	int32 index = alert->Go();
93 	if (index == 0)
94 		return false;
95 
96 	if (index == 2)
97 		_Save();
98 
99 	return true;
100 }
101 
102 
103 void
104 PrefWindow::_SaveAs()
105 {
106 	if (!fSavePanel) {
107 		BMessenger messenger(this);
108 		fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger);
109 	}
110 
111 	fSavePanel->Show();
112 }
113 
114 
115 void
116 PrefWindow::_SaveRequested(BMessage *msg)
117 {
118 	entry_ref dirref;
119 	const char *filename;
120 
121 	msg->FindRef("directory", &dirref);
122 	msg->FindString("name", &filename);
123 
124 	BDirectory dir(&dirref);
125 	BPath path(&dir, filename);
126 
127 	PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE, TERM_SIGNATURE);
128 }
129 
130 
131 void
132 PrefWindow::_Save()
133 {
134 	delete fPreviousPref;
135 	fPreviousPref = new PrefHandler(PrefHandler::Default());
136 
137 	BPath path;
138 	if (PrefHandler::GetDefaultPath(path) == B_OK) {
139 		PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE);
140 		fDirty = false;
141 	}
142 }
143 
144 
145 void
146 PrefWindow::_Revert()
147 {
148 	if (fDirty) {
149 		PrefHandler::SetDefault(new PrefHandler(fPreviousPref));
150 
151 		fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED);
152 		fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED);
153 		fTerminalMessenger.SendMessage(MSG_INPUT_METHOD_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