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