[cairo] Using cario in wxwidgets
Timothée Lecomte
timothee.lecomte at ens.fr
Sun Oct 2 15:11:18 PDT 2005
Hello !
I'm trying to use cairo in wxwidgets. In order to obtain a simple "hello
world" program, I get the following code to compile properly, but I
don't get any text from cairo. Can somebody give me come help ?
Thanks,
Timothée Lecomte
Here is the code :
////
/// ============================================================================/
/// declarations/
/// ============================================================================/
/// ----------------------------------------------------------------------------/
/// headers/
/// ----------------------------------------------------------------------------/
/// For compilers that support precompilation, includes "wx/wx.h"./
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#ifdef __WXGTK__
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <cairo-xlib.h>
#else
#error Not implemented yet
#endif
/// ----------------------------------------------------------------------------/
/// resources/
/// ----------------------------------------------------------------------------/
//* The function that creates a X11 surface from wxwidgets information *//
cairo_surface_t* create_cairo_surface(wxWindow* wxwin, int width, int height);
/// Define a new application type, each program should derive a class from wxApp/
*class* MyApp : *public* wxApp
{
*public*:
/// this one is called on application startup/
*virtual* bool OnInit();
};
/// this is going to be our main frame/
*class* MyFrame : *public* wxFrame
{
*public*:
/// ctor(s)/
MyFrame(const wxString& title);
/// event handlers (these functions should _not_ be virtual)/
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
*private*:
/// any class wishing to process wxWidgets events must use this macro/
DECLARE_EVENT_TABLE()
};
*class* MyPanel : *public* wxPanel
{
*public* :
//* constructor*//
MyPanel( wxWindow* parent);
void OnPaint(wxPaintEvent& event);
//* destructor*//
~MyPanel() {};
*private*:
//* any class wishing to process wxWidgets events must use this macro *//
DECLARE_EVENT_TABLE()
};
/// ----------------------------------------------------------------------------/
/// event tables and other macros for wxWidgets/
/// ----------------------------------------------------------------------------/
/// the event tables connect the wxWidgets events with the functions/
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyPanel, wxPanel)
EVT_PAINT(MyPanel::OnPaint)
END_EVENT_TABLE()
/// Create a new application object/
IMPLEMENT_APP(MyApp)
/// ============================================================================/
/// implementation/
/// ============================================================================/
/// ----------------------------------------------------------------------------/
/// the application class/
/// ----------------------------------------------------------------------------/
/// 'Main program' equivalent: the program execution "starts" here/
bool MyApp::OnInit()
{
/// create the main application window/
MyFrame *frame = *new* MyFrame(_T("Minimal wxWidgets App"));
/// show it/
frame->Show(*true*);
*return* *true*;
}
/// ----------------------------------------------------------------------------/
/// main frame/
/// ----------------------------------------------------------------------------/
/// frame constructor/
MyFrame::MyFrame(const wxString& title)
: wxFrame(NULL, wxID_ANY, title)
{
MyPanel *panel = *new* MyPanel(*this*);
}
MyPanel::MyPanel( wxWindow* parent)
: wxPanel( parent, -1, wxDefaultPosition, parent->GetClientSize())
{
}
void MyPanel::OnPaint(wxPaintEvent& event)
{
cairo_surface_t *surface;
cairo_t *cr;
int width, height;
GetClientSize(&width,&height);
surface = create_cairo_surface(*this*, width, height);
cr = cairo_create(surface);
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_save(cr);
cairo_set_font_size (cr, 10);
cairo_move_to (cr, 10, 10);
//*cairo_rotate(cr, 3.14/ 2);*//
cairo_show_text (cr, "Hello World.");
cairo_restore(cr);
*if* (cairo_status (cr)) {
printf("Cairo is unhappy: %s\n",
cairo_status_to_string (cairo_status (cr)));
exit(0);
}
cairo_destroy(cr);
cairo_surface_destroy (surface);
}
cairo_surface_t* create_cairo_surface(wxWindow* wxwin, int width, int height)
{
#ifdef __WXGTK__
GdkWindow * window = gtk_widget_get_parent_window(wxwin->GetHandle());
*return* cairo_xlib_surface_create(
GDK_WINDOW_XDISPLAY(window),
GDK_WINDOW_XID(window),
GDK_VISUAL_XVISUAL(gdk_drawable_get_visual(window)),
width,
height );
#else
#error This port is not supported by Cairo
#endif
}
More information about the cairo
mailing list