1+ #include " TextInput.h"
2+
13#include < SDL.h>
2- #include < algorithm>
34#include < string>
4- #include < sstream>
55#include < vector>
6- #include < iostream>
76
87#include " KeyPoll.h"
9- #include " TextInput.h"
108#include " UTF8.h"
119#include " UtilityClass.h"
1210#include " Vlogging.h"
1311
14- namespace TextInput {
12+ namespace TextInput
13+ {
1514 bool taking_input;
1615 bool selecting;
1716 int flash_timer;
@@ -24,11 +23,13 @@ namespace TextInput {
2423 SDL_Point cursor_select_pos;
2524 int cursor_x_tallest;
2625
27- void init (void ) {
26+ void init (void )
27+ {
2828 taking_input = false ;
2929 }
3030
31- void attach_input (std::vector<std::string>* text) {
31+ void attach_input (std::vector<std::string>* text)
32+ {
3233 taking_input = true ;
3334 current_text = text;
3435 selecting = false ;
@@ -41,7 +42,8 @@ namespace TextInput {
4142 send_cursor_to_end ();
4243 }
4344
44- void attach_input (std::string* text) {
45+ void attach_input (std::string* text)
46+ {
4547 taking_input = true ;
4648 current_text_line = text;
4749 selecting = false ;
@@ -58,7 +60,8 @@ namespace TextInput {
5860 cursor_x_tallest = cursor_pos.x ;
5961 }
6062
61- void detach_input (void ) {
63+ void detach_input (void )
64+ {
6265 taking_input = false ;
6366 SDL_StopTextInput ();
6467 }
@@ -70,13 +73,16 @@ namespace TextInput {
7073 cursor_x_tallest = cursor_pos.x ;
7174 }
7275
73- void insert_text (std::string text) {
76+ void insert_text (std::string text)
77+ {
7478 // Insert text at cursor position, respecting newlines
7579 // Don't bother deleting selection, already done
7680
7781 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+ {
8086 insert_newline ();
8187 }
8288 else if (*it != ' \r ' && *it != ' \0 ' )
@@ -193,7 +199,8 @@ namespace TextInput {
193199
194200 SelectionRect rect = reorder_selection_positions ();
195201
196- if (rect.y == rect.y2 ) {
202+ if (rect.y == rect.y2 )
203+ {
197204 return UTF8_substr (get_line (rect.y ), rect.x , rect.x2 );
198205 }
199206
@@ -202,14 +209,16 @@ namespace TextInput {
202209
203210 // Loop through the lines in between
204211 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+ {
206214 total_length += SDL_strlen (get_line (i)) + 1 ;
207215 }
208216
209217 char * select_part = (char *)SDL_malloc (total_length);
210218 strcpy (select_part, select_part_first_line);
211219 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+ {
213222 strcat (select_part, get_line (i));
214223 strcat (select_part, " \n " );
215224 }
@@ -221,15 +230,19 @@ namespace TextInput {
221230 return select_part;
222231 }
223232
224- SelectionRect reorder_selection_positions (void ) {
233+ SelectionRect reorder_selection_positions (void )
234+ {
225235 SelectionRect positions;
226236 bool in_front = false ;
227237
228- if (cursor_pos.y > cursor_select_pos.y ) {
238+ if (cursor_pos.y > cursor_select_pos.y )
239+ {
229240 in_front = true ;
230241 }
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+ {
233246 in_front = true ;
234247 }
235248 }
@@ -254,7 +267,8 @@ namespace TextInput {
254267
255268 void remove_characters (int x, int y, int x2, int y2)
256269 {
257- if (x == x2 && y == y2) {
270+ if (x == x2 && y == y2)
271+ {
258272 return ;
259273 }
260274 // Get the rest of the last line
@@ -295,14 +309,17 @@ namespace TextInput {
295309 void move_cursor_up (void )
296310 {
297311 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+ {
299314 cursor_pos.y = cursor_select_pos.y ;
300315 }
301316
302- if (cursor_pos.y > 0 ) {
317+ if (cursor_pos.y > 0 )
318+ {
303319 cursor_pos.y --;
304320 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+ {
306323 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
307324 }
308325 }
@@ -311,14 +328,17 @@ namespace TextInput {
311328 void move_cursor_down (void )
312329 {
313330 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+ {
315333 cursor_pos.y = cursor_select_pos.y ;
316334 }
317335
318- if (cursor_pos.y < get_lines () - 1 ) {
336+ if (cursor_pos.y < get_lines () - 1 )
337+ {
319338 cursor_pos.y ++;
320339 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+ {
322342 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
323343 }
324344 }
@@ -330,17 +350,19 @@ namespace TextInput {
330350
331351 bool is_word_part (char character)
332352 {
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 == ' -' ;
334354 }
335355
336356 void move_cursor_left (void )
337357 {
338358 bool reset = process_selection (); // Only returns true if you don't hold shift
339- if (reset) {
359+ if (reset)
360+ {
340361 cursor_pos.x = cursor_select_pos.x ;
341362 }
342363
343- if (cursor_pos.x == 0 && cursor_pos.y == 0 ) {
364+ if (cursor_pos.x == 0 && cursor_pos.y == 0 )
365+ {
344366 cursor_x_tallest = 0 ;
345367 return ;
346368 }
@@ -354,7 +376,7 @@ namespace TextInput {
354376
355377 while (true )
356378 {
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 );
358380 if (is_word_part (character[0 ]) != is_word)
359381 {
360382 SDL_free (character);
@@ -372,7 +394,8 @@ namespace TextInput {
372394 }
373395 else
374396 {
375- if (cursor_pos.x > 0 ) {
397+ if (cursor_pos.x > 0 )
398+ {
376399 cursor_pos.x --;
377400 }
378401 else if (cursor_pos.y > 0 )
@@ -388,11 +411,13 @@ namespace TextInput {
388411 void move_cursor_right (void )
389412 {
390413 bool reset = process_selection (); // Only returns true if you don't hold shift
391- if (reset) {
414+ if (reset)
415+ {
392416 cursor_pos.x = cursor_select_pos.x ;
393417 }
394418
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+ {
396421 cursor_x_tallest = cursor_pos.x ;
397422 return ;
398423 }
@@ -406,15 +431,15 @@ namespace TextInput {
406431
407432 while (true )
408433 {
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 );
410435 if (is_word_part (character[0 ]) != is_word)
411436 {
412437 SDL_free (character);
413438 break ;
414439 }
415440 SDL_free (character);
416441 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 )
418443 {
419444 cursor_pos.y ++;
420445 cursor_pos.x = 0 ;
@@ -425,7 +450,8 @@ namespace TextInput {
425450 else
426451 {
427452
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+ {
429455 cursor_pos.x ++;
430456 }
431457 else if (cursor_pos.y < get_lines () - 1 )
@@ -441,7 +467,8 @@ namespace TextInput {
441467 void backspace (void )
442468 {
443469 // The user pressed backspace.
444- if (selecting) {
470+ if (selecting)
471+ {
445472 remove_selection ();
446473 return ;
447474 }
@@ -481,7 +508,7 @@ namespace TextInput {
481508 int start_x = cursor_pos.x ;
482509 while (true )
483510 {
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 );
485512 if (is_word_part (character[0 ]) != is_word)
486513 {
487514 SDL_free (character);
@@ -537,12 +564,13 @@ namespace TextInput {
537564 void delete_key (void )
538565 {
539566 // The user pressed delete.
540- if (selecting) {
567+ if (selecting)
568+ {
541569 remove_selection ();
542570 return ;
543571 }
544572
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 )))
546574 {
547575 if (cursor_pos.y < get_lines () - 1 )
548576 {
@@ -570,7 +598,7 @@ namespace TextInput {
570598 int start_x = cursor_pos.x ;
571599 while (true )
572600 {
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 );
574602 if (is_word_part (character[0 ]) != is_word)
575603 {
576604 SDL_free (character);
@@ -598,10 +626,12 @@ namespace TextInput {
598626 }
599627 }
600628
601- void handle_events (SDL_Event e) {
629+ void handle_events (SDL_Event e)
630+ {
602631 if (!taking_input) return ;
603632
604- if (e.type == SDL_KEYDOWN) {
633+ if (e.type == SDL_KEYDOWN)
634+ {
605635 // Show cursor!!
606636 flash_timer = 0 ;
607637
@@ -613,7 +643,8 @@ namespace TextInput {
613643 }
614644 else if (e.key .keysym .sym == SDLK_v && SDL_GetModState () & KMOD_CTRL)
615645 {
616- if (selecting) {
646+ if (selecting)
647+ {
617648 remove_selection ();
618649 }
619650 char * clipboard_text = SDL_GetClipboardText ();
@@ -695,20 +726,24 @@ namespace TextInput {
695726 else if (e.key .keysym .sym == SDLK_PAGEUP && can_move_cursor)
696727 {
697728 cursor_pos.y -= 10 ;
698- if (cursor_pos.y < 0 ) {
729+ if (cursor_pos.y < 0 )
730+ {
699731 cursor_pos.y = 0 ;
700732 }
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+ {
702735 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
703736 }
704737 }
705738 else if (e.key .keysym .sym == SDLK_PAGEDOWN && can_move_cursor)
706739 {
707740 cursor_pos.y += 10 ;
708- if (cursor_pos.y >= get_lines ()) {
741+ if (cursor_pos.y >= get_lines ())
742+ {
709743 cursor_pos.y = get_lines () - 1 ;
710744 }
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+ {
712747 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
713748 }
714749 }
@@ -720,7 +755,8 @@ namespace TextInput {
720755 flash_timer = 0 ;
721756
722757 // Append character(s)
723- if (selecting) {
758+ if (selecting)
759+ {
724760 remove_selection ();
725761 }
726762 insert_text (e.text .text );
0 commit comments