@@ -24,11 +24,13 @@ namespace TextInput {
2424 SDL_Point cursor_select_pos;
2525 int cursor_x_tallest;
2626
27- void init (void ) {
27+ void init (void )
28+ {
2829 taking_input = false ;
2930 }
3031
31- void attach_input (std::vector<std::string>* text) {
32+ void attach_input (std::vector<std::string>* text)
33+ {
3234 taking_input = true ;
3335 current_text = text;
3436 selecting = false ;
@@ -41,7 +43,8 @@ namespace TextInput {
4143 send_cursor_to_end ();
4244 }
4345
44- void attach_input (std::string* text) {
46+ void attach_input (std::string* text)
47+ {
4548 taking_input = true ;
4649 current_text_line = text;
4750 selecting = false ;
@@ -58,7 +61,8 @@ namespace TextInput {
5861 cursor_x_tallest = cursor_pos.x ;
5962 }
6063
61- void detach_input (void ) {
64+ void detach_input (void )
65+ {
6266 taking_input = false ;
6367 SDL_StopTextInput ();
6468 }
@@ -70,13 +74,16 @@ namespace TextInput {
7074 cursor_x_tallest = cursor_pos.x ;
7175 }
7276
73- void insert_text (std::string text) {
77+ void insert_text (std::string text)
78+ {
7479 // Insert text at cursor position, respecting newlines
7580 // Don't bother deleting selection, already done
7681
7782 std::string::iterator it;
78- for (it = text.begin (); it != text.end (); it++) {
79- if (*it == ' \n ' ) {
83+ for (it = text.begin (); it != text.end (); it++)
84+ {
85+ if (*it == ' \n ' )
86+ {
8087 insert_newline ();
8188 }
8289 else if (*it != ' \r ' && *it != ' \0 ' )
@@ -193,7 +200,8 @@ namespace TextInput {
193200
194201 SelectionRect rect = reorder_selection_positions ();
195202
196- if (rect.y == rect.y2 ) {
203+ if (rect.y == rect.y2 )
204+ {
197205 return UTF8_substr (get_line (rect.y ), rect.x , rect.x2 );
198206 }
199207
@@ -202,14 +210,16 @@ namespace TextInput {
202210
203211 // Loop through the lines in between
204212 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++) {
213+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
214+ {
206215 total_length += SDL_strlen (get_line (i)) + 1 ;
207216 }
208217
209218 char * select_part = (char *)SDL_malloc (total_length);
210219 strcpy (select_part, select_part_first_line);
211220 strcat (select_part, " \n " );
212- for (int i = rect.y + 1 ; i < rect.y2 ; i++) {
221+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
222+ {
213223 strcat (select_part, get_line (i));
214224 strcat (select_part, " \n " );
215225 }
@@ -221,15 +231,19 @@ namespace TextInput {
221231 return select_part;
222232 }
223233
224- SelectionRect reorder_selection_positions (void ) {
234+ SelectionRect reorder_selection_positions (void )
235+ {
225236 SelectionRect positions;
226237 bool in_front = false ;
227238
228- if (cursor_pos.y > cursor_select_pos.y ) {
239+ if (cursor_pos.y > cursor_select_pos.y )
240+ {
229241 in_front = true ;
230242 }
231- else if (cursor_pos.y == cursor_select_pos.y ) {
232- if (cursor_pos.x >= cursor_select_pos.x ) {
243+ else if (cursor_pos.y == cursor_select_pos.y )
244+ {
245+ if (cursor_pos.x >= cursor_select_pos.x )
246+ {
233247 in_front = true ;
234248 }
235249 }
@@ -254,7 +268,8 @@ namespace TextInput {
254268
255269 void remove_characters (int x, int y, int x2, int y2)
256270 {
257- if (x == x2 && y == y2) {
271+ if (x == x2 && y == y2)
272+ {
258273 return ;
259274 }
260275 // Get the rest of the last line
@@ -295,14 +310,17 @@ namespace TextInput {
295310 void move_cursor_up (void )
296311 {
297312 bool reset = process_selection (); // Only returns true if you don't hold shift
298- if (reset && (cursor_pos.y > cursor_select_pos.y )) {
313+ if (reset && (cursor_pos.y > cursor_select_pos.y ))
314+ {
299315 cursor_pos.y = cursor_select_pos.y ;
300316 }
301317
302- if (cursor_pos.y > 0 ) {
318+ if (cursor_pos.y > 0 )
319+ {
303320 cursor_pos.y --;
304321 cursor_pos.x = cursor_x_tallest;
305- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
322+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
323+ {
306324 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
307325 }
308326 }
@@ -311,14 +329,17 @@ namespace TextInput {
311329 void move_cursor_down (void )
312330 {
313331 bool reset = process_selection (); // Only returns true if you don't hold shift
314- if (reset && (cursor_pos.y < cursor_select_pos.y )) {
332+ if (reset && (cursor_pos.y < cursor_select_pos.y ))
333+ {
315334 cursor_pos.y = cursor_select_pos.y ;
316335 }
317336
318- if (cursor_pos.y < get_lines () - 1 ) {
337+ if (cursor_pos.y < get_lines () - 1 )
338+ {
319339 cursor_pos.y ++;
320340 cursor_pos.x = cursor_x_tallest;
321- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
341+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
342+ {
322343 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
323344 }
324345 }
@@ -330,17 +351,19 @@ namespace TextInput {
330351
331352 bool is_word_part (char character)
332353 {
333- return character > 0x7F || isalpha (character) || isdigit (character) || character == ' _' || character == ' -' ;
354+ return character < 0 || isalpha (character) || isdigit (character) || character == ' _' || character == ' -' ;
334355 }
335356
336357 void move_cursor_left (void )
337358 {
338359 bool reset = process_selection (); // Only returns true if you don't hold shift
339- if (reset) {
360+ if (reset)
361+ {
340362 cursor_pos.x = cursor_select_pos.x ;
341363 }
342364
343- if (cursor_pos.x == 0 && cursor_pos.y == 0 ) {
365+ if (cursor_pos.x == 0 && cursor_pos.y == 0 )
366+ {
344367 cursor_x_tallest = 0 ;
345368 return ;
346369 }
@@ -372,7 +395,8 @@ namespace TextInput {
372395 }
373396 else
374397 {
375- if (cursor_pos.x > 0 ) {
398+ if (cursor_pos.x > 0 )
399+ {
376400 cursor_pos.x --;
377401 }
378402 else if (cursor_pos.y > 0 )
@@ -388,11 +412,13 @@ namespace TextInput {
388412 void move_cursor_right (void )
389413 {
390414 bool reset = process_selection (); // Only returns true if you don't hold shift
391- if (reset) {
415+ if (reset)
416+ {
392417 cursor_pos.x = cursor_select_pos.x ;
393418 }
394419
395- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 ) {
420+ if (cursor_pos.x >= (int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 )
421+ {
396422 cursor_x_tallest = cursor_pos.x ;
397423 return ;
398424 }
@@ -414,7 +440,7 @@ namespace TextInput {
414440 }
415441 SDL_free (character);
416442 cursor_pos.x ++;
417- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
443+ if (cursor_pos.x >= ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
418444 {
419445 cursor_pos.y ++;
420446 cursor_pos.x = 0 ;
@@ -425,7 +451,8 @@ namespace TextInput {
425451 else
426452 {
427453
428- if (cursor_pos.x < UTF8_total_codepoints (get_line (cursor_pos.y ))) {
454+ if (cursor_pos.x < (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
455+ {
429456 cursor_pos.x ++;
430457 }
431458 else if (cursor_pos.y < get_lines () - 1 )
@@ -441,7 +468,8 @@ namespace TextInput {
441468 void backspace (void )
442469 {
443470 // The user pressed backspace.
444- if (selecting) {
471+ if (selecting)
472+ {
445473 remove_selection ();
446474 return ;
447475 }
@@ -537,12 +565,13 @@ namespace TextInput {
537565 void delete_key (void )
538566 {
539567 // The user pressed delete.
540- if (selecting) {
568+ if (selecting)
569+ {
541570 remove_selection ();
542571 return ;
543572 }
544573
545- if (cursor_pos.x == UTF8_total_codepoints (get_line (cursor_pos.y )))
574+ if (cursor_pos.x == ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
546575 {
547576 if (cursor_pos.y < get_lines () - 1 )
548577 {
@@ -598,10 +627,12 @@ namespace TextInput {
598627 }
599628 }
600629
601- void handle_events (SDL_Event e) {
630+ void handle_events (SDL_Event e)
631+ {
602632 if (!taking_input) return ;
603633
604- if (e.type == SDL_KEYDOWN) {
634+ if (e.type == SDL_KEYDOWN)
635+ {
605636 // Show cursor!!
606637 flash_timer = 0 ;
607638
@@ -613,7 +644,8 @@ namespace TextInput {
613644 }
614645 else if (e.key .keysym .sym == SDLK_v && SDL_GetModState () & KMOD_CTRL)
615646 {
616- if (selecting) {
647+ if (selecting)
648+ {
617649 remove_selection ();
618650 }
619651 char * clipboard_text = SDL_GetClipboardText ();
@@ -695,20 +727,24 @@ namespace TextInput {
695727 else if (e.key .keysym .sym == SDLK_PAGEUP && can_move_cursor)
696728 {
697729 cursor_pos.y -= 10 ;
698- if (cursor_pos.y < 0 ) {
730+ if (cursor_pos.y < 0 )
731+ {
699732 cursor_pos.y = 0 ;
700733 }
701- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
734+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
735+ {
702736 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
703737 }
704738 }
705739 else if (e.key .keysym .sym == SDLK_PAGEDOWN && can_move_cursor)
706740 {
707741 cursor_pos.y += 10 ;
708- if (cursor_pos.y >= get_lines ()) {
742+ if (cursor_pos.y >= get_lines ())
743+ {
709744 cursor_pos.y = get_lines () - 1 ;
710745 }
711- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
746+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
747+ {
712748 cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
713749 }
714750 }
@@ -720,7 +756,8 @@ namespace TextInput {
720756 flash_timer = 0 ;
721757
722758 // Append character(s)
723- if (selecting) {
759+ if (selecting)
760+ {
724761 remove_selection ();
725762 }
726763 insert_text (e.text .text );
0 commit comments