fix logic
[personal-kdelibs.git] / khtml / ecma / kjs_debugwin.h
blobbd70cda7e1c4bda49474a7675b2d208aa435caad
1 /*
2 * This file is part of the KDE libraries
3 * Copyright (C) 2000-2001 Harri Porten (porten@kde.org)
4 * Copyright (C) 2001,2003 Peter Kelly (pmk@post.com)
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef _KJS_DEBUGGER_H_
22 #define _KJS_DEBUGGER_H_
24 #include <QtCore/QBool>
26 #define KJS_DEBUGGER
28 #ifdef KJS_DEBUGGER
30 #include <qwidget.h>
31 #include <q3multilineedit.h>
32 #include <qpixmap.h>
33 #include <q3ptrlist.h>
34 #include <QtCore/QStack>
35 #include <qcheckbox.h>
36 #include <kdialog.h>
37 #include <kcomponentdata.h>
38 #include <kmainwindow.h>
39 #include <q3scrollview.h>
41 #include <kjs/debugger.h>
42 #include <kjs/completion.h>
43 #include <kjs/interpreter.h>
44 #include <kjs/value.h>
45 #include "kjs_binding.h"
47 #include "dom/dom_misc.h"
49 class QListWidget;
50 class QComboBox;
51 class KAction;
53 namespace KJS {
54 class List;
55 class KJSDebugWin;
57 class SourceFile : public DOM::DomShared
59 public:
60 SourceFile(QString u, QString c, Interpreter *interp)
61 : url(u), code(c), interpreter(interp) {}
62 QString getCode();
63 QString url;
64 QString code;
65 Interpreter *interpreter;
68 /**
69 * @internal
71 * When kjs parses some code, it generates a source code fragment (or just "source").
72 * This is referenced by its source id in future calls to functions such as atLine()
73 * and callEvent(). We keep a record of all source fragments parsed in order to display
74 * then to the user.
76 * For .js files, the source fragment will be the entire file. For js code included
77 * in html files, however, there may be multiple source fragments within the one file
78 * (e.g. multiple SCRIPT tags or onclick="..." attributes)
80 * In the case where a single file has multiple source fragments, the source objects
81 * for these fragments will all point to the same SourceFile for their code.
83 class SourceFragment
85 public:
86 SourceFragment(int sid, int bl, int el, SourceFile *sf);
87 ~SourceFragment();
89 int sourceId;
90 int baseLine;
91 int errorLine;
92 SourceFile *sourceFile;
93 private:
94 SourceFragment(const SourceFragment& other);
95 SourceFragment& operator = (const SourceFragment& other);
98 class KJSErrorDialog : public KDialog {
99 Q_OBJECT
100 public:
101 KJSErrorDialog(QWidget *parent, const QString& errorMessage, bool showDebug);
102 virtual ~KJSErrorDialog();
104 bool debugSelected() const { return m_debugSelected; }
105 bool dontShowAgain() const { return m_dontShowAgainCb->isChecked(); }
107 protected Q_SLOTS:
108 virtual void slotUser1();
110 private:
111 QCheckBox *m_dontShowAgainCb;
112 bool m_debugSelected;
115 class EvalMultiLineEdit : public Q3MultiLineEdit {
116 Q_OBJECT
117 public:
118 EvalMultiLineEdit(QWidget *parent);
119 const QString & code() const { return m_code; }
120 protected:
121 void keyPressEvent(QKeyEvent * e);
122 private:
123 QString m_code;
126 class SourceDisplay : public Q3ScrollView {
127 Q_OBJECT
128 public:
129 SourceDisplay(KJSDebugWin *debugWin, QWidget *parent, const char *name = 0);
130 ~SourceDisplay();
132 void setSource(SourceFile *sourceFile);
133 void setCurrentLine(int lineno, bool doCenter = true);
135 Q_SIGNALS:
136 void lineDoubleClicked(int lineno);
138 protected:
139 virtual void contentsMousePressEvent(QMouseEvent *e);
140 virtual void showEvent(QShowEvent *);
141 virtual void drawContents(QPainter *p, int clipx, int clipy, int clipw, int cliph);
143 QString m_source;
144 int m_currentLine;
145 SourceFile *m_sourceFile;
146 QStringList m_lines;
148 KJSDebugWin *m_debugWin;
149 QFont m_font;
150 QPixmap m_breakpointIcon;
154 * @internal
156 * KJSDebugWin represents the debugger window that is visible to the user. It contains
157 * a stack frame list, a code viewer and a source fragment selector, plus buttons
158 * to control execution including next, step and continue.
160 * There is only one debug window per program. This can be obtained by calling #instance
162 class KJSDebugWin : public KMainWindow, public Debugger, public KComponentData
164 Q_OBJECT
165 friend class SourceDisplay;
166 public:
167 KJSDebugWin(QWidget *parent=0, const char *name=0);
168 virtual ~KJSDebugWin();
170 static KJSDebugWin *createInstance();
171 static void destroyInstance();
172 static KJSDebugWin *debugWindow() { return kjs_html_debugger; }
174 enum Mode { Disabled = 0, // No break on any statements
175 Next = 1, // Will break on next statement in current context
176 Step = 2, // Will break on next statement in current or deeper context
177 Continue = 3, // Will continue until next breakpoint
178 Stop = 4 // The script will stop execution completely,
179 // as soon as possible
182 void setSourceLine(int sourceId, int lineno);
183 void setNextSourceInfo(QString url, int baseLine);
184 void sourceChanged(Interpreter *interpreter, QString url);
185 bool inSession() const { return !m_execStates.isEmpty(); }
186 void setMode(Mode m) { m_mode = m; }
187 void clearInterpreter(Interpreter *interpreter);
188 ExecState *getExecState() const { return m_execStates.top(); }
190 // functions overridden from KJS:Debugger
191 bool sourceParsed(ExecState *exec, int sourceId,
192 const UString &source, int errorLine);
193 bool sourceUnused(ExecState * exec, int sourceId);
194 bool exception(ExecState *exec, JSValue *value, bool inTryCatch);
195 bool atStatement(ExecState *exec);
196 bool enterContext(ExecState *exec);
197 bool exitContext(ExecState *exec, const Completion &completion);
199 public Q_SLOTS:
200 void slotNext();
201 void slotStep();
202 void slotContinue();
203 void slotStop();
204 void slotBreakNext();
205 void slotToggleBreakpoint(int lineno);
206 void slotShowFrame(int frameno);
207 void slotSourceSelected(int sourceSelIndex);
208 void slotEval();
210 protected:
212 void closeEvent(QCloseEvent *e);
213 bool eventFilter(QObject *obj, QEvent *evt);
214 void disableOtherWindows();
215 void enableOtherWindows();
217 private:
219 SourceFile *getSourceFile(Interpreter *interpreter, QString url);
220 void setSourceFile(Interpreter *interpreter, QString url, SourceFile *sourceFile);
221 void removeSourceFile(Interpreter *interpreter, QString url);
223 void checkBreak(ExecState *exec);
224 void enterSession(ExecState *exec);
225 void leaveSession();
226 void displaySourceFile(SourceFile *sourceFile, bool forceRefresh);
227 void updateContextList();
229 QString contextStr(const Context &ctx);
231 struct Breakpoint {
232 int sourceId;
233 int lineno;
235 Breakpoint *m_breakpoints;
236 int m_breakpointCount;
237 bool setBreakpoint(int sourceId, int lineno);
238 bool deleteBreakpoint(int sourceId, int lineno);
239 bool haveBreakpoint(SourceFile *sourceFile, int line0, int line1);
240 bool haveBreakpoint(int sourceId, int line0, int line1) const {
241 for (int i = 0; i < m_breakpointCount; i++) {
242 if (m_breakpoints[i].sourceId == sourceId &&
243 m_breakpoints[i].lineno >= line0 &&
244 m_breakpoints[i].lineno <= line1)
245 return true;
247 return false;
250 SourceFile *m_curSourceFile;
251 Mode m_mode;
252 QString m_nextSourceUrl;
253 int m_nextSourceBaseLine;
254 QStack<ExecState*> m_execStates;
255 ExecState **m_execs;
256 int m_execsCount;
257 int m_execsAlloc;
258 int m_steppingDepth;
260 QMap<QString,SourceFile*> m_sourceFiles; /* maps url->SourceFile */
261 QMap<int,SourceFragment*> m_sourceFragments; /* maps SourceId->SourceFragment */
262 Q3PtrList<SourceFile> m_sourceSelFiles; /* maps combobox index->SourceFile */
264 QPixmap m_stopIcon;
265 QPixmap m_emptyIcon;
266 SourceDisplay *m_sourceDisplay;
267 QListWidget *m_contextList;
269 KAction *m_stepAction;
270 KAction *m_nextAction;
271 KAction *m_continueAction;
272 KAction *m_stopAction;
273 KAction *m_breakAction;
275 QComboBox *m_sourceSel;
276 EvalMultiLineEdit *m_evalEdit;
277 int m_evalDepth;
279 static KJSDebugWin *kjs_html_debugger;
282 } // namespace
284 #endif // KJS_DEBUGGER
286 #endif // _KJS_DEBUGGER_H_