Add static void Fl_Surface_Device::push_current(Fl_Surface_Device *new_current) and pop_current() to set/unset the current drawing surface.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12140 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
parent
0a1eed150a
commit
43935dffeb
@ -40,24 +40,24 @@ class Fl_Widget;
|
||||
|
||||
A drawing surface other than the computer's display, is typically used as follows:
|
||||
<ol><li> Create \c surface, an object from a particular Fl_Surface_Device derived class (e.g., Fl_Copy_Surface, Fl_Printer).
|
||||
<li> Memorize what is the current drawing surface with <tt> Fl_Surface_Device *old_current = Fl_Surface_Device::surface();</tt>
|
||||
<li> Call \c surface->set_current(); to redirect all graphics requests to \c surface which becomes the new
|
||||
<li> Call \c Fl_Surface_Device::push_current(surface); to redirect all graphics requests to \c surface which becomes the new
|
||||
current drawing surface (not necessary with class Fl_Printer because it is done by Fl_Printer::start_job()).
|
||||
<li> At this point any of the \ref fl_drawings (e.g., fl_rect()) or the \ref fl_attributes or \ref drawing_images functions
|
||||
(e.g., fl_draw_image(), Fl_Image::draw()) operates on the new current drawing surface.
|
||||
<li> At this point all of the \ref fl_drawings (e.g., fl_rect()) or the \ref fl_attributes or \ref drawing_images functions
|
||||
(e.g., fl_draw_image(), Fl_Image::draw()) operate on the new current drawing surface.
|
||||
Certain drawing surfaces allow additional ways to draw to them (e.g., Fl_Printer::print_widget(), Fl_Image_Surface::draw()).
|
||||
<li> After all drawing requests have been performed, redirect graphics requests back to their previous destination
|
||||
with \c old_current->set_current();.
|
||||
with \c Fl_Surface_Device::pop_current();.
|
||||
<li> Delete \c surface.
|
||||
</ol>
|
||||
For back-compatibility, it is also possible to use the Fl_Surface_Device::set_current() member function
|
||||
to change the current drawing surface, once to the new surface, once to the previous one.
|
||||
*/
|
||||
class FL_EXPORT Fl_Surface_Device {
|
||||
/** The graphics driver in use by this surface. */
|
||||
Fl_Graphics_Driver *pGraphicsDriver;
|
||||
static Fl_Surface_Device *surface_; // the surface that currently receives graphics output
|
||||
static Fl_Surface_Device *surface_; // the surface that currently receives graphics requests
|
||||
static Fl_Surface_Device *default_surface(); // create surface if none exists yet
|
||||
static Fl_Surface_Device *pre_surface_;
|
||||
// Some drawing surfaces (e.g., Fl_XXX_Image_Surface_Driver) need to re-implement this.
|
||||
// Some drawing surfaces (e.g., Fl_XXX_Image_Surface_Driver) re-implement this.
|
||||
// Gets called each time a surface ceases to be the current drawing surface.
|
||||
virtual void end_current_() {}
|
||||
protected:
|
||||
@ -70,12 +70,14 @@ public:
|
||||
/** \brief Returns the graphics driver of this drawing surface. */
|
||||
inline Fl_Graphics_Driver *driver() {return pGraphicsDriver; };
|
||||
/** The current drawing surface.
|
||||
In other words, the Fl_Surface_Device object that currently receives all graphics output */
|
||||
In other words, the Fl_Surface_Device object that currently receives all graphics requests */
|
||||
static inline Fl_Surface_Device *surface() {
|
||||
return surface_ ? surface_ : default_surface();
|
||||
};
|
||||
/** \brief The destructor. */
|
||||
virtual ~Fl_Surface_Device();
|
||||
static void push_current(Fl_Surface_Device *new_current);
|
||||
static Fl_Surface_Device *pop_current();
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -76,8 +76,7 @@ Fl_RGB_Image* prepare_shape(int w)
|
||||
{
|
||||
// draw a white circle with a hole in it on black background
|
||||
Fl_Image_Surface *surf = new Fl_Image_Surface(w, w);
|
||||
Fl_Surface_Device* current = Fl_Surface_Device::surface();
|
||||
surf->set_current();
|
||||
Fl_Surface_Device::push_current(surf);
|
||||
fl_color(FL_BLACK);
|
||||
fl_rectf(-1, -1, w+2, w+2);
|
||||
fl_color(FL_WHITE);
|
||||
@ -86,7 +85,7 @@ Fl_RGB_Image* prepare_shape(int w)
|
||||
fl_pie(0.7*w,w/2,w/4,w/4,0,360);
|
||||
Fl_RGB_Image* img = surf->image();
|
||||
delete surf;
|
||||
current->set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
return img; // return white image on black background
|
||||
}
|
||||
|
||||
|
@ -42,23 +42,18 @@
|
||||
|
|
||||
+- Fl_..._Graphics_Driver: platform specific graphics driver
|
||||
|
||||
TODO:
|
||||
Window Device to handle creation of surfaces and manage events
|
||||
System Device to handle file system acces, standard dialogs, etc.
|
||||
|
||||
*/
|
||||
|
||||
Fl_Surface_Device *Fl_Surface_Device::pre_surface_ = NULL;
|
||||
|
||||
/** \brief Make this surface the current drawing surface.
|
||||
This surface will receive all future graphics requests. */
|
||||
/** Make this surface the current drawing surface.
|
||||
This surface will receive all future graphics requests.
|
||||
\p Starting from FLTK 1.4.0, another convenient API to set/unset the current drawing surface
|
||||
is Fl_Surface_Device::push_current( ) / Fl_Surface_Device::pop_current().*/
|
||||
void Fl_Surface_Device::set_current(void)
|
||||
{
|
||||
if (pre_surface_) pre_surface_->end_current_();
|
||||
if (surface_) surface_->end_current_();
|
||||
fl_graphics_driver = pGraphicsDriver;
|
||||
surface_ = this;
|
||||
pGraphicsDriver->global_gc();
|
||||
pre_surface_ = this;
|
||||
}
|
||||
|
||||
Fl_Surface_Device* Fl_Surface_Device::surface_; // the current target surface of graphics operations
|
||||
@ -66,7 +61,7 @@ Fl_Surface_Device* Fl_Surface_Device::surface_; // the current target surface of
|
||||
|
||||
Fl_Surface_Device::~Fl_Surface_Device()
|
||||
{
|
||||
if (pre_surface_ == this) pre_surface_ = NULL;
|
||||
if (surface_ == this) surface_ = NULL;
|
||||
}
|
||||
|
||||
|
||||
@ -88,6 +83,31 @@ Fl_Surface_Device *Fl_Surface_Device::default_surface()
|
||||
return Fl_Display_Device::display_device();
|
||||
}
|
||||
|
||||
static int surface_stack_height = 0;
|
||||
static Fl_Surface_Device *surface_stack[16];
|
||||
|
||||
/** Pushes \p new_current on top of the stack of current drawing surfaces, and makes it current.
|
||||
\p new_current will receive all future graphics requests.
|
||||
\version 1.4.0 */
|
||||
void Fl_Surface_Device::push_current(Fl_Surface_Device *new_current)
|
||||
{
|
||||
if (surface_stack_height < sizeof(surface_stack)/sizeof(void*)) {
|
||||
surface_stack[surface_stack_height++] = surface();
|
||||
} else {
|
||||
fprintf(stderr, "FLTK Fl_Surface_Device::push_current Stack overflow error\n");
|
||||
}
|
||||
new_current->set_current();
|
||||
}
|
||||
|
||||
/** Removes the top element from the current drawing surface stack, and makes the new top element current.
|
||||
\return A pointer to the new current drawing surface.
|
||||
\version 1.4.0 */
|
||||
Fl_Surface_Device *Fl_Surface_Device::pop_current()
|
||||
{
|
||||
if (surface_stack_height > 0) surface_stack[--surface_stack_height]->set_current();
|
||||
return surface_;
|
||||
}
|
||||
|
||||
//
|
||||
// End of "$Id$".
|
||||
//
|
||||
|
@ -359,8 +359,7 @@ void Fl_Gl_Window::draw_overlay() {}
|
||||
*/
|
||||
void Fl_Gl_Window::draw() {
|
||||
#ifdef FL_CFG_GFX_OPENGL
|
||||
Fl_Surface_Device *prev_device = Fl_Surface_Device::surface();
|
||||
Fl_OpenGL_Display_Device::display_device()->set_current();
|
||||
Fl_Surface_Device::push_current( Fl_OpenGL_Display_Device::display_device() );
|
||||
glPushAttrib(GL_ENABLE_BIT);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
glPushMatrix();
|
||||
@ -375,7 +374,7 @@ void Fl_Gl_Window::draw() {
|
||||
|
||||
glPopMatrix();
|
||||
glPushAttrib(GL_ENABLE_BIT);
|
||||
prev_device->set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
#else
|
||||
Fl::fatal("Fl_Gl_Window::draw() *must* be overriden. Please refer to the documentation.");
|
||||
#endif
|
||||
|
@ -146,21 +146,13 @@ void fl_delete_offscreen(Fl_Offscreen ctx) {
|
||||
}
|
||||
}
|
||||
|
||||
static Fl_Surface_Device* stack_surface[16];
|
||||
static unsigned stack_height = 0;
|
||||
|
||||
/** Send all subsequent drawing commands to this offscreen buffer.
|
||||
\param ctx the offscreen buffer.
|
||||
*/
|
||||
void fl_begin_offscreen(Fl_Offscreen ctx) {
|
||||
for (int i = 0; i < count_offscreens; i++) {
|
||||
if (offscreen_api_surface[i] && offscreen_api_surface[i]->offscreen() == ctx) {
|
||||
if (stack_height < sizeof(stack_surface)/sizeof(void*)) {
|
||||
stack_surface[stack_height++] = Fl_Surface_Device::surface();
|
||||
} else {
|
||||
fprintf(stderr, "FLTK fl_begin_offscreen Stack overflow error\n");
|
||||
}
|
||||
offscreen_api_surface[i]->set_current();
|
||||
Fl_Surface_Device::push_current(offscreen_api_surface[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -169,9 +161,7 @@ void fl_begin_offscreen(Fl_Offscreen ctx) {
|
||||
/** Quit sending drawing commands to the current offscreen buffer.
|
||||
*/
|
||||
void fl_end_offscreen() {
|
||||
if (stack_height > 0) {
|
||||
stack_surface[--stack_height]->set_current();
|
||||
}
|
||||
Fl_Surface_Device::pop_current();
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
@ -2392,9 +2392,8 @@ void Fl_WinAPI_Window_Driver::capture_titlebar_and_borders(Fl_Shared_Image*& top
|
||||
float scaling = 1;
|
||||
RECT r = border_width_title_bar_height(wsides, hbottom, bt, &scaling);
|
||||
int htop = bt + hbottom;
|
||||
Fl_Surface_Device *previous = Fl_Surface_Device::surface();
|
||||
Window save_win = fl_window;
|
||||
Fl_Display_Device::display_device()->set_current();
|
||||
Fl_Surface_Device::push_current( Fl_Display_Device::display_device() );
|
||||
pWindow->show();
|
||||
Fl::check();
|
||||
void* save_gc = fl_graphics_driver->gc();
|
||||
@ -2417,7 +2416,7 @@ void Fl_WinAPI_Window_Driver::capture_titlebar_and_borders(Fl_Shared_Image*& top
|
||||
ReleaseDC(NULL, (HDC)fl_graphics_driver->gc());
|
||||
fl_window = save_win;
|
||||
fl_graphics_driver->gc(save_gc);
|
||||
previous->set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
}
|
||||
|
||||
|
||||
|
@ -251,12 +251,11 @@ static void pmProviderRelease (void *ctxt, const void *data, size_t size) {
|
||||
}
|
||||
|
||||
fl_uintptr_t Fl_Quartz_Graphics_Driver::cache(Fl_Pixmap *img, int w, int h, const char *const*data) {
|
||||
Fl_Surface_Device *old = Fl_Surface_Device::surface();
|
||||
Fl_Image_Surface *surf = new Fl_Image_Surface(w, h);
|
||||
surf->set_current();
|
||||
Fl_Surface_Device::push_current(surf);
|
||||
fl_draw_pixmap(data, 0, 0, FL_BLACK);
|
||||
CGContextRef src = surf->get_offscreen_before_delete();
|
||||
old->set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
delete surf;
|
||||
void *cgdata = CGBitmapContextGetData(src);
|
||||
int sw = CGBitmapContextGetWidth(src);
|
||||
|
@ -29,7 +29,6 @@ class Fl_Quartz_Image_Surface_Driver : public Fl_Image_Surface_Driver {
|
||||
friend class Fl_Image_Surface;
|
||||
virtual void end_current_();
|
||||
public:
|
||||
Fl_Surface_Device *previous;
|
||||
Window pre_window;
|
||||
Fl_Quartz_Image_Surface_Driver(int w, int h, int high_res);
|
||||
~Fl_Quartz_Image_Surface_Driver();
|
||||
@ -47,7 +46,6 @@ Fl_Image_Surface_Driver *Fl_Image_Surface_Driver::newImageSurfaceDriver(int w, i
|
||||
|
||||
|
||||
Fl_Quartz_Image_Surface_Driver::Fl_Quartz_Image_Surface_Driver(int w, int h, int high_res) : Fl_Image_Surface_Driver(w, h, high_res, 0) {
|
||||
previous = 0;
|
||||
int W = high_res ? 2*w : w;
|
||||
int H = high_res ? 2*h : h;
|
||||
CGColorSpaceRef lut = CGColorSpaceCreateDeviceRGB();
|
||||
@ -77,7 +75,6 @@ Fl_Quartz_Image_Surface_Driver::~Fl_Quartz_Image_Surface_Driver() {
|
||||
|
||||
void Fl_Quartz_Image_Surface_Driver::set_current() {
|
||||
pre_window = fl_window;
|
||||
if (!previous) previous = Fl_Surface_Device::surface();
|
||||
driver()->gc(offscreen);
|
||||
fl_window = 0;
|
||||
Fl_Surface_Device::set_current();
|
||||
|
@ -387,8 +387,7 @@ void Fl_X11_Window_Driver::capture_titlebar_and_borders(Fl_Shared_Image*& top, F
|
||||
top = left = bottom = right = NULL;
|
||||
if (pWindow->decorated_h() == h()) return;
|
||||
Window from = fl_window;
|
||||
Fl_Surface_Device *previous = Fl_Surface_Device::surface();
|
||||
Fl_Display_Device::display_device()->set_current();
|
||||
Fl_Surface_Device::push_current( Fl_Display_Device::display_device() );
|
||||
pWindow->show();
|
||||
Fl::check();
|
||||
pWindow->make_current();
|
||||
@ -421,7 +420,7 @@ void Fl_X11_Window_Driver::capture_titlebar_and_borders(Fl_Shared_Image*& top, F
|
||||
}
|
||||
}
|
||||
fl_window = from;
|
||||
previous->Fl_Surface_Device::set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
}
|
||||
|
||||
|
||||
|
@ -28,10 +28,10 @@
|
||||
|
||||
class Fl_Xlib_Copy_Surface_Driver : public Fl_Copy_Surface_Driver {
|
||||
friend class Fl_Copy_Surface_Driver;
|
||||
virtual void end_current_();
|
||||
protected:
|
||||
Fl_Offscreen xid;
|
||||
Window oldwindow;
|
||||
Fl_Surface_Device *_ss;
|
||||
Fl_Xlib_Copy_Surface_Driver(int w, int h);
|
||||
~Fl_Xlib_Copy_Surface_Driver();
|
||||
void set_current();
|
||||
@ -51,26 +51,22 @@ Fl_Copy_Surface_Driver *Fl_Copy_Surface_Driver::newCopySurfaceDriver(int w, int
|
||||
|
||||
Fl_Xlib_Copy_Surface_Driver::Fl_Xlib_Copy_Surface_Driver(int w, int h) : Fl_Copy_Surface_Driver(w, h) {
|
||||
driver(new Fl_Translated_Xlib_Graphics_Driver());
|
||||
Fl::first_window()->make_current();
|
||||
oldwindow = fl_xid(Fl::first_window());
|
||||
oldwindow = fl_window;
|
||||
xid = fl_create_offscreen(w,h);
|
||||
_ss = NULL;
|
||||
Fl_Surface_Device *present_surface = Fl_Surface_Device::surface();
|
||||
Fl_Surface_Device::set_current();
|
||||
fl_push_no_clip();
|
||||
driver()->push_no_clip();
|
||||
fl_window = xid;
|
||||
driver()->color(FL_WHITE);
|
||||
driver()->rectf(0, 0, w, h);
|
||||
present_surface->set_current();
|
||||
fl_window = oldwindow;
|
||||
}
|
||||
|
||||
|
||||
Fl_Xlib_Copy_Surface_Driver::~Fl_Xlib_Copy_Surface_Driver() {
|
||||
fl_pop_clip();
|
||||
driver()->pop_clip();
|
||||
bool need_push = (Fl_Surface_Device::surface() != this);
|
||||
if (need_push) Fl_Surface_Device::push_current(this);
|
||||
unsigned char *data = fl_read_image(NULL,0,0,width,height,0);
|
||||
fl_window = oldwindow;
|
||||
_ss->set_current();
|
||||
if (need_push) Fl_Surface_Device::pop_current();
|
||||
Fl_X11_Screen_Driver::copy_image(data, width, height, 1);
|
||||
delete[] data;
|
||||
fl_delete_offscreen(xid);
|
||||
@ -79,11 +75,14 @@ Fl_Xlib_Copy_Surface_Driver::~Fl_Xlib_Copy_Surface_Driver() {
|
||||
|
||||
|
||||
void Fl_Xlib_Copy_Surface_Driver::set_current() {
|
||||
oldwindow = fl_window;
|
||||
fl_window = xid;
|
||||
if (!_ss) _ss = Fl_Surface_Device::surface();
|
||||
Fl_Surface_Device::set_current();
|
||||
}
|
||||
|
||||
void Fl_Xlib_Copy_Surface_Driver::end_current_() {
|
||||
fl_window = oldwindow;
|
||||
}
|
||||
|
||||
void Fl_Xlib_Copy_Surface_Driver::translate(int x, int y) {
|
||||
((Fl_Translated_Xlib_Graphics_Driver*)driver())->translate_all(x, y);
|
||||
|
@ -714,10 +714,9 @@ void Fl_Xlib_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, in
|
||||
depth |= FL_IMAGE_WITH_ALPHA;
|
||||
}
|
||||
if (surface) {
|
||||
Fl_Surface_Device *old_surf = Fl_Surface_Device::surface();
|
||||
surface->set_current();
|
||||
Fl_Surface_Device::push_current(surface);
|
||||
fl_draw_image(img->array, 0, 0, img->w(), img->h(), depth, img->ld());
|
||||
old_surf->set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
*Fl_Graphics_Driver::id(img) = surface->get_offscreen_before_delete();
|
||||
delete surface;
|
||||
}
|
||||
|
@ -29,7 +29,6 @@ class Fl_Xlib_Image_Surface_Driver : public Fl_Image_Surface_Driver {
|
||||
friend class Fl_Image_Surface;
|
||||
virtual void end_current_();
|
||||
public:
|
||||
Fl_Surface_Device *previous;
|
||||
Window pre_window;
|
||||
int was_high;
|
||||
Fl_Xlib_Image_Surface_Driver(int w, int h, int high_res, Fl_Offscreen off);
|
||||
@ -46,7 +45,6 @@ Fl_Image_Surface_Driver *Fl_Image_Surface_Driver::newImageSurfaceDriver(int w, i
|
||||
}
|
||||
|
||||
Fl_Xlib_Image_Surface_Driver::Fl_Xlib_Image_Surface_Driver(int w, int h, int high_res, Fl_Offscreen off) : Fl_Image_Surface_Driver(w, h, high_res, off) {
|
||||
previous = 0;
|
||||
if (!off) {
|
||||
fl_open_display();
|
||||
offscreen = XCreatePixmap(fl_display, RootWindow(fl_display, fl_screen), w, h, fl_visual->depth);
|
||||
@ -61,7 +59,6 @@ Fl_Xlib_Image_Surface_Driver::~Fl_Xlib_Image_Surface_Driver() {
|
||||
|
||||
void Fl_Xlib_Image_Surface_Driver::set_current() {
|
||||
pre_window = fl_window;
|
||||
if (!previous) previous = Fl_Surface_Device::surface();
|
||||
Fl_Surface_Device::set_current();
|
||||
fl_window = offscreen;
|
||||
fl_push_no_clip();
|
||||
|
@ -570,7 +570,7 @@ void copy(Fl_Widget *, void *data) {
|
||||
decorated = 0;
|
||||
}
|
||||
rgb_surf = new Fl_Image_Surface(W, H, 1);
|
||||
rgb_surf->set_current();
|
||||
Fl_Surface_Device::push_current(rgb_surf);
|
||||
fl_color(FL_YELLOW);fl_rectf(0,0,W,H);
|
||||
if (decorated)
|
||||
rgb_surf->draw_decorated_window(target->as_window());
|
||||
@ -578,7 +578,7 @@ void copy(Fl_Widget *, void *data) {
|
||||
rgb_surf->draw(target);
|
||||
Fl_Image *img = rgb_surf->highres_image();
|
||||
delete rgb_surf;
|
||||
Fl_Display_Device::display_device()->set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
if (img) {
|
||||
Fl_Window* g2 = new Fl_Window(img->w()+10, img->h()+10);
|
||||
g2->color(FL_YELLOW);
|
||||
@ -596,17 +596,17 @@ void copy(Fl_Widget *, void *data) {
|
||||
Fl_Copy_Surface *copy_surf;
|
||||
if (target->as_window() && !target->parent()) {
|
||||
copy_surf = new Fl_Copy_Surface(target->as_window()->decorated_w(), target->as_window()->decorated_h());
|
||||
copy_surf->set_current();
|
||||
Fl_Surface_Device::push_current(copy_surf);
|
||||
copy_surf->draw_decorated_window(target->as_window(), 0, 0);
|
||||
}
|
||||
else {
|
||||
copy_surf = new Fl_Copy_Surface(target->w()+10, target->h()+20);
|
||||
copy_surf->set_current();
|
||||
Fl_Surface_Device::push_current(copy_surf);
|
||||
fl_color(FL_YELLOW);fl_rectf(0,0,copy_surf->w(), copy_surf->h());
|
||||
copy_surf->draw(target, 5, 10);
|
||||
}
|
||||
delete copy_surf;
|
||||
Fl_Display_Device::display_device()->set_current();
|
||||
Fl_Surface_Device::pop_current();
|
||||
}
|
||||
|
||||
if (strcmp(operation, "Fl_Printer") == 0 || strcmp(operation, "Fl_PostScript_File_Device") == 0) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user