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>
31 #include <q3multilineedit.h>
33 #include <q3ptrlist.h>
34 #include <QtCore/QStack>
35 #include <qcheckbox.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"
57 class SourceFile
: public DOM::DomShared
60 SourceFile(QString u
, QString c
, Interpreter
*interp
)
61 : url(u
), code(c
), interpreter(interp
) {}
65 Interpreter
*interpreter
;
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
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.
86 SourceFragment(int sid
, int bl
, int el
, SourceFile
*sf
);
92 SourceFile
*sourceFile
;
94 SourceFragment(const SourceFragment
& other
);
95 SourceFragment
& operator = (const SourceFragment
& other
);
98 class KJSErrorDialog
: public KDialog
{
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(); }
108 virtual void slotUser1();
111 QCheckBox
*m_dontShowAgainCb
;
112 bool m_debugSelected
;
115 class EvalMultiLineEdit
: public Q3MultiLineEdit
{
118 EvalMultiLineEdit(QWidget
*parent
);
119 const QString
& code() const { return m_code
; }
121 void keyPressEvent(QKeyEvent
* e
);
126 class SourceDisplay
: public Q3ScrollView
{
129 SourceDisplay(KJSDebugWin
*debugWin
, QWidget
*parent
, const char *name
= 0);
132 void setSource(SourceFile
*sourceFile
);
133 void setCurrentLine(int lineno
, bool doCenter
= true);
136 void lineDoubleClicked(int lineno
);
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
);
145 SourceFile
*m_sourceFile
;
148 KJSDebugWin
*m_debugWin
;
150 QPixmap m_breakpointIcon
;
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
165 friend class SourceDisplay
;
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
);
204 void slotBreakNext();
205 void slotToggleBreakpoint(int lineno
);
206 void slotShowFrame(int frameno
);
207 void slotSourceSelected(int sourceSelIndex
);
212 void closeEvent(QCloseEvent
*e
);
213 bool eventFilter(QObject
*obj
, QEvent
*evt
);
214 void disableOtherWindows();
215 void enableOtherWindows();
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
);
226 void displaySourceFile(SourceFile
*sourceFile
, bool forceRefresh
);
227 void updateContextList();
229 QString
contextStr(const Context
&ctx
);
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
)
250 SourceFile
*m_curSourceFile
;
252 QString m_nextSourceUrl
;
253 int m_nextSourceBaseLine
;
254 QStack
<ExecState
*> m_execStates
;
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 */
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
;
279 static KJSDebugWin
*kjs_html_debugger
;
284 #endif // KJS_DEBUGGER
286 #endif // _KJS_DEBUGGER_H_