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