### Tutorial
« File Selections | Images »

## Font Selection

The font selection widget is an interactive widget that lists all the available fonts, styles and sizes, allowing the user to select a font. It is used in the FontSelectionDialog widget to provide a dialog box for selecting fonts.
A FontSelection can be created with the constructor:

`FontSelection();`

You'll probably not be using this constructor directly. It creates an orphan FontSelection widget which you'll have to parent yourself. The FontSelection widget inherits from the Gtk::VBox.

`FontSelectionDialog(); explicit FontSelectionDialog(const String& title);`

These are the most common font selection constructors. Both constructors create a FontSelectionDialog, the first with the default title, which is just the program name and the second with the title specified. A FontSelectionDialog consists of a VBox containing the FontSelection widget, an HSeparator and an HBox with three buttons, "Ok", "Cancel" and "Apply". The Apply button is not shown by default. You can reach these buttons by using one of the corresponding accessors:

`Gtk::Button* ok_button() const; Gtk::Button* apply_button() const; Gtk::Button* cancel_button() const;`

You can set and retrieve the selected font name by calling either of the following FontSelection or FontSelectionDialog methods:

`bool set_font_name(const String& fontname); String get_font_name() const;`

You can set and retrieve the text shown in the preview area by calling either of the following FontSelection or FontSelectionDialog methods:

`void set_preview_text(const String& text); String get_preview_text() const;`

There are two FontSelection methods which can be used to retrieve Pango font information:

`Pango::FontFamily* get_font_family() const; Pango::FontFace* get_font_face() const;`

`
` The methods return the current font family and face.
To change the font used by a widget that displays text you will need to create a FontDescription. You can do this with the following Pango::FontDescription constructor:

`FontDescription(const String& desc);`

The desc argument is the string returned by the call to get_font_name() above. To change a widget's font you must call the Gtk::Widget method:

`void modify_font(const Pango::FontDescription& font_desc);`

Here's a simple example demonstrating the use of the FontSelectionDialog. The program displays a window that contains two widgets: a label displaying the text "Welcome to Inti" and a button. When the button is clicked a FontSelectionDialog appears. Selecting a new font, font style and/or font size and clicking on the "OK" button will result in the selected font being applied to the text in the label. The FontSelectionDialog looks like this:
The header file for the FontSelection example is fontselection.h:

`#include<inti/main.h> #include <inti/core.h> #include <inti/gtk/label.h> using namespace Inti; class FontSelectionWindow : public Gtk::Window { Gtk::Label *label; protected: void on_select_font(); public: FontSelectionWindow(); virtual ~FontSelectionWindow(); };`

and the source file is fontselection.cc:

`#include"fontselection.h" #include <inti/gtk/button.h> #include <inti/gtk/fontselection.h> #include <inti/pango/font.h> #include <iostream> FontSelectionWindow::FontSelectionWindow() { // Sets the border width of the window. set_title("Font Selection Example"); set_border_width(10); Gtk::VBox *vbox = new Gtk::VBox; add(*vbox); // Create a label and add it to the vbox label = new Gtk::Label("Welcome to Inti"); label->set_size_request(300, 100); vbox->pack_start(*label); // Creates a new button and add it to the vbox Gtk::Button *button = new Gtk::Button("Select a font"); vbox->pack_start(*button); // When the button receives the "clicked" signal, it calls the on_select_font() slot. button->sig_clicked().connect(slot(this, &FontSelectionWindow::on_select_font)); // The final step is to display this newly created widgets. vbox->show_all(); } FontSelectionWindow::~FontSelectionWindow() { } void FontSelectionWindow::on_select_font() { // Create a temporary FontSelectionDialog on the stack Gtk::FontSelectionDialog dialog("Select a font"); // Call Gtk::Dialog::run() which runs a modal dialog if (dialog.run()) { // Get the selected font name` `String font_name = dialog.get_font_name(); // Print the font name to the standard output std::cout << "The selected font name is \"" << font_name << "\"" << std::endl; // Modify the label's font and destroy the dialog label->modify_font(Pango::FontDescription(font_name)); dialog.dispose(); }``` `} int main (int argc, char *argv[]) { using namespace Main; init(&argc, &argv); FontSelectionWindow window; window.sig_destroy().connect(slot(&Inti::Main::quit)); window.show(); run(); return 0; }`

« File Selections | Index Miscellaneous Widgets Top | Images »