Xfce Foundation Classes
 « Main Page | Index

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:


You will 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(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 specified title. 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;

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;

These 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);

Font Selection Example

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 XFC" 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 header file for the Font Selection example is <fontselection.hh>:

#include <xfc/main.hh>
#include <xfc/gtk/label.h>
#include <xfc/gtk/window.hh>

using namespace Xfc;

class FontSelectionWindow : public Gtk::Window
    Gtk::Label *label;

    void on_select_font();

    virtual ~FontSelectionWindow();

and the source file is <fontselection.cc>:

#include "fontselection.hh"
#include <xfc/gtk/box.hh>
#include <xfc/gtk/button.hh>
#include <xfc/gtk/fontselection.hh>
#include <xfc/pango/font.hh>
#include <iostream>

    // Sets the border width of the window.
    set_title("Font Selection Example");

    Gtk::VBox *vbox = new Gtk::VBox;

    // Create a label and add it to the vbox
    label = new Gtk::Label("Welcome to XFC");
    label->set_size_request(300, 100);

    // Creates a new button and add it to the vbox
    Gtk::Button *button = new Gtk::Button("Select a font");

    // When the button receives the "clicked" signal, it calls the on_select_font() slot.
    button->signal_clicked().connect(sigc::mem_fun(this, &FontSelectionWindow::on_select_font));

    // The final step is to display this newly created widgets.


    // 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

int main (int argc, char *argv[])
    using namespace Main;

    init(&argc, &argv);

    FontSelectionWindow window;

    return 0;

Compiling Font Selection

If you compiled and installed XFC yourself, you will find the source code for Font Selection in the <examples/howto/fontselection > source directory along with a Makefile. If XFC came pre-installed, or you installed it from an RPM package, you will find the source code in the </usr/share/doc/xfcui-X.X/examples/howto/fontselection> subdirectory. In this case you will have to create the Makefile yourself (replace X.X with the version number of the libXFCui library you have installed).

To create a Makefile for Font Selection, add the following lines to a new text file and save it using the name "Makefile":

CC = g++

CFLAGS = -Wall -O2

fontselection: fontselection.cc fontselection.hh
    $(CC) fontselection.cc -o fontselection $(CFLAGS) `pkg-config xfcui-X.X --cflags --libs`

    rm -f *.o fontselection

If you cut and paste these lines make sure the whitespace before $(CC) and rm is a tab character. When you compile and run this program you will see the following window appear:

Click the 'Select a font' button to display a FontSelectionDialog. Any changes made to the selected font will be applied to the 'Welcome to XFC' label when you click the OK button:

Copyright © 2004-2005 The XFC Development Team Top
XFC 4.4