xref: /haiku/src/apps/charactermap/UnicodeBlockView.cpp (revision 91054f1d38dd7827c0f0ba9490c213775ec7b471)
1 /*
2  * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "UnicodeBlockView.h"
8 
9 #include <stdio.h>
10 #include <string.h>
11 
12 #include "UnicodeBlocks.h"
13 
14 
15 BlockListItem::BlockListItem(const char* label, uint32 blockIndex)
16 	: BStringItem(label),
17 	fBlockIndex(blockIndex)
18 {
19 }
20 
21 
22 //	#pragma mark -
23 
24 
25 UnicodeBlockView::UnicodeBlockView(const char* name)
26 	: BListView(name),
27 	fBlocks(kNumUnicodeBlocks, true),
28 	fShowPrivateBlocks(false),
29 	fShowContainedBlocksOnly(false)
30 {
31 	_CreateBlocks();
32 }
33 
34 
35 UnicodeBlockView::~UnicodeBlockView()
36 {
37 }
38 
39 
40 void
41 UnicodeBlockView::SetFilter(const char* filter)
42 {
43 	fFilter = filter;
44 	_UpdateBlocks();
45 }
46 
47 
48 void
49 UnicodeBlockView::ShowPrivateBlocks(bool show)
50 {
51 	if (fShowPrivateBlocks == show)
52 		return;
53 
54 	fShowPrivateBlocks = show;
55 	_UpdateBlocks();
56 }
57 
58 
59 void
60 UnicodeBlockView::ShowContainedBlocksOnly(bool show)
61 {
62 	if (fShowContainedBlocksOnly == show)
63 		return;
64 
65 	fShowContainedBlocksOnly = show;
66 	_UpdateBlocks();
67 }
68 
69 
70 bool
71 UnicodeBlockView::IsShowingBlock(int32 blockIndex) const
72 {
73 	if (blockIndex < 0 || blockIndex >= (int32)kNumUnicodeBlocks)
74 		return false;
75 
76 	if (!fShowPrivateBlocks && kUnicodeBlocks[blockIndex].private_block)
77 		return false;
78 
79 	return true;
80 }
81 
82 
83 void
84 UnicodeBlockView::_UpdateBlocks()
85 {
86 	MakeEmpty();
87 
88 	for (int32 i = 0; i < fBlocks.CountItems(); i++) {
89 		if (fFilter.Length() != 0) {
90 			if (strcasestr(kUnicodeBlocks[i].name, fFilter.String()) == NULL)
91 				continue;
92 		}
93 
94 		if (!IsShowingBlock(i))
95 			continue;
96 
97 		AddItem(fBlocks.ItemAt(i));
98 	}
99 }
100 
101 
102 void
103 UnicodeBlockView::_CreateBlocks()
104 {
105 	float minWidth = 0;
106 	for (uint32 i = 0; i < kNumUnicodeBlocks; i++) {
107 		BlockListItem* item = new BlockListItem(kUnicodeBlocks[i].name, i);
108 		fBlocks.AddItem(item);
109 
110 		float width = StringWidth(item->Text());
111 		if (minWidth < width)
112 			minWidth = width;
113 	}
114 
115 	SetExplicitMinSize(BSize(minWidth / 2, 32));
116 	SetExplicitMaxSize(BSize(minWidth, B_SIZE_UNSET));
117 
118 	_UpdateBlocks();
119 }
120 
121