1 /*
2  * Copyright (C) 2008 Emweb bv, Herent, Belgium.
3  *
4  * See the LICENSE file for terms of use.
5  */
6 #include <stdio.h>
7 
8 #include "CornerImage.h"
9 
10 #include <Wt/WPainter.h>
11 #include <Wt/WRasterImage.h>
12 #include <Wt/WWidget.h>
13 
14 class CornerResource : public WResource
15 {
16 public:
CornerResource(CornerImage * img)17   CornerResource(CornerImage *img)
18     : WResource(),
19     img_(img)
20   { }
21 
~CornerResource()22   virtual ~CornerResource()
23   {
24     beingDeleted();
25   }
26 
handleRequest(const Http::Request & request,Http::Response & response)27   virtual void handleRequest(const Http::Request& request,
28 			     Http::Response& response) {
29     WRasterImage device("png", img_->radius(), img_->radius());
30     paint(&device, img_);
31 
32     device.handleRequest(request, response);
33   }
34 
paint(WPaintDevice * device,CornerImage * img)35   void paint(WPaintDevice *device, CornerImage *img)
36   {
37     WPainter painter(device);
38 
39     painter.setPen(WPen(PenStyle::None));
40 
41     painter.setBrush(img->background());
42     painter.drawRect(0, 0, img->radius(), img->radius());
43 
44     double cx, cy;
45 
46     if (img->corner() == Corner::TopLeft ||
47 	img->corner() == Corner::TopRight)
48       cy = img->radius() + 0.5;
49     else
50       cy = -0.5;
51 
52     if (img->corner() == Corner::TopLeft ||
53 	img->corner() == Corner::BottomLeft)
54       cx = img->radius() + 0.5;
55     else
56       cx = -0.5;
57 
58     painter.setBrush(img->foreground());
59     painter.drawEllipse(cx - img->radius() - 0.5, cy - img->radius() - 0.5,
60 			2 * img->radius(), 2 * img->radius());
61   }
62 private:
63   CornerImage *img_;
64 };
65 
CornerImage(Corner c,WColor fg,WColor bg,int radius)66 CornerImage::CornerImage(Corner c, WColor fg, WColor bg, int radius)
67   : WImage(),
68     corner_(c),
69     fg_(fg),
70     bg_(bg),
71     radius_(radius)
72 {
73   resource_ = std::make_shared<CornerResource>(this);
74   setImageLink(WLink(resource_));
75 }
76 
setRadius(int radius)77 void CornerImage::setRadius(int radius)
78 {
79   if (radius != radius_) {
80     radius_ = radius;
81     resource_->setChanged();
82   }
83 }
84 
setForeground(WColor color)85 void CornerImage::setForeground(WColor color)
86 {
87   if (fg_ != color) {
88     fg_ = color;
89     resource_->setChanged();
90   }
91 }
92