xref: /haiku/src/apps/terminal/PrefWindow.cpp (revision e6b30aee0fd7a23d6a6baab9f3718945a0cd838a)
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 <Path.h>
20 #include <Screen.h>
21 
22 #include <stdio.h>
23 
24 
25 PrefWindow::PrefWindow(BMessenger messenger)
26 	: BWindow(_CenteredRect(BRect(0, 0, 350, 215)), "Terminal Settings",
27 		B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
28 		B_NOT_RESIZABLE|B_NOT_ZOOMABLE),
29 	fPrefTemp(new PrefHandler(PrefHandler::Default())),
30 	fSavePanel(NULL),
31 	fDirty(false),
32 	fPrefDlgMessenger(messenger)
33 {
34 	BView *top = new BView(Bounds(), "topview", B_FOLLOW_NONE, B_WILL_DRAW);
35 	top->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
36 	AddChild(top);
37 
38 	BRect rect = top->Bounds();
39 	rect.bottom *= .75;
40 	AppearancePrefView *prefView= new AppearancePrefView(rect, "Appearance",
41 		fPrefDlgMessenger);
42 	top->AddChild(prefView);
43 
44 	fSaveAsFileButton = new BButton(BRect(0, 0, 1, 1), "savebutton", "Save to File" B_UTF8_ELLIPSIS,
45 		new BMessage(MSG_SAVEAS_PRESSED), B_FOLLOW_TOP, B_WILL_DRAW);
46 	fSaveAsFileButton->ResizeToPreferred();
47 	fSaveAsFileButton->MoveTo(5, top->Bounds().Height() - 5 -
48 		fSaveAsFileButton->Bounds().Height());
49 	top->AddChild(fSaveAsFileButton);
50 
51 	fSaveButton = new BButton(BRect(0, 0, 1, 1), "okbutton", "OK",
52 		new BMessage(MSG_SAVE_PRESSED), B_FOLLOW_TOP, B_WILL_DRAW);
53 	fSaveButton->ResizeToPreferred();
54 	fSaveButton->MoveTo(top->Bounds().Width() - 5 - fSaveButton->Bounds().Width(),
55 		top->Bounds().Height() - 5 - fSaveButton->Bounds().Height());
56 	fSaveButton->MakeDefault(true);
57 	top->AddChild(fSaveButton);
58 
59 	fRevertButton = new BButton(BRect(0, 0, 1, 1), "revertbutton",
60 		"Cancel", new BMessage(MSG_REVERT_PRESSED), B_FOLLOW_TOP, B_WILL_DRAW);
61 	fRevertButton->ResizeToPreferred();
62 	fRevertButton->MoveTo(fSaveButton->Frame().left - 10 -
63 		fRevertButton->Bounds().Width(), top->Bounds().Height() - 5 -
64 		fRevertButton->Bounds().Height());
65 	top->AddChild(fRevertButton);
66 
67 	AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
68 	AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
69 
70 	Show();
71 }
72 
73 
74 PrefWindow::~PrefWindow()
75 {
76 }
77 
78 
79 void
80 PrefWindow::Quit()
81 {
82 	fPrefDlgMessenger.SendMessage(MSG_PREF_CLOSED);
83 	delete fPrefTemp;
84 	delete fSavePanel;
85 	BWindow::Quit();
86 }
87 
88 
89 bool
90 PrefWindow::QuitRequested()
91 {
92 	if (!fDirty)
93 		return true;
94 
95 	BAlert *alert = new BAlert("", "Save changes to this preference panel?",
96 		"Cancel", "Don't Save", "Save",
97 		B_WIDTH_AS_USUAL, B_OFFSET_SPACING,
98 		B_WARNING_ALERT);
99 	alert->SetShortcut(0, B_ESCAPE);
100 	alert->SetShortcut(1, 'd');
101 	alert->SetShortcut(2, 's');
102 
103 	int32 index = alert->Go();
104 	if (index == 0)
105 		return false;
106 
107 	if (index == 2)
108 		_Save();
109 
110 	return true;
111 }
112 
113 
114 void
115 PrefWindow::_SaveAs()
116 {
117 	if (!fSavePanel)
118 		fSavePanel = new BFilePanel(B_SAVE_PANEL, new BMessenger(this));
119 
120 	fSavePanel->Show();
121 }
122 
123 
124 void
125 PrefWindow::_SaveRequested(BMessage *msg)
126 {
127 	entry_ref dirref;
128 	const char *filename;
129 
130 	msg->FindRef("directory", &dirref);
131 	msg->FindString("name", &filename);
132 
133 	BDirectory dir(&dirref);
134 	BPath path(&dir, filename);
135 
136 	PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE, TERM_SIGNATURE);
137 }
138 
139 
140 void
141 PrefWindow::_Save()
142 {
143 	delete fPrefTemp;
144 	fPrefTemp = new PrefHandler(PrefHandler::Default());
145 
146 	BPath path;
147 	if (PrefHandler::GetDefaultPath(path) == B_OK) {
148 		PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE);
149 		fDirty = false;
150 	}
151 }
152 
153 
154 void
155 PrefWindow::_Revert()
156 {
157 	PrefHandler::SetDefault(new PrefHandler(fPrefTemp));
158 
159 	fPrefDlgMessenger.SendMessage(MSG_HALF_FONT_CHANGED);
160 	fPrefDlgMessenger.SendMessage(MSG_COLOR_CHANGED);
161 	fPrefDlgMessenger.SendMessage(MSG_INPUT_METHOD_CHANGED);
162 
163 	fDirty = false;
164 }
165 
166 
167 void
168 PrefWindow::MessageReceived(BMessage *msg)
169 {
170 	switch (msg->what) {
171 		case MSG_SAVE_PRESSED:
172 			_Save();
173 			PostMessage(B_QUIT_REQUESTED);
174 			break;
175 
176 		case MSG_SAVEAS_PRESSED:
177 			_SaveAs();
178 			break;
179 
180 		case MSG_REVERT_PRESSED:
181 			_Revert();
182 			PostMessage(B_QUIT_REQUESTED);
183 			break;
184 
185 		case MSG_PREF_MODIFIED:
186 			fDirty = true;
187 			break;
188 
189 		case B_SAVE_REQUESTED:
190 			_SaveRequested(msg);
191 			break;
192 
193 		default:
194 			BWindow::MessageReceived(msg);
195 			break;
196 	}
197 }
198 
199 
200 BRect
201 PrefWindow::_CenteredRect(BRect rect)
202 {
203 	BRect screenRect = BScreen().Frame();
204 
205 	screenRect.InsetBy(10,10);
206 
207 	float x = screenRect.left + (screenRect.Width() - rect.Width()) / 2;
208 	float y = screenRect.top + (screenRect.Height() - rect.Height()) / 3;
209 
210 	rect.OffsetTo(x, y);
211 
212 	return rect;
213 }
214