When a pointer to a struct is expanded the struct is also expanded.
[kdbg.git] / kdbg / exprwnd.cpp
blobfdb9ddcc092246ac8ac023bfa115685bde7de6ee
1 // $Id$
3 // Copyright by Johannes Sixt
4 // This file is under GPL, the GNU General Public Licence
6 #include "exprwnd.h"
7 #include "exprwnd.moc"
8 #include "typetable.h"
9 #include <qstrlist.h>
10 #include <qpainter.h>
11 #include <kapp.h>
12 #include <kiconloader.h> /* icons */
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16 #include "mydebug.h"
18 VarTree::VarTree(const QString& name, NameKind aKind) :
19 KTreeViewItem(name),
20 m_varKind(VKsimple),
21 m_nameKind(aKind),
22 m_valueChanged(false),
23 m_type(0),
24 m_exprIndex(0),
25 m_exprIndexUseGuard(false)
29 VarTree::~VarTree()
33 void VarTree::paintValue(QPainter* p)
35 p->save();
36 int cellHeight = height(p->fontMetrics());
37 int textX = 2;
38 int textY = (cellHeight - p->fontMetrics().height()) / 2 +
39 p->fontMetrics().ascent();
41 if (m_valueChanged) {
42 p->setPen(red);
44 // p->setBackgroundColor(cg.base());
45 p->drawText(textX, textY, m_value, m_value.length());
46 p->restore();
49 int VarTree::valueWidth()
51 assert(owner != 0);
52 return owner->fontMetrics().width(m_value) + 4;
55 QString VarTree::computeExpr() const
57 assert(getParent() != 0);
58 // just to be sure
59 if (getParent() == 0)
60 return QString();
62 // top-level items are special
63 if (getParent()->getParent() == 0)
64 return getText();
66 // get parent expr
67 VarTree* par = static_cast<VarTree*>(getParent());
68 QString parentExpr = par->computeExpr();
70 /* don't add this item's name if this is a base class sub-item */
71 if (m_nameKind == NKtype) {
72 return parentExpr;
74 /* augment by this item's text */
75 QString result;
76 /* if this is an address, dereference it */
77 if (m_nameKind == NKaddress) {
78 ASSERT(par->m_varKind == VKpointer);
79 result = "*" + parentExpr;
80 return result;
82 switch (par->m_varKind) {
83 case VKarray:
85 QString index = getText();
86 int i = 1;
87 // skip past the index
88 while (index[i].isDigit())
89 i++;
91 * Some array indices are actually ranges due to repeated array
92 * values. We use the first index in these cases.
94 if (index[i] != ']') {
95 // remove second index
96 index.remove(i, index.length()-i-1);
98 result = "(" + parentExpr + ")" + index;
100 break;
101 case VKstruct:
102 result = "(" + parentExpr + ")." + getText();
103 break;
104 case VKsimple: /* parent can't be simple */
105 case VKpointer: /* handled in NKaddress */
106 case VKdummy: /* can't occur at all */
107 ASSERT(false);
108 result = parentExpr; /* paranoia */
109 break;
111 return result;
114 bool VarTree::isToplevelExpr() const
116 return getParent() != 0 && getParent()->getParent() == 0;
119 bool VarTree::isAncestorEq(const VarTree* child) const
121 const KTreeViewItem* c = child;
122 while (c != 0 && c != this) {
123 c = c->getParent();
125 return c != 0;
128 bool VarTree::updateValue(const QString& newValue)
130 // check whether the value changed
131 bool prevValueChanged = m_valueChanged;
132 m_valueChanged = false;
133 if (m_value != newValue) {
134 m_value = newValue;
135 m_valueChanged = true;
138 * We must repaint the cell if the value changed. If it did not change,
139 * we still must repaint the cell if the value changed previously,
140 * because the color of the display must be changed (from red to
141 * black).
143 return m_valueChanged || prevValueChanged;
146 void VarTree::inferTypesOfChildren(ProgramTypeTable& typeTable)
149 * Type inference works like this: We use type information of those
150 * children that have a type name in their name (base classes) or in
151 * their value (pointers)
154 // first recurse children
155 VarTree* child = static_cast<VarTree*>(getChild());
156 while (child != 0) {
157 child->inferTypesOfChildren(typeTable);
158 child = static_cast<VarTree*>(child->getSibling());
161 // if this is a pointer, get the type from the value (less the pointer)
162 if (m_varKind == VKpointer) {
163 #ifndef I_know_a_way_to_do_this_cleanly
164 return;
165 #else
166 const char* p = m_value.data();
167 const char* start = p;
168 // the type of the pointer shows up in the value (sometimes)
169 if (p == 0 || *p != '(')
170 return;
171 skipNested(p, '(', ')');
173 * We only recognize pointers to data "(int *)" but not pointers
174 * to functions "(void (*)())".
176 if (p-start < 3 && /* at least 3 chars necessary: (*) */
177 p[-2] != '*') /* skip back before the closing paren */
179 return;
181 const QString& typeName =
182 FROM_LATIN1(start+1, p-start-3) // minus 3 chars
183 .stripWhiteSpace();
184 m_type = typeTable.lookup(typeName);
185 if (m_type == 0) {
186 m_type = TypeInfo::unknownType();
188 #endif
189 } else if (m_varKind == VKstruct) {
190 // check if this is a base class part
191 if (m_nameKind == NKtype) {
192 const QString& typeName =
193 text.mid(1, text.length()-2); // strip < and >
194 m_type = typeTable.lookup(typeName);
196 /* if we don't have a type yet, get it from the base class */
197 if (m_type == 0) {
198 m_type = inferTypeFromBaseClass();
200 * If there is a known type now, it is the one from the
201 * first base class whose type we know.
206 * If we still don't have a type, the type is really unknown.
208 if (m_type == 0) {
209 m_type = TypeInfo::unknownType();
211 } // else
213 * This is not a base class part. We don't assign a type so
214 * that later we can ask gdb.
220 * Get the type of the first base class whose type we know.
222 TypeInfo* VarTree::inferTypeFromBaseClass()
224 if (m_varKind == VKstruct) {
225 VarTree* child = static_cast<VarTree*>(getChild());
226 while (child != 0 &&
227 // only check base class parts (i.e. type names)
228 child->m_nameKind == NKtype)
230 if (child->m_type != 0 &&
231 child->m_type != TypeInfo::unknownType())
233 // got a type!
234 return child->m_type;
236 child = static_cast<VarTree*>(child->getSibling());
239 return 0;
243 ExprWnd::ExprWnd(QWidget* parent, const char* name) :
244 KTreeView(parent, name),
245 maxValueWidth(0)
247 setNumCols(2);
249 connect(this, SIGNAL(expanded(int)), SLOT(slotExpandOrCollapse(int)));
250 connect(this, SIGNAL(collapsed(int)), SLOT(slotExpandOrCollapse(int)));
252 m_pixPointer = BarIcon("pointer.xpm");
253 if (m_pixPointer.isNull())
254 TRACE("Can't load pointer.xpm");
257 ExprWnd::~ExprWnd()
261 void ExprWnd::exprList(QStrList& exprs)
263 // ASSERT(exprs does deep-copies)
264 KTreeViewItem* item;
265 for (item = itemAt(0); item != 0; item = item->getSibling()) {
266 exprs.append(item->getText());
270 void ExprWnd::insertExpr(VarTree* expr)
272 // append the expression
273 insertItem(expr);
275 collectUnknownTypes(expr);
277 updateValuesWidth();
280 void ExprWnd::updateExpr(VarTree* expr)
282 // search the root variable
283 QString p = expr->getText();
284 KPath path;
285 path.push(&p);
286 KTreeViewItem* item = itemAt(path);
287 path.pop();
288 if (item == 0) {
289 return;
291 // now update it
292 if (updateExprRec(static_cast<VarTree*>(item), expr)) {
293 updateVisibleItems();
294 updateValuesWidth();
295 repaint();
297 collectUnknownTypes(static_cast<VarTree*>(item));
300 void ExprWnd::updateExpr(VarTree* display, VarTree* newValues)
302 if (updateExprRec(display, newValues) && display->isVisible()) {
303 updateVisibleItems();
304 updateValuesWidth();
305 repaint();
307 collectUnknownTypes(display);
311 * returns true if there's a visible change
313 bool ExprWnd::updateExprRec(VarTree* display, VarTree* newValues)
315 bool isExpanded = display->isExpanded();
318 * If we are updating a pointer without children by a dummy, we don't
319 * collapse it, but simply insert the new children. This happens when a
320 * pointer has just been expanded by the user.
322 if (display->m_varKind == VarTree::VKpointer &&
323 display->childCount() == 0 &&
324 newValues->m_varKind == VarTree::VKdummy)
326 replaceChildren(display, newValues);
327 return isExpanded; /* no visible change if not expanded */
331 * If the display and newValues have different kind or if their number
332 * of children is different, replace the whole sub-tree.
334 if (// the next two lines mean: not(m_varKind remains unchanged)
335 !(newValues->m_varKind == VarTree::VKdummy ||
336 display->m_varKind == newValues->m_varKind)
338 (display->childCount() != newValues->childCount() &&
340 * If this is a pointer and newValues doesn't have children, we
341 * don't replace the sub-tree; instead, below we mark this
342 * sub-tree for requiring an update.
344 (display->m_varKind != VarTree::VKpointer ||
345 newValues->childCount() != 0)))
347 if (isExpanded) {
348 collapseSubTree(display, false);
351 // since children changed, it is likely that the type has also changed
352 display->m_type = 0; /* will re-evaluate the type */
354 // display the new value
355 updateSingleExpr(display, newValues);
356 replaceChildren(display, newValues);
358 // update the m_varKind
359 if (newValues->m_varKind != VarTree::VKdummy) {
360 display->m_varKind = newValues->m_varKind;
361 display->setDelayedExpanding(newValues->m_varKind == VarTree::VKpointer);
364 // (note that the new value might not have a sub-tree at all)
365 return display->m_valueChanged || isExpanded; /* no visible change if not expanded */
368 // display the new value
369 updateSingleExpr(display, newValues);
372 * If this is an expanded pointer, record it for being updated.
374 if (display->m_varKind == VarTree::VKpointer) {
375 if (isExpanded &&
376 // if newValues is a dummy, we have already updated this pointer
377 newValues->m_varKind != VarTree::VKdummy)
379 m_updatePtrs.append(display);
382 * If the visible sub-tree has children, but newValues doesn't, we
383 * can stop here.
385 if (newValues->childCount() == 0) {
386 return display->m_valueChanged;
390 ASSERT(display->childCount() == newValues->childCount());
392 // go for children
393 bool childChanged = false;
395 VarTree* vDisplay = static_cast<VarTree*>(display->getChild());
396 VarTree* vNew = static_cast<VarTree*>(newValues->getChild());
397 while (vDisplay != 0) {
398 // check whether the names are the same
399 if (strcmp(vDisplay->getText(), vNew->getText()) != 0) {
400 // set new name
401 vDisplay->setText(vNew->getText());
402 int i = itemRow(vDisplay);
403 if (i >= 0) {
404 updateCell(i, 0, true);
405 childChanged = true;
408 // recurse
409 if (updateExprRec(vDisplay, vNew)) {
410 childChanged = true;
412 vDisplay = static_cast<VarTree*>(vDisplay->getSibling());
413 vNew = static_cast<VarTree*>(vNew->getSibling());
416 // update of children propagates only if this node is expanded
417 return display->m_valueChanged || (display->isExpanded() && childChanged);
420 void ExprWnd::updateSingleExpr(VarTree* display, VarTree* newValue)
423 * If newValues is a VKdummy, we are only interested in its children.
424 * No need to update anything here.
426 if (newValue->m_varKind == VarTree::VKdummy) {
427 return;
431 * If this node is a struct and we know its type then don't update its
432 * value now. This is a node for which we know how to find a nested
433 * value. So register the node for an update.
435 if (display->m_varKind == VarTree::VKstruct &&
436 display->m_type != 0 &&
437 display->m_type != TypeInfo::unknownType())
439 ASSERT(newValue->m_varKind == VarTree::VKstruct);
440 display->m_partialValue = display->m_type->m_displayString[0];
441 m_updateStruct.append(display);
443 else
445 if (display->updateValue(newValue->m_value)) {
446 int i = itemRow(display);
447 if (i >= 0) {
448 updateCell(i, 1, true);
454 void ExprWnd::updateStructValue(VarTree* display)
456 ASSERT(display->m_varKind == VarTree::VKstruct);
458 if (display->updateValue(display->m_partialValue)) {
459 int i = itemRow(display);
460 if (i >= 0) {
461 updateValuesWidth();
462 updateCell(i, 1, true);
465 // reset the value
466 display->m_partialValue = "";
467 display->m_exprIndex = -1;
470 void ExprWnd::replaceChildren(VarTree* display, VarTree* newValues)
472 ASSERT(display->childCount() == 0 || display->m_varKind != VarTree::VKsimple);
474 // delete all children of display
475 KTreeViewItem* c;
476 while ((c = display->getChild()) != 0) {
477 display->removeChild(c);
479 // insert copies of the newValues
480 for (c = newValues->getChild(); c != 0; c = c->getSibling()) {
481 VarTree* v = static_cast<VarTree*>(c);
482 VarTree* vNew = new VarTree(v->getText(), v->m_nameKind);
483 vNew->m_varKind = v->m_varKind;
484 vNew->m_value = v->m_value;
485 vNew->m_type = v->m_type;
486 vNew->setDelayedExpanding(vNew->m_varKind == VarTree::VKpointer);
487 vNew->setExpanded(v->isExpanded());
488 display->appendChild(vNew);
489 // recurse
490 replaceChildren(vNew, v);
494 void ExprWnd::collectUnknownTypes(VarTree* var)
497 * forEveryItem does not scan the root item itself. So we must do it
498 * ourselves.
500 ASSERT(var->m_varKind != VarTree::VKpointer || var->m_nameKind != VarTree::NKtype);
501 if (var->m_type == 0 &&
502 var->m_varKind == VarTree::VKstruct &&
503 var->m_nameKind != VarTree::NKtype)
505 /* this struct node doesn't have a type yet: register it */
506 m_updateType.append(var);
509 // add pointer pixmap to pointers
510 if (var->m_varKind == VarTree::VKpointer) {
511 var->setPixmap(m_pixPointer);
514 forEveryItem(collectUnknownTypes, this, var);
517 bool ExprWnd::collectUnknownTypes(KTreeViewItem* item, void* user)
519 VarTree* var = static_cast<VarTree*>(item);
520 ExprWnd* tree = static_cast<ExprWnd*>(user);
521 ASSERT(var->m_varKind != VarTree::VKpointer || var->m_nameKind != VarTree::NKtype);
522 if (var->m_type == 0 &&
523 var->m_varKind == VarTree::VKstruct &&
524 var->m_nameKind != VarTree::NKtype)
526 /* this struct node doesn't have a type yet: register it */
527 tree->m_updateType.append(var);
529 // add pointer pixmap to pointers
530 if (var->m_varKind == VarTree::VKpointer) {
531 var->setPixmap(tree->m_pixPointer);
533 return false;
537 VarTree* ExprWnd::topLevelExprByName(const char* name)
539 QString p = name;
540 KPath path;
541 path.push(&p);
542 KTreeViewItem* item = itemAt(path);
543 path.pop();
545 return static_cast<VarTree*>(item);
548 void ExprWnd::removeExpr(VarTree* item)
550 // must remove any pointers scheduled for update from the list
551 sweepList(m_updatePtrs, item);
552 sweepList(m_updateType, item);
553 sweepList(m_updateStruct, item);
555 takeItem(item);
556 delete item;
558 updateValuesWidth();
561 void ExprWnd::sweepList(QList<VarTree>& list, VarTree* subTree)
563 if (subTree == 0)
564 return;
566 VarTree* checkItem = list.first();
567 while (checkItem != 0) {
568 if (!subTree->isAncestorEq(checkItem)) {
569 // checkItem is not an item from subTree
570 // advance
571 checkItem = list.next();
572 } else {
573 // checkItem is an item from subTree
575 * If checkItem is the last item in the list, we need a special
576 * treatment, because remove()ing it steps the current item of
577 * the list in the "wrong" direction.
579 if (checkItem == list.getLast()) { // does not set current item
580 list.remove();
581 /* we deleted the last element, so we've finished */
582 checkItem = 0;
583 } else {
584 list.remove();
585 /* remove() advanced already */
586 checkItem = list.current();
592 QString ExprWnd::exprStringAt(int index)
594 KTreeViewItem* item = itemAt(index);
595 if (item == 0) return QString(); /* paranoia */
596 VarTree* expr = static_cast<VarTree*>(item);
597 return expr->computeExpr();
600 void ExprWnd::clearPendingUpdates()
602 m_updatePtrs.clear();
603 m_updateType.clear();
604 m_updateStruct.clear();
607 VarTree* ExprWnd::nextUpdatePtr()
609 VarTree* ptr = m_updatePtrs.first();
610 if (ptr != 0) {
611 m_updatePtrs.remove();
613 return ptr;
616 VarTree* ExprWnd::nextUpdateType()
618 VarTree* ptr = m_updateType.first();
619 if (ptr != 0) {
620 m_updateType.remove();
622 return ptr;
625 VarTree* ExprWnd::nextUpdateStruct()
627 VarTree* ptr = m_updateStruct.first();
628 if (ptr != 0) {
629 m_updateStruct.remove();
631 return ptr;
634 void ExprWnd::paintCell(QPainter* painter, int row, int col)
636 if (col == 0) {
637 KTreeView::paintCell(painter, row, col);
638 } else {
639 VarTree* item = static_cast<VarTree*>(itemAt(row));
640 if (item != 0) {
641 item->paintValue(painter);
646 int ExprWnd::cellWidth(int col) const
648 if (col == 0) {
649 return KTreeView::cellWidth(col);
650 } else {
651 return maxValueWidth;
655 void ExprWnd::updateValuesWidth()
657 int maxW = 0;
658 forEveryVisibleItem(static_cast<KForEveryFunc>(&getMaxValueWidth), &maxW);
659 maxValueWidth = maxW;
660 updateTableSize();
663 // called by updateValuesWidth() for each item in the visible list
664 bool ExprWnd::getMaxValueWidth(KTreeViewItem* item, void* user)
666 int *maxW = (int *)user;
667 VarTree* v = static_cast<VarTree*>(item);
668 int w = v->valueWidth();
669 if(w > *maxW)
670 *maxW = w;
671 return false;
674 void ExprWnd::slotExpandOrCollapse(int)
676 updateValuesWidth();