CornerImage.C 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  3. *
  4. * See the LICENSE file for terms of use.
  5. */
  6. #include <stdio.h>
  7. #include "CornerImage.h"
  8. #include <Wt/WPainter>
  9. #include <Wt/WRasterImage>
  10. class CornerResource : public WResource
  11. {
  12. public:
  13. CornerResource(CornerImage *parent)
  14. : WResource(parent)
  15. { }
  16. virtual void handleRequest(const Http::Request& request,
  17. Http::Response& response) {
  18. CornerImage *img = dynamic_cast<CornerImage *>(parent());
  19. WRasterImage device("png", img->radius(), img->radius());
  20. paint(&device, img);
  21. device.handleRequest(request, response);
  22. }
  23. void paint(WPaintDevice *device, CornerImage *img)
  24. {
  25. WPainter painter(device);
  26. painter.setPen(NoPen);
  27. painter.setBrush(img->background());
  28. painter.drawRect(0, 0, img->radius(), img->radius());
  29. double cx, cy;
  30. if (img->corner() & Top)
  31. cy = img->radius() + 0.5;
  32. else
  33. cy = -0.5;
  34. if (img->corner() & Left)
  35. cx = img->radius() + 0.5;
  36. else
  37. cx = -0.5;
  38. painter.setBrush(img->foreground());
  39. painter.drawEllipse(cx - img->radius() - 0.5, cy - img->radius() - 0.5,
  40. 2 * img->radius(), 2 * img->radius());
  41. }
  42. };
  43. CornerImage::CornerImage(Corner c, WColor fg, WColor bg,
  44. int radius, WContainerWidget *parent)
  45. : WImage(parent),
  46. corner_(c),
  47. fg_(fg),
  48. bg_(bg),
  49. radius_(radius)
  50. {
  51. resource_ = new CornerResource(this);
  52. setImageLink(resource_);
  53. }
  54. void CornerImage::setRadius(int radius)
  55. {
  56. if (radius != radius_) {
  57. radius_ = radius;
  58. resource_->setChanged();
  59. }
  60. }
  61. void CornerImage::setForeground(WColor color)
  62. {
  63. if (fg_ != color) {
  64. fg_ = color;
  65. resource_->setChanged();
  66. }
  67. }