]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/scout-gfx/include/horizontal_shadow
Update
[l4.git] / l4 / pkg / scout-gfx / include / horizontal_shadow
1 // vi:ft=cpp
2 /*
3  * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  */
10 #pragma once
11
12 #include <l4/mag-gfx/canvas>
13 #include <l4/scout-gfx/widget>
14
15 namespace Scout_gfx { namespace Pt {
16
17 template <typename PT>
18 class Horizontal_shadow : public Widget
19 {
20 private:
21   int const _intensity;
22   int const _h;
23
24 public:
25
26   explicit Horizontal_shadow(int height, int intensity)
27   : _intensity(intensity), _h(height)
28   { _size = Area(0, height); }
29
30   /**
31    * Element interface
32    */
33   void draw(Canvas *c, Point const &p);
34   Widget *find(Point const &) { return 0; }
35
36   Area min_size() const { return Area(0, _h); }
37   Area preferred_size() const { return Area(0, _h); }
38   Area max_size() const { return Area(Area::Max_w, _h); }
39   bool empty() const { return false; }
40   Orientations expanding() const { return Horizontal; }
41   Rect geometry() const { return Rect(_pos, _size); }
42   void set_geometry(Rect const &s)
43   {
44     Area x = s.area().max(min_size()).max(min_size());
45     _pos = s.p1(); _size = x;
46   }
47
48 };
49
50
51 /***********************
52  ** Horizontal shadow **
53  ***********************/
54
55 template <typename PT>
56 void Horizontal_shadow<PT>::draw(Canvas *c, Point const &_p)
57 {
58   typedef typename PT::Pixel Pixel;
59   typedef typename PT::Color Color;
60
61   char *addr = (char *)c->buffer();
62
63   if (!addr)
64     return;
65
66   Rect clip = c->clip();
67   Rect cr = (Rect(_size) + _p) & clip;
68
69   if (!cr.valid())
70     return;
71
72   int curr_a = _intensity;
73   int step   = _size.h() ? (curr_a/_size.h()) : 0;
74
75   int bpl = c->bytes_per_line();
76
77   addr += bpl * cr.y1() + cr.x1() * sizeof(Pixel);
78
79   Color shadow_color(0,0,0);
80
81   int h = cr.h();
82   for (int j = 0; j < h; j++, addr += bpl)
83     {
84       Pixel *d = reinterpret_cast<Pixel*>(addr);
85       int w = cr.w();
86
87       for (int i = 0; i < w; i++, d++)
88         *d = PT::mix(*d, shadow_color, curr_a);
89
90       curr_a -= step;
91     }
92 }
93
94 }}