Skip to content

Commit

Permalink
Dynamically resize Fl_Choice widgets
Browse files Browse the repository at this point in the history
The label and options might vary in size, so it's difficult to pick a
size that always fits. Instead, look at the actual strings and
dynamically resize the widget so everything fits.
  • Loading branch information
CendioOssman committed Jan 9, 2025
1 parent e622f57 commit 12e9e9e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vncviewer/OptionsDialog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,8 @@ void OptionsDialog::createInputPage(int tx, int ty, int tw, int th)
fltk_menu_add(cursorTypeChoice, _("Dot"), 0, nullptr, nullptr, 0);
fltk_menu_add(cursorTypeChoice, _("System"), 0, nullptr, nullptr, 0);

fltk_adjust_choice(cursorTypeChoice);

ty += CHOICE_HEIGHT + TIGHT_MARGIN;

}
Expand Down Expand Up @@ -904,6 +906,8 @@ void OptionsDialog::createInputPage(int tx, int ty, int tw, int th)
for (int idx = 0; idx < getMenuKeySymbolCount(); idx++)
fltk_menu_add(menuKeyChoice, getMenuKeySymbols()[idx].name, 0, nullptr, nullptr, 0);

fltk_adjust_choice(menuKeyChoice);

ty += CHOICE_HEIGHT + TIGHT_MARGIN;
}
ty -= TIGHT_MARGIN;
Expand Down
13 changes: 13 additions & 0 deletions vncviewer/fltk/layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#ifndef __FLTK_LAYOUT_H__
#define __FLTK_LAYOUT_H__

#include <FL/Fl_Choice.H>
#include <FL/fl_draw.H>

/* Calculates the width of a string as printed by FLTK (pixels) */
Expand All @@ -38,6 +39,18 @@ static inline int gui_str_len(const char *str)
return (int)(len + 0.5f);
}

/* Adjusts an Fl_Choice so that all options are visible */
static inline void fltk_adjust_choice(Fl_Choice *choice)
{
int option_len;

option_len = 0;
for (int i = 0; i < choice->size(); i++)
option_len = std::max(option_len, gui_str_len(choice->text(i)));

choice->size(option_len + 30, choice->h());
}

/**** MARGINS ****/

#define OUTER_MARGIN 15
Expand Down

0 comments on commit 12e9e9e

Please sign in to comment.