Xfce Foundation Classes
 « Main Page | Index

Aspect Frames


The aspect frame widget is like a frame widget, except that it also enforces the aspect ratio (that is, the ratio of the width to the height) of the child widget to have a certain value, adding extra space if necessary. This is useful, for instance, if you want to preview a larger image. The size of the preview should vary when the user resizes the window, but the aspect ratio needs to always match the original image.

To create a new aspect frame use one of the following constructors:

AspectFrame();

AspectFrame(float xalign, float yalign);

AspectFrame(float xalign, float yalign, float ratio);

AspectFrame(const String& label, float xalign, float yalign, float ratio);

Aspect frames have an 'obey_child' (bool) and a 'ratio' (float) property. If obey_child is true, the aspect ratio of a child widget will match the aspect ratio of the size it requests. If obey_child is false the aspect ratio is given by the ratio property. When obey_child is true ratio is ignored. The 'xalign' and 'yalign' arguments specify the alignment, as with Alignment widgets.

The first constructor creates an AspectFrame with the default values: xalign 0.5, yalign 0.5, ratio 1.0 and obey_child true. The second constructor creates an AspectFrame with the specified xalign and yalign values. This constructor takes the aspect ratio from the requisition of the child and sets obey_child to true. The third constructor creates an AspectFrame with the specified xalign, yalign and ratio values. This constructor takes the aspect ratio from the ratio argument and sets obey_child to false. The fourth constructor creates a labeled AspectFrame with the specified xalign, yalign and ratio values. This constructor takes the aspect ratio from the ratio argument and sets obey_child to false.

To change the options of an existing AspectFrame, you can use:

void set(float xalign, float yalign, float ratio, bool obey_child);

AspectFrame is derived from Gtk::Frame so you can call the Frame methods that set the label, label widget or shadow type.

Aspect Frame Example

As an example, the following program uses an AspectFrame to present a drawing area whose aspect ratio will always be 2:1, no matter how the user resizes the top-level window.

The header file for the Aspect Frame example is <aspectframe.hh>:

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

using namespace Xfc;

class AspectFrameWindow : public Gtk::Window
{
public:
    AspectFrameWindow();
    virtual ~AspectFrameWindow();
};

and the source file is <aspectframe.cc>:

#include "aspectframe.hh"
#include <xfc/gtk/aspectframe.hh>
#include <xfc/gtk/drawingarea.hh>

AspectFrameWindow::AspectFrameWindow()
{
    set_title("Aspect Frame");
    set_border_width(10);

    // Create an aspect_frame and add it to our toplevel window
    Gtk::AspectFrame *aspect_frame = new Gtk::AspectFrame("2x1", 0.5, 0.5, 2);
    add(*aspect_frame);
    aspect_frame->show();

    // Now add a child widget to the aspect frame
    Gtk::DrawingArea *drawing_area = new Gtk::DrawingArea;

    // We ask for a 200x200 window but get a 200x100 window since we are forcing a 2x1 aspect ratio.
    drawing_area->set_size_request(200, 200);
    aspect_frame->add(*drawing_area);
    drawing_area->show();
}

AspectFrameWindow::~AspectFrameWindow()
{
}

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

    init(&argc, &argv);

    AspectFrameWindow window;
    window.signal_destroy().connect(sigc::ptr_fun(&Xfc::Main::quit));
    window.show();

    run();
    return 0;
}

Compiling Aspect Frame

If you compiled and installed XFC yourself, you will find the source code for Aspect Frame in the <examples/aspectframe> 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/aspectframe> 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 Aspect Frame, add the following lines to a new text file and save it using the name "Makefile":

CC = g++

CFLAGS = -Wall -O2

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

clean:
    rm -f *.o aspectframe


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:




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