~unity-team/unity/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2013 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Marco Trevisan <marco.trevisan@canonical.com>
 */

#ifndef UNITY_DECORATION_WIDGETS
#define UNITY_DECORATION_WIDGETS

#include <deque>
#include <NuxCore/Size.h>
#include <NuxCore/Property.h>
#include <UnityCore/UWeakPtr.h>
#include "Introspectable.h"
#include "CompizUtils.h"
#include "RawPixel.h"

namespace unity
{
namespace decoration
{
namespace cu = compiz_utils;

class BasicContainer;

class Item : public sigc::trackable, public debug::Introspectable
{
public:
  typedef std::shared_ptr<Item> Ptr;
  typedef unity::uweak_ptr<Item> WeakPtr;
  typedef std::deque<Item::Ptr> List;

  Item();
  virtual ~Item() = default;

  nux::Property<bool> visible;
  nux::Property<bool> focused;
  nux::Property<bool> sensitive;
  nux::Property<bool> mouse_owner;
  nux::Property<double> scale;

  CompRect const& Geometry() const;
  virtual int GetNaturalWidth() const;
  virtual int GetNaturalHeight() const;

  virtual void SetCoords(int x, int y);
  virtual void SetX(int x) { SetCoords(x, Geometry().y()); }
  virtual void SetY(int y) { SetCoords(Geometry().x(), y); }
  virtual void SetSize(int width, int height);
  virtual void SetWidth(int width) { SetSize(width, Geometry().height()); }
  virtual void SetHeight(int height) { SetSize(Geometry().width(), height); };

  virtual void SetMaxWidth(int max_width);
  virtual void SetMaxHeight(int max_height);
  virtual void SetMinWidth(int min_width);
  virtual void SetMinHeight(int min_height);

  int GetMaxWidth() const;
  int GetMaxHeight() const;
  int GetMinWidth() const;
  int GetMinHeight() const;

  void SetParent(std::shared_ptr<BasicContainer> const&);
  std::shared_ptr<BasicContainer> GetParent() const;
  std::shared_ptr<BasicContainer> GetTopParent() const;

  void Damage();
  virtual void Draw(GLWindow*, GLMatrix const&, GLWindowPaintAttrib const&, CompRegion const&, unsigned mask) {}

protected:
  virtual CompRect& InternalGeo() = 0;
  sigc::signal<void> geo_parameters_changed;

  virtual bool IsContainer() const { return false; }
  void RequestRelayout();

  friend class InputMixer;
  virtual void MotionEvent(CompPoint const&, Time) {}
  virtual void ButtonDownEvent(CompPoint const&, unsigned button, Time) {}
  virtual void ButtonUpEvent(CompPoint const&, unsigned button, Time) {}

  std::string GetName() const { return "Item"; }
  void AddProperties(debug::IntrospectionData&);

private:
  Item(Item const&) = delete;
  Item& operator=(Item const&) = delete;

protected:
  nux::Size max_;
  nux::Size min_;
  nux::Size natural_;

private:
  unity::uweak_ptr<BasicContainer> parent_;
};

class SimpleItem : public Item
{
protected:
  CompRect& InternalGeo() { return rect_; }
  CompRect rect_;
};


class TexturedItem : public Item
{
public:
  typedef std::shared_ptr<TexturedItem> Ptr;

  void SetTexture(cu::SimpleTexture::Ptr const&);
  void Draw(GLWindow*, GLMatrix const&, GLWindowPaintAttrib const&, CompRegion const&, unsigned mask);
  void SetCoords(int x, int y);

  int GetNaturalWidth() const;
  int GetNaturalHeight() const;

protected:
  std::string GetName() const { return "TexturedItem"; }

  CompRect& InternalGeo();
  cu::SimpleTextureQuad texture_;
};


class BasicContainer : public SimpleItem, public std::enable_shared_from_this<BasicContainer>
{
public:
  typedef std::shared_ptr<BasicContainer> Ptr;

  BasicContainer();
  Item::List const& Items() const { return items_; }

  virtual CompRect ContentGeometry() const;

protected:
  friend class Item;
  void Relayout();
  bool IsContainer() const { return true; }

  std::string GetName() const { return "BasicContainer"; }
  void AddProperties(debug::IntrospectionData&);
  IntrospectableList GetIntrospectableChildren();

  Item::List items_;

private:
  virtual void DoRelayout() = 0;
  bool relayouting_;
};


class Layout : public BasicContainer
{
public:
  typedef std::shared_ptr<Layout> Ptr;

  Layout();

  nux::Property<RawPixel> inner_padding;
  nux::Property<RawPixel> left_padding;
  nux::Property<RawPixel> right_padding;
  nux::Property<RawPixel> top_padding;
  nux::Property<RawPixel> bottom_padding;

  void Append(Item::Ptr const&);
  void Remove(Item::Ptr const&);

  CompRect ContentGeometry() const;
  void Draw(GLWindow*, GLMatrix const&, GLWindowPaintAttrib const&, CompRegion const&, unsigned mask);

protected:
  std::string GetName() const { return "Layout"; }
  void AddProperties(debug::IntrospectionData&);
  void DoRelayout();

private:
  bool SetPadding(RawPixel& target, RawPixel const& new_value);
};

} // decoration namespace
} // unity namespace

#endif