summaryrefslogtreecommitdiffstats
path: root/tdehtml/ecma/kjs_debugwin.h
blob: be6b31dda3d578cad0682f8296ff03dbb0c8bd3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 2000-2001 Harri Porten ([email protected])
 *  Copyright (C) 2001,2003 Peter Kelly ([email protected])
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifndef _KJS_DEBUGGER_H_
#define _KJS_DEBUGGER_H_

#include <tqglobal.h>

#define KJS_DEBUGGER

#ifdef KJS_DEBUGGER

#include <tqwidget.h>
#include <tqmultilineedit.h>
#include <tqpixmap.h>
#include <tqptrlist.h>
#include <tqptrstack.h>
#include <tqcheckbox.h>
#include <kdialogbase.h>
#include <tdemainwindow.h>
#include <tqscrollview.h>

#include <kjs/debugger.h>

#include "dom/dom_misc.h"

class TQListBox;
class TQComboBox;
class TDEActionCollection;
class TDEAction;

namespace KJS {
  class FunctionImp;
  class List;
  class Interpreter;
  class KJSDebugWin;

  class SourceFile : public DOM::DomShared
  {
   public:
    SourceFile(TQString u, TQString c, Interpreter *interp) 
	: url(u), code(c), interpreter(interp) {}
    TQString getCode();
    TQString url;
    TQString code;
    Interpreter *interpreter;
  };

  /**
   * @internal
   *
   * When kjs parses some code, it generates a source code fragment (or just "source").
   * This is referenced by its source id in future calls to functions such as atLine()
   * and callEvent(). We keep a record of all source fragments parsed in order to display
   * then to the user.
   *
   * For .js files, the source fragment will be the entire file. For js code included
   * in html files, however, there may be multiple source fragments within the one file
   * (e.g. multiple SCRIPT tags or onclick="..." attributes)
   *
   * In the case where a single file has multiple source fragments, the source objects
   * for these fragments will all point to the same SourceFile for their code.
   */
  class SourceFragment
  {
  public:
    SourceFragment(int sid, int bl, int el, SourceFile *sf);
    ~SourceFragment();

    int sourceId;
    int baseLine;
    int errorLine;
    SourceFile *sourceFile;
  private:
    SourceFragment(const SourceFragment& other);
    SourceFragment& operator = (const SourceFragment& other);
  };

  class KJSErrorDialog : public KDialogBase {
    Q_OBJECT
  public:
    KJSErrorDialog(TQWidget *parent, const TQString& errorMessage, bool showDebug);
    virtual ~KJSErrorDialog();

    bool debugSelected() const { return m_debugSelected; }
    bool dontShowAgain() const { return m_dontShowAgainCb->isChecked(); }

  protected slots:
    virtual void slotUser1();

  private:
    TQCheckBox *m_dontShowAgainCb;
    bool m_debugSelected;
  };

  class EvalMultiLineEdit : public TQMultiLineEdit {
    Q_OBJECT
  public:
      EvalMultiLineEdit(TQWidget *parent);
      const TQString & code() const { return m_code; }
  protected:
      void keyPressEvent(TQKeyEvent * e);
  private:
      TQString m_code;
  };

  class SourceDisplay : public TQScrollView {
    Q_OBJECT
  public:
    SourceDisplay(KJSDebugWin *debugWin, TQWidget *parent, const char *name = 0);
    ~SourceDisplay();

    void setSource(SourceFile *sourceFile);
    void setCurrentLine(int lineno, bool doCenter = true);

  signals:
    void lineDoubleClicked(int lineno);

  protected:
    virtual void contentsMousePressEvent(TQMouseEvent *e);
    virtual void showEvent(TQShowEvent *);
    virtual void drawContents(TQPainter *p, int clipx, int clipy, int clipw, int cliph);

    TQString m_source;
    int m_currentLine;
    SourceFile *m_sourceFile;
    TQStringList m_lines;

    KJSDebugWin *m_debugWin;
    TQFont m_font;
    TQPixmap m_breakpointIcon;
  };

  /**
   * @internal
   *
   * KJSDebugWin represents the debugger window that is visible to the user. It contains
   * a stack frame list, a code viewer and a source fragment selector, plus buttons
   * to control execution including next, step and continue.
   *
   * There is only one debug window per program. This can be obtained by calling #instance
   */
  class KJSDebugWin : public TDEMainWindow, public Debugger, public TDEInstance
  {
    Q_OBJECT
    friend class SourceDisplay;
  public:
    KJSDebugWin(TQWidget *parent=0, const char *name=0);
    virtual ~KJSDebugWin();

    static KJSDebugWin *createInstance();
    static void destroyInstance();
    static KJSDebugWin *debugWindow() { return kjs_html_debugger; }

    enum Mode { Disabled = 0, // No break on any statements
		Next     = 1, // Will break on next statement in current context
		Step     = 2, // Will break on next statement in current or deeper context
		Continue = 3, // Will continue until next breakpoint
		Stop     = 4  // The script will stop execution completely,
			      // as soon as possible
    };

    void setSourceLine(int sourceId, int lineno);
    void setNextSourceInfo(TQString url, int baseLine);
    void sourceChanged(Interpreter *interpreter, TQString url);
    bool inSession() const { return !m_execStates.isEmpty(); }
    void setMode(Mode m) { m_mode = m; }
    void clearInterpreter(Interpreter *interpreter);
    ExecState *getExecState() const { return m_execStates.top(); }

    // functions overridden from KJS:Debugger
    bool sourceParsed(ExecState *exec, int sourceId,
		      const UString &source, int errorLine);
    bool sourceUnused(ExecState * exec, int sourceId);
    bool exception(ExecState *exec, const Value &value, bool inTryCatch);
    bool atStatement(ExecState *exec);
    bool enterContext(ExecState *exec);
    bool exitContext(ExecState *exec, const Completion &completion);

  public slots:
    void slotNext();
    void slotStep();
    void slotContinue();
    void slotStop();
    void slotBreakNext();
    void slotToggleBreakpoint(int lineno);
    void slotShowFrame(int frameno);
    void slotSourceSelected(int sourceSelIndex);
    void slotEval();

  protected:

    void closeEvent(TQCloseEvent *e);
    bool eventFilter(TQObject *obj, TQEvent *evt);
    void disableOtherWindows();
    void enableOtherWindows();

  private:

    SourceFile *getSourceFile(Interpreter *interpreter, TQString url);
    void setSourceFile(Interpreter *interpreter, TQString url, SourceFile *sourceFile);
    void removeSourceFile(Interpreter *interpreter, TQString url);

    void checkBreak(ExecState *exec);
    void enterSession(ExecState *exec);
    void leaveSession();
    void displaySourceFile(SourceFile *sourceFile, bool forceRefresh);
    void updateContextList();

    TQString contextStr(const Context &ctx);

    struct Breakpoint {
      int sourceId;
      int lineno;
    };
    Breakpoint *m_breakpoints;
    int m_breakpointCount;
    bool setBreakpoint(int sourceId, int lineno);
    bool deleteBreakpoint(int sourceId, int lineno);
    bool haveBreakpoint(SourceFile *sourceFile, int line0, int line1);
    bool haveBreakpoint(int sourceId, int line0, int line1) const {
      for (int i = 0; i < m_breakpointCount; i++) {
	if (m_breakpoints[i].sourceId == sourceId &&
	    m_breakpoints[i].lineno >= line0 &&
	    m_breakpoints[i].lineno <= line1)
	  return true;
      }
      return false;
    }

    SourceFile *m_curSourceFile;
    Mode m_mode;
    TQString m_nextSourceUrl;
    int m_nextSourceBaseLine;
    TQPtrStack<ExecState> m_execStates;
    ExecState **m_execs;
    int m_execsCount;
    int m_execsAlloc;
    int m_steppingDepth;

    TQMap<TQString,SourceFile*> m_sourceFiles; /* maps url->SourceFile */
    TQMap<int,SourceFragment*> m_sourceFragments; /* maps SourceId->SourceFragment */
    TQPtrList<SourceFile> m_sourceSelFiles; /* maps combobox index->SourceFile */

    TDEActionCollection *m_actionCollection;
    TQPixmap m_stopIcon;
    TQPixmap m_emptyIcon;
    SourceDisplay *m_sourceDisplay;
    TQListBox *m_contextList;

    TDEAction *m_stepAction;
    TDEAction *m_nextAction;
    TDEAction *m_continueAction;
    TDEAction *m_stopAction;
    TDEAction *m_breakAction;

    TQComboBox *m_sourceSel;
    EvalMultiLineEdit *m_evalEdit;
    int m_evalDepth;

    static KJSDebugWin *kjs_html_debugger;
  };

} // namespace

#endif // KJS_DEBUGGER

#endif // _KJS_DEBUGGER_H_