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
15BlockListItem::BlockListItem(const char* label, uint32 blockIndex)
16	: BStringItem(label),
17	fBlockIndex(blockIndex)
18{
19}
20
21
22//	#pragma mark -
23
24
25UnicodeBlockView::UnicodeBlockView(const char* name)
26	: BListView(name),
27	fBlocks(kNumUnicodeBlocks, true),
28	fShowPrivateBlocks(false),
29	fShowContainedBlocksOnly(false)
30{
31	_CreateBlocks();
32}
33
34
35UnicodeBlockView::~UnicodeBlockView()
36{
37}
38
39
40void
41UnicodeBlockView::SetFilter(const char* filter)
42{
43	fFilter = filter;
44	_UpdateBlocks();
45}
46
47
48void
49UnicodeBlockView::ShowPrivateBlocks(bool show)
50{
51	if (fShowPrivateBlocks == show)
52		return;
53
54	fShowPrivateBlocks = show;
55	_UpdateBlocks();
56}
57
58
59void
60UnicodeBlockView::ShowContainedBlocksOnly(bool show)
61{
62	if (fShowContainedBlocksOnly == show)
63		return;
64
65	fShowContainedBlocksOnly = show;
66	_UpdateBlocks();
67}
68
69
70bool
71UnicodeBlockView::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
83void
84UnicodeBlockView::_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
102void
103UnicodeBlockView::_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
122void
123UnicodeBlockView::SelectBlockForCharacter(uint32 character)
124{
125	// find block containing the character
126	int32 blockNumber = BlockForCharacter(character);
127
128	if (blockNumber > 0) {
129		BlockListItem* block = fBlocks.ItemAt(blockNumber);
130
131		int32 blockIndex = IndexOf(block);
132
133		if (blockIndex >= 0) {
134			Select(blockIndex);
135			ScrollToSelection();
136		}
137	}
138}
139