Convert source code window to Qt4's QPlainTextEdit.
[kdbg.git] / kdbg / sourcewnd.h
blob9f299407d6e46b843debdefb3ae705aa45e731bd
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #ifndef SOURCEWND_H
8 #define SOURCEWND_H
10 #include <QPixmap>
11 #include <QPlainTextEdit>
12 #include <QSyntaxHighlighter>
13 #include <vector>
14 #include "dbgdriver.h"
16 // forward declarations
17 class KDebugger;
18 struct DbgAddr;
19 class LineInfoArea;
20 class HighlightCpp;
22 class SourceWindow : public QPlainTextEdit
24 Q_OBJECT
25 public:
26 SourceWindow(const QString& fileName, QWidget* parent);
27 ~SourceWindow();
29 bool loadFile();
30 void reloadFile();
31 bool fileNameMatches(const QString& other);
32 void scrollTo(int lineNo, const DbgAddr& address);
33 const QString& fileName() const { return m_fileName; }
34 void updateLineItems(const KDebugger* dbg);
35 void setPC(bool set, int lineNo, const DbgAddr& address, int frameNo);
36 enum FindDirection { findForward = 1, findBackward = -1 };
37 void find(const QString& text, bool caseSensitive, FindDirection dir);
38 bool wordAtPoint(const QPoint& p, QString& word, QRect& r);
39 /**
40 * Translates row number (zero-based) to zero-based source line number.
41 * If sourceRow is non-zero, it is filled with the source code row
42 * belonging to the line number.
44 int rowToLine(int row, int* sourceRow = 0);
45 /** Translates zero-based source line number to row number (zero-based) */
46 int lineToRow(int line);
47 /** Is the row disassembled? */
48 bool isRowExpanded(int row);
49 /** Does the row show disassembled code? */
50 bool isRowDisassCode(int row);
52 /** lineNo is zero-based */
53 void disassembled(int lineNo, const std::list<DisassembledCode>& disass);
55 void activeLine(int& lineNo, DbgAddr& address);
57 protected:
58 void drawLineInfoArea(QPainter* p, QPaintEvent* event);
59 void infoMousePress(QMouseEvent* ev);
60 virtual void resizeEvent(QResizeEvent* e);
61 virtual void contextMenuEvent(QContextMenuEvent* e);
62 virtual void keyPressEvent(QKeyEvent* ev);
63 virtual void paletteChange(const QPalette&);
64 void expandRow(int row);
65 void collapseRow(int row);
66 void scrollToRow(int row);
67 /** translates (0-based) line number plus a code address into a row number */
68 int lineToRow(int row, const DbgAddr& address);
69 int lineInfoAreaWidth() const;
71 void actionExpandRow(int row);
72 void actionCollapseRow(int row);
74 signals:
75 void clickedLeft(const QString&, int, const DbgAddr& address, bool);
76 void clickedMid(const QString&, int, const DbgAddr& address);
77 void disassemble(const QString&, int);
78 void expanded(int lineNo); /* source lineNo has been expanded */
79 void collapsed(int lineNo); /* source lineNo has been collapsed */
80 public slots:
81 void setTabWidth(int numChars);
82 void cursorChanged();
84 protected:
85 QString m_fileName;
86 enum LineItem { liPC = 1, liPCup = 2,
87 liBP = 4, liBPdisabled = 8, liBPtemporary = 16,
88 liBPconditional = 32, liBPorphan = 64,
89 liBPany = liBP|liBPdisabled|liBPtemporary|liBPconditional|liBPorphan
92 struct SourceLine {
93 std::vector<QString> disass; /* its disassembled code */
94 std::vector<DbgAddr> disassAddr; /* the addresses thereof */
95 bool canDisass; /* if line can be disassembled */
96 SourceLine() : canDisass(true) { }
97 int findAddressRowOffset(const DbgAddr& address) const;
99 std::vector<SourceLine> m_sourceCode;
100 HighlightCpp* m_highlighter;
102 std::vector<int> m_rowToLine; //!< The source line number for each row
103 std::vector<uchar> m_lineItems; //!< Icons displayed on the line
104 QPixmap m_pcinner; /* PC at innermost frame */
105 QPixmap m_pcup; /* PC at frame up the stack */
106 QPixmap m_brkena; /* enabled breakpoint */
107 QPixmap m_brkdis; /* disabled breakpoint */
108 QPixmap m_brktmp; /* temporary breakpoint marker */
109 QPixmap m_brkcond; /* conditional breakpoint marker */
110 QPixmap m_brkorph; /* orphaned breakpoint marker */
111 QFont m_lineNoFont; //!< The font used to draw line numbers
112 int m_widthItems; //!< The width of the item column
113 int m_widthPlus; //!< The width of the expander column
114 int m_widthLineNo; //!< The width of the line number columns
115 LineInfoArea* m_lineInfoArea;
117 friend class LineInfoArea;
120 class LineInfoArea : public QWidget
122 public:
123 LineInfoArea(QWidget* parent) : QWidget(parent) { }
124 virtual void paintEvent(QPaintEvent* e);
125 virtual void mousePressEvent(QMouseEvent* ev);
126 virtual void contextMenuEvent(QContextMenuEvent* e);
129 class HighlightCpp : public QSyntaxHighlighter
131 SourceWindow* m_srcWnd;
133 public:
134 HighlightCpp(SourceWindow* srcWnd);
135 virtual void highlightBlock(const QString& text);
136 int highlight(const QString& text, int state);
139 #endif // SOURCEWND_H