-
Notifications
You must be signed in to change notification settings - Fork 1
/
DOCView.cpp
196 lines (153 loc) · 4.58 KB
/
DOCView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// DOCView.cpp
// Copyright (c) 2014 Markus Himmel <[email protected]>
// This file is distributed under the terms of the MIT license
#include "DOCView.h"
#include <stdio.h>
#include <Alert.h>
#include <Catalog.h>
#include <CheckBox.h>
#include <LayoutBuilder.h>
#include <MenuBar.h>
#include <MenuField.h>
#include <PopUpMenu.h>
#include <StringView.h>
#include "DOCTranslator.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "DOCTranslator"
BMenuItem *
generate_item(uint32 index, uint32 current, uint32 messageID,
const char * const labels[])
{
BMessage *message = new BMessage(messageID);
message->AddInt32("value", index);
BMenuItem *item = new BMenuItem(labels[index], message);
item->SetMarked(current == index);
return item;
}
DOCView::DOCView(const BRect &frame, const char *name, uint32 resizeMode,
uint32 flags, TranslatorSettings *settings)
:
BView(frame, name, resizeMode, flags| B_FRAME_EVENTS)
{
fSettings = settings;
fSettings->Acquire();
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
SetLowColor(ViewColor());
fTitle = new BStringView("title", B_TRANSLATE("DOC document translator"));
fTitle->SetFont(be_bold_font);
char version[256];
snprintf(version, sizeof(version), B_TRANSLATE("Version %d.%d.%d, %s"),
static_cast<int>(B_TRANSLATION_MAJOR_VERSION(DOC_TRANSLATOR_VERSION)),
static_cast<int>(B_TRANSLATION_MINOR_VERSION(DOC_TRANSLATOR_VERSION)),
static_cast<int>(B_TRANSLATION_REVISION_VERSION(
DOC_TRANSLATOR_VERSION)),
__DATE__);
fInfo = new BStringView("version", version);
fAuthor = new BStringView(
"Copyright ",
B_UTF8_COPYRIGHT "2014 Markus Himmel <[email protected]>");
BPopUpMenu *menu = new BPopUpMenu("mapping");
uint32 currentMapping = fSettings->SetGetInt32(
DOC_SETTING_CHARACTER_MAPPING);
size_t numMappings = sizeof(mappings) / sizeof(char *);
for (int i = 0; i < numMappings; i++)
{
menu->AddItem(generate_item(i, currentMapping, MSG_CHARMAP_CHANGED,
mappings));
}
fCharacterMapping = new BMenuField(B_TRANSLATE("Character mapping:"), menu);
menu = new BPopUpMenu("paper");
uint32 currentPaper = fSettings->SetGetInt32(
DOC_SETTING_PAPER);
size_t numPaper = sizeof(paper) / sizeof(char *);
for (int i = 0; i < numPaper; i++)
{
menu->AddItem(generate_item(i, currentPaper, MSG_PAPER_CHANGED,
paper));
}
fPaper = new BMenuField(B_TRANSLATE("Paper size:"), menu);
BMessage *msg = new BMessage(DOCView::MSG_LANDSCAPE_CHANGED);
fLandscape = new BCheckBox(B_TRANSLATE("Landscape mode"), msg);
bool current = fSettings->SetGetBool(DOC_SETTING_LANDSCAPE);
fLandscape->SetValue(current);
msg = new BMessage(DOCView::MSG_REMOVED_CHANGED);
fRemoved = new BCheckBox(B_TRANSLATE("Show removed text"), msg);
current = fSettings->SetGetBool(DOC_SETTING_REMOVED);
fRemoved->SetValue(current);
msg = new BMessage(DOCView::MSG_HIDDEN_CHANGED);
fHidden = new BCheckBox(B_TRANSLATE("Show hidden text"), msg);
current = fSettings->SetGetBool(DOC_SETTING_HIDDEN);
fHidden->SetValue(current);
BLayoutBuilder::Group<>(this, B_VERTICAL, 7)
.SetInsets(5)
.Add(fTitle)
.Add(fInfo)
.AddGlue()
.AddGroup(B_HORIZONTAL)
.Add(fPaper)
.AddGlue()
.End()
.AddGroup(B_HORIZONTAL)
.Add(fCharacterMapping)
.AddGlue()
.End()
.Add(fLandscape)
.Add(fHidden)
.Add(fRemoved)
.AddGlue()
.Add(fAuthor)
.AddGlue();
}
DOCView::~DOCView()
{
fSettings->Release();
}
void
DOCView::AllAttached()
{
fLandscape->SetTarget(this);
fRemoved->SetTarget(this);
fHidden->SetTarget(this);
fPaper->Menu()->SetTargetForItems(this);
fCharacterMapping->Menu()->SetTargetForItems(this);
}
void
DOCView::MessageReceived(BMessage *message)
{
int32 value;
bool boolValue;
switch (message->what)
{
case MSG_CHARMAP_CHANGED:
if (message->FindInt32("value", &value) == B_OK)
{
fSettings->SetGetInt32(DOC_SETTING_CHARACTER_MAPPING, &value);
fSettings->SaveSettings();
}
break;
case MSG_LANDSCAPE_CHANGED:
boolValue = fLandscape->Value();
fSettings->SetGetBool(DOC_SETTING_LANDSCAPE, &boolValue);
fSettings->SaveSettings();
break;
case MSG_PAPER_CHANGED:
if (message->FindInt32("value", &value) == B_OK)
{
fSettings->SetGetInt32(DOC_SETTING_PAPER, &value);
fSettings->SaveSettings();
}
case MSG_REMOVED_CHANGED:
boolValue = fRemoved->Value();
fSettings->SetGetBool(DOC_SETTING_REMOVED, &boolValue);
fSettings->SaveSettings();
break;
case MSG_HIDDEN_CHANGED:
boolValue = fHidden->Value();
fSettings->SetGetBool(DOC_SETTING_HIDDEN, &boolValue);
fSettings->SaveSettings();
break;
default:
BView::MessageReceived(message);
break;
}
}