1
+ #include " TextInput.h"
2
+
1
3
#include < SDL.h>
2
- #include < algorithm>
3
4
#include < string>
4
- #include < sstream>
5
5
#include < vector>
6
- #include < iostream>
7
6
8
7
#include " KeyPoll.h"
9
- #include " TextInput.h"
10
8
#include " UTF8.h"
11
9
#include " UtilityClass.h"
12
10
#include " Vlogging.h"
13
11
14
- namespace TextInput {
12
+ namespace TextInput
13
+ {
15
14
bool taking_input;
16
15
bool selecting;
17
16
int flash_timer;
@@ -24,11 +23,13 @@ namespace TextInput {
24
23
SDL_Point cursor_select_pos;
25
24
int cursor_x_tallest;
26
25
27
- void init (void ) {
26
+ void init (void )
27
+ {
28
28
taking_input = false ;
29
29
}
30
30
31
- void attach_input (std::vector<std::string>* text) {
31
+ void attach_input (std::vector<std::string>* text)
32
+ {
32
33
taking_input = true ;
33
34
current_text = text;
34
35
selecting = false ;
@@ -41,7 +42,8 @@ namespace TextInput {
41
42
send_cursor_to_end ();
42
43
}
43
44
44
- void attach_input (std::string* text) {
45
+ void attach_input (std::string* text)
46
+ {
45
47
taking_input = true ;
46
48
current_text_line = text;
47
49
selecting = false ;
@@ -58,7 +60,8 @@ namespace TextInput {
58
60
cursor_x_tallest = cursor_pos.x ;
59
61
}
60
62
61
- void detach_input (void ) {
63
+ void detach_input (void )
64
+ {
62
65
taking_input = false ;
63
66
SDL_StopTextInput ();
64
67
}
@@ -70,13 +73,16 @@ namespace TextInput {
70
73
cursor_x_tallest = cursor_pos.x ;
71
74
}
72
75
73
- void insert_text (std::string text) {
76
+ void insert_text (std::string text)
77
+ {
74
78
// Insert text at cursor position, respecting newlines
75
79
// Don't bother deleting selection, already done
76
80
77
81
std::string::iterator it;
78
- for (it = text.begin (); it != text.end (); it++) {
79
- if (*it == ' \n ' ) {
82
+ for (it = text.begin (); it != text.end (); it++)
83
+ {
84
+ if (*it == ' \n ' )
85
+ {
80
86
insert_newline ();
81
87
}
82
88
else if (*it != ' \r ' && *it != ' \0 ' )
@@ -193,7 +199,8 @@ namespace TextInput {
193
199
194
200
SelectionRect rect = reorder_selection_positions ();
195
201
196
- if (rect.y == rect.y2 ) {
202
+ if (rect.y == rect.y2 )
203
+ {
197
204
return UTF8_substr (get_line (rect.y ), rect.x , rect.x2 );
198
205
}
199
206
@@ -202,14 +209,16 @@ namespace TextInput {
202
209
203
210
// Loop through the lines in between
204
211
int total_length = SDL_strlen (select_part_first_line) + SDL_strlen (select_part_last_line) + 1 ;
205
- for (int i = rect.y + 1 ; i < rect.y2 ; i++) {
212
+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
213
+ {
206
214
total_length += SDL_strlen (get_line (i)) + 1 ;
207
215
}
208
216
209
217
char * select_part = (char *)SDL_malloc (total_length);
210
218
strcpy (select_part, select_part_first_line);
211
219
strcat (select_part, " \n " );
212
- for (int i = rect.y + 1 ; i < rect.y2 ; i++) {
220
+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
221
+ {
213
222
strcat (select_part, get_line (i));
214
223
strcat (select_part, " \n " );
215
224
}
@@ -221,15 +230,19 @@ namespace TextInput {
221
230
return select_part;
222
231
}
223
232
224
- SelectionRect reorder_selection_positions (void ) {
233
+ SelectionRect reorder_selection_positions (void )
234
+ {
225
235
SelectionRect positions;
226
236
bool in_front = false ;
227
237
228
- if (cursor_pos.y > cursor_select_pos.y ) {
238
+ if (cursor_pos.y > cursor_select_pos.y )
239
+ {
229
240
in_front = true ;
230
241
}
231
- else if (cursor_pos.y == cursor_select_pos.y ) {
232
- if (cursor_pos.x >= cursor_select_pos.x ) {
242
+ else if (cursor_pos.y == cursor_select_pos.y )
243
+ {
244
+ if (cursor_pos.x >= cursor_select_pos.x )
245
+ {
233
246
in_front = true ;
234
247
}
235
248
}
@@ -254,7 +267,8 @@ namespace TextInput {
254
267
255
268
void remove_characters (int x, int y, int x2, int y2)
256
269
{
257
- if (x == x2 && y == y2) {
270
+ if (x == x2 && y == y2)
271
+ {
258
272
return ;
259
273
}
260
274
// Get the rest of the last line
@@ -295,14 +309,17 @@ namespace TextInput {
295
309
void move_cursor_up (void )
296
310
{
297
311
bool reset = process_selection (); // Only returns true if you don't hold shift
298
- if (reset && (cursor_pos.y > cursor_select_pos.y )) {
312
+ if (reset && cursor_pos.y > cursor_select_pos.y )
313
+ {
299
314
cursor_pos.y = cursor_select_pos.y ;
300
315
}
301
316
302
- if (cursor_pos.y > 0 ) {
317
+ if (cursor_pos.y > 0 )
318
+ {
303
319
cursor_pos.y --;
304
320
cursor_pos.x = cursor_x_tallest;
305
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
321
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
322
+ {
306
323
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
307
324
}
308
325
}
@@ -311,14 +328,17 @@ namespace TextInput {
311
328
void move_cursor_down (void )
312
329
{
313
330
bool reset = process_selection (); // Only returns true if you don't hold shift
314
- if (reset && (cursor_pos.y < cursor_select_pos.y )) {
331
+ if (reset && cursor_pos.y < cursor_select_pos.y )
332
+ {
315
333
cursor_pos.y = cursor_select_pos.y ;
316
334
}
317
335
318
- if (cursor_pos.y < get_lines () - 1 ) {
336
+ if (cursor_pos.y < get_lines () - 1 )
337
+ {
319
338
cursor_pos.y ++;
320
339
cursor_pos.x = cursor_x_tallest;
321
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
340
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
341
+ {
322
342
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
323
343
}
324
344
}
@@ -330,17 +350,19 @@ namespace TextInput {
330
350
331
351
bool is_word_part (char character)
332
352
{
333
- return character > 0x7F || isalpha (character) || isdigit (character) || character == ' _' || character == ' -' ;
353
+ return (( short ) character) < 0 || (( short ) character) > 0x7F || SDL_isdigit (character) || SDL_isdigit (character) || character == ' _' || character == ' -' ;
334
354
}
335
355
336
356
void move_cursor_left (void )
337
357
{
338
358
bool reset = process_selection (); // Only returns true if you don't hold shift
339
- if (reset) {
359
+ if (reset)
360
+ {
340
361
cursor_pos.x = cursor_select_pos.x ;
341
362
}
342
363
343
- if (cursor_pos.x == 0 && cursor_pos.y == 0 ) {
364
+ if (cursor_pos.x == 0 && cursor_pos.y == 0 )
365
+ {
344
366
cursor_x_tallest = 0 ;
345
367
return ;
346
368
}
@@ -354,7 +376,7 @@ namespace TextInput {
354
376
355
377
while (true )
356
378
{
357
- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
379
+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
358
380
if (is_word_part (character[0 ]) != is_word)
359
381
{
360
382
SDL_free (character);
@@ -372,7 +394,8 @@ namespace TextInput {
372
394
}
373
395
else
374
396
{
375
- if (cursor_pos.x > 0 ) {
397
+ if (cursor_pos.x > 0 )
398
+ {
376
399
cursor_pos.x --;
377
400
}
378
401
else if (cursor_pos.y > 0 )
@@ -388,11 +411,13 @@ namespace TextInput {
388
411
void move_cursor_right (void )
389
412
{
390
413
bool reset = process_selection (); // Only returns true if you don't hold shift
391
- if (reset) {
414
+ if (reset)
415
+ {
392
416
cursor_pos.x = cursor_select_pos.x ;
393
417
}
394
418
395
- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 ) {
419
+ if (cursor_pos.x >= (int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 )
420
+ {
396
421
cursor_x_tallest = cursor_pos.x ;
397
422
return ;
398
423
}
@@ -406,15 +431,15 @@ namespace TextInput {
406
431
407
432
while (true )
408
433
{
409
- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
434
+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
410
435
if (is_word_part (character[0 ]) != is_word)
411
436
{
412
437
SDL_free (character);
413
438
break ;
414
439
}
415
440
SDL_free (character);
416
441
cursor_pos.x ++;
417
- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
442
+ if (cursor_pos.x >= ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
418
443
{
419
444
cursor_pos.y ++;
420
445
cursor_pos.x = 0 ;
@@ -425,7 +450,8 @@ namespace TextInput {
425
450
else
426
451
{
427
452
428
- if (cursor_pos.x < UTF8_total_codepoints (get_line (cursor_pos.y ))) {
453
+ if (cursor_pos.x < (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
454
+ {
429
455
cursor_pos.x ++;
430
456
}
431
457
else if (cursor_pos.y < get_lines () - 1 )
@@ -441,7 +467,8 @@ namespace TextInput {
441
467
void backspace (void )
442
468
{
443
469
// The user pressed backspace.
444
- if (selecting) {
470
+ if (selecting)
471
+ {
445
472
remove_selection ();
446
473
return ;
447
474
}
@@ -481,7 +508,7 @@ namespace TextInput {
481
508
int start_x = cursor_pos.x ;
482
509
while (true )
483
510
{
484
- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
511
+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
485
512
if (is_word_part (character[0 ]) != is_word)
486
513
{
487
514
SDL_free (character);
@@ -537,12 +564,13 @@ namespace TextInput {
537
564
void delete_key (void )
538
565
{
539
566
// The user pressed delete.
540
- if (selecting) {
567
+ if (selecting)
568
+ {
541
569
remove_selection ();
542
570
return ;
543
571
}
544
572
545
- if (cursor_pos.x == UTF8_total_codepoints (get_line (cursor_pos.y )))
573
+ if (cursor_pos.x == ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
546
574
{
547
575
if (cursor_pos.y < get_lines () - 1 )
548
576
{
@@ -570,7 +598,7 @@ namespace TextInput {
570
598
int start_x = cursor_pos.x ;
571
599
while (true )
572
600
{
573
- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
601
+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
574
602
if (is_word_part (character[0 ]) != is_word)
575
603
{
576
604
SDL_free (character);
@@ -598,10 +626,12 @@ namespace TextInput {
598
626
}
599
627
}
600
628
601
- void handle_events (SDL_Event e) {
629
+ void handle_events (SDL_Event e)
630
+ {
602
631
if (!taking_input) return ;
603
632
604
- if (e.type == SDL_KEYDOWN) {
633
+ if (e.type == SDL_KEYDOWN)
634
+ {
605
635
// Show cursor!!
606
636
flash_timer = 0 ;
607
637
@@ -613,7 +643,8 @@ namespace TextInput {
613
643
}
614
644
else if (e.key .keysym .sym == SDLK_v && SDL_GetModState () & KMOD_CTRL)
615
645
{
616
- if (selecting) {
646
+ if (selecting)
647
+ {
617
648
remove_selection ();
618
649
}
619
650
char * clipboard_text = SDL_GetClipboardText ();
@@ -695,20 +726,24 @@ namespace TextInput {
695
726
else if (e.key .keysym .sym == SDLK_PAGEUP && can_move_cursor)
696
727
{
697
728
cursor_pos.y -= 10 ;
698
- if (cursor_pos.y < 0 ) {
729
+ if (cursor_pos.y < 0 )
730
+ {
699
731
cursor_pos.y = 0 ;
700
732
}
701
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
733
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
734
+ {
702
735
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
703
736
}
704
737
}
705
738
else if (e.key .keysym .sym == SDLK_PAGEDOWN && can_move_cursor)
706
739
{
707
740
cursor_pos.y += 10 ;
708
- if (cursor_pos.y >= get_lines ()) {
741
+ if (cursor_pos.y >= get_lines ())
742
+ {
709
743
cursor_pos.y = get_lines () - 1 ;
710
744
}
711
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
745
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
746
+ {
712
747
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
713
748
}
714
749
}
@@ -720,7 +755,8 @@ namespace TextInput {
720
755
flash_timer = 0 ;
721
756
722
757
// Append character(s)
723
- if (selecting) {
758
+ if (selecting)
759
+ {
724
760
remove_selection ();
725
761
}
726
762
insert_text (e.text .text );
0 commit comments