Debian Codegraph @

src/tools/dlgedit/dlg_module.h

raw
1 /*
2 Copyright (C) 2002/2003 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file dlg_module.h
21 *
22 * @author Kai Sterker
23 * @brief One complete dialogue or sub-dialogue.
24 */
25
26 #ifndef DLG_MODULE_H
27 #define DLG_MODULE_H
28
29 #include "dlg_module_entry.h"
30 #include "kb_traverse.h"
31
32 /**
33 * This class contains a dialogue, made up of elementary nodes and
34 * (optionally) sub-dialogues. It is different from the other
35 * nodes as it contains a complete dialogue, but can also be part
36 * of another dialogue.
37 */
38 class DlgModule : public DlgNode
39 {
40 public:
41 /**
42 * Create a new, empty dialogue module.
43 * @param p The full path to the file containing the module.
44 * @param n The actual name without file extension.
45 * @param u Unique id of the module.
46 * @param d Description of the module.
47 * @param id Serial number of the module.
48 */
49 DlgModule (std::string p, std::string n, std::string u, std::string d);
50
51 /**
52 * Delete a subdialogue.
53 */
54 ~DlgModule ();
55
56 /**
57 * Reset the dialogue to its state after construction. Deletes all
58 * nodes and clears the DlgModuleEntry.
59 */
60 void clear ();
61
62 /**
63 * Set the node id of this module. The toplevel module will always
64 * have the id 0. Sub-modules will get the next unused node id of
65 * the top-level module. That means, sub-modules may end up with the
66 * same id than circles of the same parent module. But this is not as
67 * tragic as it might seem, as circles and modules are treated
68 * differently when searching for a certain node.
69 */
70 void setID ()
71 {
72 int &id = toplevel ()->serial ();
73 nid_ = id;
74 id++;
75 }
76
77 /**
78 * @name Module drawing
79 */
80 //@{
81 /**
82 * Calculate this module's shape. The shape is a rectangle centered
83 * around the given DlgPoint. It is large enough to display the
84 * module's name.
85 * @param center The point to center the module shape around.
86 */
87 void initShape (const DlgPoint &center);
88
89 /**
90 * Draw this node to the given surface with the specified offset.
91 * The node's current mode determines the color thw node is drawn with.
92 * @param surface the cairo_surface_t to draw to
93 * @param offset the DlgPoint to use as offset
94 * @param widget the GtkWidget to update after drawing
95 */
96 void draw (cairo_surface_t *surface, DlgPoint &offset, GtkWidget *widget);
97 //@}
98
99 /**
100 * @name Node handling
101 */
102 //@{
103 /**
104 * Add the given node to the dialogue.
105 * @param node the DlgNode to add.
106 */
107 void addNode (DlgNode *node);
108
109 /**
110 * Delete the currently selected node from the dialogue. If the node
111 * is a circle, also deletes all attached arrows.
112 * @return \b true> if the node has been deleted, \b false otherwise.
113 */
114 bool deleteNode ();
115
116 /**
117 * Delete the given node from the dialogue.
118 * @param node The node to delete.
119 */
120 void deleteNode (DlgNode *node);
121
122 /**
123 * Select a node from the list of nodes.
124 * @param node The DlgNode to select.
125 * @return \b true if the node has been selected, \b false otherwise.
126 */
127 bool selectNode (DlgNode *node);
128
129 /**
130 * Mark the given node as highlighted, to show that is under the cursor.
131 * @param node the DlgNode to be highlighted
132 * @return the the previously highlighted node, or \b NULL if no
133 * node has been highlighted
134 */
135 DlgNode* highlightNode (DlgNode *node);
136
137 /**
138 * Select the root node of the dialogue.
139 * @return \b true if the node has been selected, \b false otherwise.
140 */
141 bool selectRoot ();
142
143 /**
144 * Deselect a previously selected node.
145 * @return the previously selected DlgNode, or \b NULL if no
146 * node has been deselected
147 */
148 DlgNode* deselectNode ();
149
150 /**
151 * Get the node at the given position.
152 * @return the DlgNode at the positon, or \b NULL if there is none.
153 */
154 DlgNode* getNode (DlgPoint &point);
155
156 /**
157 * Get the node that is currently selected.
158 * @return the DlgNode currently selected
159 */
160 DlgNode* selected () { return selected_; }
161 //@}
162
163 /**
164 * @name Node retrieval
165 */
166 //@{
167 /**
168 * Get the node with the given module and node id. First locates
169 * the module with given mid. Then locates the node with the given
170 * nid within this module.
171 * @param mid The node id of the module the node is located in.
172 * @param nid The node id of the node to retrieve.
173 * @return the DlgNode with that id, or \b NULL if there is none.
174 */
175 DlgNode* getNode (int mid, int nid);
176 /**
177 * Get the node with the given node id in the current module.
178 * @param nid The node id of the node to retrieve.
179 * @return the DlgNode with that id, or \b NULL if there is none.
180 */
181 DlgNode* getNode (int id);
182 /**
183 * Get the (sub-)module with the given node id in the current module.
184 * @param nid The node id of the node to retrieve.
185 * @return the DlgNode with that id, or \b NULL if there is none.
186 */
187 DlgModule* getModule (int id);
188 /**
189 * Return the first parent in the chain of parents, i.e. the
190 * toplevel module.
191 * @return the module in the chain of parents with no parent.
192 */
193 DlgModule *toplevel ();
194 //@}
195
196 /**
197 * Get the extension of the module for centering in view.
198 *
199 * @param min_x will contain position of leftmost node
200 * @param min_y will contain position of rightmost node
201 * @param y will contain position of topmost node
202 */
203 void extension (int &min_x, int &max_x, int &y);
204
205 /**
206 * @name Loading / Saving
207 */
208 //@{
209 /**
210 * Init the Dialogue from a file
211 * @return \b true if loading was successful, \b false otherwise.
212 */
213 bool load ();
214
215 /**
216 * Init a sub-dialogue from a file.
217 */
218 void loadSubdialogue ();
219
220 /**
221 * Save the Dialogue to a file
222 * @param path full path of the dialogue.
223 * @param name file name without extension.
224 * @return \b true if saving was successful, \b false otherwise.
225 */
226 bool save (std::string &path, std::string &name);
227
228 /**
229 * Save module as sub-dialogue. In contrast to the method above,
230 * this only saves a 'reference' to the module. It will have to be
231 * restored from the actual source file.
232 *
233 * In order to allow different users to work on the same dialogues,
234 * the reference will be relative to CfgProject::BaseDir. That way,
235 * a dialogue referencing sub-dialogues can be loaded even if it is
236 * moved to a different location, provided the relative paths of
237 * included modules have not changed.
238 *
239 * @param file an opened file.
240 */
241 void save (std::ofstream &file);
242 //@}
243
244 /**
245 * @name Filename and Path retrieval
246 */
247 //@{
248 /**
249 * Get the name of this dialogue.
250 * @return a reference to the dialogue's name.
251 */
252 std::string &name () { return name_; }
253 /**
254 * Get the name and id of this dialogue. To be used for window
255 * captions and window menu.
256 * @return string composed of name and unique id.
257 */
258 std::string shortName () { return name_ + uid_; }
259 /**
260 * Get the full path and filename of this dialogue. To be used when
261 * saving the dialogue.
262 * @return string composed of path, filename and file extension.
263 */
264 std::string fullName () { return path_ + name_ + FILE_EXT; }
265 /**
266 * Return the path and filename of this dialogue relative to its
267 * parent.
268 * @return location on disk relative to parent.
269 */
270 std::string relativeName ();
271 /**
272 * Get the module's path
273 * @return the full path of the module
274 */
275 std::string path () { return path_; }
276 //@}
277
278 /**
279 * @name Member access
280 */
281 //@{
282 /**
283 * Get the list of nodes in this dialogue.
284 * @return a reference to the list of nodes.
285 */
286 std::vector<DlgNode*> &getNodes () { return nodes; }
287
288 /**
289 * Get the current offset of this dialogue.
290 * @return a reference to the dialogue's offset.
291 */
292 DlgPoint &offset () { return offset_; }
293
294 /**
295 * Get the serial number for the next node that is
296 * created. It is up to the caller of this function to
297 * increase the number if neccessary.
298 * @return Id to use for the next node.
299 */
300 int &serial () { return serial_; }
301
302 /**
303 * Whether this (sub-)dialogue has been in view before switching
304 * dialogues.
305 * @return \b true if that is the case, \b false otherwise.
306 */
307 bool displayed () { return displayed_; }
308 /**
309 * Mark this dialogue as displayed.
310 * @param c Set to \b true to mark the dialogue as displayed.
311 */
312 void setDisplayed (bool displayed) { displayed_ = displayed; }
313
314 /**
315 * Check whether this dialogue has been changed since it's been
316 * opened or saved.
317 * @return \b true if that is the case, \b false otherwise.
318 */
319 bool changed () { return changed_; }
320 /**
321 * Mark this dialogue as changed.
322 * @param c Set to \b true to mark the dialogue as changed.
323 */
324 void setChanged (bool c = true);
325
326 /**
327 * Retrieve the parent dialogue of this module. If the module is
328 * a sub-dialogue, this will point to the dialogue that contains
329 * the module. If it is the top-level dialogue, the parent will be
330 * NULL.
331 * @return parent of this dialogue, or \b NULL if it's a top-level dialogue
332 */
333 DlgModule *parent () { return parent_; }
334 /**
335 * Set the parent of this dialogue.
336 * @param parent The module containing this sub-dialogue
337 */
338 void setParent (DlgModule *parent) { parent_ = parent; }
339
340 /**
341 * Get the current state of the module, i.e. whether a node is selected,
342 * highlighted, etc ... .
343 * @return The module's state.
344 */
345 mode_type state () { return state_; }
346 /**
347 * Set the state mode of the module.
348 * @param state The modules new state.
349 */
350 void setState (mode_type state) { state_ = state; }
351
352 /**
353 * Get a pointer to the module entry
354 * @return the DlgModuleEntry of this module
355 */
356 DlgModuleEntry *entry () { return &entry_; }
357
358 /**
359 * Get a pointer to the keyboard graph traversal helper. This keeps track
360 * of the current selection's siblings. Users can navigate through them
361 * via the arrow keys.
362 * @return the KBTraverse of this module
363 */
364 KBTraverse *traverse () { return &traverse_; }
365 //@}
366
367 protected:
368 std::vector<DlgNode*> nodes;// all the nodes in this dialogue
369 DlgNode *selected_; // the node currently selected
370 DlgNode *highlighted_; // the node currently under the cursor
371 DlgModule *parent_; // parent of sub-dialogue
372
373 DlgPoint offset_; // The current offset in the graph view
374 bool displayed_; // Whether that (sub-)dialogue was in view
375 bool changed_; // Whether there were changes since saving
376 int serial_; // Id to use for the next new node
377
378 mode_type state_; // one of NONE, HILIGHTED, SELECTED
379
380 std::string name_; // Short (file-) name of the dialogue
381 std::string path_; // Path of the dialogue
382 std::string uid_; // Unique number of the dialogue
383
384 DlgModuleEntry entry_; // further content of the dialogue
385
386 KBTraverse traverse_; // Keyboard graph traversal helper
387
388 private:
389 void init (); // initialize a newly constructed DlgModule
390 };
391
392 #endif // DLG_MODULE_H