xref: /haiku/src/apps/terminal/AppearPrefView.cpp (revision a381c8a06378de22ff08adf4282b4e3f7e50d250)
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 		PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY));
46 	fFont = new BMenuField(r, "font", "Font:", menu);
47 	fFont->SetDivider(fontDividerSize);
48 	AddChild(fFont);
49 
50 	r.OffsetBy(r.Width() + 10, 0);
51 	menu = _MakeSizeMenu(MSG_HALF_SIZE_CHANGED,
52 		PrefHandler::Default()->getInt32(PREF_HALF_FONT_SIZE));
53 	fFontSize = new BMenuField(r, "size", "Size:", menu);
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]));
60 	fColorField->SetDivider(StringWidth(fColorField->Label()) + 8.0);
61 	AddChild(fColorField);
62 
63   	fColorControl = SetupColorControl(BPoint(r.left, r.bottom + 10),
64   		B_CELLS_32x8, 6, MSG_COLOR_CHANGED);
65   	fColorControl->SetValue(PrefHandler::Default()->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(PrefHandler::Default()->getRGB(PREF_TEXT_FORE_COLOR));
85 
86 	fFont->Menu()->FindItem(PrefHandler::Default()->getString(PREF_HALF_FONT_FAMILY))->SetMarked(true);
87 	fFontSize->Menu()->FindItem(PrefHandler::Default()->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 			PrefHandler::Default()->setString(PREF_HALF_FONT_FAMILY,
110 				fFont->Menu()->FindMarked()->Label());
111 			modified = true;
112 			break;
113 
114 		case MSG_HALF_SIZE_CHANGED:
115 			PrefHandler::Default()->setString(PREF_HALF_FONT_SIZE,
116 				fFontSize->Menu()->FindMarked()->Label());
117 			modified = true;
118 			break;
119 
120 		case MSG_COLOR_CHANGED:
121 			PrefHandler::Default()->setRGB(fColorField->Menu()->FindMarked()->Label(),
122 				fColorControl->ValueAsColor());
123 			modified = true;
124 			break;
125 
126 		case MSG_COLOR_FIELD_CHANGED:
127 			fColorControl->SetValue(PrefHandler::Default()->getRGB(
128 				fColorField->Menu()->FindMarked()->Label()));
129 			break;
130 
131 		default:
132 			PrefView::MessageReceived(msg);
133 			return;
134 	}
135 
136 	if (modified) {
137 		fAppearancePrefViewMessenger.SendMessage(msg);
138 
139 		BMessenger messenger(this);
140 		messenger.SendMessage(MSG_PREF_MODIFIED);
141 	}
142 }
143 
144 
145 BMenu *
146 AppearancePrefView::_MakeFontMenu(uint32 command, const char *defaultFontName)
147 {
148 	BPopUpMenu *menu = new BPopUpMenu("");
149 	int32 numFamilies = count_font_families();
150 
151 	for (int32 i = 0; i < numFamilies; i++) {
152 		font_family family;
153 		uint32 flags;
154 
155 		if (get_font_family(i, &family, &flags) == B_OK) {
156 			BFont font;
157 			font.SetFamilyAndStyle(family, NULL);
158 			if (font.IsFixed()) {
159 				BMenuItem *item = new BMenuItem(family, new BMessage(command));
160 				menu->AddItem(item);
161 				if (!strcmp(defaultFontName, family))
162 					item->SetMarked(true);
163 			}
164 		}
165 	}
166 
167 	return menu;
168 }
169 
170 
171 BMenu *
172 AppearancePrefView::_MakeSizeMenu(uint32 command, uint8 defaultSize)
173 {
174 	BPopUpMenu *menu = new BPopUpMenu("size");
175 	int32 sizes[] = {9, 10, 11, 12, 14, 16, 18, 0};
176 
177 	for (uint32 i = 0; sizes[i]; i++) {
178 		BString string;
179 		string << sizes[i];
180 
181 		BMenuItem* item = new BMenuItem(string.String(), new BMessage(command));
182 		menu->AddItem(item);
183 
184 		if (sizes[i] == defaultSize)
185 			item->SetMarked(true);
186 	}
187 
188 	return menu;
189 }
190