PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Cairomm Surface Speichern/Wiederherstellen



barton4
26-01-2010, 17:05
Hi,

Ich benutze cairomm in einem gtkmm Programm. Auf einem DrawingArea Widget wird etwas gezeichnet. Das was auf der DrawingArea "draufgezeichnet" wurde, soll gespeichert werden. Bei bestimmten events soll es möglich sein den vorher gespeichert Content wieder einzufügen.

Folgender Bsp Code sollte dies eigentlich tun:



#include <gtkmm.h>
#include <cairomm/context.h>
#include <gdk/gdkkeysyms.h>
#include <iostream>
using namespace std;



class GraphDrawingArea : public Gtk::DrawingArea
{
private:
Cairo::RefPtr<Cairo::Surface> mSurface;
public:
GraphDrawingArea()
{
this->add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::BUTTON_PRESS_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK );
}

bool on_button_press_event(GdkEventButton* event)
{
//das gespeicherte this->mSurface soll wiederhergestellt werden
cout<<"degub: button-event"<<endl;
Glib::RefPtr<Gdk::Window> window = get_window();
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();

//inhalt wird wiederhergestellt
cr->set_source(this->mSurface, 0, 0);
cr->paint();
}

bool on_motion_notify_event (GdkEventMotion* event)
{
//diese funktion überschreibt den inhalt einfach
cout<<"debug: motion-event"<<endl;
Glib::RefPtr<Gdk::Window> window = get_window();
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();

cr->set_source_rgb(0,0,0);
cr->paint();
cr->set_source_rgb(0,0,1);
cr->rectangle(10, 10, 100, 100);
cr->fill();
}

bool on_expose_event(GdkEventExpose* event)
{
Glib::RefPtr<Gdk::Window> window = get_window();
Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
Gtk::Allocation allocation = get_allocation();
const int width = allocation.get_width();
const int height = allocation.get_height();

//das erste Bild was erschein soll gespeichert werden
cr->set_source_rgb(1, 0, 0);
for(int i=0; i<width; i+=20)
for(int j=0; j<height; j+=20)
{
cr->save();
//cr->rotate(0.4);
cr->rectangle(i, j, 18, 18);
cr->fill();
cr->restore();
}

//inhalt wird gespeichert???
this->mSurface = Cairo::Surface::create(cr->get_target (), Cairo::CONTENT_COLOR_ALPHA, width, height) ;
}
};

int main(int argc, char** argv)
{
Gtk::Main mainGtk(argc, argv);
Gtk::Window window;
GraphDrawingArea draw;
window.set_default_size(800, 768);
window.add(draw);
window.show_all_children();
Gtk::Main::run(window);
}


- mit einer Mausbewegung wird etwas neues in die DrawingArea gezeichnet
- mit einem Klick sollte das anfängliche Bild wiederhergesellt werden, leider ohne Erfolg :-(

hat jemand eine Idee?