From 06f5833b8f41196bc427c06e39ed21d431c1e665 Mon Sep 17 00:00:00 2001 From: Sebastian Geerken Date: Tue, 10 Sep 2013 21:00:58 +0200 Subject: Some refactoring: the image decoding code now only accesses dw::core::ImgRenderer, not dw::Image (which now implements the former) anymore. --- dw/Makefile.am | 1 + dw/core.hh | 1 + dw/image.hh | 2 +- dw/imgrenderer.hh | 28 ++++++++++++++++++++++++++++ src/dicache.c | 7 ++++--- src/form.cc | 8 ++++++-- src/html.cc | 25 ++++++++++++++++++------- src/image.cc | 13 +++++++------ src/image.hh | 4 ++-- src/imgbuf.cc | 6 +++--- src/imgbuf.hh | 2 +- 11 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 dw/imgrenderer.hh diff --git a/dw/Makefile.am b/dw/Makefile.am index e108da60..06a7af51 100644 --- a/dw/Makefile.am +++ b/dw/Makefile.am @@ -14,6 +14,7 @@ libDw_core_a_SOURCES = \ findtext.cc \ findtext.hh \ imgbuf.hh \ + imgrenderer.hh \ iterator.cc \ iterator.hh \ layout.cc \ diff --git a/dw/core.hh b/dw/core.hh index f39c38cc..022574b1 100644 --- a/dw/core.hh +++ b/dw/core.hh @@ -44,6 +44,7 @@ class ResourceFactory; #include "types.hh" #include "events.hh" #include "imgbuf.hh" +#include "imgrenderer.hh" #include "style.hh" #include "view.hh" #include "platform.hh" diff --git a/dw/image.hh b/dw/image.hh index a8314d4f..7ac05c20 100644 --- a/dw/image.hh +++ b/dw/image.hh @@ -116,7 +116,7 @@ public: * * \sa\ref dw-images-and-backgrounds */ -class Image: public core::Widget +class Image: public core::Widget, public core::ImgRenderer { private: char *altText; diff --git a/dw/imgrenderer.hh b/dw/imgrenderer.hh new file mode 100644 index 00000000..9c7c6525 --- /dev/null +++ b/dw/imgrenderer.hh @@ -0,0 +1,28 @@ +#ifndef __DW_IMGRENDERER_HH__ +#define __DW_IMGRENDERER_HH__ + +#ifndef __INCLUDED_FROM_DW_CORE_HH__ +# error Do not include this file directly, use "core.hh" instead. +#endif + +namespace dw { +namespace core { + +/** + * \brief ... + * + * \sa \ref dw-images-and-backgrounds + */ +class ImgRenderer +{ +public: + virtual void setBuffer (core::Imgbuf *buffer, bool resize = false) = 0; + virtual void drawRow (int row) = 0; +}; + +} // namespace core +} // namespace dw + +#endif // __DW_IMGRENDERER_HH__ + + diff --git a/src/dicache.c b/src/dicache.c index 55232846..673d5907 100644 --- a/src/dicache.c +++ b/src/dicache.c @@ -276,7 +276,8 @@ void a_Dicache_set_parms(DilloUrl *url, int version, DilloImage *Image, /* BUG: there's just one image-type now */ #define I_RGB 0 - DicEntry->v_imgbuf = a_Imgbuf_new(Image->dw, I_RGB, width, height, gamma); + DicEntry->v_imgbuf = + a_Imgbuf_new(Image->layout, I_RGB, width, height, gamma); DicEntry->TotalSize = width * height * 3; DicEntry->width = width; @@ -402,7 +403,7 @@ static void *Dicache_image(int ImgType, const char *MimeType, void *Ptr, dReturn_val_if_fail(MimeType && Ptr, NULL); if (!web->Image) { - web->Image = a_Image_new(NULL, web->bgColor); + web->Image = a_Image_new(NULL, NULL, web->bgColor); a_Image_ref(web->Image); } @@ -429,7 +430,7 @@ static void *Dicache_image(int ImgType, const char *MimeType, void *Ptr, *Data = DicEntry->DecoderData; *Call = (CA_Callback_t) a_Dicache_callback; - return (web->Image->dw); + return (web->Image->img_rnd); } /* diff --git a/src/form.cc b/src/form.cc index f756a1c9..7c747c5a 100644 --- a/src/form.cc +++ b/src/form.cc @@ -2004,10 +2004,14 @@ static Embed *Html_input_image(DilloHtml *html, const char *tag, int tagsize) /* create new image and add it to the button */ a_Html_image_attrs(html, tag, tagsize); if ((Image = a_Html_image_new(html, tag, tagsize))) { - IM2DW(Image)->setStyle (html->styleEngine->backgroundStyle ()); + // At this point, we know that Image->ir represents an image + // widget. Notice that the order of the casts matters, because + // of multiple inheritance. + dw::Image *dwi = (dw::Image*)(dw::core::ImgRenderer*)Image->img_rnd; + dwi->setStyle (html->styleEngine->backgroundStyle ()); ResourceFactory *factory = HT2LT(html)->getResourceFactory(); ComplexButtonResource *complex_b_r = - factory->createComplexButtonResource(IM2DW(Image), false); + factory->createComplexButtonResource(dwi, false); button = new Embed(complex_b_r); HT2TB(html)->addWidget (button, html->styleEngine->style ()); } diff --git a/src/html.cc b/src/html.cc index adfd818e..e8aeb12d 100644 --- a/src/html.cc +++ b/src/html.cc @@ -2195,7 +2195,9 @@ DilloImage *a_Html_image_new(DilloHtml *html, const char *tag, int tagsize) alt_ptr = dStrdup("[IMG]"); // Place holder for img_off mode } - image = a_Image_new(alt_ptr, 0); + dw::Image *dw = new dw::Image(alt_ptr); + image = + a_Image_new(html->dw->getLayout(), (void*)(dw::core::ImgRenderer*)dw, 0); if (HT2TB(html)->getBgColor()) image->bg_color = HT2TB(html)->getBgColor()->getColor(); @@ -2272,11 +2274,15 @@ static void Html_tag_content_img(DilloHtml *html, const char *tag, int tagsize) /* TODO: usemap URLs outside of the document are not used. */ usemap_url = a_Html_url_new(html, attrbuf, NULL, 0); - HT2TB(html)->addWidget((Widget*)Image->dw, html->styleEngine->style()); + // At this point, we know that Image->ir represents an image + // widget. Notice that the order of the casts matters, because of + // multiple inheritance. + dw::Image *dwi = (dw::Image*)(dw::core::ImgRenderer*)Image->img_rnd; + HT2TB(html)->addWidget(dwi, html->styleEngine->style()); /* Image maps */ if (a_Html_get_attr(html, tag, tagsize, "ismap")) { - ((::dw::Image*)Image->dw)->setIsMap(); + dwi->setIsMap(); _MSG(" Html_tag_open_img: server-side map (ISMAP)\n"); } else if (html->styleEngine->style ()->x_link != -1 && usemap_url == NULL) { @@ -2286,8 +2292,7 @@ static void Html_tag_content_img(DilloHtml *html, const char *tag, int tagsize) } if (usemap_url) { - ((::dw::Image*)Image->dw)->setUseMap(&html->maps, - new ::object::String(URL_STR(usemap_url))); + dwi->setUseMap(&html->maps, new ::object::String(URL_STR(usemap_url))); a_Url_free (usemap_url); } } @@ -2330,8 +2335,14 @@ static void Html_tag_close_map(DilloHtml *html) for (int i = 0; i < html->images->size(); i++) { DilloImage *img = html->images->get(i)->image; - if (img) - ((dw::Image*) img->dw)->forceMapRedraw(); + if (img) { + // At this point, we know that img->ir represents an image + // widget. (Really? Is this assumtion safe?) Notice that the + // order of the casts matters, because of multiple + // inheritance. + dw::Image *dwi = (dw::Image*)(dw::core::ImgRenderer*)img->img_rnd; + dwi->forceMapRedraw(); + } } html->InFlags &= ~IN_MAP; } diff --git a/src/image.cc b/src/image.cc index c499d977..570c012a 100644 --- a/src/image.cc +++ b/src/image.cc @@ -23,19 +23,20 @@ using namespace dw::core; -// Image to Object-Image macro -#define I2DW(Image) ((dw::Image*)(Image->dw)) +// Image to Object-ImgRenderer macro +#define I2IR(Image) ((dw::core::ImgRenderer*)(Image->img_rnd)) /* * Create and initialize a new image structure. */ -DilloImage *a_Image_new(const char *alt_text, int32_t bg_color) +DilloImage *a_Image_new(void *layout, void *img_rnd, int32_t bg_color) { DilloImage *Image; Image = dNew(DilloImage, 1); - Image->dw = (void*) new dw::Image(alt_text); + Image->layout = layout; + Image->img_rnd = img_rnd; Image->width = 0; Image->height = 0; Image->bg_color = bg_color; @@ -88,7 +89,7 @@ void a_Image_set_parms(DilloImage *Image, void *v_imgbuf, DilloUrl *url, _MSG("a_Image_set_parms: width=%d height=%d\n", width, height); bool resize = (Image->width != width || Image->height != height); - I2DW(Image)->setBuffer((Imgbuf*)v_imgbuf, resize); + I2IR(Image)->setBuffer((Imgbuf*)v_imgbuf, resize); if (!Image->BitVec) Image->BitVec = a_Bitvec_new(height); @@ -106,7 +107,7 @@ void a_Image_write(DilloImage *Image, uint_t y) dReturn_if_fail ( y < Image->height ); /* Update the row in DwImage */ - I2DW(Image)->drawRow(y); + I2IR(Image)->drawRow(y); a_Bitvec_set_bit(Image->BitVec, y); Image->State = IMG_Write; } diff --git a/src/image.hh b/src/image.hh index a66edaae..80f23461 100644 --- a/src/image.hh +++ b/src/image.hh @@ -44,7 +44,7 @@ typedef enum { } ImageState; struct _DilloImage { - void *dw; + void *layout, *img_rnd; /* Parameters as told by image data */ uint_t width; @@ -62,7 +62,7 @@ struct _DilloImage { /* * Function prototypes */ -DilloImage *a_Image_new(const char *alt_text, int32_t bg_color); +DilloImage *a_Image_new(void *layout, void *img_rnd, int32_t bg_color); void a_Image_ref(DilloImage *Image); void a_Image_unref(DilloImage *Image); diff --git a/src/imgbuf.cc b/src/imgbuf.cc index 16eb5c31..48e6bde5 100644 --- a/src/imgbuf.cc +++ b/src/imgbuf.cc @@ -90,10 +90,9 @@ void a_Imgbuf_unref(void *v_imgbuf) /* * Create a new Imgbuf */ -void *a_Imgbuf_new(void *v_dw, int img_type, uint_t width, uint_t height, +void *a_Imgbuf_new(void *layout, int img_type, uint_t width, uint_t height, double gamma) { - Layout *layout = ((Widget*)v_dw)->getLayout(); if (!layout) { MSG_ERR("a_Imgbuf_new: layout is NULL.\n"); exit(1); @@ -104,7 +103,8 @@ void *a_Imgbuf_new(void *v_dw, int img_type, uint_t width, uint_t height, linebuf = (uchar_t*) dRealloc(linebuf, linebuf_size); } - return (void*)layout->createImgbuf(Imgbuf::RGB, width, height, gamma); + return (void*)((Layout*)layout)->createImgbuf(Imgbuf::RGB, width, height, + gamma); } /* diff --git a/src/imgbuf.hh b/src/imgbuf.hh index af0bf9a6..efa1b48c 100644 --- a/src/imgbuf.hh +++ b/src/imgbuf.hh @@ -16,7 +16,7 @@ extern "C" { */ void a_Imgbuf_ref(void *v_imgbuf); void a_Imgbuf_unref(void *v_imgbuf); -void *a_Imgbuf_new(void *v_dw, int img_type, uint_t width, uint_t height, +void *a_Imgbuf_new(void *v_ir, int img_type, uint_t width, uint_t height, double gamma); int a_Imgbuf_last_reference(void *v_imgbuf); void a_Imgbuf_update(void *v_imgbuf, const uchar_t *buf, DilloImgType type, -- cgit v1.2.3