1 #include "LayDes.h"
2
FormatFont(Font font)3 String FormatFont(Font font)
4 {
5 int q = font.GetFace();
6 String txt;
7 int h = font.GetHeight();
8 switch(q) {
9 case Font::SERIF:
10 txt << (h ? "SerifZ" : "Serif");
11 break;
12 case Font::SANSSERIF:
13 txt << (h ? "SansSerifZ" : "SansSerif");
14 break;
15 case Font::MONOSPACE:
16 txt << (h ? "MonospaceZ" : "Monospace");
17 break;
18 default:
19 txt << (h ? "StdFontZ" : "StdFont");
20 break;
21 }
22 txt << '(' << (h ? Format("%d)", h) : ")");
23 if(font.IsBold())
24 txt << ".Bold()";
25 if(font.IsItalic())
26 txt << ".Italic()";
27 if(font.IsUnderline())
28 txt << ".Underline()";
29 if(font.IsStrikeout())
30 txt << ".Strikeout()";
31 if(font.IsNonAntiAliased())
32 txt << ".NonAntiAliased()";
33 return txt;
34 }
35
36 struct FontDisplay : public Display {
PaintFontDisplay37 virtual void Paint(Draw& w, const Rect& r, const Value& q,
38 Color ink, Color paper, dword style) const {
39 w.DrawRect(r, paper);
40 Font font = q;
41 String text = FormatFont(font);
42 font.Height(StdFont().GetHeight() - 1);
43 w.DrawText(2, (r.Height() - font.Info().GetHeight()) / 2, text, font, ink);
44 }
45 };
46
47 struct FontDlg : public WithFontPropertyDlgLayout<TopWindow> {
48 void SetFont(Font f);
49 Font GetFont() const;
50
ActionFontDlg51 void Action() { WhenAction(); }
52
53 typedef FontDlg CLASSNAME;
54
55 FontDlg();
56 };
57
SetFont(Font f)58 void FontDlg::SetFont(Font f)
59 {
60 face <<= f.GetFace();
61 height <<= f.GetHeight() ? f.GetHeight() : Null;
62 bold = f.IsBold();
63 italic = f.IsItalic();
64 underline = f.IsUnderline();
65 strikeout = f.IsStrikeout();
66 nonaa = f.IsNonAntiAliased();
67 }
68
GetFont() const69 Font FontDlg::GetFont() const
70 {
71 Font f;
72 f.Face(~face);
73 if(!IsNull(~height))
74 f.Height(~height);
75 else
76 f.Height(0);
77 f.Bold(bold);
78 f.Italic(italic);
79 f.Underline(underline);
80 f.Strikeout(strikeout);
81 f.NonAntiAliased(nonaa);
82 return f;
83 }
84
FontDlg()85 FontDlg::FontDlg()
86 {
87 int i;
88 CtrlLayoutOKCancel(*this, "Font");
89 ToolWindow();
90 for(i = 6; i < 70; i++)
91 height.AddList(i);
92 face <<= height
93 <<= bold <<= italic <<= underline <<= strikeout <<= nonaa <<= THISBACK(Action);
94 height.Min(0);
95 }
96
97 struct FontProperty : public EditorProperty<DataPusher> {
SaveFontProperty98 virtual String Save() const { return FormatFont(~editor); }
99 virtual void Read(CParser& p);
100
101 One<FontDlg> fdlg;
102
103 FontDlg& Dlg();
104
105 void FontChanged();
106 void Perform();
107
108 typedef FontProperty CLASSNAME;
109
110 FontProperty();
111
CreateFontProperty112 static ItemProperty *Create() { return new FontProperty; }
113 };
114
Read(CParser & p)115 void FontProperty::Read(CParser& p) {
116 Font f = StdFont();
117 if(p.Id("StdFont") || p.Id("StdFontZ"))
118 f.Face(Font::STDFONT);
119 else
120 if(p.Id("Serif") || p.Id("SerifZ") || p.Id("ScreenSerif") || p.Id("ScreenSerifZ") ||
121 p.Id("Roman") || p.Id("RomanZ"))
122 f.Face(Font::SERIF);
123 else
124 if(p.Id("SansSerif") || p.Id("SansSerifZ") || p.Id("ScreenSans") || p.Id("ScreenSansZ") ||
125 p.Id("Arial") || p.Id("ArialZ"))
126 f.Face(Font::SANSSERIF);
127 else
128 if(p.Id("Monospace") || p.Id("MonospaceZ") || p.Id("ScreenFixed") || p.Id("ScreenFixedZ") ||
129 p.Id("Courier") || p.Id("CourierZ"))
130 f.Face(Font::MONOSPACE);
131 p.PassChar('(');
132 if(p.IsInt())
133 f.Height(p.ReadInt());
134 else
135 f.Height(0);
136 p.PassChar(')');
137 while(p.Char('.')) {
138 if(p.Id("Bold"))
139 f.Bold();
140 else
141 if(p.Id("Italic"))
142 f.Italic();
143 else
144 if(p.Id("Underline"))
145 f.Underline();
146 else
147 if(p.Id("Strikeout"))
148 f.Strikeout();
149 else {
150 p.PassId("NonAntiAliased");
151 f.NonAntiAliased();
152 }
153 p.PassChar('(');
154 p.PassChar(')');
155 }
156 editor.SetData(f);
157 }
158
Dlg()159 FontDlg& FontProperty::Dlg()
160 {
161 if(!fdlg)
162 fdlg = new FontDlg;
163 return *fdlg;
164 }
165
FontChanged()166 void FontProperty::FontChanged()
167 {
168 editor.SetData(Dlg().GetFont());
169 WhenAction();
170 }
171
Perform()172 void FontProperty::Perform()
173 {
174 editor.SetFocus();
175 Font f = editor.GetData();
176 Dlg().WhenAction = THISBACK(FontChanged);
177 Dlg().SetFont(f);
178 Size sz = Dlg().GetRect().Size();
179 Rect er = editor.GetScreenRect();
180 Rect wa = Ctrl::GetWorkArea();
181 Rect r(er.TopRight(), sz);
182 if(r.bottom > wa.bottom) {
183 r.top = wa.bottom - sz.cy;
184 r.bottom = wa.bottom;
185 }
186 if(r.right > wa.right) {
187 r.left = wa.right - sz.cx;
188 r.right = wa.right;
189 }
190 Dlg().NoCenter();
191 Dlg().SetRect(r);
192 if(Dlg().Execute() == IDOK)
193 editor.SetData(Dlg().GetFont());
194 else
195 editor.SetData(f);
196 editor.SetFocus();
197 }
198
FontProperty()199 FontProperty::FontProperty() {
200 editor.SetData(StdFont());
201 Add(editor.HSizePosZ(100, 2).TopPos(2));
202 editor.SetDisplay(Single<FontDisplay>());
203 editor.WhenAction = THISBACK(Perform);
204 editor <<= StdFont();
205 }
206
RegisterFontProperty()207 void RegisterFontProperty()
208 {
209 ItemProperty::Register("Font", FontProperty::Create);
210 }
211