Restructure wchar_t support.
[kdbg.git] / kdbg / exprwnd.cpp
blob83e3e11d47a1c972d5705bf8088a9bb82c86ff23
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 <qscrollbar.h>
12 #include <kapp.h>
13 #include <kiconloader.h> /* icons */
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 #include "mydebug.h"
19 VarTree::VarTree(const QString& name, NameKind aKind) :
20 KTreeViewItem(name),
21 m_varKind(VKsimple),
22 m_nameKind(aKind),
23 m_valueChanged(false),
24 m_type(0),
25 m_exprIndex(0),
26 m_exprIndexUseGuard(false)
30 VarTree::~VarTree()
34 void VarTree::paintValue(QPainter* p)
36 p->save();
37 int cellHeight = height(p->fontMetrics());
38 int textX = 2;
39 int textY = (cellHeight - p->fontMetrics().height()) / 2 +
40 p->fontMetrics().ascent();
42 if (m_valueChanged) {
43 p->setPen(red);
45 // p->setBackgroundColor(cg.base());
46 p->drawText(textX, textY, m_value, m_value.length());
47 p->restore();
50 int VarTree::valueWidth()
52 assert(owner != 0);
53 return owner->fontMetrics().width(m_value) + 4;
56 QString VarTree::computeExpr() const
58 assert(getParent() != 0);
59 // just to be sure
60 if (getParent() == 0)
61 return QString();
63 // top-level items are special
64 if (getParent()->getParent() == 0)
65 return getText();
67 // get parent expr
68 VarTree* par = static_cast<VarTree*>(getParent());
69 QString parentExpr = par->computeExpr();
71 /* don't add this item's name if this is a base class sub-item */
72 if (m_nameKind == NKtype) {
73 return parentExpr;
75 /* augment by this item's text */
76 QString result;
77 /* if this is an address, dereference it */
78 if (m_nameKind == NKaddress) {
79 ASSERT(par->m_varKind == VKpointer);
80 result = "*" + parentExpr;
81 return result;
83 switch (par->m_varKind) {
84 case VKarray:
86 QString index = getText();
87 int i = 1;
88 // skip past the index
89 while (index[i].isDigit())
90 i++;
92 * Some array indices are actually ranges due to repeated array
93 * values. We use the first index in these cases.
95 if (index[i] != ']') {
96 // remove second index
97 index.remove(i, index.length()-i-1);
99 result = "(" + parentExpr + ")" + index;
101 break;
102 case VKstruct:
103 result = "(" + parentExpr + ")." + getText();
104 break;
105 case VKsimple: /* parent can't be simple */
106 case VKpointer: /* handled in NKaddress */
107 case VKdummy: /* can't occur at all */
108 ASSERT(false);
109 result = parentExpr; /* paranoia */
110 break;
112 return result;
115 bool VarTree::isToplevelExpr() const
117 return getParent() != 0 && getParent()->getParent() == 0;
120 bool VarTree::isAncestorEq(const VarTree* child) const
122 const KTreeViewItem* c = child;
123 while (c != 0 && c != this) {
124 c = c->getParent();
126 return c != 0;
129 bool VarTree::updateValue(const QString& newValue)
131 // check whether the value changed
132 bool prevValueChanged = m_valueChanged;
133 m_valueChanged = false;
134 if (m_value != newValue) {
135 m_value = newValue;
136 m_valueChanged = true;
139 * We must repaint the cell if the value changed. If it did not change,
140 * we still must repaint the cell if the value changed previously,
141 * because the color of the display must be changed (from red to
142 * black).
144 return m_valueChanged || prevValueChanged;
147 void VarTree::inferTypesOfChildren(ProgramTypeTable& typeTable)
150 * Type inference works like this: We use type information of those
151 * children that have a type name in their name (base classes) or in
152 * their value (pointers)
155 // first recurse children
156 VarTree* child = static_cast<VarTree*>(getChild());
157 while (child != 0) {
158 child->inferTypesOfChildren(typeTable);
159 child = static_cast<VarTree*>(child->getSibling());
162 // if this is a pointer, get the type from the value (less the pointer)
163 if (m_varKind == VKpointer) {
164 if (isWcharT())
167 * wchart_t pointers must be treated as struct, because the array
168 * of characters is printed similar to how QStrings are decoded.
170 m_varKind = VKstruct;
171 setDelayedExpanding(false);
172 return;
174 #ifndef I_know_a_way_to_do_this_cleanly
175 return;
176 #else
177 const char* p = m_value.data();
178 const char* start = p;
179 // the type of the pointer shows up in the value (sometimes)
180 if (p == 0 || *p != '(')
181 return;
182 skipNested(p, '(', ')');
184 * We only recognize pointers to data "(int *)" but not pointers
185 * to functions "(void (*)())".
187 if (p-start < 3 && /* at least 3 chars necessary: (*) */
188 p[-2] != '*') /* skip back before the closing paren */
190 return;
192 const QString& typeName =
193 FROM_LATIN1(start+1, p-start-3) // minus 3 chars
194 .stripWhiteSpace();
195 m_type = typeTable.lookup(typeName);
196 if (m_type == 0) {
197 m_type = TypeInfo::unknownType();
199 #endif
200 } else if (m_varKind == VKstruct) {
201 // check if this is a base class part
202 if (m_nameKind == NKtype) {
203 const QString& typeName =
204 text.mid(1, text.length()-2); // strip < and >
205 m_type = typeTable.lookup(typeName);
207 /* if we don't have a type yet, get it from the base class */
208 if (m_type == 0) {
209 m_type = inferTypeFromBaseClass();
211 * If there is a known type now, it is the one from the
212 * first base class whose type we know.
217 * If we still don't have a type, the type is really unknown.
219 if (m_type == 0) {
220 m_type = TypeInfo::unknownType();
222 } // else
224 * This is not a base class part. We don't assign a type so
225 * that later we can ask gdb.
230 // the value contains the pointer type in parenthesis
231 bool VarTree::isWcharT() const
233 return m_value.startsWith("(const wchar_t *)") ||
234 m_value.startsWith("(wchar_t *)");
238 * Get the type of the first base class whose type we know.
240 TypeInfo* VarTree::inferTypeFromBaseClass()
242 if (m_varKind == VKstruct) {
243 VarTree* child = static_cast<VarTree*>(getChild());
244 while (child != 0 &&
245 // only check base class parts (i.e. type names)
246 child->m_nameKind == NKtype)
248 if (child->m_type != 0 &&
249 child->m_type != TypeInfo::unknownType())
251 // got a type!
252 return child->m_type;
254 child = static_cast<VarTree*>(child->getSibling());
257 return 0;
261 ExprWnd::ExprWnd(QWidget* parent, const char* name) :
262 KTreeView(parent, name),
263 maxValueWidth(0),
264 m_edit(this)
266 setNumCols(2);
268 connect(this, SIGNAL(expanded(int)), SLOT(slotExpandOrCollapse(int)));
269 connect(this, SIGNAL(collapsed(int)), SLOT(slotExpandOrCollapse(int)));
271 m_pixPointer = UserIcon("pointer.xpm");
272 if (m_pixPointer.isNull())
273 TRACE("Can't load pointer.xpm");
276 ExprWnd::~ExprWnd()
280 void ExprWnd::exprList(QStrList& exprs)
282 // ASSERT(exprs does deep-copies)
283 KTreeViewItem* item;
284 for (item = itemAt(0); item != 0; item = item->getSibling()) {
285 exprs.append(item->getText());
289 void ExprWnd::insertExpr(VarTree* expr)
291 // append the expression
292 insertItem(expr);
294 collectUnknownTypes(expr);
296 updateValuesWidth();
299 void ExprWnd::updateExpr(VarTree* expr)
301 // search the root variable
302 QString p = expr->getText();
303 KPath path;
304 path.push(&p);
305 KTreeViewItem* item = itemAt(path);
306 path.pop();
307 if (item == 0) {
308 return;
310 // now update it
311 if (updateExprRec(static_cast<VarTree*>(item), expr)) {
312 updateVisibleItems();
313 updateValuesWidth();
314 repaint();
316 collectUnknownTypes(static_cast<VarTree*>(item));
319 void ExprWnd::updateExpr(VarTree* display, VarTree* newValues)
321 if (updateExprRec(display, newValues) && display->isVisible()) {
322 updateVisibleItems();
323 updateValuesWidth();
324 repaint();
326 collectUnknownTypes(display);
330 * returns true if there's a visible change
332 bool ExprWnd::updateExprRec(VarTree* display, VarTree* newValues)
334 bool isExpanded = display->isExpanded();
337 * If we are updating a pointer without children by a dummy, we don't
338 * collapse it, but simply insert the new children. This happens when a
339 * pointer has just been expanded by the user.
341 if (display->m_varKind == VarTree::VKpointer &&
342 display->childCount() == 0 &&
343 newValues->m_varKind == VarTree::VKdummy)
345 replaceChildren(display, newValues);
346 return isExpanded; /* no visible change if not expanded */
350 * If the display and newValues have different kind or if their number
351 * of children is different, replace the whole sub-tree.
353 if (// the next two lines mean: not(m_varKind remains unchanged)
354 !(newValues->m_varKind == VarTree::VKdummy ||
355 display->m_varKind == newValues->m_varKind)
357 (display->childCount() != newValues->childCount() &&
359 * If this is a pointer and newValues doesn't have children, we
360 * don't replace the sub-tree; instead, below we mark this
361 * sub-tree for requiring an update.
363 (display->m_varKind != VarTree::VKpointer ||
364 newValues->childCount() != 0)))
366 if (isExpanded) {
367 collapseSubTree(display, false);
370 // since children changed, it is likely that the type has also changed
371 display->m_type = 0; /* will re-evaluate the type */
373 // display the new value
374 updateSingleExpr(display, newValues);
375 replaceChildren(display, newValues);
377 // update the m_varKind
378 if (newValues->m_varKind != VarTree::VKdummy) {
379 display->m_varKind = newValues->m_varKind;
380 display->setDelayedExpanding(newValues->m_varKind == VarTree::VKpointer);
383 // (note that the new value might not have a sub-tree at all)
384 return display->m_valueChanged || isExpanded; /* no visible change if not expanded */
387 // display the new value
388 updateSingleExpr(display, newValues);
391 * If this is an expanded pointer, record it for being updated.
393 if (display->m_varKind == VarTree::VKpointer) {
394 if (isExpanded &&
395 // if newValues is a dummy, we have already updated this pointer
396 newValues->m_varKind != VarTree::VKdummy)
398 m_updatePtrs.append(display);
401 * If the visible sub-tree has children, but newValues doesn't, we
402 * can stop here.
404 if (newValues->childCount() == 0) {
405 return display->m_valueChanged;
409 ASSERT(display->childCount() == newValues->childCount());
411 // go for children
412 bool childChanged = false;
414 VarTree* vDisplay = static_cast<VarTree*>(display->getChild());
415 VarTree* vNew = static_cast<VarTree*>(newValues->getChild());
416 while (vDisplay != 0) {
417 // check whether the names are the same
418 if (strcmp(vDisplay->getText(), vNew->getText()) != 0) {
419 // set new name
420 vDisplay->setText(vNew->getText());
421 int i = itemRow(vDisplay);
422 if (i >= 0) {
423 updateCell(i, 0, true);
424 childChanged = true;
427 // recurse
428 if (updateExprRec(vDisplay, vNew)) {
429 childChanged = true;
431 vDisplay = static_cast<VarTree*>(vDisplay->getSibling());
432 vNew = static_cast<VarTree*>(vNew->getSibling());
435 // update of children propagates only if this node is expanded
436 return display->m_valueChanged || (display->isExpanded() && childChanged);
439 void ExprWnd::updateSingleExpr(VarTree* display, VarTree* newValue)
442 * If newValues is a VKdummy, we are only interested in its children.
443 * No need to update anything here.
445 if (newValue->m_varKind == VarTree::VKdummy) {
446 return;
450 * If this node is a struct and we know its type then don't update its
451 * value now. This is a node for which we know how to find a nested
452 * value. So register the node for an update.
454 * wchar_t types are also treated specially here: We consider them
455 * as struct (has been set in inferTypesOfChildren()).
457 if (display->m_varKind == VarTree::VKstruct &&
458 display->m_type != 0 &&
459 display->m_type != TypeInfo::unknownType())
461 ASSERT(newValue->m_varKind == VarTree::VKstruct);
462 if (display->m_type == TypeInfo::wchartType())
465 * We do not copy the new pointer value to the destination right
466 * away, but consider it as the first part of the nested value.
467 * Then the display will change its color only when the new value
468 * is completed.
470 display->m_partialValue = newValue->m_value + " L";
472 else
473 display->m_partialValue = display->m_type->m_displayString[0];
474 m_updateStruct.append(display);
476 else
478 if (display->updateValue(newValue->m_value)) {
479 int i = itemRow(display);
480 if (i >= 0) {
481 updateCell(i, 1, true);
487 void ExprWnd::updateStructValue(VarTree* display)
489 ASSERT(display->m_varKind == VarTree::VKstruct);
491 if (display->updateValue(display->m_partialValue)) {
492 int i = itemRow(display);
493 if (i >= 0) {
494 updateValuesWidth();
495 updateCell(i, 1, true);
498 // reset the value
499 display->m_partialValue = "";
500 display->m_exprIndex = -1;
503 void ExprWnd::replaceChildren(VarTree* display, VarTree* newValues)
505 ASSERT(display->childCount() == 0 || display->m_varKind != VarTree::VKsimple);
507 // delete all children of display
508 KTreeViewItem* c;
509 while ((c = display->getChild()) != 0) {
510 display->removeChild(c);
512 // insert copies of the newValues
513 for (c = newValues->getChild(); c != 0; c = c->getSibling()) {
514 VarTree* v = static_cast<VarTree*>(c);
515 VarTree* vNew = new VarTree(v->getText(), v->m_nameKind);
516 vNew->m_varKind = v->m_varKind;
517 vNew->m_value = v->m_value;
518 vNew->m_type = v->m_type;
519 vNew->setDelayedExpanding(vNew->m_varKind == VarTree::VKpointer);
520 vNew->setExpanded(v->isExpanded());
521 display->appendChild(vNew);
522 // recurse
523 replaceChildren(vNew, v);
527 void ExprWnd::collectUnknownTypes(VarTree* var)
530 * forEveryItem does not scan the root item itself. So we must do it
531 * ourselves.
533 ASSERT(var->m_varKind != VarTree::VKpointer || var->m_nameKind != VarTree::NKtype);
534 if (var->m_type == 0 &&
535 var->m_varKind == VarTree::VKstruct &&
536 var->m_nameKind != VarTree::NKtype)
538 if (!var->isWcharT())
540 /* this struct node doesn't have a type yet: register it */
541 m_updateType.append(var);
543 else
545 var->m_type = TypeInfo::wchartType();
546 // see updateSingleExpr() why we move the value
547 var->m_partialValue = var->m_value + " L";
548 var->m_value.truncate(0);
549 m_updateStruct.append(var);
553 // add pointer pixmap to pointers
554 if (var->m_varKind == VarTree::VKpointer) {
555 var->setPixmap(m_pixPointer);
558 forEveryItem(collectUnknownTypes, this, var);
561 bool ExprWnd::collectUnknownTypes(KTreeViewItem* item, void* user)
563 VarTree* var = static_cast<VarTree*>(item);
564 ExprWnd* tree = static_cast<ExprWnd*>(user);
565 ASSERT(var->m_varKind != VarTree::VKpointer || var->m_nameKind != VarTree::NKtype);
566 if (var->m_type == 0 &&
567 var->m_varKind == VarTree::VKstruct &&
568 var->m_nameKind != VarTree::NKtype)
570 if (!var->isWcharT())
572 /* this struct node doesn't have a type yet: register it */
573 tree->m_updateType.append(var);
575 else
577 var->m_type = TypeInfo::wchartType();
578 // see updateSingleExpr() why we move the value
579 var->m_partialValue = var->m_value + " L";
580 var->m_value.truncate(0);
581 tree->m_updateStruct.append(var);
584 // add pointer pixmap to pointers
585 if (var->m_varKind == VarTree::VKpointer) {
586 var->setPixmap(tree->m_pixPointer);
588 return false;
592 VarTree* ExprWnd::topLevelExprByName(const char* name)
594 QString p = name;
595 KPath path;
596 path.push(&p);
597 KTreeViewItem* item = itemAt(path);
598 path.pop();
600 return static_cast<VarTree*>(item);
603 void ExprWnd::removeExpr(VarTree* item)
605 // must remove any pointers scheduled for update from the list
606 sweepList(m_updatePtrs, item);
607 sweepList(m_updateType, item);
608 sweepList(m_updateStruct, item);
610 takeItem(item);
611 delete item;
613 updateValuesWidth();
616 void ExprWnd::sweepList(QList<VarTree>& list, VarTree* subTree)
618 if (subTree == 0)
619 return;
621 VarTree* checkItem = list.first();
622 while (checkItem != 0) {
623 if (!subTree->isAncestorEq(checkItem)) {
624 // checkItem is not an item from subTree
625 // advance
626 checkItem = list.next();
627 } else {
628 // checkItem is an item from subTree
630 * If checkItem is the last item in the list, we need a special
631 * treatment, because remove()ing it steps the current item of
632 * the list in the "wrong" direction.
634 if (checkItem == list.getLast()) { // does not set current item
635 list.remove();
636 /* we deleted the last element, so we've finished */
637 checkItem = 0;
638 } else {
639 list.remove();
640 /* remove() advanced already */
641 checkItem = list.current();
647 QString ExprWnd::exprStringAt(int index)
649 KTreeViewItem* item = itemAt(index);
650 if (item == 0) return QString(); /* paranoia */
651 VarTree* expr = static_cast<VarTree*>(item);
652 return expr->computeExpr();
655 void ExprWnd::clearPendingUpdates()
657 m_updatePtrs.clear();
658 m_updateType.clear();
659 m_updateStruct.clear();
662 VarTree* ExprWnd::nextUpdatePtr()
664 VarTree* ptr = m_updatePtrs.first();
665 if (ptr != 0) {
666 m_updatePtrs.remove();
668 return ptr;
671 VarTree* ExprWnd::nextUpdateType()
673 VarTree* ptr = m_updateType.first();
674 if (ptr != 0) {
675 m_updateType.remove();
677 return ptr;
680 VarTree* ExprWnd::nextUpdateStruct()
682 VarTree* ptr = m_updateStruct.first();
683 if (ptr != 0) {
684 m_updateStruct.remove();
686 return ptr;
689 void ExprWnd::paintCell(QPainter* painter, int row, int col)
691 if (col == 0) {
692 KTreeView::paintCell(painter, row, col);
693 } else {
694 VarTree* item = static_cast<VarTree*>(itemAt(row));
695 if (item != 0) {
696 item->paintValue(painter);
701 int ExprWnd::cellWidth(int col) const
703 if (col == 0) {
704 return KTreeView::cellWidth(col);
705 } else {
706 return maxValueWidth;
710 void ExprWnd::updateValuesWidth()
712 int maxW = 0;
713 forEveryVisibleItem(static_cast<KForEveryFunc>(&getMaxValueWidth), &maxW);
714 maxValueWidth = maxW;
715 updateTableSize();
718 // called by updateValuesWidth() for each item in the visible list
719 bool ExprWnd::getMaxValueWidth(KTreeViewItem* item, void* user)
721 int *maxW = (int *)user;
722 VarTree* v = static_cast<VarTree*>(item);
723 int w = v->valueWidth();
724 if(w > *maxW)
725 *maxW = w;
726 return false;
729 void ExprWnd::slotExpandOrCollapse(int)
731 updateValuesWidth();
734 void ExprWnd::editValue(int row, const QString& text)
736 int x;
737 colXPos(1, &x);
738 int y;
739 rowYPos(row, &y);
740 int w = cellWidth(1);
741 int h = cellHeight(row);
742 QScrollBar* sbV = static_cast<QScrollBar*>(child("table_sbV"));
745 * Make the edit widget at least 5 characters wide (but not wider than
746 * this widget). If less than half of this widget is used to display
747 * the text, scroll this widget so that half of it shows the text (or
748 * less than half of it if the text is shorter).
750 QFontMetrics metr = m_edit.font();
751 int wMin = metr.width("88888");
752 if (w < wMin)
753 w = wMin;
754 int wThis = width();
755 if (sbV->isVisible()) // subtract width of scrollbar
756 wThis -= sbV->width();
757 if (x >= wThis/2 && // less than half the width displays text
758 x+w > wThis) // not all text is visible
760 // scroll so that more text is visible
761 int wScroll = QMIN(x-wThis/2, x+w-wThis);
762 sbHor(xOffset()+wScroll);
763 colXPos(1, &x);
765 else if (x < 0)
767 // don't let the edit move out at the left
768 x = 0;
771 // make the edit box as wide as the visible column
772 QRect rect(x,y, wThis-x,h);
773 m_edit.setText(text);
774 m_edit.selectAll();
776 m_edit.setGeometry(rect);
777 m_edit.m_finished = false;
778 m_edit.m_row = row;
779 m_edit.show();
780 m_edit.setFocus();
783 bool ExprWnd::isEditing() const
785 return m_edit.isVisible();
789 ValueEdit::ValueEdit(ExprWnd* parent) :
790 QLineEdit(parent, "valueedit")
792 setFrame(false);
793 hide();
794 lower(); // lower the window below scrollbars
795 connect(parent, SIGNAL(selected(int)), SLOT(slotSelectionChanged()));
796 connect(parent, SIGNAL(collapsed(int)), SLOT(slotSelectionChanged()));
797 connect(parent, SIGNAL(expanded(int)), SLOT(slotSelectionChanged()));
798 connect(this, SIGNAL(done(int, const QString&)),
799 parent, SIGNAL(editValueCommitted(int, const QString&)));
802 ValueEdit::~ValueEdit()
806 void ValueEdit::terminate(bool commit)
808 TRACE(commit?"ValueEdit::terminate(true)":"ValueEdit::terminate(false)");
809 if (!m_finished)
811 m_finished = true;
812 hide(); // will call focusOutEvent, that's why we need m_finished
813 if (commit) {
814 emit done(m_row, text());
819 void ValueEdit::keyPressEvent(QKeyEvent *e)
821 if(e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
822 terminate(true);
823 else if(e->key() == Qt::Key_Escape)
824 terminate(false);
825 else
826 QLineEdit::keyPressEvent(e);
829 void ValueEdit::paintEvent(QPaintEvent* e)
831 QLineEdit::paintEvent(e);
833 QPainter p(this);
834 p.drawRect(rect());
837 void ValueEdit::focusOutEvent(QFocusEvent* ev)
839 TRACE("ValueEdit::focusOutEvent");
840 QFocusEvent* focusEv = static_cast<QFocusEvent*>(ev);
841 // Don't let a RMB close the editor
842 if (focusEv->reason() != QFocusEvent::Popup &&
843 focusEv->reason() != QFocusEvent::ActiveWindow)
845 terminate(true);
849 void ValueEdit::slotSelectionChanged()
851 TRACE("ValueEdit::slotSelectionChanged");
852 terminate(false);