OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / libedittext / drd64_libedittext_cursorctrl.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_LIBEDITTEXT_CURSORCTRL
38 #include"drd64_libedittext.h"
39
40 #define LOCATION(n)     DRD64_ERR_LOCATION( \
41                                                 DRD64_ERROR_MODULE_LIBEDITTEXT, DRD64_ERROR_ARCH_NODEPEND, \
42                                                 DRD64_SRC_LIBEDITTEXT_CURSORCTRL_SRCID, (n))
43
44
45 /*----------------------------------------------------------------------
46 ----------------------------------------------------------------------*/
47 #define FUNCID_LibEditText_CursorCtrl_AdjustCursorPosition_DeleteString 0x01
48 LIBEDITTEXT_CURSORCTRL_EXTERN
49 int
50         LibEditText_CursorCtrl_AdjustCursorPosition_DeleteString(
51                         LibEditText_TextInfo    *p_tinfo,
52                         LibEditText_LineInfo    *p_line, 
53                         DWord   dw_start,
54                         DWord   dw_len )
55 {
56         int             i_cnt;
57         LibEditText_Cursor      *p_cursor;
58
59         assert( NULL != p_tinfo );
60         assert( NULL != p_line );
61
62         for( i_cnt = 0; i_cnt < p_tinfo->dw_maxcursors; i_cnt++ )       {
63                 p_cursor        = &(p_tinfo->t_cursor[i_cnt]);
64                 
65                 if( 0 == p_cursor->dw_rangelines )      { continue; }
66
67                 if(( p_line->dw_id == p_cursor->dw_line )
68                                 && ( dw_start < p_cursor->dw_pos )
69                                 && ( (dw_start + dw_len) >= p_cursor->dw_pos ))
70                         { p_cursor->dw_pos      = dw_start; }
71         }
72         
73         return 0x00;
74 }
75
76
77 /*----------------------------------------------------------------------
78 ----------------------------------------------------------------------*/
79 #define FUNCID_LibEditText_CursorCtrl_AdjustCursorPosition_InsertString 0x02
80 LIBEDITTEXT_CURSORCTRL_EXTERN
81 int
82         LibEditText_CursorCtrl_AdjustCursorPosition_InsertString(
83                         LibEditText_TextInfo    *p_tinfo,
84                         LibEditText_LineInfo    *p_line, 
85                         DWord   dw_start,
86                         DWord   dw_len )
87 {
88         int             i_cnt;
89         LibEditText_Cursor      *p_cursor;
90
91         assert( NULL != p_tinfo );
92         assert( NULL != p_line );
93
94         for( i_cnt = 0; i_cnt < p_tinfo->dw_maxcursors; i_cnt++ )       {
95                 p_cursor        = &(p_tinfo->t_cursor[i_cnt]);
96                 
97                 if( 0 == p_cursor->dw_rangelines )      { continue; }
98
99                 if(( p_line->dw_id == p_cursor->dw_lineid )
100                                 && ( dw_start < p_cursor->dw_pos )
101                                 && ( (dw_start + dw_len) >= p_cursor->dw_pos ))
102                         { p_cursor->dw_pos      = dw_start; }
103         }
104         
105         return 0x00;
106 }
107
108
109 /*----------------------------------------------------------------------
110 ----------------------------------------------------------------------*/
111 #define FUNCID_LibEditText_CursorCtrl_CheckAdujstCursorRange    0x03
112 int
113         LibEditText_CursorCtrl_CheckAdujstCursorRange(
114                         LibEditText_TextInfo    *p_tinfo,
115                         LibEditText_Cursor              *p_cursor )
116 {
117         DWord   dw_start;
118         DWord   dw_end;
119         DWord   dw_center;
120         DWord   dw_remain;
121         DWord   dw_curlrow;
122         LibEditText_LineInfo    *p_lnow;
123
124         assert( NULL != p_tinfo );
125         assert( NULL != p_cursor );
126
127         // Calc. Cursor Range Start&End Line --- 
128         dw_curlrow      = p_cursor->dw_line;
129         dw_center       = (p_cursor->dw_rangelines - 1) / 2;
130         dw_start        = ((dw_curlrow > dw_center) ? (dw_curlrow - dw_center) : 0);
131         dw_remain       = ((dw_center > dw_curlrow) ? (dw_center - dw_curlrow) : 0);
132         dw_end          = (p_cursor->dw_rangelines - dw_center - 1) + dw_curlrow + dw_remain;
133         dw_end          = ((p_tinfo->dw_maxline > dw_end) ? dw_end: (p_tinfo->dw_maxline - 1));
134         dw_remain       = (p_cursor->dw_rangelines - dw_center - 1) - (dw_end - dw_curlrow);
135         dw_start        = ((dw_start >= dw_remain) ? (dw_start - dw_remain) : 0);
136
137         // Adjust Cursor-Start line ---
138         p_lnow  = LINFO(p_tinfo, p_cursor->dw_start_lineid);
139         if( p_cursor->dw_start_line < dw_start )        {
140                 p_cursor->dw_start_lineid       = p_lnow->dw_next;
141                 p_cursor->dw_start_line++;
142         }
143         else if( p_cursor->dw_start_line > dw_start )   {
144                 p_cursor->dw_start_lineid       = p_lnow->dw_before;
145                 p_cursor->dw_start_line--;
146         }
147
148         // Adjust Cursor-End line ---
149         p_lnow  = LINFO(p_tinfo, p_cursor->dw_end_lineid);
150         if( p_cursor->dw_end_line < dw_end )    {
151                 p_cursor->dw_end_lineid = p_lnow->dw_next;
152                 p_cursor->dw_end_line++;
153         }
154         else if( p_cursor->dw_end_line > dw_end )       {
155                 p_cursor->dw_end_lineid = p_lnow->dw_before;
156                 p_cursor->dw_end_line--;
157         }
158
159         return 0x00;
160 }
161
162
163 /*----------------------------------------------------------------------
164 ----------------------------------------------------------------------*/
165 #define FUNCID_LibEditText_CursorCtrl_InsertLine        0x04
166 LIBEDITTEXT_CURSORCTRL_EXTERN
167 int
168         LibEditText_CursorCtrl_InsertLine(
169                         LibEditText_TextInfo    *p_tinfo,
170                         DWord   dw_line )
171 {
172         int             i_cnt;
173         LibEditText_Cursor      *p_cursor;
174
175         assert( NULL != p_tinfo );
176
177         for( i_cnt = 0; i_cnt < p_tinfo->dw_maxcursors; i_cnt++ )       {
178                 p_cursor        = &(p_tinfo->t_cursor[i_cnt]);
179
180                 if( 0 == p_cursor->dw_rangelines )      { continue; }
181
182                 assert( INVALID_LINE != p_cursor->dw_lineid );
183                 assert( INVALID_LINE != p_cursor->dw_start_lineid );
184                 assert( INVALID_LINE != p_cursor->dw_end_lineid );
185
186                 if(( dw_line <= p_cursor->dw_line )
187                                         && ( p_cursor->dw_line + 1 < p_tinfo->dw_maxline))
188                         { p_cursor->dw_line++; }
189
190                 if(( dw_line <= p_cursor->dw_start_line )
191                                         && ( p_cursor->dw_start_line + 1 < p_tinfo->dw_maxline))
192                         { p_cursor->dw_start_line++; }
193
194                 if(( dw_line <= p_cursor->dw_end_line )
195                                         && ( p_cursor->dw_end_line + 1 < p_tinfo->dw_maxline))
196                         { p_cursor->dw_end_line++; }
197
198                 // Adjust CursorRange
199                 LibEditText_CursorCtrl_CheckAdujstCursorRange( p_tinfo, p_cursor );
200         }
201         
202         return 0x00;
203 }
204
205
206 /*----------------------------------------------------------------------
207 ----------------------------------------------------------------------*/
208 #define FUNCID_LibEditText_CursorCtrl_PreProc_DeleteLine        0x05
209 LIBEDITTEXT_CURSORCTRL_EXTERN
210 int
211         LibEditText_CursorCtrl_PreProc_DeleteLine(
212                         LibEditText_TextInfo    *p_tinfo,
213                         DWord   dw_line )
214 {
215         int             i_cnt;
216         LibEditText_Cursor      *p_cursor;
217         LibEditText_LineInfo    *p_line;
218
219         assert( NULL != p_tinfo );
220         assert( 1 < p_tinfo->dw_maxline );
221
222         for( i_cnt = 0; i_cnt < p_tinfo->dw_maxcursors; i_cnt++ )       {
223                 p_cursor        = &(p_tinfo->t_cursor[i_cnt]);
224
225                 if( 0 == p_cursor->dw_rangelines )      { continue; }
226
227                 assert( INVALID_LINE != p_cursor->dw_lineid );
228                 assert( INVALID_LINE != p_cursor->dw_start_lineid );
229                 assert( INVALID_LINE != p_cursor->dw_end_lineid );
230
231                 if( dw_line == p_cursor->dw_line )      {
232                         if( dw_line + 1 == p_tinfo->dw_maxline )        {
233                                 assert( p_cursor->dw_line > 0 );
234                                 p_cursor->dw_line--;
235                                 p_line  = LINFO(p_tinfo, p_cursor->dw_lineid);
236                                 assert( NULL != p_line );
237                                 p_cursor->dw_lineid     = p_line->dw_before;
238                         }
239                         else    {
240                                 p_cursor->dw_line++;
241                                 p_line  = LINFO(p_tinfo, p_cursor->dw_lineid);
242                                 assert( NULL != p_line );
243                                 p_cursor->dw_lineid     = p_line->dw_next;
244                         }
245                 }
246
247                 if( dw_line == p_cursor->dw_start_line )        {
248                         assert( p_cursor->dw_start_line + 1 < p_tinfo->dw_maxline );
249                         p_cursor->dw_start_line++;
250                         p_line  = LINFO(p_tinfo, p_cursor->dw_start_lineid);
251                         assert( NULL != p_line );
252                         p_cursor->dw_start_lineid       = p_line->dw_next;
253                 }
254
255                 if( dw_line == p_cursor->dw_end_line )  {
256                         if( p_cursor->dw_end_line + 1 == p_tinfo->dw_maxline )  {
257                                 assert( p_cursor->dw_end_line > 0 );
258                                 p_cursor->dw_end_line--;
259                                 p_line  = LINFO(p_tinfo, p_cursor->dw_end_lineid);
260                                 assert( NULL != p_line );
261                                 p_cursor->dw_end_lineid = p_line->dw_before;
262                         }
263                         else    {
264                                 p_cursor->dw_end_line++;
265                                 p_line  = LINFO(p_tinfo, p_cursor->dw_end_lineid);
266                                 assert( NULL != p_line );
267                                 p_cursor->dw_end_lineid = p_line->dw_next;
268                         }
269
270                 }
271         }
272         
273         return 0x00;
274 }
275
276
277 /*----------------------------------------------------------------------
278 ----------------------------------------------------------------------*/
279 #define FUNCID_LibEditText_CursorCtrl_DeleteLine        0x06
280 LIBEDITTEXT_CURSORCTRL_EXTERN
281 int
282         LibEditText_CursorCtrl_DeleteLine(
283                         LibEditText_TextInfo    *p_tinfo,
284                         DWord   dw_line )
285 {
286         int             i_cnt;
287         LibEditText_Cursor      *p_cursor;
288
289         assert( NULL != p_tinfo );
290
291         for( i_cnt = 0; i_cnt < p_tinfo->dw_maxcursors; i_cnt++ )       {
292                 p_cursor        = &(p_tinfo->t_cursor[i_cnt]);
293
294                 if( 0 == p_cursor->dw_rangelines )      { continue; }
295
296                 assert( INVALID_LINE != p_cursor->dw_lineid );
297                 assert( INVALID_LINE != p_cursor->dw_start_lineid );
298                 assert( INVALID_LINE != p_cursor->dw_end_lineid );
299
300                 if(( dw_line < p_cursor->dw_line ) && ( 0 < p_cursor->dw_line ))
301                         { p_cursor->dw_line--; }
302
303                 if(( dw_line < p_cursor->dw_start_line ) && ( 0 < p_cursor->dw_start_line ))
304                         { p_cursor->dw_start_line--; }
305
306                 if(( dw_line < p_cursor->dw_end_line ) && ( 0 < p_cursor->dw_end_line ))
307                         { p_cursor->dw_end_line--; }
308
309                 // Adjust CursorRange
310                 LibEditText_CursorCtrl_CheckAdujstCursorRange( p_tinfo, p_cursor );
311         }
312         
313         return 0x00;
314 }
315
316
317 /* EOF of drd64_.c ----------------------------------- */