1 // -*- C++ -*-
2 
3 /*
4  * GChemPaint library
5  * widgetdata.h
6  *
7  * Copyright (C) 2002-2012 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 GCHEMPAINT_WIDGET_DATA_H
26 #define GCHEMPAINT_WIDGET_DATA_H
27 
28 #include <gcu/object.h>
29 #include <gccv/structs.h>
30 #include <map>
31 #include <set>
32 
33 /*!\file*/
34 namespace gcp {
35 
36 class Application;
37 class View;
38 
39 /*!\var ClipboardDataType
40 The data type available for the default clipboard.
41 */
42 /*!\var ClipboardDataType
43 The data type available for the primary clipboard.
44 */
45 extern guint ClipboardDataType, ClipboardDataType1;
46 /*!
47 A global variable to store clipboard data as a string representation of an xml
48 document.
49 */
50 	extern xmlChar* ClipboardData;
51 /*!
52 A global variable to store clipboard data as a string, used for text.
53 */
54 extern char* ClipboardTextData;
55 /*!
56 @param clipboard a GtkClipboard.
57 @param selection_data the data to paste.
58 @param App the Application.
59 
60 A callback to use for receiving targets from a clipboard.
61 */
62 void on_receive_targets (GtkClipboard *clipboard, GtkSelectionData *selection_data, Application *App);
63 /*!
64 @param clipboard a GtkClipboard.
65 @param obj an object, might be the Application.
66 
67 A callback to use for clearing the clipboard data.
68 */
69 void on_clear_data (GtkClipboard *clipboard, gcu::Object *obj);
70 
71 /*!\enum SelectionState
72 Enumeration of the selection states used in GChemPaint in the gcu::Object::SetSelected method.
73 */
74 enum SelectionState
75 {
76 /*!
77 Unselected object.
78 */
79 	SelStateUnselected = 0,
80 /*!
81 Unselected object.
82 */
83 	SelStateSelected,
84 /*!
85 Edited or new object.
86 */
87 	SelStateUpdating,
88 /*!
89 The object is marked for deletion.
90 */
91 	SelStateErasing
92 };
93 
94 /*!\class WidgetData
95 This class contains all data associated with a widget displaying a document. It
96 might be deprecated in future versions since it was mostly useful for the Bonobo
97 version which is not anymore supported.
98 */
99 class WidgetData
100 {
101 public:
102 /*!
103 The document view.
104 */
105 	View* m_View;
106 /*!
107 The canvas widget to which this instance is associated.
108 */
109 	GtkWidget *Canvas;
110 /*!
111 The current zoom factor.
112 */
113 	double Zoom;
114 /*!
115 Maps the document objects to the canvas items which represent them.
116 */
117 //	std::map<gcu::Object const*, GnomeCanvasGroup*>Items;
118 /*!
119 The set of selected objects.
120 */
121 	std::set < gcu::Object * >SelectedObjects;
122 
123 /*!
124 @param obj an object.
125 @return true if the object is selected, false otherwise.
126 */
127 	bool IsSelected (gcu::Object const *obj) const;
128 
129 /*!
130 @param obj an object.
131 @return true if all the object children are selected, false otherwise or if \a obj
132 has no children.
133 */
134 	bool ChildrenSelected (gcu::Object const *obj) const;
135 
136 
137 /*!
138 @param obj an object.
139 @return the topmost ancestor whose all children are selected, NULL if
140 none or if the ancestor is the document.
141 */
142 	gcu::Object *GetSelectedAncestor (gcu::Object *obj);
143 
144 /*!
145 @param obj the object to select.
146 @param state the new selection state.
147 
148 Selects the specified object.
149 */
150 	void SetSelected (gcu::Object *obj, int state = gcp::SelStateSelected);
151 /*!
152 @param obj the object to unselect.
153 
154 Unselects a specified object.
155 */
156 	void Unselect (gcu::Object *obj);
157 /*!
158 Unselects everything.
159 */
160 	void UnselectAll ();
161 /*!
162 @param dx the x coordinate of the translation vector.
163 @param dy the y coordinate of the translation vector.
164 
165 Moves the items representing the selection, but don't move the objects
166 themselves and don't modify the document. This is used by the selection tool
167 but might be deprecated in the future.
168 */
169 	void MoveSelectedItems (double dx, double dy);
170 /*!
171 @param dx the x coordinate of the translation vector.
172 @param dy the y coordinate of the translation vector.
173 
174 Moves the selection. This method creates an Operation instance for the
175 Undo/Redo framework.
176 */
177 	void MoveSelection (double dx, double dy);
178 /*!
179 @param x the x coordinate of the rotation center.
180 @param y the y coordinate of the rotation center.
181 @param angle the rotation angle.
182 
183 Rotates the selection. This method does not create an Operation instance.
184 */
185 	void RotateSelection (double x, double y, double angle);
186 /*!
187 Empties the set of selected objects. Called after objects have been deleted.
188 */
ClearSelection()189 	void ClearSelection () {SelectedObjects.clear();}
190 /*!
191 @param clipboard a GtkClipboard.
192 
193 Copies the current selection to the clipboard.
194 */
195 	void Copy (GtkClipboard* clipboard);
196 /*!
197 @param rect an ArtDRect which will receive the selection bounds.
198 
199 Gets the selection bounds in canvas coordinates.
200 */
201 	void GetSelectionBounds (gccv::Rect &rect) const;
202 /*!
203 @return true if at least one object is selected, false otherwise.
204 */
HasSelection()205 	bool HasSelection () {return !(SelectedObjects.empty());}
206 /*!
207 Selects the whole document.
208 */
209 	void SelectAll ();
210 /*!
211 @param clipboard a GtkClipboard.
212 @return the xmlDocPtr associtated with the clipboard.
213 */
214 	static xmlDocPtr GetXmlDoc (GtkClipboard* clipboard);
215 /*!
216 @param state whether to show or not the selection.
217 
218 If \a state is true, the selection is highlighted, otherwise, it is displayed
219 normally. This is used when printing or exporting an image.
220 */
221 	void ShowSelection (bool state);
222 /*!
223 @param obj a gcu::Object.
224 @param rect a gccv::Rect which will receive the object bounds.
225 
226 Gets the object bounds in canvas coordinates.
227 */
228 	void GetObjectBounds (gcu::Object const *obj, gccv::Rect *rect) const;
229 /*!
230 @param objects a set of gcu::Object.
231 @param rect a gccv::Rect which will receive the object bounds.
232 
233 Gets the objects bounds in canvas coordinates.
234 */
235 	void GetObjectsBounds (std::set <gcu::Object const *> const &objects, gccv::Rect *rect) const;
236 	void GetObjectsBounds (std::set <gcu::Object *> const &objects, gccv::Rect *rect) const;
237 /*!
238 @param obj a gcu::Object.
239 @param rect a gccv::Rect which will receive the object bounds.
240 
241 Gets the object bounds in canvas coordinates.
242 */
243 	void GetObjectBounds (gcu::Object const* obj, gccv::Rect &rect) const;
244 
245 /*!
246 Replace the selected objects by their parents if all parents children are
247 selected.
248 */
249 	void SimplifySelection ();
250 
251 private:
252 	void MoveItems (gcu::Object *obj, double dx, double dy);
253 	void _GetObjectBounds (gcu::Object const* obj, gccv::Rect &rect) const;
254 
255 };
256 
257 }	// namespace gcp
258 
259 #endif //GCHEMPAINT_WIDGET_DATA_H
260