.
1 /*
2 Copyright (C) 2014 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 using Cairo;
16 using Math;
17
18 namespace BirdFont {
19
20 public abstract class Widget : GLib.Object {
21
22 public double margin_bottom = 0;
23 public double widget_x = 0;
24 public double widget_y = 0;
25
26 public abstract double get_height ();
27 public abstract double get_width ();
28 public abstract void draw (Context cr);
29
30 public WidgetAllocation allocation = new WidgetAllocation ();
31
32 public void draw_rounded_rectangle (Context cr, double x, double y, double w, double h, double radius) {
33 // fixme radius is padding not margin
34 cr.move_to (x, y + radius);
35 cr.arc (x + radius, y + radius, radius, 2 * (PI / 2), 3 * (PI / 2));
36 cr.line_to (x + w - radius, y);
37 cr.arc (x + w - radius, y + radius, radius, 3 * (PI / 2), 4 * (PI / 2));
38 cr.line_to (x + w, y + h);
39 cr.arc (x + w - radius, y + h, radius, 4 * (PI / 2), 5 * (PI / 2));
40 cr.line_to (x + radius, y + h + radius);
41 cr.arc (x + radius, y + h, radius, 5 * (PI / 2), 6 * (PI / 2));
42 cr.line_to (x, y + radius);
43 cr.close_path ();
44 }
45
46 public bool is_over (double x, double y) {
47 return widget_x <= x <= widget_x + get_width ()
48 && widget_y <= y <= widget_y + get_height ();
49 }
50
51 public bool is_on_screen () {
52 return (widget_y <= 0 <= widget_y + get_height ())
53 || (widget_y <= allocation.height <= widget_y + get_height ())
54 || (0 <= widget_y <= allocation.height);
55 }
56 }
57
58 }
59