Help.cpp

Zbigniew Rebacz, 11/05/2013 02:46 PM

Download (5.77 KB)

 
1
#include "CtrlLib.h"
2

    
3
NAMESPACE_UPP
4

    
5
Topic HelpWindow::AcquireTopic(const String& topic)
6
{
7
        return GetTopic(topic);
8
}
9

    
10
void HelpWindow::BarEx(Bar& bar)
11
{}
12

    
13
void HelpWindow::FinishText(RichText& text)
14
{}
15

    
16
bool HelpWindow::GoTo0(const String& link)
17
{
18
        if(IsNull(link) || current_link == link)
19
                return true;
20
        Topic t = AcquireTopic(link);
21
        SetBar();
22
        if(!IsNull(t.text)) {
23
                label = t.label;
24
                topic = t.link;
25
                Title(FromUtf8(t.title));
26
                RichText txt = ParseQTF(t.text);
27
                FinishText(txt);
28
                view.Pick(txt, zoom);
29
                view.GotoLabel(label, true);
30
                current_link = link;
31
                tree.FindSetCursor(topic);
32
                return true;
33
        }
34
        return false;
35
}
36

    
37
HelpWindow::Pos HelpWindow::GetPos()
38
{
39
        Pos p;
40
        p.link = topic;
41
        p.scy = view.GetSb();
42
        return p;
43
}
44

    
45
void HelpWindow::GoTo(const String& link)
46
{
47
        if(IsNull(link) || current_link == link)
48
                return;
49
        Pos p = GetPos();
50
        if(GoTo0(link)) {
51
                if(!IsNull(p.link))
52
                        back.Add(p);
53
                forward.Clear();
54
                SetBar();
55
                return;
56
        }
57
        if(link.StartsWith("www.") || link.StartsWith("http") || link.StartsWith("mailto:"))
58
                LaunchWebBrowser(link);
59
}
60

    
61
void HelpWindow::Back()
62
{
63
        Pos p = GetPos();
64
        if(back.GetCount() && GoTo0(back.Top().link)) {
65
                if(back.GetCount()) {
66
                        view.SetSb(back.Top().scy);
67
                        back.Drop();
68
                }
69
                if(!IsNull(p.link))
70
                        forward.Add(p);
71
                SetBar();
72
        }
73
}
74

    
75
void HelpWindow::Forward()
76
{
77
        Pos p = GetPos();
78
        if(forward.GetCount() && GoTo0(forward.Top().link)) {
79
                if(forward.GetCount()) {
80
                        view.SetSb(forward.Top().scy);
81
                        forward.Drop();
82
                }
83
                if(!IsNull(p.link))
84
                        back.Add(p);
85
                SetBar();
86
        }
87
}
88

    
89
void HelpWindow::SetZoom()
90
{
91
        zoom.d = 1000;
92
        current_link = Null;
93
        GoTo0(topic);
94
        Refresh();
95
}
96

    
97
void HelpWindow::FontSize()
98
{
99
        int q = zoom.m / 20;
100
        if(q < 6 || q > 10)
101
                q = 6;
102
        else
103
                q++;
104
        zoom.m = 20 * q;
105
        SetZoom();
106
}
107

    
108
void HelpWindow::Print()
109
{
110
#ifndef PLATFORM_PDA
111
        Topic t = AcquireTopic(topic);
112
        UPP::Print(ParseQTF(t.text), Size(3968, 6074), 0);
113
#endif
114
}
115

    
116
void HelpWindow::Tools(Bar& bar)
117
{
118
        bar.Add(back.GetCount(), t_("Go Back"), CtrlImg::go_back(), THISBACK(Back))
119
           .Key(K_ALT_LEFT);
120
        bar.Add(forward.GetCount(), t_("Go Forward"), CtrlImg::go_forward(), THISBACK(Forward))
121
           .Key(K_ALT_RIGHT);
122
        bar.Gap();
123
        bar.Add(t_("Font size"), CtrlImg::font_size(), THISBACK(FontSize));
124
        bar.Gap();
125
#ifndef PLATFORM_PDA
126
        bar.Add(t_("Print"), CtrlImg::print(), THISBACK(Print));
127
#endif
128
        BarEx(bar);
129
}
130

    
131
void HelpWindow::SetBar()
132
{
133
        toolbar.Set(THISBACK(Tools));
134
}
135

    
136
bool HelpWindow::Key(dword key, int count)
137
{
138
        if(key == K_ESCAPE) {
139
                Close();
140
                return true;
141
        }
142
        return view.Key(key, count);
143
}
144

    
145
void HelpWindow::ClearTree()
146
{
147
        tree.Clear();
148
}
149

    
150
int HelpWindow::AddTree(int parent, const Image& img, const String& topic, const String& title)
151
{
152
        tree_view.NoZoom();
153
        return tree.Add(parent, img, topic, title, false);
154
}
155

    
156
void HelpWindow::SortTree(int id)
157
{
158
        tree.SortDeep(id);
159
}
160

    
161
void HelpWindow::SortTree(int id, int (*cmp)(const Value& v1, const Value& v2))
162
{
163
        tree.SortDeep(id, cmp);
164
}
165

    
166
void HelpWindow::SortTree(int id, int (*cmp)(const Value& k1, const Value& v1,
167
                                             const Value& k2, const Value& v2))
168
{
169
        tree.SortDeep(id, cmp);
170
}
171

    
172
void HelpWindow::FinishTree()
173
{
174
        if(!tree.FindSetCursor(topic))
175
                CurrentOrHome();
176
}
177

    
178
void HelpWindow::OpenDeep(int id)
179
{
180
        tree.OpenDeep(id);
181
}
182

    
183
void HelpWindow::Ids(int pid, Vector<int>& r)
184
{
185
        int n = tree.GetChildCount(pid);
186
        for(int i = 0; i < n; i++) {
187
                int id = tree.GetChild(pid, i);
188
                if(!IsNull(tree.GetValue(id))) {
189
                        r.Add(id);
190
                        Ids(id, r);
191
                }
192
        }
193
}
194

    
195
Vector<int> HelpWindow::Ids()
196
{
197
        Vector<int> r;
198
        Ids(0, r);
199
        return r;
200
}
201

    
202
bool HelpWindow::PrevNext(int d, bool allowempty)
203
{
204
        Vector<int> r = Ids();
205
        int id = tree.GetCursor();
206
        if(id < 0)
207
                return false;
208
        int ii = FindIndex(r, id);
209
        if(ii < 0)
210
                return false;
211
        for(;;) {
212
                ii += d;
213
                if(ii >= r.GetCount() || ii < 0)
214
                        return false;
215
                if(!IsNull(tree.Get(r[ii])) || allowempty) {
216
                        tree.SetCursor(r[ii]);
217
                        return true;
218
                }
219
        }
220
}
221

    
222
bool HelpWindow::Next(bool allowempty)
223
{
224
        return PrevNext(1, allowempty);
225
}
226

    
227
bool HelpWindow::Prev(bool allowempty)
228
{
229
        return PrevNext(-1, allowempty);
230
}
231

    
232
void HelpWindow::Serialize(Stream& s)
233
{
234
        s % zoom.m;
235
        s % tree_view;
236
        SerializePlacement(s);
237
        SetZoom();
238
}
239

    
240
void HelpWindow::TreeCursor()
241
{
242
        if(!IsNull(tree))
243
                GoTo(~tree);
244
}
245

    
246
void HelpWindow::CurrentOrHome()
247
{
248
        if(~tree != current_link || IsNull(current_link)) {
249
                if(!IsNull(current_link) && tree.FindSetCursor(current_link))
250
                        return;
251
                for(int i = 0; i < tree.GetLineCount(); i++) {
252
                        Value k = tree.Get(tree.GetItemAtLine(i));
253
                        if(!IsNull(k) && tree.FindSetCursor(k))
254
                                break;
255
                }
256
        }
257
}
258

    
259
Vector<int> HelpWindow::ScPositions(const Vector<int>& p)
260
{
261
        Vector<int> r;
262
        for(int i = 0; i < p.GetCount(); i++) {
263
                int y = max(0, view.GetZoom() * view.Get().GetCaret(p[i], view.GetPage()).top - GetSize().cy / 2);
264
                int ii = FindLowerBound(r, y);
265
                if(ii == r.GetCount() || r[ii] != y)
266
                        r.Insert(ii, y);
267
        }
268
        return r;
269
}
270

    
271
bool HelpWindow::Up(const Vector<int>& poslist)
272
{
273
        int q = view.GetSb();
274
        Vector<int> p = ScPositions(poslist);
275
        for(int i = p.GetCount() - 1; i >= 0; i--)
276
                if(p[i] < q) {
277
                        view.SetSb(p[i]);
278
                        return view.GetSb() != q;
279
                }
280
        return false;
281
}
282

    
283
bool HelpWindow::Down(const Vector<int>& poslist)
284
{
285
        int q = view.GetSb();
286
        Vector<int> p = ScPositions(poslist);
287
        for(int i = 0; i < p.GetCount(); i++)
288
                if(p[i] > q) {
289
                        view.SetSb(p[i]);
290
                        return view.GetSb() != q;
291
                }
292
        return false;
293
}
294

    
295
HelpWindow::HelpWindow()
296
{
297
        tree_view.Horz(tree, view);
298
        tree_view.SetPos(3000);
299
        Add(tree_view.SizePos());
300
        tree_view.Zoom(1);
301
        Sizeable().Zoomable();
302
        Title(t_("Help"));
303
        BackPaint();
304
        view.WhenLink = THISBACK(GoTo);
305
        AddFrame(toolbar);
306
        view.SetZoom(Zoom(1, 1));
307
        zoom.m = 160;
308
        SetZoom();
309
        view.Margins(Rect(12, 0, 12, 0));
310
        SetRect(Ctrl::GetWorkArea().Deflated(80));
311
        tree.WhenSel = THISBACK(TreeCursor);
312
        tree.NoRoot();
313
        Icon(CtrlImg::help());
314
        SetBar();
315
        tree.BackPaint();
316
        view.BackPaintHint();
317
}
318

    
319
END_UPP_NAMESPACE