-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPreeditManager.c
143 lines (111 loc) · 4.57 KB
/
PreeditManager.c
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
// Copyright (C) 2022 Daijiro Fukuda <[email protected]>
// This file is part of RaylibIMEInputSampleApp.
// RaylibIMEInputSampleApp 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 3 of the License, or
// (at your option) any later version.
// RaylibIMEInputSampleApp 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, see <https://www.gnu.org/licenses/>.
#include "PreeditManager.h"
#include "raylib.h"
#include <string.h>
#define MAX_PREDIT_LEN 128
static int s_PreeditArray[MAX_PREDIT_LEN];
static int s_PreeditLen = 0;
static const int s_PreeditWindowPosXOffset = 15;
static const int s_PreeditWindowPosYOffset = 5;
static PreeditManager_OnPreeditChanged s_OnPreeditChanged;
static void OnPreedit(int preeditLength, int* preeditString, int blockCount,
int* blockSizes, int focusedBlock, int caret);
#ifdef MANAGE_PREEDIT_CANDIDATE
#define MAX_CANDIDATE_PAGE_SIZE 10
static int* s_CandidateTextsArray[MAX_CANDIDATE_PAGE_SIZE];
static int s_CandidateTextLengths[MAX_CANDIDATE_PAGE_SIZE];
static PreeditManager_OnPreeditCandidateChanged s_OnPreeditCandidateChanged;
static void OnPreeditCandidate(int candidatesCount, int selectedIndex, int pageStart, int pageSize);
#endif
void PreeditManager_Init()
{
SetPreeditCallback(OnPreedit);
#ifdef MANAGE_PREEDIT_CANDIDATE
SetPreeditCandidateCallback(OnPreeditCandidate);
#endif
}
void PreeditManager_UpdateCursorRectangle(int x, int y, int w, int h)
{
SetPreeditCursorRectangle(x, y, w, h);
}
void PreeditManager_SetOnPreeditChanged(PreeditManager_OnPreeditChanged onPreeditChanged)
{
s_OnPreeditChanged = onPreeditChanged;
}
void PreeditManager_GetPreeditText(int* text)
{
memcpy(text, s_PreeditArray, sizeof(int) * s_PreeditLen);
}
static void OnPreedit(int preeditLength, int* preeditString, int blockCount,
int* blockSizes, int focusedBlock, int caret)
{
int blockIndex = -1, remainingBlockSize = 0;
s_PreeditLen = 0;
if (preeditLength == 0 || blockCount == 0)
{
if (s_OnPreeditChanged != NULL)
s_OnPreeditChanged(s_PreeditArray, s_PreeditLen);
return;
}
for (int i = 0; i < preeditLength; i++)
{
if (i == caret)
s_PreeditArray[s_PreeditLen++] = '|';
if (remainingBlockSize == 0)
{
if (blockIndex == focusedBlock)
s_PreeditArray[s_PreeditLen++] = ']';
blockIndex++;
remainingBlockSize = blockSizes[blockIndex];
if (blockIndex == focusedBlock)
s_PreeditArray[s_PreeditLen++] = '[';
}
s_PreeditArray[s_PreeditLen++] = preeditString[i];
remainingBlockSize--;
}
if (blockIndex == focusedBlock)
s_PreeditArray[s_PreeditLen++] = ']';
if (caret == preeditLength)
s_PreeditArray[s_PreeditLen++] = '|';
if (s_OnPreeditChanged != NULL)
s_OnPreeditChanged(s_PreeditArray, s_PreeditLen);
}
#ifdef MANAGE_PREEDIT_CANDIDATE
void PreeditManager_SetOnPreeditCandidateChanged(PreeditManager_OnPreeditCandidateChanged onPreeditCandidateChanged)
{
s_OnPreeditCandidateChanged = onPreeditCandidateChanged;
}
static void OnPreeditCandidate(int candidatesCount, int selectedIndex, int pageStart, int pageSize)
{
int i, j;
int selectedIndexInThePage;
if (MAX_CANDIDATE_PAGE_SIZE < pageSize)
{
printf("Too large candidate page size: %d. Stop processing the candidates.\n", pageSize);
return;
}
for (i = 0; i < pageSize; ++i)
{
int index = i + pageStart;
int textCount;
int* text = GetPreeditCandidate(index, &textCount);
s_CandidateTextsArray[i] = text;
s_CandidateTextLengths[i] = textCount;
if (selectedIndex == index)
selectedIndexInThePage = i;
}
if (s_OnPreeditCandidateChanged != NULL)
s_OnPreeditCandidateChanged(s_CandidateTextsArray, s_CandidateTextLengths, pageSize, selectedIndexInThePage);
}
#endif