cairo-boilerplate-beos.cpp [plain text]
#include "cairo-boilerplate.h"
#include <cairo-beos.h>
#include <Application.h>
#include <Window.h>
#include <View.h>
#include <Bitmap.h>
class CairoTestWindow : public BWindow
{
public:
CairoTestWindow(BRect frame, const char* title);
virtual ~CairoTestWindow();
BView* View() const { return mView; }
private:
BView* mView;
};
CairoTestWindow::CairoTestWindow(BRect frame, const char* title)
: BWindow(frame, title, B_TITLED_WINDOW,
B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
{
mView = new BView(frame, "CairoWindowTestView", B_FOLLOW_ALL_SIDES, 0);
AddChild(mView);
Show();
Lock();
Sync();
mView->SetViewColor(B_TRANSPARENT_COLOR);
mView->Sync();
Unlock();
}
CairoTestWindow::~CairoTestWindow()
{
RemoveChild(mView);
delete mView;
}
class nsBeOSApp : public BApplication
{
public:
nsBeOSApp(sem_id sem) : BApplication(GetAppSig()), init(sem)
{}
void ReadyToRun()
{
release_sem(init);
}
static int32 Main(void *args)
{
nsBeOSApp *app = new nsBeOSApp( (sem_id)args );
if(app == NULL)
return B_ERROR;
return app->Run();
}
private:
const char *GetAppSig()
{
return "application/x-vnd.cairo-test-app";
}
sem_id init;
};
class AppRunner
{
public:
AppRunner();
~AppRunner();
};
AppRunner::AppRunner()
{
if (be_app)
return;
sem_id initsem = create_sem(0, "Cairo BApplication init");
if (initsem < B_OK) {
fprintf (stderr, "Error creating BeOS initialization semaphore\n");
return;
}
thread_id tid = spawn_thread(nsBeOSApp::Main, "Cairo/BeOS test", B_NORMAL_PRIORITY, (void *)initsem);
if (tid < B_OK || B_OK != resume_thread(tid)) {
fprintf (stderr, "Error spawning thread\n");
return;
}
if (B_OK != acquire_sem(initsem)) {
fprintf (stderr, "Error acquiring semaphore\n");
return;
}
delete_sem(initsem);
return;
}
AppRunner::~AppRunner()
{
if (be_app) {
if (be_app->Lock())
be_app->Quit();
delete be_app;
be_app = NULL;
}
}
static AppRunner sAppRunner;
struct beos_boilerplate_closure {
BView* view;
BBitmap* bitmap;
BWindow* window;
};
static cairo_surface_t *
_cairo_boilerplate_beos_create_surface (const char *name,
cairo_content_t content,
double width,
double height,
cairo_boilerplate_mode_t mode,
void **closure)
{
float right = width ? width - 1 : 0;
float bottom = height ? height - 1 : 0;
BRect rect(0.0, 0.0, right, bottom);
CairoTestWindow* wnd = new CairoTestWindow(rect, name);
beos_boilerplate_closure* bclosure = new beos_boilerplate_closure;
bclosure->view = wnd->View();
bclosure->bitmap = NULL;
bclosure->window = wnd;
*closure = bclosure;
return cairo_beos_surface_create(wnd->View());
}
static void
_cairo_boilerplate_beos_cleanup (void *closure)
{
beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure);
bclosure->window->Lock();
bclosure->window->Quit();
delete bclosure;
}
static cairo_surface_t *
_cairo_boilerplate_beos_create_surface_for_bitmap (const char *name,
cairo_content_t content,
double width,
double height,
cairo_boilerplate_mode_t mode,
void **closure)
{
BRect rect(0.0, 0.0, width - 1, height - 1);
color_space beosformat = (content == CAIRO_CONTENT_COLOR_ALPHA) ? B_RGBA32
: B_RGB32;
BBitmap* bmp = new BBitmap(rect, beosformat, true);
BView* view = new BView(rect, "Cairo test view", B_FOLLOW_ALL_SIDES, 0);
bmp->AddChild(view);
beos_boilerplate_closure* bclosure = new beos_boilerplate_closure;
bclosure->view = view;
bclosure->bitmap = bmp;
bclosure->window = NULL;
*closure = bclosure;
return cairo_beos_surface_create_for_bitmap(view, bmp);
}
static void
_cairo_boilerplate_beos_cleanup_bitmap (void *closure)
{
beos_boilerplate_closure* bclosure = reinterpret_cast<beos_boilerplate_closure*>(closure);
bclosure->bitmap->RemoveChild(bclosure->view);
delete bclosure->view;
delete bclosure->bitmap;
delete bclosure;
}
static const cairo_boilerplate_target_t targets[] = {
{
"beos", "beos", NULL, NULL,
CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR, 1,
_cairo_boilerplate_beos_create_surface,
NULL, NULL,
_cairo_boilerplate_get_image_surface,
cairo_surface_write_to_png,
_cairo_boilerplate_beos_cleanup
},
{
"beos-bitmap", "beos", NULL, NULL,
CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR, 1,
_cairo_boilerplate_beos_create_surface_for_bitmap,
NULL, NULL,
_cairo_boilerplate_get_image_surface,
cairo_surface_write_to_png,
_cairo_boilerplate_beos_cleanup_bitmap
},
{
"beos-bitmap", "beos", NULL, NULL,
CAIRO_SURFACE_TYPE_BEOS, CAIRO_CONTENT_COLOR_ALPHA, 1,
_cairo_boilerplate_beos_create_surface_for_bitmap,
NULL, NULL,
_cairo_boilerplate_get_image_surface,
cairo_surface_write_to_png,
_cairo_boilerplate_beos_cleanup_bitmap
},
};
CAIRO_BOILERPLATE (beos, targets)