diff options
| -rw-r--r-- | FL/Fl_Device.H | 12 | ||||
| -rw-r--r-- | FL/Fl_Image.H | 1 | ||||
| -rw-r--r-- | FL/Fl_PostScript.H | 2 | ||||
| -rw-r--r-- | FL/Fl_Shared_Image.H | 22 | ||||
| -rw-r--r-- | src/Fl_Image.cxx | 80 | ||||
| -rw-r--r-- | src/Fl_Pixmap.cxx | 29 | ||||
| -rw-r--r-- | src/Fl_Shared_Image.cxx | 114 | ||||
| -rw-r--r-- | src/ps_image.cxx | 26 | ||||
| -rw-r--r-- | test/pixmap_browser.cxx | 56 |
9 files changed, 302 insertions, 40 deletions
diff --git a/FL/Fl_Device.H b/FL/Fl_Device.H index 7e8d7e5f0..3d86518f3 100644 --- a/FL/Fl_Device.H +++ b/FL/Fl_Device.H @@ -402,6 +402,14 @@ public: inline Fl_Font_Descriptor *font_descriptor() { return font_descriptor_;} /** Sets the current Fl_Font_Descriptor for the graphics driver */ inline void font_descriptor(Fl_Font_Descriptor *d) { font_descriptor_ = d;} +#if FLTK_ABI_VERSION >= 10304 || defined(FL_DOXYGEN) + virtual +#endif + int draw_scaled(Fl_RGB_Image *img, int X, int Y, int W, int H); +#if FLTK_ABI_VERSION >= 10304 || defined(FL_DOXYGEN) + virtual +#endif + int draw_scaled(Fl_Pixmap *img, int X, int Y, int W, int H); /** \brief The destructor */ virtual ~Fl_Graphics_Driver() { if (p) free(p); } }; @@ -428,6 +436,8 @@ public: void draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy); void draw(Fl_Bitmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy); void draw(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int cx, int cy); + int draw_scaled(Fl_RGB_Image *img, int XP, int YP, int WP, int HP); + int draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP); void draw_image(const uchar* buf, int X,int Y,int W,int H, int D=3, int L=0); void draw_image(Fl_Draw_Image_Cb cb, void* data, int X,int Y,int W,int H, int D=3); void draw_image_mono(const uchar* buf, int X,int Y,int W,int H, int D=1, int L=0); @@ -488,6 +498,8 @@ public: const char *class_name() {return class_id;}; void draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy); void draw(Fl_Bitmap *bm, int XP, int YP, int WP, int HP, int cx, int cy); + int draw_scaled(Fl_RGB_Image *img, int X, int Y, int W, int H); + int draw_scaled(Fl_Pixmap *img, int X, int Y, int W, int H); }; #endif #if !(defined(__APPLE__) || defined(WIN32)) diff --git a/FL/Fl_Image.H b/FL/Fl_Image.H index b00f5678b..1f1ed16bb 100644 --- a/FL/Fl_Image.H +++ b/FL/Fl_Image.H @@ -183,6 +183,7 @@ class FL_EXPORT Fl_Image { class FL_EXPORT Fl_RGB_Image : public Fl_Image { friend class Fl_Quartz_Graphics_Driver; friend class Fl_GDI_Graphics_Driver; + friend class Fl_GDI_Printer_Graphics_Driver; friend class Fl_Xlib_Graphics_Driver; static size_t max_size_; public: diff --git a/FL/Fl_PostScript.H b/FL/Fl_PostScript.H index 8860e331f..19692c455 100644 --- a/FL/Fl_PostScript.H +++ b/FL/Fl_PostScript.H @@ -213,6 +213,8 @@ class Clip { void draw(Fl_Pixmap * pxm,int XP, int YP, int WP, int HP, int cx, int cy); void draw(Fl_Bitmap * bitmap,int XP, int YP, int WP, int HP, int cx, int cy); void draw(Fl_RGB_Image * rgb,int XP, int YP, int WP, int HP, int cx, int cy); + int draw_scaled(Fl_RGB_Image *img, int XP, int YP, int WP, int HP); + int draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP); int clocale_printf(const char *format, ...); ~Fl_PostScript_Graphics_Driver(); }; diff --git a/FL/Fl_Shared_Image.H b/FL/Fl_Shared_Image.H index bcb41219f..5f96f7f2d 100644 --- a/FL/Fl_Shared_Image.H +++ b/FL/Fl_Shared_Image.H @@ -3,7 +3,7 @@ // // Shared image header file for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2010 by Bill Spitzak and others. +// Copyright 1998-2015 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -41,7 +41,12 @@ class FL_EXPORT Fl_Shared_Image : public Fl_Image { friend class Fl_JPEG_Image; friend class Fl_PNG_Image; - + +private: + static Fl_RGB_Scaling scaling_algorithm_; // method used to rescale RGB source images +#if FLTK_ABI_VERSION >= 10304 + Fl_Image *scaled_image_; +#endif protected: static Fl_Shared_Image **images_; // Shared images @@ -66,7 +71,7 @@ protected: void add(); void update(); - public: +public: /** Returns the filename of the shared image */ const char *name() { return name_; } /** Returns the number of references of this shared image. When reference is below 1, the image is deleted. */ @@ -80,14 +85,25 @@ protected: virtual void desaturate(); virtual void draw(int X, int Y, int W, int H, int cx, int cy); void draw(int X, int Y) { draw(X, Y, w(), h(), 0, 0); } + void scale(int width, int height, int proportional = 1, int can_expand = 0); virtual void uncache(); static Fl_Shared_Image *find(const char *n, int W = 0, int H = 0); static Fl_Shared_Image *get(const char *n, int W = 0, int H = 0); + static Fl_Shared_Image *get(Fl_RGB_Image *rgb, int own_it = 1); static Fl_Shared_Image **images(); static int num_images(); static void add_handler(Fl_Shared_Handler f); static void remove_handler(Fl_Shared_Handler f); + /** Sets what algorithm is used when resizing a source image. + The default algorithm is FL_RGB_SCALING_BILINEAR. + Drawing an Fl_Shared_Image is sometimes performed by first resizing the source image + and then drawing the resized copy. This occurs, e.g., when drawing to screen under Linux or MSWindows + after having called Fl_Shared_Image::scale(). + This function controls what method is used when the image to be resized is an Fl_RGB_Image. + \version 1.3.4 and requires compiling with FLTK_ABI_VERSION = 10304 + */ + static void scaling_algorithm(Fl_RGB_Scaling algorithm) {scaling_algorithm_ = algorithm; } }; // diff --git a/src/Fl_Image.cxx b/src/Fl_Image.cxx index 3df1efde4..e4e88478e 100644 --- a/src/Fl_Image.cxx +++ b/src/Fl_Image.cxx @@ -3,7 +3,7 @@ // // Image drawing code for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2014 by Bill Spitzak and others. +// Copyright 1998-2015 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -548,6 +548,21 @@ static int start(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int w, int h return 0; } +/** Draws an Fl_RGB_Image scaled to width \p W & height \p H with top-left corner at \em X,Y + \return zero when the graphics driver doesn't implement scaled drawing, non-zero if it does implement it. + */ +int Fl_Graphics_Driver::draw_scaled(Fl_RGB_Image *img, int X, int Y, int W, int H) { + return 0; +} + +/** Draws an Fl_Pixmap scaled to width \p W & height \p H with top-left corner at \em X,Y + \return zero when the graphics driver doesn't implement scaled drawing, non-zero if it does implement it. + */ +int Fl_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int X, int Y, int W, int H) { + return 0; +} + + #ifdef __APPLE__ static void imgProviderReleaseData (void *info, const void *data, size_t size) { @@ -612,7 +627,40 @@ void Fl_Quartz_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, } } +int Fl_Quartz_Graphics_Driver::draw_scaled(Fl_RGB_Image *img, int XP, int YP, int WP, int HP) { + int X, Y, W, H; + fl_clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + fl_push_no_clip(); // remove the FLTK clip that can't be rescaled + CGContextSaveGState(fl_gc); + CGContextClipToRect(fl_gc, CGRectMake(X, Y, W, H)); // this clip path will be rescaled & translated + CGContextTranslateCTM(fl_gc, XP, YP); + CGContextScaleCTM(fl_gc, float(WP)/img->w(), float(HP)/img->h()); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + CGContextRestoreGState(fl_gc); + fl_pop_clip(); // restore FLTK's clip + return 1; +} + #elif defined(WIN32) +static Fl_Offscreen build_id(Fl_RGB_Image *img, void **pmask) +{ + Fl_Offscreen offs = fl_create_offscreen(img->w(), img->h()); + if ((img->d() == 2 || img->d() == 4) && fl_can_do_alpha_blending()) { + fl_begin_offscreen(offs); + fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d()|FL_IMAGE_WITH_ALPHA, img->ld()); + fl_end_offscreen(); + } else { + fl_begin_offscreen(offs); + fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d(), img->ld()); + fl_end_offscreen(); + if (img->d() == 2 || img->d() == 4) { + *pmask = fl_create_alphamask(img->w(), img->h(), img->d(), img->ld(), img->array); + } + } + return offs; +} + void Fl_GDI_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; // Don't draw an empty image... @@ -623,21 +671,7 @@ void Fl_GDI_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int if (start(img, XP, YP, WP, HP, img->w(), img->h(), cx, cy, X, Y, W, H)) { return; } - if (!img->id_) { - img->id_ = fl_create_offscreen(img->w(), img->h()); - if ((img->d() == 2 || img->d() == 4) && fl_can_do_alpha_blending()) { - fl_begin_offscreen((Fl_Offscreen)img->id_); - fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d()|FL_IMAGE_WITH_ALPHA, img->ld()); - fl_end_offscreen(); - } else { - fl_begin_offscreen((Fl_Offscreen)img->id_); - fl_draw_image(img->array, 0, 0, img->w(), img->h(), img->d(), img->ld()); - fl_end_offscreen(); - if (img->d() == 2 || img->d() == 4) { - img->mask_ = fl_create_alphamask(img->w(), img->h(), img->d(), img->ld(), img->array); - } - } - } + if (!img->id_) img->id_ = build_id(img, &(img->mask_)); if (img->mask_) { HDC new_gc = CreateCompatibleDC(fl_gc); int save = SaveDC(new_gc); @@ -654,6 +688,20 @@ void Fl_GDI_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int } } +int Fl_GDI_Printer_Graphics_Driver::draw_scaled(Fl_RGB_Image *img, int XP, int YP, int WP, int HP) { + XFORM old_tr, tr; + GetWorldTransform(fl_gc, &old_tr); // storing old transform + tr.eM11 = float(WP)/float(img->w()); + tr.eM22 = float(HP)/float(img->h()); + tr.eM12 = tr.eM21 = 0; + tr.eDx = XP; + tr.eDy = YP; + ModifyWorldTransform(fl_gc, &tr, MWT_LEFTMULTIPLY); + Fl_GDI_Graphics_Driver::draw(img, 0, 0, img->w(), img->h(), 0, 0); + SetWorldTransform(fl_gc, &old_tr); +return 1; +} + #else void Fl_Xlib_Graphics_Driver::draw(Fl_RGB_Image *img, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; diff --git a/src/Fl_Pixmap.cxx b/src/Fl_Pixmap.cxx index 8064979dd..2bb776b6b 100644 --- a/src/Fl_Pixmap.cxx +++ b/src/Fl_Pixmap.cxx @@ -135,6 +135,21 @@ void Fl_Quartz_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int copy_offscreen(X, Y, W, H, (Fl_Offscreen)pxm->id_, cx, cy); } +int Fl_Quartz_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP) { + int X, Y, W, H; + fl_clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + fl_push_no_clip(); // remove the FLTK clip that can't be rescaled + CGContextSaveGState(fl_gc); + CGContextClipToRect(fl_gc, CGRectMake(X, Y, W, H)); // this clip path will be rescaled & translated + CGContextTranslateCTM(fl_gc, XP, YP); + CGContextScaleCTM(fl_gc, float(WP)/img->w(), float(HP)/img->h()); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + CGContextRestoreGState(fl_gc); + fl_pop_clip(); // restore FLTK's clip + return 1; +} + #elif defined(WIN32) void Fl_GDI_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; @@ -181,6 +196,20 @@ void Fl_GDI_Printer_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP } } +int Fl_GDI_Printer_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP) { + XFORM old_tr, tr; + GetWorldTransform(fl_gc, &old_tr); // storing old transform + tr.eM11 = float(WP)/float(img->w()); + tr.eM22 = float(HP)/float(img->h()); + tr.eM12 = tr.eM21 = 0; + tr.eDx = XP; + tr.eDy = YP; + ModifyWorldTransform(fl_gc, &tr, MWT_LEFTMULTIPLY); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + SetWorldTransform(fl_gc, &old_tr); +} + + #else // Xlib void Fl_Xlib_Graphics_Driver::draw(Fl_Pixmap *pxm, int XP, int YP, int WP, int HP, int cx, int cy) { int X, Y, W, H; diff --git a/src/Fl_Shared_Image.cxx b/src/Fl_Shared_Image.cxx index 2289ccdb4..4a05a46ef 100644 --- a/src/Fl_Shared_Image.cxx +++ b/src/Fl_Shared_Image.cxx @@ -3,7 +3,7 @@ // // Shared image code for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2010 by Bill Spitzak and others. +// Copyright 1998-2015 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -25,7 +25,8 @@ #include <FL/Fl_Shared_Image.H> #include <FL/Fl_XBM_Image.H> #include <FL/Fl_XPM_Image.H> - +#include <FL/Fl_Preferences.H> +#include <FL/fl_draw.H> // // Global class vars... @@ -89,6 +90,9 @@ Fl_Shared_Image::Fl_Shared_Image() : Fl_Image(0,0,0) { original_ = 0; image_ = 0; alloc_image_ = 0; +#if FLTK_ABI_VERSION >= 10304 + scaled_image_= 0; +#endif } @@ -109,6 +113,9 @@ Fl_Shared_Image::Fl_Shared_Image(const char *n, // I - Filename image_ = img; alloc_image_ = !img; original_ = 1; +#if FLTK_ABI_VERSION >= 10304 + scaled_image_= 0; +#endif if (!img) reload(); else update(); @@ -170,6 +177,9 @@ Fl_Shared_Image::update() { Fl_Shared_Image::~Fl_Shared_Image() { if (name_) delete[] (char *)name_; if (alloc_image_) delete image_; +#if FLTK_ABI_VERSION >= 10304 + delete scaled_image_; +#endif } @@ -317,13 +327,95 @@ Fl_Shared_Image::desaturate() { // // 'Fl_Shared_Image::draw()' - Draw a shared image... // - -void -Fl_Shared_Image::draw(int X, int Y, int W, int H, int cx, int cy) { +void Fl_Shared_Image::draw(int X, int Y, int W, int H, int cx, int cy) { +#if FLTK_ABI_VERSION >= 10304 + if (!image_) { + Fl_Image::draw(X, Y, W, H, cx, cy); + return; + } + if (w() == image_->w() && h() == image_->h()) { + image_->draw(X, Y, W, H, cx, cy); + return; + } + fl_push_clip(X, Y, W, H); + int use_scaled_image = 0, done; + if (image_->d() == 0) { // handles Fl_Bitmap + use_scaled_image = 1; + } + else if (image_->count() >= 2) { // handles Fl_Pixmap + done = fl_graphics_driver->draw_scaled((Fl_Pixmap*)image_, X-cx, Y-cy, w(), h()); + if (done == 0) use_scaled_image = 1; + } + else { // handles Fl_RGB_Image + done = fl_graphics_driver->draw_scaled((Fl_RGB_Image*)image_, X-cx, Y-cy, w(), h()); + if (done == 0) use_scaled_image = 1; + } + if (use_scaled_image) { + if (scaled_image_ && (scaled_image_->w() != w() || scaled_image_->h() != h())) { + delete scaled_image_; + scaled_image_ = NULL; + } + if (!scaled_image_) { + Fl_RGB_Scaling previous = RGB_scaling(); + RGB_scaling(scaling_algorithm_); // useless but no harm if image_ is not an Fl_RGB_Image + scaled_image_ = image_->copy(w(), h()); + RGB_scaling(previous); + } + scaled_image_->draw(X-cx, Y-cy, scaled_image_->w(), scaled_image_->h(), 0, 0); + } + fl_pop_clip(); +#else if (image_) image_->draw(X, Y, W, H, cx, cy); else Fl_Image::draw(X, Y, W, H, cx, cy); +#endif // FLTK_ABI_VERSION } +/** Sets the drawing size of the shared image. + This function gives the shared image its own size, independently from the size of the original image + that is typically larger than the shared image. + This can be useful to draw a shared image on a drawing surface whose resolution is higher + than the drawing unit for this surface. Examples of such drawing surfaces: laser printers, + PostScript files, PDF printers, retina displays on Apple hardware. + + \param width,height maximum width and height (in drawing units) to use when drawing the shared image + \param proportional if not null, keep the width and height of the shared image proportional to those of its original image + \param can_expand if null, the width and height of the shared image will not exceed those of the original image + + \version 1.3.4 and requires compiling with FLTK_ABI_VERSION = 10304 + + Example code: scale an image to fit in a box + \code + Fl_Box *b = ... // a box + Fl_Shared_Image *shared = Fl_Shared_Image::get("/path/to/picture.jpeg"); // read a picture file + b->image(shared); // use the shared image as the box image + b->align(FL_ALIGN_INSIDE | FL_ALIGN_CENTER | FL_ALIGN_CLIP); // the image is to be drawn centered in the box + shared->scale(b->w(), b->h(), 1); // set the drawing size of the shared image to the size of the box + \endcode + */ +void Fl_Shared_Image::scale(int width, int height, int proportional, int can_expand) +{ +#if FLTK_ABI_VERSION >= 10304 + w(width); + h(height); + if (!image_) return; + float fw = image_->w() / float(width); + float fh = image_->h() / float(height); + if (proportional) { + if (fh > fw) fw = fh; + else fh = fw; + } + if (!can_expand) { + if (fw < 1) fw = 1; + if (fh < 1) fh = 1; + } + w(image_->w() / fw); + h(image_->h() / fh); +#endif +} + + +Fl_RGB_Scaling Fl_Shared_Image::scaling_algorithm_ = FL_RGB_SCALING_BILINEAR; + // // 'Fl_Shared_Image::uncache()' - Uncache the shared image... @@ -408,6 +500,18 @@ Fl_Shared_Image* Fl_Shared_Image::get(const char *n, int W, int H) { return temp; } +/** Builds a shared image from a pre-existing Fl_RGB_Image + \param rgb an Fl_RGB_Image used to build a new shared image. + \param own_it 1 if the shared image should delete \p rgb when it is itself deleted, 0 otherwise + \version 1.3.4 + */ +Fl_Shared_Image *Fl_Shared_Image::get(Fl_RGB_Image *rgb, int own_it) +{ + Fl_Shared_Image *shared = new Fl_Shared_Image(Fl_Preferences::newUUID(), rgb); + shared->alloc_image_ = own_it; + shared->add(); + return shared; +} /** Adds a shared image handler, which is basically a test function for adding new formats */ diff --git a/src/ps_image.cxx b/src/ps_image.cxx index 3939a7511..ca71121b9 100644 --- a/src/ps_image.cxx +++ b/src/ps_image.cxx @@ -602,6 +602,32 @@ void Fl_PostScript_Graphics_Driver::draw(Fl_RGB_Image * rgb,int XP, int YP, int mask=0; } +int Fl_PostScript_Graphics_Driver::draw_scaled(Fl_RGB_Image *rgb, int XP, int YP, int WP, int HP){ + int X, Y, W, H; + clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + push_no_clip(); // remove the FLTK clip that can't be rescaled + clocale_printf("%d %d %i %i CL\n", X, Y, W, H); + clocale_printf("GS %d %d TR %f %f SC GS\n", XP, YP, float(WP)/rgb->w(), float(HP)/rgb->h()); + draw(rgb, 0, 0, rgb->w(), rgb->h(), 0, 0); + clocale_printf("GR GR\n"); + pop_clip(); // restore FLTK's clip + return 1; +} + +int Fl_PostScript_Graphics_Driver::draw_scaled(Fl_Pixmap *img, int XP, int YP, int WP, int HP){ + int X, Y, W, H; + clip_box(XP,YP,WP,HP,X,Y,W,H); // X,Y,W,H will give the unclipped area of XP,YP,WP,HP + if (W == 0 || H == 0) return 1; + push_no_clip(); // remove the FLTK clip that can't be rescaled + clocale_printf("%d %d %i %i CL\n", X, Y, W, H); + clocale_printf("GS %d %d TR %f %f SC GS\n", XP, YP, float(WP)/img->w(), float(HP)/img->h()); + draw(img, 0, 0, img->w(), img->h(), 0, 0); + clocale_printf("GR GR\n"); + pop_clip(); // restore FLTK's clip + return 1; +} + void Fl_PostScript_Graphics_Driver::draw(Fl_Bitmap * bitmap,int XP, int YP, int WP, int HP, int cx, int cy){ const uchar * di = bitmap->array; int w,h; diff --git a/test/pixmap_browser.cxx b/test/pixmap_browser.cxx index fa79e65a6..c226e4bcf 100644 --- a/test/pixmap_browser.cxx +++ b/test/pixmap_browser.cxx @@ -3,7 +3,7 @@ // // A shared image test program for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2010 by Bill Spitzak and others. +// Copyright 1998-2015 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -21,6 +21,7 @@ #include <FL/Fl_Double_Window.H> #include <FL/Fl_Button.H> #include <FL/Fl_Shared_Image.H> +#include <FL/Fl_Printer.H> #include <string.h> #include <errno.h> #include <FL/Fl_File_Chooser.H> @@ -35,7 +36,7 @@ static char name[1024]; void load_file(const char *n) { if (img) { - img->release(); + ((Fl_Shared_Image*)b->image())->release(); img = 0L; } if (fl_filename_isdir(n)) { @@ -46,8 +47,9 @@ void load_file(const char *n) { b->redraw(); return; } - img = Fl_Shared_Image::get(n); - if (!img) { + Fl_Shared_Image *img2 = Fl_Shared_Image::get(n); + + if (!img2) { b->label("@filenew"); // show an empty document b->labelsize(64); b->labelcolor(FL_LIGHT2); @@ -55,18 +57,23 @@ void load_file(const char *n) { b->redraw(); return; } - if (img->w() > b->w() || img->h() > b->h()) { - Fl_Image *temp; - if (img->w() > img->h()) temp = img->copy(b->w(), b->h() * img->h() / img->w()); - else temp = img->copy(b->w() * img->w() / img->h(), b->h()); - - img->release(); - img = (Fl_Shared_Image *)temp; - } - b->label(name); + img = img2; b->labelsize(14); b->labelcolor(FL_FOREGROUND_COLOR); +#if FLTK_ABI_VERSION >= 10304 b->image(img); + img->scale(b->w(), b->h()); +#else + if (img->w() <= b->w() && img->h() <= b->h()) b->image(img); + else { + float fw = img->w() / float(b->w()); + float fh = img->h() / float(b->h()); + float f = fw > fh ? fw : fh; + b->image(img->copy(img->w()/f, img->h()/f)); + img->release(); + } +#endif + b->label(NULL); b->redraw(); } @@ -83,6 +90,20 @@ void button_cb(Fl_Widget *,void *) { puts(fname ? fname : "(null)"); fflush(stdout); fl_file_chooser_callback(0); } +void print_cb(Fl_Widget *widget, void *) { + Fl_Printer printer; + int width, height; + if (printer.start_job(1)) return; + printer.start_page(); + printer.printable_rect(&width, &height); + float fw = widget->window()->decorated_w() / float(width); + float fh = widget->window()->decorated_h() / float(height); + if (fh > fw) fw = fh; + printer.scale(1/fw); + printer.print_window(widget->window()); + printer.end_page(); + printer.end_job(); +} int dvisual = 0; int arg(int, char **argv, int &i) { @@ -97,15 +118,18 @@ int main(int argc, char **argv) { Fl::args(argc,argv,i,arg); - Fl_Double_Window window(400,435); ::w = &window; + Fl_Double_Window window(400,450); ::w = &window; Fl_Box b(10,45,380,380); ::b = &b; b.box(FL_THIN_DOWN_BOX); - b.align(FL_ALIGN_INSIDE|FL_ALIGN_CENTER); + b.align(FL_ALIGN_INSIDE|FL_ALIGN_CENTER|FL_ALIGN_CLIP); Fl_Button button(150,5,100,30,"load"); button.callback(button_cb); if (!dvisual) Fl::visual(FL_RGB); if (argv[1]) load_file(argv[1]); - window.resizable(window); + window.resizable(b); + Fl_Button print(300,425,50,25,"Print"); + print.callback(print_cb); + window.show(argc,argv); return Fl::run(); } |
