-
Notifications
You must be signed in to change notification settings - Fork 23
/
PreferencesWindow.cpp
494 lines (422 loc) · 14.4 KB
/
PreferencesWindow.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
* BePDF: The PDF reader for Haiku.
* Copyright (C) 1997 Benoit Triquet.
* Copyright (C) 1998-2000 Hubert Figuiere.
* Copyright (C) 2000-2011 Michael Pfeiffer.
* Copyright (C) 2013 waddlesplash.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// xpdf
#include <GlobalParams.h>
#include <locale/Catalog.h>
#include <Box.h>
#include <CheckBox.h>
#include <Directory.h>
#include <Entry.h>
#include <LayoutBuilder.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <ObjectList.h>
#include <Path.h>
#include <PopUpMenu.h>
#include <RadioButton.h>
#include <ScrollView.h>
#include <StringView.h>
#include <String.h>
#include "BepdfApplication.h"
#include "LayoutUtils.h"
#include "PreferencesWindow.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "AnnotationWindow"
void PreferencesWindow::UpdateWorkspace() {
GlobalSettings* settings = gApp->GetSettings();
bool enabled = settings->GetOpenInWorkspace();
int32 ws = settings->GetWorkspace();
BMenu *m = mOpenInWorkspace->Menu();
int32 n = m->CountItems();
int32 i;
i = (enabled) ? 1 + ws : 0;
if (i >= n) i = n - 1;
BMenuItem *item = m->ItemAt(i);
item->SetMarked(true);
}
void PreferencesWindow::BuildWorkspaceMenu(BMenu *m) {
m->AddItem(new BMenuItem(B_TRANSLATE("current"), new BMessage(WORKSPACE_CHANGED)));
m->AddSeparatorItem();
int n = count_workspaces();
BString buffer;
for (int i = 1; i <= n; i ++) {
buffer.SetToFormat("%d", i);
m->AddItem(new BMenuItem(buffer.String(), new BMessage(WORKSPACE_CHANGED)));
}
m->SetLabelFromMarked(true);
m->SetRadioMode(true);
}
void PreferencesWindow::SetupView() {
char workspace[3];
GlobalSettings* settings = gApp->GetSettings();
sprintf(workspace, "%d", (int)settings->GetWorkspace());
mPreferences = new BOutlineListView("mPreferences");
BScrollView* prefScroll = new BScrollView("SV", mPreferences,
B_FRAME_EVENTS | B_WILL_DRAW, false, true);
BListItem *item;
mPreferences->AddItem(new BStringItem(B_TRANSLATE("Document")));
mPreferences->AddItem(item = new BStringItem(B_TRANSLATE("Display")));
// reverse order:
mPreferences->AddUnder(new BStringItem(B_TRANSLATE("Asian fonts")), item);
mPreferences->SetSelectionMessage(new BMessage(PREFERENCE_SELECTED));
mPreferences->SetExplicitMinSize(BSize(200, 0));
BCheckBox *pageNumber = new BCheckBox("pageNumber",
B_TRANSLATE("Restore page number"), new BMessage(RESTORE_PAGE_NO_CHANGED));
pageNumber->SetValue(settings->GetRestorePageNumber());
BCheckBox *windowPos = new BCheckBox("windowPos",
B_TRANSLATE("Restore window position and size"),
new BMessage(RESTORE_WINDOW_FRAME_CHANGED));
windowPos->SetValue(settings->GetRestoreWindowFrame());
BPopUpMenu *openMenu = new BPopUpMenu("openMenu");
mOpenInWorkspace = new BMenuField("mOpeninWorkspace",
B_TRANSLATE("Open in workspace:"), openMenu);
BTextControl *author = new BTextControl("author", B_TRANSLATE("Author"),
settings->GetAuthor(), new BMessage(AUTHOR_CHANGED));
BGroupLayout *docBox = BLayoutBuilder::Group<>(B_VERTICAL, 0)
.SetInsets(B_USE_SMALL_INSETS, B_USE_SMALL_INSETS, B_USE_SMALL_INSETS, 0)
.Add(pageNumber)
.Add(windowPos)
.AddStrut(B_USE_ITEM_INSETS)
.Add(mOpenInWorkspace)
.AddStrut(B_USE_SMALL_INSETS)
.Add(author)
.AddGlue();
BBox *document = new BBox("document");
document->SetLabel("Document");
document->AddChild(docBox->View());
BRadioButton *docOnly = new BRadioButton("docOnly",
B_TRANSLATE("Show document view only"),
new BMessage(QUASI_FULLSCREEN_MODE_OFF));
docOnly->SetValue(!(settings->GetQuasiFullscreenMode()));
BRadioButton *docMore = new BRadioButton("docMore",
B_TRANSLATE("Show toolbar, statusbar and scrollbars, too"),
new BMessage(QUASI_FULLSCREEN_MODE_ON));
docMore->SetValue(settings->GetQuasiFullscreenMode());
BGroupLayout *fsBox = BLayoutBuilder::Group<>(B_VERTICAL, 0)
.SetInsets(B_USE_SMALL_INSETS, B_USE_SMALL_INSETS, B_USE_SMALL_INSETS, 0)
.Add(docOnly)
.Add(docMore)
.AddStrut(B_USE_SMALL_INSETS);
BBox *fullscreen = new BBox("fullscreen");
fullscreen->SetLabel(B_TRANSLATE("Fullscreen mode"));
fullscreen->AddChild(fsBox->View());
BRadioButton *filledRect = new BRadioButton("filledRect",
B_TRANSLATE("Filled rectangle"),
new BMessage(FILLED_SELECTION_FILLED));
filledRect->SetValue(settings->GetFilledSelection());
BRadioButton *strokedRect = new BRadioButton("strokedRect",
B_TRANSLATE("Stroked rectangle"),
new BMessage(FILLED_SELECTION_STROKED));
strokedRect->SetValue(!(settings->GetFilledSelection()));
BGroupLayout *rectBox = BLayoutBuilder::Group<>(B_VERTICAL, 0)
.SetInsets(B_USE_SMALL_INSETS, B_USE_SMALL_INSETS, B_USE_SMALL_INSETS, 0)
.Add(filledRect)
.Add(strokedRect)
.AddStrut(B_USE_SMALL_INSETS);
BBox *selection = new BBox("selection");
selection->SetLabel(B_TRANSLATE("Selection rectangle"));
selection->AddChild(rectBox->View());
BCheckBox *scrolling = new BCheckBox("scrolling",
B_TRANSLATE("Invert vertical mouse scrolling"),
new BMessage(INVERT_VERTICAL_SCROLLING_CHANGED));
scrolling->SetValue(settings->GetInvertVerticalScrolling());
BBox *asian = new BBox("asian");
asian->SetLabel(B_TRANSLATE("Asian fonts"));
asian->AddChild(BuildAsianFontsView());
BLayoutBuilder::Group<>(this, B_HORIZONTAL)
.SetInsets(B_USE_WINDOW_INSETS)
.Add(prefScroll)
.AddCards()
.Add(document)
.AddGroup(B_VERTICAL)
.Add(fullscreen)
.Add(selection)
.Add(scrolling)
.AddGlue()
.End()
.Add(asian)
.GetLayout(&mLayers)
.End();
author->SetModificationMessage(new BMessage(AUTHOR_CHANGED));
mLayers->SetVisibleItem((int32)1);
mLayers->SetVisibleItem((int32)0);
mPreferences->Select(0);
BuildWorkspaceMenu(mOpenInWorkspace->Menu());
UpdateWorkspace();
ResizeTo(0, 0);
CenterOnScreen();
}
static int comparePopupLabels(const BMenuField* p1,
const BMenuField* p2) {
return strcmp(p1->Label(), p2->Label());
}
BView* PreferencesWindow::BuildAsianFontsView() {
if (mDisplayCIDFonts->GetSize() == 0) {
return new BView("languages", 0);
}
BObjectList<BMenuField> menus;
for (int32 index = 0; index < mDisplayCIDFonts->GetSize(); index ++) {
BString name;
BString file;
DisplayCIDFonts::Type type;
mDisplayCIDFonts->Get(index, name, file, type);
BString label(B_TRANSLATE(name.String()));
label << ":";
BPopUpMenu *popupInner = new BPopUpMenu("");
BMenuField *popup = new BMenuField("popup", label.String(), popupInner);
FillFontFileMenu(popup, name.String(), file.String());
menus.AddItem(popup);
}
// sort MPopup alphabetically by label
menus.SortItems(comparePopupLabels);
BStringView *restart = new BStringView("restart",
B_TRANSLATE("Will take effect at next program launch."));
// add popup menus into vertical group
BGridLayout *grid = new BGridLayout();
BView *ret = new BView("ret" , 0);
BLayoutBuilder::Group<>(ret, B_VERTICAL)
.SetInsets(B_USE_SMALL_INSETS)
.Add(grid)
.AddGlue()
.Add(restart);
for (int32 index = 0; index < menus.CountItems(); index ++) {
BMenuField *item = menus.ItemAt(index);
grid->AddItem(item->CreateLabelLayoutItem(), 0, index);
grid->AddItem(item->CreateMenuBarLayoutItem(), 1, index);
}
return ret;
}
static bool endsWith(const BString& string, const char* suffix) {
const char* stringPtr = string.String();
int stringLength = string.Length();
int suffixLength = strlen(suffix);
if (stringLength < suffixLength) {
return gFalse;
}
return strcmp(suffix, &stringPtr[stringLength - suffixLength]) == 0;
}
DisplayCIDFonts::Type PreferencesWindow::GetType(const char* file) {
BString string(file);
string.ToLower();
if (endsWith(string, ".ttf") ||
endsWith(string, ".ttc")) {
return DisplayCIDFonts::kTrueType;
} else if (endsWith(string, ".pfb")) {
return DisplayCIDFonts::kType1;
}
return DisplayCIDFonts::kUnknownType;
}
void PreferencesWindow::FillFontFileMenu(BMenuField* menuField, const char* name, const char* file) {
FillFontFileMenu(menuField, B_SYSTEM_FONTS_DIRECTORY, B_TRANSLATE("System fonts"), name, file);
FillFontFileMenu(menuField, B_USER_FONTS_DIRECTORY, B_TRANSLATE("User fonts"), name, file);
FillFontFileMenu(menuField, B_SYSTEM_NONPACKAGED_FONTS_DIRECTORY, B_TRANSLATE("System fonts"), name, file);
FillFontFileMenu(menuField, B_USER_NONPACKAGED_FONTS_DIRECTORY, B_TRANSLATE("User fonts"), name, file);
// B_USER_FONTS_DIRECTORY is same as B_COMMON_FONTS_DIRECTORY in
// BeOS R5
// FillFontFileMenu(menu, B_USER_FONTS_DIRECTORY, B_TRANSLATE("User fonts"), name, file);
}
void PreferencesWindow::FillFontFileMenu(BMenuField* menuField, directory_which which, const char* label, const char* name, const char* file) {
BMenu* menu = menuField->Menu();
mFontMenuFields.AddPointer(name, menuField);
BPath path;
if (find_directory(which, &path) != B_OK) {
return;
}
BDirectory fontDirectory(path.Path());
BEntry entry;
BMenu* fontMenu = NULL;
// iterate through font directory
while (fontDirectory.GetNextEntry(&entry) == B_OK) {
if (!entry.IsDirectory()) {
continue;
}
BDirectory subDirectory(&entry);
BEntry fontFile;
BMenu* fontSubMenu = NULL;
// iteratoe through sub directory in font directory
while (subDirectory.GetNextEntry(&fontFile) == B_OK) {
if (!fontFile.IsFile()) {
continue;
}
BPath filePath;
if (fontFile.GetPath(&filePath) != B_OK) {
continue;
}
// skip unknown font types
if (GetType(filePath.Path()) == DisplayCIDFonts::kUnknownType) {
continue;
}
if (fontMenu == NULL) {
// lazy add font menu
fontMenu = new BMenu(label);
fontMenu->SetRadioMode(false);
menu->AddItem(fontMenu);
}
if (fontSubMenu == NULL) {
// lazy add sub font menu
char name[B_FILE_NAME_LENGTH+1];
entry.GetName(name);
fontSubMenu = new BMenu(name);
fontSubMenu->SetRadioMode(false);
fontMenu->AddItem(fontSubMenu);
}
// add menu item for font
BMessage* msg = new BMessage(DISPLAY_CID_FONT_SELECTED);
msg->AddString("name", name);
msg->AddString("file", filePath.Path());
BMenuItem* item = new BMenuItem(filePath.Leaf(), msg);
fontSubMenu->AddItem(item);
// set label from current font
if (strcmp(filePath.Path(), file) == 0) {
menuField->MenuItem()->SetLabel(filePath.Leaf());
}
}
}
}
void PreferencesWindow::ClearView()
{
}
PreferencesWindow::PreferencesWindow(GlobalSettings *settings, BLooper *looper)
: BWindow(BRect(0, 0, 100, 100)
, B_TRANSLATE("Preferences")
, B_TITLED_WINDOW_LOOK,
B_FLOATING_APP_WINDOW_FEEL,
B_AUTO_UPDATE_SIZE_LIMITS)
, mLooper(looper)
, mSettings(settings)
{
BMessage msg;
settings->GetDisplayCIDFonts(msg);
mDisplayCIDFonts = new DisplayCIDFonts(msg);
AddCommonFilter(new EscapeMessageFilter(this, B_QUIT_REQUESTED));
SetupView();
Show();
}
PreferencesWindow::~PreferencesWindow() {
delete mDisplayCIDFonts;
mDisplayCIDFonts = NULL;
}
class TranslatedFileItem : public BStringItem {
BString mFileName;
public:
TranslatedFileItem(const char* name, const char* filename) :
BStringItem(name), mFileName(filename) { }
const char* FileName() const { return mFileName.String(); }
};
bool PreferencesWindow::DecodeMessage(BMessage *msg, int16 &kind, int16 &which, int16 &index) {
// assert msg->what == PREFERENCES_CHANGED_NOTIFY
return ((B_OK == msg->FindInt16("kind", &kind))
&& (B_OK == msg->FindInt16("which", &which))
&& (B_OK == msg->FindInt16("index", &index)));
}
void PreferencesWindow::Notify(uint32 what) {
BMessage m(what);
mLooper->PostMessage(&m);
}
void PreferencesWindow::NotifyRestartDoc() {
Notify(RESTART_DOC_NOTIFY);
}
void PreferencesWindow::DisplayCIDFontSelected(BMessage* msg) {
BString name;
BString file;
if (msg->FindString("name", &name) != B_OK ||
msg->FindString("file", &file) != B_OK) {
return;
}
DisplayCIDFonts::Type type = GetType(file.String());
// save new font to global settings
mDisplayCIDFonts->Set(name.String(), file.String(), type);
BMessage archive;
mDisplayCIDFonts->Archive(archive);
mSettings->SetDisplayCIDFonts(archive);
// set menu field label
void* pointer;
if (mFontMenuFields.FindPointer(name.String(), &pointer) != B_OK) {
return;
}
BPath path(file.String());
BMenuField* menuField = (BMenuField*)pointer;
menuField->MenuItem()->SetLabel(path.Leaf());
}
void PreferencesWindow::MessageReceived(BMessage *msg) {
switch (msg->what) {
case PREFERENCE_SELECTED:
if (mPreferences->FullListCurrentSelection() >= 0) {
mLayers->SetVisibleItem(mPreferences->FullListCurrentSelection());
}
break;
case RESTORE_PAGE_NO_CHANGED: mSettings->SetRestorePageNumber(IsOn(msg));
break;
case RESTORE_WINDOW_FRAME_CHANGED: mSettings->SetRestoreWindowFrame(IsOn(msg));
break;
case QUASI_FULLSCREEN_MODE_ON:
gApp->GetSettings()->SetQuasiFullscreenMode(true);
break;
case QUASI_FULLSCREEN_MODE_OFF:
gApp->GetSettings()->SetQuasiFullscreenMode(false);
break;
case WORKSPACE_CHANGED: {
int32 index;
if (B_OK == msg->FindInt32("index", &index)) {
if (index == 1) index = 0;
bool enabled = index != 0;
gApp->GetSettings()->SetOpenInWorkspace(enabled);
if (enabled) gApp->GetSettings()->SetWorkspace(index - 1);
UpdateWorkspace();
}
}
break;
case AUTHOR_CHANGED: {
void* p;
if (msg->FindPointer("source", &p) == B_OK) {
BTextControl* t = (BTextControl*)p;
gApp->GetSettings()->SetAuthor(t->Text());
}
}
break;
case INVERT_VERTICAL_SCROLLING_CHANGED:
mSettings->SetInvertVerticalScrolling(IsOn(msg));
Notify(UPDATE_NOTIFY);
break;
case DISPLAY_CID_FONT_SELECTED:
DisplayCIDFontSelected(msg);
break;
case FILLED_SELECTION_FILLED:
case FILLED_SELECTION_STROKED: {
BMessage nmsg(CHANGE_NOTIFY);
nmsg.AddInt16("kind", DISPLAY);
nmsg.AddInt16("which", DISPLAY_FILLED_SELECTION);
nmsg.AddInt16("index", msg->what == FILLED_SELECTION_FILLED ? 0 : 1);
mLooper->PostMessage(&nmsg);
}
break;
}
BWindow::MessageReceived(msg);
}
bool PreferencesWindow::QuitRequested() {
if (mLooper) {
BMessage msg(QUIT_NOTIFY);
mLooper->PostMessage(&msg);
}
ClearView();
return true;
}