1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * gccv/group.h
6  *
7  * Copyright (C) 2008-2010 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
22  * USA
23  */
24 
25 #ifndef GCCV_GROUP_H
26 #define GCCV_GROUP_H
27 
28 /*!\file*/
29 
30 #include "item.h"
31 #include <list>
32 
33 namespace gccv {
34 
35 /*!
36 @brief Item with Item children.
37 
38 A Group is an Item grouping several children Item instances in a std::list.
39 Children might also be Group items themselves so that it allows for a
40 hierarchical tree if Item instances. The Canvas class owns a top level Group
41 (see Canvas::GetRoot()) and all Item instances in the canvas are descendants
42 of this root Group.
43 The Group class owns a pair of coordinates, x and y, which are used to shift all
44 the children.
45 */
46 class Group: public Item
47 {
48 public:
49 /*!
50 @param canvas a Canvas.
51 
52 Creates a new Group and sets it as a child of the root Group of \a canvas.
53 */
54 	Group (Canvas *canvas);
55 /*!
56 @param canvas a Canvas.
57 @param x the horizontal group shift.
58 @param y the vertical group shift.
59 
60 Creates a new Group at (\a x, \a y) and sets it as a child of the root Group of \a canvas.
61 */
62 	Group (Canvas *canvas, double x, double y);
63 /*!
64 @param parent the Group to which the new Group will be added.
65 @param client the ItemClient for the new Group if any.
66 
67 Creates a new Group inside \a parent and sets \a client as its associated
68 ItemClient.
69 */
70 	Group (Group *parent, ItemClient *client = NULL);
71 /*!
72 @param parent the Group to which the new Group will be added.
73 @param x the horizontal group shift.
74 @param y the vertical group shift.
75 @param client the ItemClient for the new Group if any.
76 
77 Creates a new Group at (\a x, \a y) inside \a parent and sets \a client as its
78 associated ItemClient.
79 */
80 	Group (Group *parent, double x, double y, ItemClient *client = NULL);
81 /*!
82 The destructor. When a Group is destroyed, all its children are destroyed too.
83 */
84 	virtual ~Group();
85 
86 /*!
87 @param item the new child.
88 
89 Adds \a item to the children list so that it will be displayed first, and so
90 will appear under other overlapping children. To add an Item on top all other
91 children, use thius method and then Group::MoveToFront().
92 */
93 	void AddChild (Item *item);
94 /*!
95 @param item to remove.
96 
97 Removes \a item to the children list but does not destroys it. \a item will not
98 be displayed anymore unless it is added to a new Group.
99 */
100 	void RemoveChild (Item *item);
101 /*!
102 @param item to move in the list.
103 
104 Changes the Item position in the children list so that it is displayed last on
105 top of other overlapping children.
106 */
107 	void MoveToFront (Item *item);
108 /*!
109 @param item to move in the list.
110 
111 Changes the Item position in the children list so that it is displayed first
112 below other overlapping children.
113 */
114 	void MoveToBack (Item *item);
115 
116 /*!
117 @param it a list iterator.
118 
119 @return the first child Item. Actually, the one displayed first.
120 */
121 	Item *GetFirstChild (std::list<Item *>::iterator &it);
122 /*!
123 @param it a list iterator initalized by a call to GetFirstChild().
124 
125 @return the next child Item if any.
126 */
127 	Item *GetNextChild (std::list<Item *>::iterator &it);
128 
129 /*!
130 @param x0 the top left horizontal bound to adjust.
131 @param y0 the top left vertical bound to adjust.
132 @param x1 the bottom right horizontal bound to adjust.
133 @param y1 the bottom right top left vertical to adjustw.
134 
135 Adjusts the parameters according to the shift values. This allows to evaluate
136 the absolute position of an Item inside the Canvas.
137 */
138 	void AdjustBounds (double &x0, double &y0, double &x1, double &y1) const;
139 /*!
140 @param x the horizontal position
141 @param y the vertical position
142 
143 Sets the Group shift values.
144 */
145 	void SetPosition (double x, double y);
146 
147 	// virtual methods
148 /*!
149 @param x horizontal position
150 @param y vertical position
151 @param item where to store the nearest Item.
152 
153 Implementation of Item::Distance() for the Group class. Sets \a item to the
154 descendant Item nearest to the given position.
155 */
156 	double Distance (double x, double y, Item **item) const;
157 /*!
158 @param cr a cairo_t.
159 @param x0 the top left horizontal bound of the region to draw.
160 @param y0 the top left vertical bound of the region to draw.
161 @param x1 the bottom right horizontal bound of the region to draw.
162 @param y1 the bottom right top left vertical bound of the region to draw.
163 @param is_vector whether the cairo_t is a vectorial context.
164 
165 Draws Group children to \a cr, limiting things to the given region.
166 */
167 	bool Draw (cairo_t *cr, double x0, double y0, double x1, double y1, bool is_vector) const;
168 /*!
169 @param x the horizontal deplacement
170 @param y the vertical deplacement
171 
172 Moves the Group and hence all its descendants by changing the Group shift values.
173 */
174 	void Move (double x, double y);
175 
176 protected:
177 	void UpdateBounds ();
178 
179 private:
180 	std::list<Item *> m_Children;
181 	double m_x, m_y;	// translation offset
182 };
183 
184 }
185 
186 #endif	//	 GCCV_GROUP_H
187