Dillo v3.1.1-111-gd4f56d0d
Loading...
Searching...
No Matches
dw_image_background.cc
Go to the documentation of this file.
1/*
2 * Dillo Widget
3 *
4 * Copyright 2013 Sebastian Geerken <sgeerken@dillo.org>
5 * Copyright 2023 Rodrigo Arias Mallo <rodarima@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <stdlib.h>
22
23#include <FL/Fl.H>
24#include <FL/Fl_Window.H>
25
26#include "dw/core.hh"
27#include "dw/fltkcore.hh"
28#include "dw/fltkviewport.hh"
29#include "dw/textblock.hh"
30#include "dw/image.hh"
31
32using namespace lout::signal;
33using namespace lout::misc;
34using namespace dw;
35using namespace dw::core;
36using namespace dw::core::style;
37using namespace dw::fltk;
38
39class ImageStyleDeletionReceiver: public ObservedObject::DeletionReceiver
40{
41public:
42 void deleted (ObservedObject *object);
43};
44
45static StyleImage *image1 = NULL, *image2 = NULL;
46static Layout *layout;
47static int imgRow1 = 0, imgRow2 = 0;
48static ImageStyleDeletionReceiver isdr;
49
50void ImageStyleDeletionReceiver::deleted (ObservedObject *object)
51{
52 if ((StyleImage*)object == image1)
53 image1 = NULL;
54 else if ((StyleImage*)object == image2)
55 image2 = NULL;
56 else
58}
59
60static void imageInitTimeout (void *data)
61{
62 if (image1) {
63 Imgbuf *imgbuf1 = layout->createImgbuf (Imgbuf::RGB, 400, 200, 1);
64 image1->getMainImgRenderer()->setBuffer (imgbuf1, false);
65 }
66
67 if (image2) {
68 Imgbuf *imgbuf2 = layout->createImgbuf (Imgbuf::RGB, 100, 100, 1);
69 image2->getMainImgRenderer()->setBuffer (imgbuf2, false);
70 }
71}
72
73static void imageDrawTimeout (void *data)
74{
75 Imgbuf *imgbuf1 = image1 ? image1->getImgbufSrc () : NULL;
76 Imgbuf *imgbuf2 = image2 ? image2->getImgbufSrc () : NULL;
77
78 if (imgbuf1 && imgRow1 < 200) {
79 byte buf[3 * 400];
80 for(int x = 0; x < 400; x++) {
81 buf[3 * x + 0] = 128 + x * 127 / 399;
82 buf[3 * x + 1] = 128 + (399 - x) * 127 / 399;
83 buf[3 * x + 2] = 128 + imgRow1 * 127 / 199;
84 }
85
86 imgbuf1->copyRow (imgRow1, buf);
88 imgRow1++;
89 }
90
91 if (imgbuf2 && imgRow2 < 100) {
92 byte buf[3 * 100];
93 for(int x = 0; x < 100; x++) {
94 int r = 128 + rand () % 127;
95 buf[3 * x + 0] = buf[3 * x + 1] = buf[3 * x + 2] = r;
96 }
97
98 imgbuf2->copyRow (imgRow2, buf);
100 imgRow2++;
101 }
102
103 if(imgRow1 < 200 || imgRow2 < 100)
104 Fl::repeat_timeout (0.5, imageDrawTimeout, NULL);
105}
106
107int main(int argc, char **argv)
108{
110 layout = new Layout (platform);
111
112 Fl_Window *window = new Fl_Window(200, 300, "Dw Example");
113 window->box(FL_NO_BOX);
114 window->begin();
115
116 FltkViewport *viewport = new FltkViewport (0, 0, 200, 300);
118
123 createAbsLength (30));
124
125 StyleAttrs styleAttrs;
126 styleAttrs.initValues ();
127 styleAttrs.margin.setVal (5);
128 styleAttrs.x_lang[0] = 'e';
129 styleAttrs.x_lang[1] = 'n';
130
131 FontAttrs fontAttrs;
132 fontAttrs.name = "Bitstream Charter";
133 fontAttrs.size = 14;
134 fontAttrs.weight = 400;
135 fontAttrs.style = FONT_STYLE_NORMAL;
136 fontAttrs.letterSpacing = 0;
138 styleAttrs.font = style::Font::create (layout, &fontAttrs);
139
140 styleAttrs.color = Color::create (layout, 0x000000);
141 //styleAttrs.backgroundColor = Color::create (layout, 0xffffff);
142
143 Style *widgetStyle = Style::create (&styleAttrs);
144
145 Textblock *textblock = new Textblock (false);
146 textblock->setStyle (widgetStyle);
147 layout->setWidget (textblock);
148
150
151 styleAttrs.margin.setVal (0);
152 styleAttrs.backgroundColor = NULL;
153 styleAttrs.backgroundImage = NULL;
154
155 Style *wordStyle = Style::create (&styleAttrs);
156
157 image2 = styleAttrs.backgroundImage = StyleImage::create ();
160 styleAttrs.backgroundPositionX = createPerLength (0);
161 styleAttrs.backgroundPositionY = createPerLength (0);
162 Style *wordStyleBg = Style::create (&styleAttrs);
163
164 for(int i = 1; i <= 1; i++) {
165 char buf[4];
166 sprintf(buf, "%d.", i);
167
168 const char *words[] = { "This", "is", "the", buf, "paragraph.",
169 "Here", "comes", "some", "more", "text",
170 "to", "demonstrate", "word", "wrapping.",
171 NULL };
172
173 for(int j = 0; words[j]; j++) {
174 textblock->addText(words[j], j == 11 ? wordStyleBg : wordStyle);
175 textblock->addSpace(wordStyle);
176 }
177
178 textblock->addParbreak(10, wordStyle);
179 }
180
181 wordStyle->unref();
182 wordStyleBg->unref();
183
184 textblock->flush ();
185
186 window->resizable(viewport);
187 window->show();
188
189 Fl::add_timeout (1.0, imageInitTimeout, NULL);
190 Fl::add_timeout (0.1, imageDrawTimeout, NULL);
191
192 int errorCode = Fl::run();
193
194 delete layout;
195
196 return errorCode;
197}
int main(void)
Definition bookmarks.c:1613
A Widget for rendering text blocks, i.e.
Definition textblock.hh:206
void addSpace(core::style::Style *style)
?
void addText(const char *text, size_t len, core::style::Style *style)
Add a word to the page structure.
void addParbreak(int space, core::style::Style *style)
Cause a paragraph break.
virtual void drawRow(int row)=0
Called, when data from a row is available and has been copied into the image buffer.
virtual void setBuffer(core::Imgbuf *buffer, bool resize=false)=0
Called, when an image buffer is attached.
The platform independent interface for image buffers.
Definition imgbuf.hh:162
virtual void copyRow(int row, const byte *data)=0
The central class for managing and drawing a widget tree.
Definition layout.hh:17
void attachView(View *view)
Attach a view to the layout.
Definition layout.cc:459
void setWidget(Widget *widget)
Definition layout.cc:433
Imgbuf * createImgbuf(Imgbuf::Type type, int width, int height, double gamma)
Definition layout.hh:410
void setBgImage(style::StyleImage *bgImage, style::BackgroundRepeat bgRepeat, style::BackgroundAttachment bgAttachment, style::Length bgPositionX, style::Length bgPositionY)
Definition layout.cc:839
virtual void setStyle(style::Style *style)
Change the style of a widget.
Definition widget.cc:1316
void setVal(int val)
Definition style.hh:510
static Color * create(Layout *layout, int color)
Definition style.cc:529
FontVariant fontVariant
Definition style.hh:688
static Font * create(Layout *layout, FontAttrs *attrs)
Definition style.cc:444
StyleImage * backgroundImage
Definition style.hh:537
BackgroundRepeat backgroundRepeat
Definition style.hh:538
ImgRenderer * getMainImgRenderer()
Definition style.hh:886
static StyleImage * create()
Definition style.hh:873
static Style * create(StyleAttrs *attrs)
Definition style.hh:629
virtual void deleted(ObservedObject *object)=0
An observed object has a signal emitter, which tells the receivers, when the object is deleted.
Definition signal.hh:275
void connectDeletion(DeletionReceiver *receiver)
Definition signal.hh:302
static Style * wordStyle
static Style * widgetStyle
static Fl_Window * window
static FltkPlatform * platform
static FltkViewport * viewport
static ImageStyleDeletionReceiver isdr
static int imgRow1
static void imageInitTimeout(void *data)
static Layout * layout
static StyleImage * image1
static int imgRow2
static StyleImage * image2
static void imageDrawTimeout(void *data)
Anything related to Dillo Widget styles is defined here.
Definition style.cc:34
@ BACKGROUND_ATTACHMENT_SCROLL
Definition style.hh:245
@ BACKGROUND_REPEAT_Y
Definition style.hh:240
Length createPerLength(double v)
Returns a percentage, v is relative to 1, not to 100.
Definition style.hh:435
Length createAbsLength(int n)
Returns a length of n pixels.
Definition style.hh:432
@ FONT_VARIANT_NORMAL
Definition style.hh:333
The core of Dw is defined in this namespace.
Definition core.hh:23
This namespace contains FLTK implementations of Dw interfaces.
Dw is in this namespace, or sub namespaces of this one.
Miscellaneous stuff, which does not fit anywhere else.
Definition misc.cc:31
void assertNotReached()
Definition misc.hh:56
This namespace provides base classes to define signals.
Definition signal.cc:25