vfs: check userland buffers before reading them.
[haiku.git] / src / apps / charactermap / UnicodeBlockView.cpp
blobee60811e2364efedfca817046d8836058899a482
1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "UnicodeBlockView.h"
9 #include <stdio.h>
10 #include <string.h>
12 #include "UnicodeBlocks.h"
15 BlockListItem::BlockListItem(const char* label, uint32 blockIndex)
16 : BStringItem(label),
17 fBlockIndex(blockIndex)
22 // #pragma mark -
25 UnicodeBlockView::UnicodeBlockView(const char* name)
26 : BListView(name),
27 fBlocks(kNumUnicodeBlocks, true),
28 fShowPrivateBlocks(false),
29 fShowContainedBlocksOnly(false)
31 _CreateBlocks();
35 UnicodeBlockView::~UnicodeBlockView()
40 void
41 UnicodeBlockView::SetFilter(const char* filter)
43 fFilter = filter;
44 _UpdateBlocks();
48 void
49 UnicodeBlockView::ShowPrivateBlocks(bool show)
51 if (fShowPrivateBlocks == show)
52 return;
54 fShowPrivateBlocks = show;
55 _UpdateBlocks();
59 void
60 UnicodeBlockView::ShowContainedBlocksOnly(bool show)
62 if (fShowContainedBlocksOnly == show)
63 return;
65 fShowContainedBlocksOnly = show;
66 _UpdateBlocks();
70 bool
71 UnicodeBlockView::IsShowingBlock(int32 blockIndex) const
73 if (blockIndex < 0 || blockIndex >= (int32)kNumUnicodeBlocks)
74 return false;
76 if (!fShowPrivateBlocks && kUnicodeBlocks[blockIndex].private_block)
77 return false;
79 return true;
83 void
84 UnicodeBlockView::_UpdateBlocks()
86 MakeEmpty();
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;
94 if (!IsShowingBlock(i))
95 continue;
97 AddItem(fBlocks.ItemAt(i));
102 void
103 UnicodeBlockView::_CreateBlocks()
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);
110 float width = StringWidth(item->Text());
111 if (minWidth < width)
112 minWidth = width;
115 SetExplicitMinSize(BSize(minWidth / 2, 32));
116 SetExplicitMaxSize(BSize(minWidth, B_SIZE_UNSET));
118 _UpdateBlocks();
122 void
123 UnicodeBlockView::SelectBlockForCharacter(uint32 character)
125 // find block containing the character
126 int32 blockNumber = BlockForCharacter(character);
128 if (blockNumber > 0) {
129 BlockListItem* block = fBlocks.ItemAt(blockNumber);
131 int32 blockIndex = IndexOf(block);
133 if (blockIndex >= 0) {
134 Select(blockIndex);
135 ScrollToSelection();