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