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