summaryrefslogtreecommitdiffstats
path: root/src/focusedwidgets.cpp
blob: 429d260cada9ccc71984974fa49c5f8198d0b8e6 (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
286
287
288
289
290
291
292
293
294
295
/***************************************************************************
 *   Copyright (C) 2003 by S�astien Laot                                 *
 *   slaout@linux62.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include <tqpopupmenu.h>
#include <klocale.h>

#include <iostream>

#include "focusedwidgets.h"
#include "bnpview.h"
#include "global.h"
#include "basket.h"

#ifdef KeyPress
#undef KeyPress
#endif
#include <tqevent.h>

/** class FocusedTextEdit */

FocusedTextEdit::FocusedTextEdit(bool disableUpdatesOnKeyPress, TQWidget *tqparent, const char *name)
 : KTextEdit(tqparent, name),
   m_disableUpdatesOnKeyPress(disableUpdatesOnKeyPress)
{
	setWFlags(TQt::WNoAutoErase); // Does not work, we still need the disableUpdatesOnKeyPress hack!
}

FocusedTextEdit::~FocusedTextEdit()
{
}

/**
  * Thanks to alex.theel@gmx.net, author of TuxCards
  * Code copied from tuxcards-1.2/src/gui/editor/editor.cpp
  *
  ***
  * Override the regular paste() methode, so that lines are
  * not separated by each other with an blank line.
  */
void FocusedTextEdit::paste()
{
	adaptClipboardText(TQClipboard::Selection);
	adaptClipboardText(TQClipboard::Clipboard);

	// If we paste a application/x-qrichtext content starting with a "-" or a "*",
	// then auto-bulletting will crash.
	// So we insert a space to be sure what we paste will not trigger the auto-bulleting.

//	enum AutoFormatting { AutoNone = 0, AutoBulletList = 0x00000001, AutoAll = 0xffffffff }
//	uint oldAutoFormating = autoFormatting();
//	setAutoFormatting(AutoNone);

	TQClipboard *clipboard = TQApplication::tqclipboard();
	int paragraph;
	int index;
	getCursorPosition(&paragraph, &index);

	bool preventAutoBullet = (index == 0) &&
		(clipboard->data(TQClipboard::Selection)->provides("application/x-qrichtext") ||
		 clipboard->data(TQClipboard::Clipboard)->provides("application/x-qrichtext")   );

	if (preventAutoBullet)
		insert(" ");

	KTextEdit::paste();

	if (preventAutoBullet) {
		int paragraph2;
		int index2;
		getCursorPosition(&paragraph2, &index2);
		setSelection(paragraph, index, paragraph, index + 1);
		removeSelectedText();
		if (paragraph == paragraph2) // We removed one character in that paragraph, so we should move the cursor back to old position... minus one character
			index2--;
		setCursorPosition(paragraph2, index2);
	}


//	setAutoFormatting(oldAutoFormating);
}

/**
  * Thanks to alex.theel@gmx.net, author of TuxCards
  * Code copied from tuxcards-1.2/src/gui/editor/editor.cpp
  *
  ***
  * Auxiliar method that takes the text from the clipboard - using the
  * specified 'mode' -, replaces all '\n' within that text and writes
  * it back to the clipboard.
  */
void FocusedTextEdit::adaptClipboardText(TQClipboard::Mode mode)
{
	TQClipboard *clipboard = TQApplication::tqclipboard();
	if (!clipboard)
		return;

	if ( (textFormat() == TQt::RichText) && (!clipboard->data(mode)->provides("application/x-qrichtext")) ) {
		TQString text = clipboard->text(mode);
		if (text) {
			text = text.tqreplace("\n", TQChar(0x2028));
			clipboard->setText(text, mode);
		}
	}
}


TQTextCursor* FocusedTextEdit::textCursor() const
{
	return KTextEdit::textCursor();
}


void FocusedTextEdit::keyPressEvent(TQKeyEvent *event)
{
	if (event->key() == TQt::Key_Escape) {
		emit escapePressed();
		return;
	// In RichTextFormat mode, [Return] create a new paragraphe.
	// To keep consistency with TextFormat mode (new line on [Return]),
	// we redirect [Return] to simulate [Ctrl+Return] (create a new line in both modes).
	// Create new paragraphes still possible in RichTextFormat mode with [Shift+Enter].
	} else if (event->key() == TQt::Key_Return && event->state() == 0)
		event = new TQKeyEvent(TQEvent::KeyPress, event->key(), event->ascii(), TQt::ControlButton,
		                      event->text(), event->isAutoRepeat(), event->count() );
	else if (event->key() == TQt::Key_Return && event->state() & TQt::ControlButton)
		event = new TQKeyEvent(TQEvent::KeyPress, event->key(), event->ascii(), TQt::ShiftButton,
		                      event->text(), event->isAutoRepeat(), event->count() );

	if (m_disableUpdatesOnKeyPress)
		setUpdatesEnabled(false);
	KTextEdit::keyPressEvent(event);
	// Workarround (for ensuring the cursor to be visible): signal not emited when pressing those keys:
	if (event->key() == TQt::Key_Home || event->key() == TQt::Key_End || event->key() == TQt::Key_PageUp || event->key() == TQt::Key_PageDown) {
		int para;
		int index;
		getCursorPosition(&para, &index);
		emit cursorPositionChanged(para, index);
	}
	if (m_disableUpdatesOnKeyPress) {
		setUpdatesEnabled(true);
		if (text().isEmpty())
			;// emit textChanged(); // TODO: DOESN'T WORK: the editor is not resized down to only one line of text
		else
			ensureCursorVisible();
		updateContents();
	}
}

void FocusedTextEdit::wheelEvent(TQWheelEvent *event)
{
	if (event->delta() > 0 && contentsY() > 0) {
		KTextEdit::wheelEvent(event);
		return;
	} else if (event->delta() < 0 && contentsY() + visibleHeight() < contentsHeight()) {
		KTextEdit::wheelEvent(event);
		return;
	} else
		Global::bnpView->currentBasket()->wheelEvent(event);
}

void FocusedTextEdit::enterEvent(TQEvent *event)
{
	emit mouseEntered();
	KTextEdit::enterEvent(event);
}

TQPopupMenu* FocusedTextEdit::createPopupMenu(const TQPoint &pos)
{
	TQPopupMenu *menu = KTextEdit::createPopupMenu(pos);

	int index = 0;
	int id = 0;
	while (true) {
		id = menu->idAt(index);
		if (id == -1)
			break;
		// Disable Spell Check for rich text editors, because it doesn't work anyway:
		if (textFormat() == TQt::RichText && (menu->text(id) == i18n("Auto Spell Check") || menu->text(id) == i18n("Check Spelling...")))
			menu->setItemEnabled(id, false);
		// Always enable tabulations!:
		if (menu->text(id) == i18n("Allow Tabulations"))
			menu->setItemEnabled(id, false);
		index++;
	}

	// And return the menu:
	return menu;
}

/** class FocusedColorCombo: */

FocusedColorCombo::FocusedColorCombo(TQWidget *tqparent, const char *name)
 : KColorCombo(tqparent, name)
{
}

FocusedColorCombo::~FocusedColorCombo()
{
}

void FocusedColorCombo::keyPressEvent(TQKeyEvent *event)
{
	if (event->key() == TQt::Key_Escape)
		emit escapePressed();
	else if (event->key() == TQt::Key_Return)
		emit returnPressed2();
	else
		KColorCombo::keyPressEvent(event);
}

/** class FocusedFontCombo: */

FocusedFontCombo::FocusedFontCombo(TQWidget *tqparent, const char *name)
 : KFontCombo(tqparent, name)
{
}

FocusedFontCombo::~FocusedFontCombo()
{
}

void FocusedFontCombo::keyPressEvent(TQKeyEvent *event)
{
	if (event->key() == TQt::Key_Escape)
		emit escapePressed();
	else if (event->key() == TQt::Key_Return)
		emit returnPressed2();
	else
		KFontCombo::keyPressEvent(event);
}

/** class FocusedComboBox: */

FocusedComboBox::FocusedComboBox(TQWidget *tqparent, const char *name)
 : KComboBox(tqparent, name)
{
}

FocusedComboBox::~FocusedComboBox()
{
}

void FocusedComboBox::keyPressEvent(TQKeyEvent *event)
{
	if (event->key() == TQt::Key_Escape)
		emit escapePressed();
	else if (event->key() == TQt::Key_Return)
		emit returnPressed2();
	else
		KComboBox::keyPressEvent(event);
}

/** class FocusedLineEdit: */

FocusedLineEdit::FocusedLineEdit(TQWidget *tqparent, const char *name)
 : KLineEdit(tqparent, name)
{
}

FocusedLineEdit::~FocusedLineEdit()
{
}

void FocusedLineEdit::keyPressEvent(TQKeyEvent *event)
{
	if (event->key() == TQt::Key_Escape)
		emit escapePressed();
	else
		KLineEdit::keyPressEvent(event);
}

void FocusedLineEdit::enterEvent(TQEvent *event)
{
	emit mouseEntered();
	KLineEdit::enterEvent(event);
}

#include "focusedwidgets.moc"