summaryrefslogtreecommitdiffstats
path: root/kexi/widget/kexieditor.cpp
blob: 82af06a3969de94f1203d2938b32948db134e286 (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
/* This file is part of the KDE project
   Copyright (C) 2003 Lucijan Busch <lucijan@gmx.at>
   Copyright (C) 2004-2005 Jaroslaw Staniek <js@iidea.pl>
   Copyright (C) 2005 Cedric Pasteur <cedric.pasteur@free.fr>

   This program 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 program 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 program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "kexieditor.h"

#include <keximainwindow.h>

#include <tqlayout.h>
#include <tqframe.h>
#include <klocale.h>
#include <kdebug.h>

//uncomment this to enable KTextEdit-based editor
//#define KTEXTEDIT_BASED_SQL_EDITOR

#ifdef KTEXTEDIT_BASED_SQL_EDITOR
# include <ktextedit.h>
#else
# include <ktexteditor/document.h>
# include <ktexteditor/view.h>
# include <ktexteditor/editorchooser.h>
# include <ktexteditor/editinterface.h>
# include <ktexteditor/viewcursorinterface.h>
# include <ktexteditor/popupmenuinterface.h>
# include <ktexteditor/undointerface.h>
# include <ktexteditor/configinterface.h>
# include <ktexteditor/highlightinginterface.h>
#endif

/** Used for the shared action framework to redirect shared actions like
copy and paste to the editor. */
class KexiEditorSharedActionConnector : public KexiSharedActionConnector
{
public:
	KexiEditorSharedActionConnector( KexiActionProxy* proxy, TQObject* obj )
		: KexiSharedActionConnector( proxy, obj )
	{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
		plugSharedAction("edit_cut", TQT_SLOT(cut()));
		plugSharedAction("edit_copy", TQT_SLOT(copy()));
		plugSharedAction("edit_paste", TQT_SLOT(paste()));
		plugSharedAction("edit_clear", TQT_SLOT(clear()));
		plugSharedAction("edit_undo", TQT_SLOT(undo()));
		plugSharedAction("edit_redo", TQT_SLOT(redo()));
                plugSharedAction("edit_select_all", TQT_SLOT(selectAll()));
#else
		TQValueList<TQCString> actions;
		actions << "edit_cut" << "edit_copy" << "edit_paste" << "edit_clear"
			<< "edit_undo" << "edit_redo" << "edit_select_all";
		plugSharedActionsToExternalGUI(actions, dynamic_cast<KXMLGUIClient*>(obj));
#endif
	}
};

//! @internal
class KexiEditorPrivate {
	public:
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
		KTextEdit *view;
#else
		KTextEditor::Document *doc;
		KTextEditor::View *view;
#endif
};

KexiEditor::KexiEditor(KexiMainWindow *mainWin, TQWidget *tqparent, const char *name)
	: KexiViewBase(mainWin, tqparent, name)
	, d(new KexiEditorPrivate())
{
	TQVBoxLayout *tqlayout = new TQVBoxLayout(this);
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	d->view = new KTextEdit( "", TQString(), this, "kexi_editor" );
	//adjust font
	connect(d->view, TQT_SIGNAL(textChanged()), this, TQT_SIGNAL(textChanged()));
	TQFont f("Courier");
	f.setStyleStrategy(TQFont::PreferAntialias);
	f.setPointSize(d->view->font().pointSize());
	d->view->setFont( f );
	d->view->setCheckSpellingEnabled(false);
#else
	TQFrame *fr = new TQFrame(this);
	fr->setFrameStyle(TQFrame::Sunken|TQFrame::WinPanel);
	tqlayout->addWidget(fr);
	tqlayout = new TQVBoxLayout(fr);
	tqlayout->setMargin( 2 );

	d->doc =  KTextEditor::EditorChooser::createDocument(TQT_TQOBJECT(fr));
	if (!d->doc)
		return;
	d->view = d->doc->createView(fr, 0L);

	KTextEditor::PopupMenuInterface *popupInt = dynamic_cast<KTextEditor::PopupMenuInterface*>( d->view );
	if(popupInt) {
			TQPopupMenu *pop = (TQPopupMenu*) mainWin->factory()->container("edit", mainWin);
			if(pop) {
				 //plugSharedAction("edit_undo", pop);
				 popupInt->installPopup(pop);
			}
	}

	connect(d->doc, TQT_SIGNAL(textChanged()), this, TQT_SIGNAL(textChanged()));
#endif
	KexiEditorSharedActionConnector c(this, TQT_TQOBJECT(d->view));
	d->view->installEventFilter(this);

	tqlayout->addWidget(d->view);
	setViewWidget(d->view, true/*focus*/);
	d->view->show();
}

KexiEditor::~KexiEditor()
{
	delete d;
}

void KexiEditor::updateActions(bool activated)
{
	KexiViewBase::updateActions(activated);
}

bool KexiEditor::isAdvancedEditor()
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	return false;
#else
	return true;
#endif
}

TQString KexiEditor::text()
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	return d->view->text();
#else
	if (!d->doc)
		return TQString();
	KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
	return eIface->text();
#endif
}

void KexiEditor::setText(const TQString &text)
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	const bool was_dirty = m_parentView ? m_parentView->dirty() : dirty();
	d->view->setText(text);
	setDirty(was_dirty);
#else
	if (!d->doc)
		return;
	const bool was_dirty = dirty();
	KTextEditor::EditInterface *eIface = KTextEditor::editInterface(d->doc);
	eIface->setText(text);
	KTextEditor::UndoInterface *undoIface = KTextEditor::undoInterface(d->doc);
	undoIface->clearUndo();
	undoIface->clearRedo();
	setDirty(was_dirty);
#endif
}

void KexiEditor::setHighlightMode(const TQString& highlightmodename)
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
#else
	KTextEditor::HighlightingInterface *hl = KTextEditor::highlightingInterface( d->doc );
	for(uint i = 0; i < hl->hlModeCount(); i++) {
			//kdDebug() << "hlmode("<<i<<"): " << hl->hlModeName(i) << endl;
			if (hl->hlModeName(i).tqcontains(highlightmodename, false))  {
				hl->setHlMode(i);
				return;
			}
	}
	hl->setHlMode(0); // 0=None, don't highlight anything.
#endif
}

void KexiEditor::slotConfigureEditor()
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	//TODO show errormessage?
#else
	KTextEditor::ConfigInterface *config = KTextEditor::configInterface( d->doc );
	if (config)
		config->configDialog();
#endif
}

void KexiEditor::jump(int character)
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	const int numRows = d->view->paragraphs();
	int row = 0, col = 0;
	for (int ch = 0; row < numRows; row++) {
		const int rowLen = d->view->paragraphLength(row)+1;
		if ((ch + rowLen) > character) {
			col = character-ch;
			break;
		}
		ch += rowLen;
	}
	d->view->setCursorPosition(row, col);
#else
	if (!d->doc)
		return;
	KTextEditor::EditInterface *ei = KTextEditor::editInterface(d->doc);
	const int numRows = ei->numLines();
	int row = 0, col = 0;
	for (int ch = 0; row < numRows; row++) {
		const int rowLen = ei->lineLength(row)+1;
		if ((ch + rowLen) > character) {
			col = character-ch;
			break;
		}
		ch += rowLen;
	}
	KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface(d->view);
	ci->setCursorPositionReal(row, col);
#endif
}

void KexiEditor::setCursorPosition(int line, int col)
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	d->view->setCursorPosition(line, col);
#else
	KTextEditor::ViewCursorInterface *ci = KTextEditor::viewCursorInterface( d->view );
	ci->setCursorPosition(line, col);
#endif
}

void KexiEditor::clearUndoRedo()
{
#ifdef KTEXTEDIT_BASED_SQL_EDITOR
	//TODO how to remove undo/redo from a KTextEdit?
#else
	KTextEditor::UndoInterface* u = KTextEditor::undoInterface( d->doc );
	u->clearUndo();
	u->clearRedo();
#endif
}

#include "kexieditor.moc"