include caret include assert include WindowOp void createCaret HWND hw

  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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "caret.h"
#include <assert.h>
#include "WindowOp.h"
void createCaret( HWND hwnd, PData data )
{
CreateCaret(hwnd, NULL, 1, getCharHeight(hwnd) + 1);
data->isCaretCreated = TRUE;
}
void destroyCaret( HWND hwnd, PData data )
{
assert(data->isCaretCreated);
HideCaret(hwnd);
DestroyCaret();
data->isCaretCreated = FALSE;
}
void showCaret( HWND hwnd, PData data )
{
int caretX;
int caretY;
if (!data->isCaretCreated)
createCaret(hwnd, data);
getCaretXY(hwnd, data, &caretX, &caretY);
SetCaretPos( caretX, caretY );
ShowCaret(hwnd);
}
void getCaretXY( HWND hwnd, PData data, int *caretX, int *caretY )
{
int chX, chY;
int pxX, pxY;
int curBlockIdx;
int curLineIdx;
char const * line;
chX = data->nCaretCharBefore;
chY = 0;
curBlockIdx = data->nFirstBlock;
curLineIdx = data->nFirstLineInFirstBlock;
if (isLower( data->nFirstBlock, data->nFirstLineInFirstBlock, data->nCaretBlock, data->nCaretLineInBlock))
{
while (curBlockIdx != data->nCaretBlock || curLineIdx != data->nCaretLineInBlock)
{
nextLine(data, curBlockIdx, curLineIdx, &curBlockIdx, &curLineIdx);
chY++;
}
}
else
{
while (curBlockIdx != data->nCaretBlock || curLineIdx != data->nCaretLineInBlock)
{
prevLine(data, curBlockIdx, curLineIdx, &curBlockIdx, &curLineIdx);
chY--;
}
}
line = data->text->blocks[data->nCaretBlock]->lines[data->nCaretLineInBlock];
pxX = (chX == 0 ? 0 : getLineLength(hwnd, line, data->nCaretCharBefore));
pxY = chY * getCharHeight(hwnd);
*caretX = pxX;
*caretY = pxY;
}
void updateCaret( HWND hwnd, PData data )
{
int caretX;
int caretY;
assert(data->isCaretCreated);
getCaretXY(hwnd, data, &caretX, &caretY);
SetCaretPos( caretX, caretY );
}
void saveCaretData( PData data, PCaretData cData )
{
int lineIdx;
cData->nBlock = data->nCaretBlock;
cData->nCharBefore = 0;
for (lineIdx = 0; lineIdx < data->nCaretLineInBlock; ++lineIdx)
{
cData->nCharBefore += (int)strlen(data->text->blocks[data->nCaretBlock]->lines[lineIdx]);
}
cData->nCharBefore += data->nCaretCharBefore;
}
void restoreFromCaretData( PData data, PCaretData cData )
{
int lineIdx;
int prevCharsCount;
PBlock block;
block = data->text->blocks[cData->nBlock];
data->nCaretBlock = cData->nBlock;
for (lineIdx = 0, prevCharsCount = 0;
lineIdx < block->linesCount && !(prevCharsCount <= cData->nCharBefore && cData->nCharBefore < prevCharsCount + (int)strlen(block->lines[lineIdx]));
lineIdx++)
{
prevCharsCount += (int)strlen(block->lines[lineIdx]);
}
if (lineIdx == block->linesCount)
{
data->nCaretLineInBlock = block->linesCount - 1;
data->nCaretCharBefore = (int)strlen(block->lines[block->linesCount - 1]);
}
else
{
data->nCaretLineInBlock = lineIdx;
data->nCaretCharBefore = cData->nCharBefore - prevCharsCount;
}
}
void moveCaretRight( HWND hwnd, PData data )
{
CaretData cData;
saveCaretData(data, &cData);
cData.nCharBefore++;
if (cData.nCharBefore > getBlockCharCount(data->text->blocks[cData.nBlock]))
{
if (cData.nBlock + 1 >= data->text->blockCount)
{
cData.nCharBefore--;
}
else
{
cData.nCharBefore = 0;
cData.nBlock++;
}
}
restoreFromCaretData(data, &cData);
}
void moveCaretLeft( HWND hwnd, PData data )
{
CaretData cData;
saveCaretData(data, &cData);
cData.nCharBefore--;
if (cData.nCharBefore < 0)
{
if (cData.nBlock - 1 < 0)
{
cData.nCharBefore++;
}
else
{
cData.nBlock--;
cData.nCharBefore = getBlockCharCount(data->text->blocks[cData.nBlock]);
}
}
restoreFromCaretData(data, &cData);
}