Using the font picker

fontcolorpickers.C summarizes how to use a x::w::font_picker widget. This widget provides a popup for selecting the most common font options of the x::w::font.

x::w::font_picker's overloaded current_font() method gives the most recently selected font and its options, or resets the font picker's options from the specified x::w::font's. on_font_update() installs a callback that gets invoked whenever a new font gets officially picked, by the popup's Ok button. Only one callback can be installed at one time, and the initial callback is one of the values that are specified in x::w::font_picker_config when creating the font picker.

The font picker lists all available font families in alphabetical order. An optional list of most recently used fonts appears first, and provides a convenient way to find often use fonts. x::w::font_picker_group_id represents a font family, and the list of most recently used fonts is just a std::vector of them.

The most recently used font list requires the application to install an on_font_update() callback. The callback controls how many most recently used fonts appear first. This callback can be installed after creating the font picker, or by setting it in the x::w::font_picker_config. The first two parameters to the callback are the picked x::w::font_picker_group_id, in addition to the picked x::w::font. The application has complete control over the most recently used vector, and how big it is. As fontcolorpickers.C demonstrates, the callback retrieves the vector of the most recently used fonts from the font picker, updates it, and installs the updated most_recently_used() vector.

The initial list of most recently used fonts gets specified in the x::w::font_picker_config when creating the font picker. The font picker widget's overloaded most_recently_used() method either returns the current list of most recently used fonts, or sets the new list, updating the font picker popup accordingly. An application can be responsible for saving and restoring the list of fonts each time it runs, but it's easier to use the existing support for preserving the position and size of windows and widgets.

A necessary prerequisite for preserving the most recently used fonts is that the table's main application window's size also gets preserved; so fontcolorpickers.C save()s its main window's size (and position), just like the word wrap label example.

Each font picker has a unique identifier, or a label. x::w::font_picker_config's restore() method sets the new font picker's label and restores the font picker's most recently used fonts, together with the font picker's current font, if they were previously save(). All font pickers created by the application must have unique labels.