@@ -661,9 +661,10 @@ static size_t get_dialog_items_count(const rg_gui_option_t *options)
661661 return opt - options ;
662662}
663663
664- void rg_gui_draw_dialog (const char * title , const rg_gui_option_t * options , int sel )
664+ void rg_gui_draw_dialog (const char * title , const rg_gui_option_t * options , size_t options_count , int sel )
665665{
666- const size_t options_count = get_dialog_items_count (options );
666+ RG_ASSERT_ARG (options || options_count == 0 );
667+
667668 const int sep_width = TEXT_RECT (": " , 0 ).width ;
668669 const int font_height = gui .font_height ;
669670 const int max_box_width = 0.82f * gui .screen_width ;
@@ -672,31 +673,26 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
672673 const int row_padding_y = 0 ; // now handled by draw_text
673674 const int row_padding_x = 8 ;
674675 const int max_inner_width = max_box_width - sep_width - (row_padding_x + box_padding ) * 2 ;
675- const int min_row_height = TEXT_RECT (" " , max_inner_width ).height + row_padding_y * 2 ;
676676
677677 int box_width = box_padding * 2 ;
678678 int box_height = box_padding * 2 + (title ? font_height + 6 : 0 );
679679 int inner_width = TEXT_RECT (title , 0 ).width ;
680680 int col1_width = -1 ;
681681 int col2_width = -1 ;
682- int row_height [options_count ];
682+ uint8_t row_height [options_count ];
683683
684684 for (size_t i = 0 ; i < options_count ; i ++ )
685685 {
686- rg_rect_t label = {0 , 0 , 0 , min_row_height };
687- rg_rect_t value = {0 };
688-
689686 if (options [i ].flags == RG_DIALOG_FLAG_HIDDEN )
690687 {
691688 row_height [i ] = 0 ;
692689 continue ;
693690 }
694691
695- if (options [i ].label )
696- {
697- label = TEXT_RECT (options [i ].label , max_inner_width );
698- inner_width = RG_MAX (inner_width , label .width );
699- }
692+ rg_rect_t label = TEXT_RECT (options [i ].label , max_inner_width );
693+ rg_rect_t value = {0 };
694+
695+ inner_width = RG_MAX (inner_width , label .width );
700696
701697 if (options [i ].value )
702698 {
@@ -735,15 +731,14 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
735731 }
736732
737733 int top_i = 0 ;
734+ int end_i = 0 ;
738735
736+ // Find top of page that contains selection
739737 if (sel >= 0 && sel < options_count )
740738 {
741- int yy = y ;
742-
743- for (int i = 0 ; i < options_count ; i ++ )
739+ for (int yy = y , i = 0 ; i <= sel ; i ++ )
744740 {
745741 yy += row_height [i ];
746-
747742 if (yy >= box_y + box_height )
748743 {
749744 if (sel < i )
@@ -754,8 +749,7 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
754749 }
755750 }
756751
757- int i = top_i ;
758- for (; i < options_count ; i ++ )
752+ for (int i = top_i ; i < options_count ; i ++ )
759753 {
760754 uint16_t color , fg , bg ;
761755 int xx = x + row_padding_x ;
@@ -776,6 +770,8 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
776770 if (y + row_height [i ] >= box_y + box_height )
777771 break ;
778772
773+ end_i = i ;
774+
779775 if (options [i ].flags == RG_DIALOG_FLAG_HIDDEN )
780776 continue ;
781777
@@ -822,7 +818,7 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s
822818 rg_gui_draw_rect (x + 2 , y - 4 , 2 , 2 , 0 , 0 , gui .style .scrollbar );
823819 }
824820
825- if (i < options_count )
821+ if (end_i + 1 < options_count )
826822 {
827823 int x = box_x + box_width - 10 ;
828824 int y = box_y + box_height - 6 ;
@@ -846,7 +842,7 @@ void rg_gui_draw_message(const char *format, ...)
846842 RG_DIALOG_END ,
847843 };
848844 // FIXME: Should rg_display_force_redraw() be called? Before? After? Both?
849- rg_gui_draw_dialog (NULL , options , 0 );
845+ rg_gui_draw_dialog (NULL , options , 1 , 0 );
850846}
851847
852848intptr_t rg_gui_dialog (const char * title , const rg_gui_option_t * options_const , int selected_index )
@@ -883,7 +879,7 @@ intptr_t rg_gui_dialog(const char *title, const rg_gui_option_t *options_const,
883879 }
884880
885881 rg_gui_draw_status_bars ();
886- rg_gui_draw_dialog (title , options , sel );
882+ rg_gui_draw_dialog (title , options , options_count , sel );
887883 rg_input_wait_for_key (RG_KEY_ALL , false, 1000 );
888884 rg_task_delay (80 );
889885
@@ -975,7 +971,7 @@ intptr_t rg_gui_dialog(const char *title, const rg_gui_option_t *options_const,
975971
976972 if (redraw )
977973 {
978- rg_gui_draw_dialog (title , options , sel );
974+ rg_gui_draw_dialog (title , options , options_count , sel );
979975 redraw = false;
980976 }
981977
0 commit comments