xref: /haiku/src/preferences/appearance/FontView.cpp (revision 83b1a68c52ba3e0e8796282759f694b7fdddf06d)
1 /*
2  * Copyright 2001-2012, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Mark Hogben
7  *		DarkWyrm <bpmagic@columbus.rr.com>
8  *		Axel Dörfler, axeld@pinc-software.de
9  *		Philippe St-Pierre, stpere@gmail.com
10  *		Stephan Aßmus <superstippi@gmx.de>
11  */
12 
13 
14 #include "FontView.h"
15 
16 #include <string.h>
17 
18 #include <Catalog.h>
19 #include <GridLayoutBuilder.h>
20 #include <GroupLayoutBuilder.h>
21 #include <Locale.h>
22 #include <MessageRunner.h>
23 #include <SpaceLayoutItem.h>
24 
25 #include "APRWindow.h"
26 #include "FontSelectionView.h"
27 
28 
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "Font view"
31 
32 
33 static const uint32 kMsgCheckFonts = 'chkf';
34 
35 
36 static void
37 add_font_selection_view(BGridLayout* layout, FontSelectionView* view,
38 	int32& row, bool withExtraSpace)
39 {
40 	layout->AddItem(view->CreateFontsLabelLayoutItem(), 0, row);
41 	layout->AddItem(view->CreateFontsMenuBarLayoutItem(), 1, row);
42 
43 	layout->AddItem(BSpaceLayoutItem::CreateGlue(), 2, row);
44 
45 	layout->AddItem(view->CreateSizesLabelLayoutItem(), 3, row);
46 	layout->AddItem(view->CreateSizesMenuBarLayoutItem(), 4, row);
47 
48 	row++;
49 
50 	layout->AddItem(BSpaceLayoutItem::CreateGlue(), 0, row);
51 	layout->AddView(view->GetPreviewBox(), 1, row, 4);
52 
53 	row++;
54 
55 	if (withExtraSpace) {
56 		layout->AddItem(BSpaceLayoutItem::CreateVerticalStrut(5), 0, row, 5);
57 		row++;
58 	}
59 }
60 
61 
62 FontView::FontView(const char* name)
63 	:
64 	BView(name, B_WILL_DRAW )
65 {
66 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
67 
68 	fPlainView = new FontSelectionView("plain", B_TRANSLATE("Plain font:"));
69 	fBoldView = new FontSelectionView("bold", B_TRANSLATE("Bold font:"));
70 	fFixedView = new FontSelectionView("fixed", B_TRANSLATE("Fixed font:"));
71 	fMenuView = new FontSelectionView("menu", B_TRANSLATE("Menu font:"));
72 
73 	BGridLayout* layout = new BGridLayout(5, 5);
74 	layout->SetInsets(10, 10, 10, 10);
75 	SetLayout(layout);
76 
77 	int32 row = 0;
78 	add_font_selection_view(layout, fPlainView, row, true);
79 	add_font_selection_view(layout, fBoldView, row, true);
80 	add_font_selection_view(layout, fFixedView, row, true);
81 	add_font_selection_view(layout, fMenuView, row, false);
82 }
83 
84 
85 void
86 FontView::AttachedToWindow()
87 {
88 	fPlainView->SetTarget(this);
89 	fBoldView->SetTarget(this);
90 	fFixedView->SetTarget(this);
91 	fMenuView->SetTarget(this);
92 
93 	UpdateFonts();
94 	fRunner = new BMessageRunner(this, new BMessage(kMsgCheckFonts), 3000000);
95 		// every 3 seconds
96 }
97 
98 
99 void
100 FontView::DetachedFromWindow()
101 {
102 	delete fRunner;
103 	fRunner = NULL;
104 }
105 
106 
107 void
108 FontView::SetDefaults()
109 {
110 	fPlainView->SetDefaults();
111 	fBoldView->SetDefaults();
112 	fFixedView->SetDefaults();
113 	fMenuView->SetDefaults();
114 }
115 
116 
117 void
118 FontView::MessageReceived(BMessage* message)
119 {
120 	switch (message->what) {
121 		case kMsgSetSize:
122 		case kMsgSetFamily:
123 		case kMsgSetStyle:
124 		{
125 			const char* name;
126 			if (message->FindString("name", &name) != B_OK)
127 				break;
128 
129 			if (!strcmp(name, "plain"))
130 				fPlainView->MessageReceived(message);
131 			else if (!strcmp(name, "bold"))
132 				fBoldView->MessageReceived(message);
133 			else if (!strcmp(name, "fixed"))
134 				fFixedView->MessageReceived(message);
135 			else if (!strcmp(name, "menu"))
136 				fMenuView->MessageReceived(message);
137 			else
138 				break;
139 
140 			Window()->PostMessage(kMsgUpdate);
141 			break;
142 		}
143 
144 		case kMsgCheckFonts:
145 			if (update_font_families(true))
146 				UpdateFonts();
147 			break;
148 
149 		default:
150 			BView::MessageReceived(message);
151 	}
152 }
153 
154 
155 void
156 FontView::Revert()
157 {
158 	fPlainView->Revert();
159 	fBoldView->Revert();
160 	fFixedView->Revert();
161 	fMenuView->Revert();
162 }
163 
164 
165 void
166 FontView::UpdateFonts()
167 {
168 	fPlainView->UpdateFontsMenu();
169 	fBoldView->UpdateFontsMenu();
170 	fFixedView->UpdateFontsMenu();
171 	fMenuView->UpdateFontsMenu();
172 }
173 
174 
175 bool
176 FontView::IsDefaultable()
177 {
178 	return fPlainView->IsDefaultable()
179 		|| fBoldView->IsDefaultable()
180 		|| fFixedView->IsDefaultable()
181 		|| fMenuView->IsDefaultable();
182 }
183 
184 
185 bool
186 FontView::IsRevertable()
187 {
188 	return fPlainView->IsRevertable()
189 		|| fBoldView->IsRevertable()
190 		|| fFixedView->IsRevertable()
191 		|| fMenuView->IsRevertable();
192 }
193 
194