| 1 |
/* |
| 2 |
Copyright (C) 2002 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 kb_traverse.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief Graph traversal via keyboard |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <algorithm> |
| 27 |
#include <vector> |
| 28 |
#include "kb_traverse.h" |
| 29 |
|
| 30 |
// ctor |
| 31 |
KBTraverse::KBTraverse () |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
// dtor |
| 36 |
KBTraverse::~KBTraverse () |
| 37 |
{ |
| 38 |
clear (); |
| 39 |
} |
| 40 |
|
| 41 |
// move to prev element |
| 42 |
DlgNode* KBTraverse::left () |
| 43 |
{ |
| 44 |
if (Siblings.empty ()) return NULL; |
| 45 |
|
| 46 |
// get prev node |
| 47 |
if (Current == Siblings.begin ()) |
| 48 |
Current = Siblings.end (); |
| 49 |
|
| 50 |
Current--; |
| 51 |
|
| 52 |
// select it |
| 53 |
return *Current; |
| 54 |
} |
| 55 |
|
| 56 |
// move to next element |
| 57 |
DlgNode* KBTraverse::right () |
| 58 |
{ |
| 59 |
if (Siblings.empty ()) return NULL; |
| 60 |
|
| 61 |
// get next node |
| 62 |
if (++Current == Siblings.end ()) |
| 63 |
Current = Siblings.begin (); |
| 64 |
|
| 65 |
// select it |
| 66 |
return *Current; |
| 67 |
} |
| 68 |
|
| 69 |
// move to parent |
| 70 |
DlgNode* KBTraverse::up () |
| 71 |
{ |
| 72 |
if (Siblings.empty ()) return NULL; |
| 73 |
|
| 74 |
// try to select parent |
| 75 |
if (selectParents ()) return *Current; |
| 76 |
else return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
// move to child |
| 80 |
DlgNode* KBTraverse::down () |
| 81 |
{ |
| 82 |
if (Siblings.empty ()) return NULL; |
| 83 |
|
| 84 |
// try to select child |
| 85 |
if (selectChildren ()) return *Current; |
| 86 |
else return NULL; |
| 87 |
} |
| 88 |
|
| 89 |
// select parents of a node |
| 90 |
bool KBTraverse::selectParents () |
| 91 |
{ |
| 92 |
DlgNode *parent; |
| 93 |
DlgNode *current = *Current; |
| 94 |
|
| 95 |
// get first parent |
| 96 |
DlgNode *arrow = current->prev (FIRST); |
| 97 |
if (arrow != NULL) parent = arrow->prev (FIRST); |
| 98 |
else return false; |
| 99 |
|
| 100 |
// cleanup |
| 101 |
clear (); |
| 102 |
|
| 103 |
while (parent != NULL) |
| 104 |
{ |
| 105 |
addSibling (parent); |
| 106 |
|
| 107 |
// get subsequent parents |
| 108 |
arrow = current->prev (NEXT); |
| 109 |
if (arrow != NULL) parent = arrow->prev (FIRST); |
| 110 |
else parent = NULL; |
| 111 |
} |
| 112 |
|
| 113 |
// select first parent |
| 114 |
Current = Siblings.begin (); |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
// select children of a node |
| 120 |
bool KBTraverse::selectChildren () |
| 121 |
{ |
| 122 |
DlgNode *child; |
| 123 |
DlgNode *current = *Current; |
| 124 |
|
| 125 |
// get first parent |
| 126 |
DlgNode *arrow = current->next (FIRST); |
| 127 |
if (arrow != NULL) child = arrow->next (FIRST); |
| 128 |
else return false; |
| 129 |
|
| 130 |
// cleanup |
| 131 |
clear (); |
| 132 |
|
| 133 |
while (child != NULL) |
| 134 |
{ |
| 135 |
addSibling (child); |
| 136 |
|
| 137 |
// get subsequent children |
| 138 |
arrow = current->next (NEXT); |
| 139 |
if (arrow != NULL) child = arrow->next (FIRST); |
| 140 |
else child = NULL; |
| 141 |
} |
| 142 |
|
| 143 |
// select first parent |
| 144 |
Current = Siblings.begin (); |
| 145 |
|
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
// select a certain node |
| 150 |
bool KBTraverse::select (DlgNode *node) |
| 151 |
{ |
| 152 |
if (node == NULL) return false; |
| 153 |
|
| 154 |
// root node |
| 155 |
if (node->prev (FIRST) == NULL) |
| 156 |
{ |
| 157 |
clear (); |
| 158 |
Siblings.push_back (node); |
| 159 |
Current = Siblings.begin (); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
DlgNode *prev; |
| 165 |
|
| 166 |
// cleanup |
| 167 |
clear (); |
| 168 |
|
| 169 |
// make sure node ain't a link |
| 170 |
if (node->type () == LINK) |
| 171 |
{ |
| 172 |
prev = node->prev (FIRST); |
| 173 |
node = node->next (FIRST); |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
prev = node->prev (FIRST)->prev (FIRST); |
| 178 |
} |
| 179 |
|
| 180 |
// select children of parent node (i.e. siblings of node) |
| 181 |
Siblings.push_back (prev); |
| 182 |
Current = Siblings.begin (); |
| 183 |
selectChildren (); |
| 184 |
|
| 185 |
// actually select node |
| 186 |
Current = find (Siblings.begin (), Siblings.end (), node); |
| 187 |
|
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
// select root node |
| 192 |
DlgNode* KBTraverse::selectRoot (std::vector<DlgNode*> *nodes) |
| 193 |
{ |
| 194 |
std::vector<DlgNode*>::iterator i; |
| 195 |
|
| 196 |
// cleanup |
| 197 |
clear (); |
| 198 |
|
| 199 |
// find all the root nodes |
| 200 |
for (i = nodes->begin (); i != nodes->end (); i++) |
| 201 |
if ((*i)->type () != LINK) |
| 202 |
if ((*i)->prev (FIRST) == NULL) |
| 203 |
addSibling (*i); |
| 204 |
|
| 205 |
// actually select node |
| 206 |
Current = Siblings.begin (); |
| 207 |
|
| 208 |
return *Current; |
| 209 |
} |
| 210 |
|
| 211 |
// clear list of Siblings |
| 212 |
void KBTraverse::clear () |
| 213 |
{ |
| 214 |
Siblings.clear (); |
| 215 |
Current = Siblings.end (); |
| 216 |
} |
| 217 |
|
| 218 |
// add node to list of siblings |
| 219 |
void KBTraverse::addSibling (DlgNode *node) |
| 220 |
{ |
| 221 |
std::list<DlgNode*>::iterator i = Siblings.begin (); |
| 222 |
DlgRect *cmp1, *cmp2 = node->type () == LINK ? node->next (FIRST) : node; |
| 223 |
|
| 224 |
// search the proper place for insertion |
| 225 |
while (i != Siblings.end ()) |
| 226 |
{ |
| 227 |
cmp1 = (*i)->type () == LINK ? (*i)->next (FIRST) : *i; |
| 228 |
if (*cmp2 < *cmp1) break; |
| 229 |
i++; |
| 230 |
} |
| 231 |
|
| 232 |
// insert |
| 233 |
Siblings.insert (i, node); |
| 234 |
} |