From 12e9e9eabd2e60fa339182506ff85f19824b4228 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 9 Jan 2025 12:33:19 +0100 Subject: [PATCH] Dynamically resize Fl_Choice widgets 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. --- vncviewer/OptionsDialog.cxx | 4 ++++ vncviewer/fltk/layout.h | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/vncviewer/OptionsDialog.cxx b/vncviewer/OptionsDialog.cxx index d988c515e9..2be3d8d93d 100644 --- a/vncviewer/OptionsDialog.cxx +++ b/vncviewer/OptionsDialog.cxx @@ -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; } @@ -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; diff --git a/vncviewer/fltk/layout.h b/vncviewer/fltk/layout.h index 01dc73e627..9a74c234d3 100644 --- a/vncviewer/fltk/layout.h +++ b/vncviewer/fltk/layout.h @@ -24,6 +24,7 @@ #ifndef __FLTK_LAYOUT_H__ #define __FLTK_LAYOUT_H__ +#include #include /* Calculates the width of a string as printed by FLTK (pixels) */ @@ -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