xref: /haiku/src/apps/terminal/AppearPrefView.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2001-2007, 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 "MenuUtil.h"
11 #include "PrefHandler.h"
12 #include "TermConst.h"
13 
14 #include <View.h>
15 #include <Button.h>
16 #include <MenuField.h>
17 #include <Menu.h>
18 #include <MenuItem.h>
19 #include <PopUpMenu.h>
20 
21 #include <stdlib.h>
22 
23 
24 AppearancePrefView::AppearancePrefView(BRect frame, const char *name,
25 	BMessenger messenger)
26 	: PrefView(frame, name),
27 	fAppearancePrefViewMessenger(messenger)
28 {
29   	const char *color_tbl[] = {
30     	PREF_TEXT_FORE_COLOR,
31     	PREF_TEXT_BACK_COLOR,
32     	PREF_CURSOR_FORE_COLOR,
33     	PREF_CURSOR_BACK_COLOR,
34     	PREF_SELECT_FORE_COLOR,
35     	PREF_SELECT_BACK_COLOR,
36     	NULL
37   	};
38 
39 	float fontDividerSize = StringWidth("Font:") + 8.0;
40 	float sizeDividerSize = StringWidth("Size:") + 8.0;
41 
42 	BRect r(5, 5, 225, 25);
43 
44 	BMenu *menu = _MakeFontMenu(MSG_HALF_FONT_CHANGED,
45 		gTermPref->getString(PREF_HALF_FONT_FAMILY));
46 	fFont = new BMenuField(r, "font", "Font:", menu, B_WILL_DRAW);
47 	fFont->SetDivider(fontDividerSize);
48 	AddChild(fFont);
49 
50 	r.OffsetBy(r.Width() + 10, 0);
51 	menu = _MakeSizeMenu(MSG_HALF_SIZE_CHANGED, 12);
52 	fFontSize = new BMenuField(r, "size", "Size:", menu, B_WILL_DRAW);
53 	fFontSize->SetDivider(sizeDividerSize);
54 	AddChild(fFontSize);
55 
56 	r.OffsetBy(-r.Width() - 10,r.Height() + 25);
57 	fColorField = new BMenuField(r, "color", "Change:",
58 		MakeMenu(MSG_COLOR_FIELD_CHANGED, color_tbl, color_tbl[0]), B_WILL_DRAW);
59 	fColorField->SetDivider(StringWidth(fColorField->Label()) + 8.0);
60 	AddChild(fColorField);
61 
62   	fColorControl = SetupBColorControl(BPoint(r.left, r.bottom + 10),
63   		B_CELLS_32x8, 6, MSG_COLOR_CHANGED);
64   	fColorControl->SetValue(gTermPref->getRGB(PREF_TEXT_FORE_COLOR));
65 }
66 
67 
68 void
69 AppearancePrefView::GetPreferredSize(float *_width, float *_height)
70 {
71 	if (_width)
72 		*_width = Bounds().Width();
73 
74 	if (*_height)
75 		*_height = fColorControl->Frame().bottom;
76 }
77 
78 
79 void
80 AppearancePrefView::Revert()
81 {
82 	fColorField->Menu()->ItemAt(0)->SetMarked(true);
83 	fColorControl->SetValue(gTermPref->getRGB(PREF_TEXT_FORE_COLOR));
84 
85 	fFont->Menu()->FindItem(gTermPref->getString(PREF_HALF_FONT_FAMILY))->SetMarked(true);
86 	fFontSize->Menu()->FindItem(gTermPref->getString(PREF_HALF_FONT_FAMILY))->SetMarked(true);
87 }
88 
89 
90 void
91 AppearancePrefView::AttachedToWindow()
92 {
93 	fFontSize->Menu()->SetTargetForItems(this);
94 	fFont->Menu()->SetTargetForItems(this);
95 
96   	fColorControl->SetTarget(this);
97   	fColorField->Menu()->SetTargetForItems(this);
98 }
99 
100 
101 void
102 AppearancePrefView::MessageReceived(BMessage *msg)
103 {
104 	bool modified = false;
105 
106 	switch (msg->what) {
107 		case MSG_HALF_FONT_CHANGED:
108 			gTermPref->setString (PREF_HALF_FONT_FAMILY,
109 				fFont->Menu()->FindMarked()->Label());
110 			modified = true;
111 			break;
112 
113 		case MSG_HALF_SIZE_CHANGED:
114 			gTermPref->setString(PREF_HALF_FONT_SIZE,
115 				fFontSize->Menu()->FindMarked()->Label());
116 			modified = true;
117 			break;
118 
119 		case MSG_COLOR_CHANGED:
120 			gTermPref->setRGB(fColorField->Menu()->FindMarked()->Label(),
121 				fColorControl->ValueAsColor());
122 			modified = true;
123 			break;
124 
125 		case MSG_COLOR_FIELD_CHANGED:
126 			fColorControl->SetValue(gTermPref->getRGB(
127 				fColorField->Menu()->FindMarked()->Label()));
128 			break;
129 
130 		default:
131 			PrefView::MessageReceived(msg);
132 			return;
133 	}
134 
135 	if (modified) {
136 		fAppearancePrefViewMessenger.SendMessage(msg);
137 			// send message to fTermWindow
138 
139 		// send the MSG_PREF_MODIFIED message
140 		// to fPrefWindow
141 		BMessenger messenger(this);
142 		messenger.SendMessage(MSG_PREF_MODIFIED);
143 	}
144 }
145 
146 
147 BMenu *
148 AppearancePrefView::_MakeFontMenu(uint32 command, const char *defaultFontName)
149 {
150 	BPopUpMenu *menu = new BPopUpMenu("");
151 	int32 numFamilies = count_font_families();
152 
153 	for (int32 i = 0; i < numFamilies; i++) {
154 		font_family family;
155 		uint32 flags;
156 
157 		if (get_font_family(i, &family, &flags) == B_OK) {
158 			menu->AddItem(new BMenuItem(family, new BMessage(command)));
159 			if (!strcmp(defaultFontName, family)) {
160 				BMenuItem* item = menu->ItemAt(i);
161 				item->SetMarked(true);
162 			}
163 		}
164 	}
165 
166 	return menu;
167 }
168 
169 
170 BMenu *
171 AppearancePrefView::_MakeSizeMenu(uint32 command, uint8 defaultSize)
172 {
173 	BPopUpMenu *menu = new BPopUpMenu("size");
174 	int32 sizes[] = {9, 10, 11, 12, 14, 16, 18, 0};
175 
176 	for (uint32 i = 0; sizes[i]; i++) {
177 		BString string;
178 		string << sizes[i];
179 
180 		BMenuItem* item = new BMenuItem(string.String(), new BMessage(command));
181 		menu->AddItem(item);
182 
183 		if (sizes[i] == defaultSize)
184 			item->SetMarked(true);
185 	}
186 
187 	return menu;
188 }
189