1111#include " UtilityClass.h"
1212#include " Vlogging.h"
1313
14- namespace TextInput {
14+ namespace TextInput
15+ {
1516 bool taking_input;
1617 bool selecting;
1718 int flash_timer;
@@ -24,11 +25,13 @@ namespace TextInput {
2425 SDL_Point cursor_select_pos;
2526 int cursor_x_tallest;
2627
27- void init (void ) {
28+ void init (void )
29+ {
2830 taking_input = false ;
2931 }
3032
31- void attach_input (std::vector<std::string>* text) {
33+ void attach_input (std::vector<std::string>* text)
34+ {
3235 taking_input = true ;
3336 current_text = text;
3437 selecting = false ;
@@ -41,7 +44,8 @@ namespace TextInput {
4144 send_cursor_to_end ();
4245 }
4346
44- void attach_input (std::string* text) {
47+ void attach_input (std::string* text)
48+ {
4549 taking_input = true ;
4650 current_text_line = text;
4751 selecting = false ;
@@ -58,7 +62,8 @@ namespace TextInput {
5862 cursor_x_tallest = cursor_pos.x ;
5963 }
6064
61- void detach_input (void ) {
65+ void detach_input (void )
66+ {
6267 taking_input = false ;
6368 SDL_StopTextInput ();
6469 }
@@ -70,13 +75,16 @@ namespace TextInput {
7075 cursor_x_tallest = cursor_pos.x ;
7176 }
7277
73- void insert_text (std::string text) {
78+ void insert_text (std::string text)
79+ {
7480 // Insert text at cursor position, respecting newlines
7581 // Don't bother deleting selection, already done
7682
7783 std::string::iterator it;
78- for (it = text.begin (); it != text.end (); it++) {
79- if (*it == ' \n ' ) {
84+ for (it = text.begin (); it != text.end (); it++)
85+ {
86+ if (*it == ' \n ' )
87+ {
8088 insert_newline ();
8189 }
8290 else if (*it != ' \r ' && *it != ' \0 ' )
@@ -193,7 +201,8 @@ namespace TextInput {
193201
194202 SelectionRect rect = reorder_selection_positions ();
195203
196- if (rect.y == rect.y2 ) {
204+ if (rect.y == rect.y2 )
205+ {
197206 return UTF8_substr (get_line (rect.y ), rect.x , rect.x2 );
198207 }
199208
@@ -202,14 +211,16 @@ namespace TextInput {
202211
203212 // Loop through the lines in between
204213 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++) {
214+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
215+ {
206216 total_length += SDL_strlen (get_line (i)) + 1 ;
207217 }
208218
209219 char * select_part = (char *)SDL_malloc (total_length);
210220 strcpy (select_part, select_part_first_line);
211221 strcat (select_part, " \n " );
212- for (int i = rect.y + 1 ; i < rect.y2 ; i++) {
222+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
223+ {
213224 strcat (select_part, get_line (i));
214225 strcat (select_part, " \n " );
215226 }
@@ -221,15 +232,19 @@ namespace TextInput {
221232 return select_part;
222233 }
223234
224- SelectionRect reorder_selection_positions (void ) {
235+ SelectionRect reorder_selection_positions (void )
236+ {
225237 SelectionRect positions;
226238 bool in_front = false ;
227239
228- if (cursor_pos.y > cursor_select_pos.y ) {
240+ if (cursor_pos.y > cursor_select_pos.y )
241+ {
229242 in_front = true ;
230243 }
231- else if (cursor_pos.y == cursor_select_pos.y ) {
232- if (cursor_pos.x >= cursor_select_pos.x ) {
244+ else if (cursor_pos.y == cursor_select_pos.y )
245+ {
246+ if (cursor_pos.x >= cursor_select_pos.x )
247+ {
233248 in_front = true ;
234249 }
235250 }
@@ -254,7 +269,8 @@ namespace TextInput {
254269
255270 void remove_characters (int x, int y, int x2, int y2)
256271 {
257- if (x == x2 && y == y2) {
272+ if (x == x2 && y == y2)
273+ {
258274 return ;
259275 }
260276 // Get the rest of the last line
@@ -295,14 +311,17 @@ namespace TextInput {
295311 void move_cursor_up (void )
296312 {
297313 bool reset = process_selection (); // Only returns true if you don't hold shift
298- if (reset && (cursor_pos.y > cursor_select_pos.y )) {
314+ if (reset && cursor_pos.y > cursor_select_pos.y )
315+ {
299316 cursor_pos.y = cursor_select_pos.y ;
300317 }
301318
302- if (cursor_pos.y > 0 ) {
319+ if (cursor_pos.y > 0 )
320+ {
303321 cursor_pos.y --;
304322 cursor_pos.x = cursor_x_tallest;
305- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
323+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
324+ {
306325 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
307326 }
308327 }
@@ -311,14 +330,17 @@ namespace TextInput {
311330 void move_cursor_down (void )
312331 {
313332 bool reset = process_selection (); // Only returns true if you don't hold shift
314- if (reset && (cursor_pos.y < cursor_select_pos.y )) {
333+ if (reset && cursor_pos.y < cursor_select_pos.y )
334+ {
315335 cursor_pos.y = cursor_select_pos.y ;
316336 }
317337
318- if (cursor_pos.y < get_lines () - 1 ) {
338+ if (cursor_pos.y < get_lines () - 1 )
339+ {
319340 cursor_pos.y ++;
320341 cursor_pos.x = cursor_x_tallest;
321- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
342+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
343+ {
322344 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
323345 }
324346 }
@@ -330,17 +352,19 @@ namespace TextInput {
330352
331353 bool is_word_part (char character)
332354 {
333- return character > 0x7F || isalpha (character) || isdigit (character) || character == ' _' || character == ' -' ;
355+ return ( short ) character < 0 || ( short ) character > 0x7F || isalpha (character) || isdigit (character) || character == ' _' || character == ' -' ;
334356 }
335357
336358 void move_cursor_left (void )
337359 {
338360 bool reset = process_selection (); // Only returns true if you don't hold shift
339- if (reset) {
361+ if (reset)
362+ {
340363 cursor_pos.x = cursor_select_pos.x ;
341364 }
342365
343- if (cursor_pos.x == 0 && cursor_pos.y == 0 ) {
366+ if (cursor_pos.x == 0 && cursor_pos.y == 0 )
367+ {
344368 cursor_x_tallest = 0 ;
345369 return ;
346370 }
@@ -354,7 +378,7 @@ namespace TextInput {
354378
355379 while (true )
356380 {
357- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
381+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
358382 if (is_word_part (character[0 ]) != is_word)
359383 {
360384 SDL_free (character);
@@ -372,7 +396,8 @@ namespace TextInput {
372396 }
373397 else
374398 {
375- if (cursor_pos.x > 0 ) {
399+ if (cursor_pos.x > 0 )
400+ {
376401 cursor_pos.x --;
377402 }
378403 else if (cursor_pos.y > 0 )
@@ -388,11 +413,13 @@ namespace TextInput {
388413 void move_cursor_right (void )
389414 {
390415 bool reset = process_selection (); // Only returns true if you don't hold shift
391- if (reset) {
416+ if (reset)
417+ {
392418 cursor_pos.x = cursor_select_pos.x ;
393419 }
394420
395- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 ) {
421+ if (cursor_pos.x >= (int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 )
422+ {
396423 cursor_x_tallest = cursor_pos.x ;
397424 return ;
398425 }
@@ -406,15 +433,15 @@ namespace TextInput {
406433
407434 while (true )
408435 {
409- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
436+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
410437 if (is_word_part (character[0 ]) != is_word)
411438 {
412439 SDL_free (character);
413440 break ;
414441 }
415442 SDL_free (character);
416443 cursor_pos.x ++;
417- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
444+ if (cursor_pos.x >= ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
418445 {
419446 cursor_pos.y ++;
420447 cursor_pos.x = 0 ;
@@ -425,7 +452,8 @@ namespace TextInput {
425452 else
426453 {
427454
428- if (cursor_pos.x < UTF8_total_codepoints (get_line (cursor_pos.y ))) {
455+ if (cursor_pos.x < (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
456+ {
429457 cursor_pos.x ++;
430458 }
431459 else if (cursor_pos.y < get_lines () - 1 )
@@ -441,7 +469,8 @@ namespace TextInput {
441469 void backspace (void )
442470 {
443471 // The user pressed backspace.
444- if (selecting) {
472+ if (selecting)
473+ {
445474 remove_selection ();
446475 return ;
447476 }
@@ -481,7 +510,7 @@ namespace TextInput {
481510 int start_x = cursor_pos.x ;
482511 while (true )
483512 {
484- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
513+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x - 1 , cursor_pos.x );
485514 if (is_word_part (character[0 ]) != is_word)
486515 {
487516 SDL_free (character);
@@ -537,12 +566,13 @@ namespace TextInput {
537566 void delete_key (void )
538567 {
539568 // The user pressed delete.
540- if (selecting) {
569+ if (selecting)
570+ {
541571 remove_selection ();
542572 return ;
543573 }
544574
545- if (cursor_pos.x == UTF8_total_codepoints (get_line (cursor_pos.y )))
575+ if (cursor_pos.x == ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
546576 {
547577 if (cursor_pos.y < get_lines () - 1 )
548578 {
@@ -570,7 +600,7 @@ namespace TextInput {
570600 int start_x = cursor_pos.x ;
571601 while (true )
572602 {
573- char * character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
603+ character = UTF8_substr (get_line (cursor_pos.y ), cursor_pos.x , cursor_pos.x + 1 );
574604 if (is_word_part (character[0 ]) != is_word)
575605 {
576606 SDL_free (character);
@@ -598,10 +628,12 @@ namespace TextInput {
598628 }
599629 }
600630
601- void handle_events (SDL_Event e) {
631+ void handle_events (SDL_Event e)
632+ {
602633 if (!taking_input) return ;
603634
604- if (e.type == SDL_KEYDOWN) {
635+ if (e.type == SDL_KEYDOWN)
636+ {
605637 // Show cursor!!
606638 flash_timer = 0 ;
607639
@@ -613,7 +645,8 @@ namespace TextInput {
613645 }
614646 else if (e.key .keysym .sym == SDLK_v && SDL_GetModState () & KMOD_CTRL)
615647 {
616- if (selecting) {
648+ if (selecting)
649+ {
617650 remove_selection ();
618651 }
619652 char * clipboard_text = SDL_GetClipboardText ();
@@ -695,20 +728,24 @@ namespace TextInput {
695728 else if (e.key .keysym .sym == SDLK_PAGEUP && can_move_cursor)
696729 {
697730 cursor_pos.y -= 10 ;
698- if (cursor_pos.y < 0 ) {
731+ if (cursor_pos.y < 0 )
732+ {
699733 cursor_pos.y = 0 ;
700734 }
701- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
735+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
736+ {
702737 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
703738 }
704739 }
705740 else if (e.key .keysym .sym == SDLK_PAGEDOWN && can_move_cursor)
706741 {
707742 cursor_pos.y += 10 ;
708- if (cursor_pos.y >= get_lines ()) {
743+ if (cursor_pos.y >= get_lines ())
744+ {
709745 cursor_pos.y = get_lines () - 1 ;
710746 }
711- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
747+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
748+ {
712749 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
713750 }
714751 }
@@ -720,7 +757,8 @@ namespace TextInput {
720757 flash_timer = 0 ;
721758
722759 // Append character(s)
723- if (selecting) {
760+ if (selecting)
761+ {
724762 remove_selection ();
725763 }
726764 insert_text (e.text .text );
0 commit comments