xref: /haiku/src/apps/charactermap/CharacterWindow.cpp (revision 410ed2fbba58819ac21e27d3676739728416761d)
1 /*
2  * Copyright 2009-2015, Axel Dörfler, axeld@pinc-software.de.
3  * Copyright 2011, Philippe Saint-Pierre, stpere@gmail.com.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include "CharacterWindow.h"
9 
10 #include <stdio.h>
11 #include <string.h>
12 
13 #include <Application.h>
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <File.h>
17 #include <FindDirectory.h>
18 #include <Font.h>
19 #include <LayoutBuilder.h>
20 #include <ListView.h>
21 #include <Menu.h>
22 #include <MenuBar.h>
23 #include <MenuItem.h>
24 #include <MessageFilter.h>
25 #include <Path.h>
26 #include <Roster.h>
27 #include <ScrollView.h>
28 #include <Slider.h>
29 #include <StringView.h>
30 #include <TextControl.h>
31 #include <UnicodeChar.h>
32 
33 #include "CharacterView.h"
34 #include "UnicodeBlockView.h"
35 
36 
37 #undef B_TRANSLATION_CONTEXT
38 #define B_TRANSLATION_CONTEXT "CharacterWindow"
39 
40 
41 static const uint32 kMsgUnicodeBlockSelected = 'unbs';
42 static const uint32 kMsgCharacterChanged = 'chch';
43 static const uint32 kMsgFontSelected = 'fnts';
44 static const uint32 kMsgFontSizeChanged = 'fsch';
45 static const uint32 kMsgPrivateBlocks = 'prbl';
46 static const uint32 kMsgContainedBlocks = 'cnbl';
47 static const uint32 kMsgFilterChanged = 'fltr';
48 static const uint32 kMsgClearFilter = 'clrf';
49 
50 static const int32 kMinFontSize = 10;
51 static const int32 kMaxFontSize = 72;
52 
53 
54 class FontSizeSlider : public BSlider {
55 public:
56 	FontSizeSlider(const char* name, const char* label, BMessage* message,
57 			int32 min, int32 max)
58 		: BSlider(name, label, NULL, min, max, B_HORIZONTAL)
59 	{
60 		SetModificationMessage(message);
61 	}
62 
63 protected:
64 	const char* UpdateText() const
65 	{
66 		snprintf(fText, sizeof(fText), "%" B_PRId32 "pt", Value());
67 		return fText;
68 	}
69 
70 private:
71 	mutable char	fText[32];
72 };
73 
74 
75 class RedirectUpAndDownFilter : public BMessageFilter {
76 public:
77 	RedirectUpAndDownFilter(BHandler* target)
78 		: BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, B_KEY_DOWN),
79 		fTarget(target)
80 	{
81 	}
82 
83 	virtual filter_result Filter(BMessage* message, BHandler** _target)
84 	{
85 		const char* bytes;
86 		if (message->FindString("bytes", &bytes) != B_OK)
87 			return B_DISPATCH_MESSAGE;
88 
89 		if (bytes[0] == B_UP_ARROW
90 			|| bytes[0] == B_DOWN_ARROW)
91 			*_target = fTarget;
92 
93 		return B_DISPATCH_MESSAGE;
94 	}
95 
96 private:
97 	BHandler*	fTarget;
98 };
99 
100 
101 class EscapeMessageFilter : public BMessageFilter {
102 public:
103 	EscapeMessageFilter(uint32 command)
104 		: BMessageFilter(B_ANY_DELIVERY, B_ANY_SOURCE, B_KEY_DOWN),
105 		fCommand(command)
106 	{
107 	}
108 
109 	virtual filter_result Filter(BMessage* message, BHandler** /*_target*/)
110 	{
111 		const char* bytes;
112 		if (message->what != B_KEY_DOWN
113 			|| message->FindString("bytes", &bytes) != B_OK
114 			|| bytes[0] != B_ESCAPE)
115 			return B_DISPATCH_MESSAGE;
116 
117 		Looper()->PostMessage(fCommand);
118 		return B_SKIP_MESSAGE;
119 	}
120 
121 private:
122 	uint32	fCommand;
123 };
124 
125 
126 CharacterWindow::CharacterWindow()
127 	:
128 	BWindow(BRect(100, 100, 700, 550), B_TRANSLATE_SYSTEM_NAME("CharacterMap"),
129 		B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE
130 			| B_AUTO_UPDATE_SIZE_LIMITS)
131 {
132 	BMessage settings;
133 	_LoadSettings(settings);
134 
135 	BRect frame;
136 	if (settings.FindRect("window frame", &frame) == B_OK) {
137 		MoveTo(frame.LeftTop());
138 		ResizeTo(frame.Width(), frame.Height());
139 	} else {
140 		float scaling = be_plain_font->Size() / 12.0f;
141 		ResizeTo(Frame().Width() * scaling, Frame().Height() * scaling);
142 		CenterOnScreen();
143 	}
144 
145 	// create GUI
146 	BMenuBar* menuBar = new BMenuBar("menu");
147 
148 	fFilterControl = new BTextControl(B_TRANSLATE("Filter:"), NULL, NULL);
149 	fFilterControl->SetModificationMessage(new BMessage(kMsgFilterChanged));
150 
151 	BButton* clearButton = new BButton("clear", B_TRANSLATE("Clear"),
152 		new BMessage(kMsgClearFilter));
153 
154 	fUnicodeBlockView = new UnicodeBlockView("unicodeBlocks");
155 	fUnicodeBlockView->SetSelectionMessage(
156 		new BMessage(kMsgUnicodeBlockSelected));
157 
158 	BScrollView* unicodeScroller = new BScrollView("unicodeScroller",
159 		fUnicodeBlockView, 0, false, true);
160 
161 	fCharacterView = new CharacterView("characters");
162 	fCharacterView->SetTarget(this, kMsgCharacterChanged);
163 
164 	fGlyphView = new BStringView("glyph", "");
165 	fGlyphView->SetExplicitMaxSize(BSize(B_SIZE_UNSET,
166 		fGlyphView->PreferredSize().Height()));
167 
168 	// TODO: have a context object shared by CharacterView/UnicodeBlockView
169 	bool show;
170 	if (settings.FindBool("show private blocks", &show) == B_OK) {
171 		fCharacterView->ShowPrivateBlocks(show);
172 		fUnicodeBlockView->ShowPrivateBlocks(show);
173 	}
174 	if (settings.FindBool("show contained blocks only", &show) == B_OK) {
175 		fCharacterView->ShowContainedBlocksOnly(show);
176 		fUnicodeBlockView->ShowContainedBlocksOnly(show);
177 	}
178 
179 	const char* family;
180 	const char* style;
181 	BString displayName;
182 
183 	if (settings.FindString("font family", &family) == B_OK
184 		&& settings.FindString("font style", &style) == B_OK) {
185 		_SetFont(family, style);
186 		displayName << family << " " << style;
187 	} else {
188 		font_family currentFontFamily;
189 		font_style currentFontStyle;
190 		fCharacterView->CharacterFont().GetFamilyAndStyle(&currentFontFamily,
191 			&currentFontStyle);
192 		displayName << currentFontFamily << " " << currentFontStyle;
193 	}
194 
195 	int32 fontSize;
196 	if (settings.FindInt32("font size", &fontSize) == B_OK) {
197 		BFont font = fCharacterView->CharacterFont();
198 		if (fontSize < kMinFontSize)
199 			fontSize = kMinFontSize;
200 		else if (fontSize > kMaxFontSize)
201 			fontSize = kMaxFontSize;
202 		font.SetSize(fontSize);
203 
204 		fCharacterView->SetCharacterFont(font);
205 		fUnicodeBlockView->SetCharacterFont(font);
206 	} else
207 		fontSize = (int32)fCharacterView->CharacterFont().Size();
208 
209 	BScrollView* characterScroller = new BScrollView("characterScroller",
210 		fCharacterView, 0, false, true);
211 
212 	fFontSizeSlider = new FontSizeSlider("fontSizeSlider",
213 		displayName,
214 		new BMessage(kMsgFontSizeChanged), kMinFontSize, kMaxFontSize);
215 	fFontSizeSlider->SetValue(fontSize);
216 
217 	fCodeView = new BStringView("code", "-");
218 	fCodeView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED,
219 		fCodeView->PreferredSize().Height()));
220 
221 	// Set minimum width for character pane to prevent UI
222 	// from jumping when longer code strings are displayed.
223 	// use 'w' character for sizing as it's likely the widest
224 	// character for a Latin font.  40 characters is a little
225 	// wider than needed so hopefully this covers other
226 	// non-Latin fonts that may be wider.
227 	BFont viewFont;
228 	fCodeView->GetFont(&viewFont);
229 	fCharacterView->SetExplicitMinSize(BSize(viewFont.StringWidth("w") * 40,
230 		B_SIZE_UNSET));
231 
232 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
233 		.Add(menuBar)
234 		.AddGroup(B_HORIZONTAL)
235 			.SetInsets(B_USE_WINDOW_SPACING)
236 			.AddGroup(B_VERTICAL)
237 				.AddGroup(B_HORIZONTAL)
238 					.Add(fFilterControl)
239 					.Add(clearButton)
240 				.End()
241 				.Add(unicodeScroller)
242 			.End()
243 			.AddGroup(B_VERTICAL)
244 				.Add(characterScroller)
245 				.Add(fFontSizeSlider)
246 				.AddGroup(B_HORIZONTAL)
247 					.Add(fGlyphView)
248 					.Add(fCodeView);
249 
250 	// Add menu
251 
252 	// "File" menu
253 	BMenu* menu = new BMenu(B_TRANSLATE("File"));
254 	BMenuItem* item;
255 
256 	menu->AddItem(new BMenuItem(B_TRANSLATE("Quit"),
257 		new BMessage(B_QUIT_REQUESTED), 'Q'));
258 	menu->SetTargetForItems(this);
259 	menuBar->AddItem(menu);
260 
261 	menu = new BMenu(B_TRANSLATE("View"));
262 	menu->AddItem(item = new BMenuItem(B_TRANSLATE("Show private blocks"),
263 		new BMessage(kMsgPrivateBlocks)));
264 	item->SetMarked(fCharacterView->IsShowingPrivateBlocks());
265 
266 	menu->AddItem(item = new BMenuItem("Only show blocks contained in font",
267 		new BMessage(kMsgContainedBlocks)));
268 	item->SetMarked(fCharacterView->IsShowingContainedBlocksOnly());
269 	menuBar->AddItem(menu);
270 
271 	fFontMenu = _CreateFontMenu();
272 	menuBar->AddItem(fFontMenu);
273 
274 	AddCommonFilter(new EscapeMessageFilter(kMsgClearFilter));
275 	AddCommonFilter(new RedirectUpAndDownFilter(fUnicodeBlockView));
276 
277 	// TODO: why is this needed?
278 	fUnicodeBlockView->SetTarget(this);
279 
280 	fFilterControl->MakeFocus();
281 
282 	fUnicodeBlockView->SelectBlockForCharacter(0);
283 }
284 
285 
286 CharacterWindow::~CharacterWindow()
287 {
288 }
289 
290 
291 void
292 CharacterWindow::MessageReceived(BMessage* message)
293 {
294 	if (message->WasDropped()) {
295 		const char* text;
296 		ssize_t size;
297 		uint32 c;
298 		if (message->FindInt32("character", (int32*)&c) == B_OK) {
299 			fCharacterView->ScrollToCharacter(c);
300 			return;
301 		} else if (message->FindData("text/plain", B_MIME_TYPE,
302 				(const void**)&text, &size) == B_OK) {
303 			fCharacterView->ScrollToCharacter(BUnicodeChar::FromUTF8(text));
304 			return;
305 		}
306 	}
307 
308 	switch (message->what) {
309 		case B_COPY:
310 			PostMessage(message, fCharacterView);
311 			break;
312 
313 		case kMsgUnicodeBlockSelected:
314 		{
315 			int32 index;
316 			if (message->FindInt32("index", &index) != B_OK
317 				|| index < 0)
318 				break;
319 
320 			BlockListItem* item
321 				= static_cast<BlockListItem*>(fUnicodeBlockView->ItemAt(index));
322 			fCharacterView->ScrollToBlock(item->BlockIndex());
323 
324 			fFilterControl->MakeFocus();
325 			break;
326 		}
327 
328 		case kMsgCharacterChanged:
329 		{
330 			uint32 character;
331 			if (message->FindInt32("character", (int32*)&character) != B_OK)
332 				break;
333 
334 			char utf8[16];
335 			CharacterView::UnicodeToUTF8(character, utf8, sizeof(utf8));
336 
337 			char utf8Hex[32];
338 			CharacterView::UnicodeToUTF8Hex(character, utf8Hex,
339 				sizeof(utf8Hex));
340 
341 			char text[128];
342 			snprintf(text, sizeof(text), " %s: %#" B_PRIx32 " (%" B_PRId32 "), UTF-8: %s",
343 				B_TRANSLATE("Code"), character, character, utf8Hex);
344 
345 			char glyph[20];
346 			snprintf(glyph, sizeof(glyph), "'%s'", utf8);
347 
348 			fGlyphView->SetText(glyph);
349 			fCodeView->SetText(text);
350 
351 			fUnicodeBlockView->SelectBlockForCharacter(character);
352 			break;
353 		}
354 
355 		case kMsgFontSelected:
356 		{
357 			BMenuItem* item;
358 
359 			if (message->FindPointer("source", (void**)&item) != B_OK)
360 				break;
361 
362 			fSelectedFontItem->SetMarked(false);
363 
364 			// If it's the family menu, just select the first style
365 			if (item->Submenu() != NULL) {
366 				item->SetMarked(true);
367 				item = item->Submenu()->ItemAt(0);
368 			}
369 
370 			if (item != NULL) {
371 				item->SetMarked(true);
372 				fSelectedFontItem = item;
373 
374 				_SetFont(item->Menu()->Name(), item->Label());
375 
376 				BString displayName;
377 				displayName << item->Menu()->Name() << " " << item->Label();
378 
379 				fFontSizeSlider->SetLabel(displayName);
380 
381 				item = item->Menu()->Superitem();
382 				item->SetMarked(true);
383 			}
384 			break;
385 		}
386 
387 		case kMsgFontSizeChanged:
388 		{
389 			int32 size = fFontSizeSlider->Value();
390 			if (size < kMinFontSize)
391 				size = kMinFontSize;
392 			else if (size > kMaxFontSize)
393 				size = kMaxFontSize;
394 
395 			BFont font = fCharacterView->CharacterFont();
396 			font.SetSize(size);
397 			fCharacterView->SetCharacterFont(font);
398 			fUnicodeBlockView->SetCharacterFont(font);
399 			break;
400 		}
401 
402 		case kMsgPrivateBlocks:
403 		{
404 			BMenuItem* item;
405 			if (message->FindPointer("source", (void**)&item) != B_OK
406 				|| item == NULL)
407 				break;
408 
409 			item->SetMarked(!item->IsMarked());
410 
411 			fCharacterView->ShowPrivateBlocks(item->IsMarked());
412 			fUnicodeBlockView->ShowPrivateBlocks(item->IsMarked());
413 			break;
414 		}
415 
416 		case kMsgContainedBlocks:
417 		{
418 			BMenuItem* item;
419 			if (message->FindPointer("source", (void**)&item) != B_OK
420 				|| item == NULL)
421 				break;
422 
423 			item->SetMarked(!item->IsMarked());
424 
425 			fCharacterView->ShowContainedBlocksOnly(item->IsMarked());
426 			fUnicodeBlockView->ShowContainedBlocksOnly(item->IsMarked());
427 			break;
428 		}
429 
430 		case kMsgFilterChanged:
431 			fUnicodeBlockView->SetFilter(fFilterControl->Text());
432 			fUnicodeBlockView->Select(0);
433 			break;
434 
435 		case kMsgClearFilter:
436 			fFilterControl->SetText("");
437 			fFilterControl->MakeFocus();
438 			break;
439 
440 		default:
441 			BWindow::MessageReceived(message);
442 			break;
443 	}
444 }
445 
446 
447 bool
448 CharacterWindow::QuitRequested()
449 {
450 	_SaveSettings();
451 	be_app->PostMessage(B_QUIT_REQUESTED);
452 	return true;
453 }
454 
455 
456 status_t
457 CharacterWindow::_OpenSettings(BFile& file, uint32 mode)
458 {
459 	BPath path;
460 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
461 		return B_ERROR;
462 
463 	path.Append("CharacterMap settings");
464 
465 	return file.SetTo(path.Path(), mode);
466 }
467 
468 
469 status_t
470 CharacterWindow::_LoadSettings(BMessage& settings)
471 {
472 	BFile file;
473 	status_t status = _OpenSettings(file, B_READ_ONLY);
474 	if (status != B_OK)
475 		return status;
476 
477 	return settings.Unflatten(&file);
478 }
479 
480 
481 status_t
482 CharacterWindow::_SaveSettings()
483 {
484 	BFile file;
485 	status_t status = _OpenSettings(file, B_WRITE_ONLY | B_CREATE_FILE
486 		| B_ERASE_FILE);
487 	if (status < B_OK)
488 		return status;
489 
490 	BMessage settings('chrm');
491 	status = settings.AddRect("window frame", Frame());
492 	if (status != B_OK)
493 		return status;
494 
495 	if (status == B_OK) {
496 		status = settings.AddBool("show private blocks",
497 			fCharacterView->IsShowingPrivateBlocks());
498 	}
499 	if (status == B_OK) {
500 		status = settings.AddBool("show contained blocks only",
501 			fCharacterView->IsShowingContainedBlocksOnly());
502 	}
503 
504 	if (status == B_OK) {
505 		BFont font = fCharacterView->CharacterFont();
506 		status = settings.AddInt32("font size", font.Size());
507 
508 		font_family family;
509 		font_style style;
510 		if (status == B_OK)
511 			font.GetFamilyAndStyle(&family, &style);
512 		if (status == B_OK)
513 			status = settings.AddString("font family", family);
514 		if (status == B_OK)
515 			status = settings.AddString("font style", style);
516 	}
517 
518 	if (status == B_OK)
519 		status = settings.Flatten(&file);
520 
521 	return status;
522 }
523 
524 
525 void
526 CharacterWindow::_SetFont(const char* family, const char* style)
527 {
528 	BFont font = fCharacterView->CharacterFont();
529 	font.SetFamilyAndStyle(family, style);
530 
531 	fCharacterView->SetCharacterFont(font);
532 	fUnicodeBlockView->SetCharacterFont(font);
533 	fGlyphView->SetFont(&font, B_FONT_FAMILY_AND_STYLE);
534 }
535 
536 
537 BMenu*
538 CharacterWindow::_CreateFontMenu()
539 {
540 	BMenu* menu = new BMenu(B_TRANSLATE("Font"));
541 	_UpdateFontMenu(menu);
542 
543 	return menu;
544 }
545 
546 
547 void
548 CharacterWindow::_UpdateFontMenu(BMenu* menu)
549 {
550 	BMenuItem* item;
551 
552 	while (menu->CountItems() > 0) {
553 		item = menu->RemoveItem(static_cast<int32>(0));
554 		delete(item);
555 	}
556 
557 	font_family currentFamily;
558 	font_style currentStyle;
559 	fCharacterView->CharacterFont().GetFamilyAndStyle(&currentFamily,
560 		&currentStyle);
561 
562 	int32 numFamilies = count_font_families();
563 
564 	menu->SetRadioMode(true);
565 
566 	for (int32 i = 0; i < numFamilies; i++) {
567 		font_family family;
568 		if (get_font_family(i, &family) == B_OK) {
569 			BMenu* subMenu = new BMenu(family);
570 			menu->AddItem(new BMenuItem(subMenu,
571 				new BMessage(kMsgFontSelected)));
572 
573 			int numStyles = count_font_styles(family);
574 			for (int32 j = 0; j < numStyles; j++) {
575 				font_style style;
576 				uint32 flags;
577 				if (get_font_style(family, j, &style, &flags) == B_OK) {
578 					item = new BMenuItem(style, new BMessage(kMsgFontSelected));
579 					subMenu->AddItem(item);
580 
581 					if (!strcmp(family, currentFamily)
582 						&& !strcmp(style, currentStyle)) {
583 						fSelectedFontItem = item;
584 						item->SetMarked(true);
585 					}
586 				}
587 			}
588 		}
589 	}
590 
591 	item = menu->FindItem(currentFamily);
592 	item->SetMarked(true);
593 }
594 
595 
596 void
597 CharacterWindow::MenusBeginning()
598 {
599 	if (update_font_families(false) == true)
600 		_UpdateFontMenu(fFontMenu);
601 }
602