summaryrefslogtreecommitdiffstats
path: root/qt/qextscintillabase.cpp
blob: 5aaa7b15e71a6fc369bb99f26f0a55f0704c7ce2 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// This module implements the "official" low-level API.
//
// Copyright (c) 2006
// 	Riverbank Computing Limited <info@riverbankcomputing.co.uk>
// 
// This file is part of TQScintilla.
// 
// This copy of TQScintilla 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, or (at your option) any
// later version.
// 
// TQScintilla is supplied 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
// TQScintilla; see the file LICENSE.  If not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


#include <tqapplication.h>
#include <tqclipboard.h>
#include <tqscrollbar.h>
#include <tqlayout.h>
#include <tqcolor.h>
#include <tqevent.h>
#include <tqdragobject.h>
#include <tqpainter.h>
#include <tqptrlist.h>

#include "qextscintillabase.h"
#include "ScintillaQt.h"


// The #defines in Scintilla.h and the enums in qextscintillabase.h conflict
// (because we want to use the same names) so we have to undefine those we use
// in this file.
#undef	SCI_SETCARETPERIOD
#undef	SCK_DOWN
#undef	SCK_UP
#undef	SCK_LEFT
#undef	SCK_RIGHT
#undef	SCK_HOME
#undef	SCK_END
#undef	SCK_PRIOR
#undef	SCK_NEXT
#undef	SCK_DELETE
#undef	SCK_INSERT
#undef	SCK_ESCAPE
#undef	SCK_BACK
#undef	SCK_TAB
#undef	SCK_RETURN
#undef	SCK_ADD
#undef	SCK_SUBTRACT
#undef	SCK_DIVIDE


// Remember if we have linked the lexers.
static bool lexersLinked = FALSE;

// The list of instances.
static TQPtrList<QextScintillaBase> poolList;


// The ctor.
QextScintillaBase::QextScintillaBase(TQWidget *parent,const char *name,WFlags f)
	: TQWidget(parent,name,f)
{
	sci = 0;

	TQGridLayout *layout = new TQGridLayout(this,2,2);

	txtarea = new TQWidget(this,0,WRepaintNoErase|WResizeNoErase);
	txtarea -> setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding,TQSizePolicy::Expanding));
	txtarea -> setMouseTracking(TRUE);
	txtarea -> setAcceptDrops(TRUE);
	txtarea -> setFocusPolicy(WheelFocus);
	txtarea -> setFocusProxy(this);
	layout -> addWidget(txtarea,0,0);

	vsb = new TQScrollBar(Qt::Vertical,this);
	layout -> addWidget(vsb,0,1);
	connect(vsb,TQT_SIGNAL(valueChanged(int)),TQT_SLOT(handleVSb(int)));

	hsb = new TQScrollBar(Qt::Horizontal,this);
	layout -> addWidget(hsb,1,0);
	connect(hsb,TQT_SIGNAL(valueChanged(int)),TQT_SLOT(handleHSb(int)));

	txtarea -> installEventFilter(this);

	setFocusPolicy(WheelFocus);

	sci = new ScintillaTQt(this);

	SendScintilla(SCI_SETCARETPERIOD,TQApplication::cursorFlashTime() / 2);

	// Make sure the lexers are linked in.
	if (!lexersLinked)
	{
		Scintilla_LinkLexers();

		lexersLinked = TRUE;
	}

	TQClipboard *cb = TQApplication::clipboard();

	if (cb -> supportsSelection())
		connect(cb,TQT_SIGNAL(selectionChanged()),TQT_SLOT(handleSelection()));

	// Add it to the pool.
	poolList.append(this);
}


// The dtor.
QextScintillaBase::~QextScintillaBase()
{
	// Remove it from the pool.
	poolList.remove(this);

	delete sci;
}


// Return the viewport widget.
TQWidget *QextScintillaBase::viewport() const
{
	return txtarea;
}


// Return an instance from the pool.
QextScintillaBase *QextScintillaBase::pool()
{
	return poolList.first();
}


// Send a message to the real Scintilla widget using the low level Scintilla
// API.
long QextScintillaBase::SendScintilla(unsigned int msg,unsigned long wParam,
				      long lParam)
{
	return sci -> WndProc(msg,wParam,lParam);
}


// Send a message to the real Scintilla widget that needs a TextRange
// structure.
long QextScintillaBase::SendScintilla(unsigned int msg,long cpMin,long cpMax,
				      char *lpstrText)
{
	TextRange tr;

	tr.chrg.cpMin = cpMin;
	tr.chrg.cpMax = cpMax;
	tr.lpstrText = lpstrText;

	return sci -> WndProc(msg,0,reinterpret_cast<long>(&tr));
}


// Send a message to the real Scintilla widget that needs a RangeToFormat
// structure.
long QextScintillaBase::SendScintilla(unsigned int msg,unsigned long wParam,
				      TQPainter *hdc,const TQRect &rc,
				      long cpMin,long cpMax)
{
	RangeToFormat rf;

	rf.hdc = rf.hdcTarget = reinterpret_cast<SurfaceID>(hdc);

	rf.rc.left = rc.left();
	rf.rc.top = rc.top();
	rf.rc.right = rc.right() + 1;
	rf.rc.bottom = rc.bottom() + 1;

	rf.chrg.cpMin = cpMin;
	rf.chrg.cpMax = cpMax;

	return sci -> WndProc(msg,wParam,reinterpret_cast<long>(&rf));
}


// Send a message to the real Scintilla widget that needs a colour.
long QextScintillaBase::SendScintilla(unsigned int msg,unsigned long wParam,
				      const TQColor &col)
{
	long lParam = (col.blue() << 16) | (col.green() << 8) | col.red();

	return sci -> WndProc(msg,wParam,lParam);
}


// Send a message to the real Scintilla widget that needs a colour.
long QextScintillaBase::SendScintilla(unsigned int msg,const TQColor &col)
{
	unsigned long wParam = (col.blue() << 16) | (col.green() << 8) | col.red();

	return sci -> WndProc(msg,wParam,0);
}


// Handle events on behalf of the text area.
bool QextScintillaBase::eventFilter(TQObject *o,TQEvent *e)
{
	if (o != txtarea)
		return TQWidget::eventFilter(o,e);

	bool used = TRUE;

	switch (e -> type())
	{
	case TQEvent::Paint:
		sci -> paintEvent(static_cast<TQPaintEvent *>(e));
		break;

	case TQEvent::Resize:
		sci -> ChangeSize();
		break;

	case TQEvent::MouseButtonPress:
		mousePress(static_cast<TQMouseEvent *>(e));
		break;

	case TQEvent::MouseButtonRelease:
		mouseRelease(static_cast<TQMouseEvent *>(e));
		break;

	case TQEvent::MouseButtonDblClick:
		mouseDoubleClick(static_cast<TQMouseEvent *>(e));
		break;

	case TQEvent::MouseMove:
		mouseMove(static_cast<TQMouseEvent *>(e));
		break;

	case TQEvent::Wheel:
		mouseWheel(static_cast<TQWheelEvent *>(e));
		break;

	case TQEvent::ContextMenu:
		contextMenu(static_cast<TQContextMenuEvent *>(e));
		break;

	case TQEvent::DragEnter:
		sci -> dragEnterEvent(static_cast<TQDragEnterEvent *>(e));
		break;

	case TQEvent::DragMove:
		sci -> dragMoveEvent(static_cast<TQDragMoveEvent *>(e));
		break;

	case TQEvent::DragLeave:
		sci -> dragLeaveEvent(static_cast<TQDragLeaveEvent *>(e));
		break;

	case TQEvent::Drop:
		sci -> dropEvent(static_cast<TQDropEvent *>(e));
		break;

	default:
		used = FALSE;
	}

	return used;
}


// Handle the timer on behalf of the ScintillaTQt instance.
void QextScintillaBase::handleTimer()
{
	sci -> Tick();
}


// Handle the context menu on behalf of the ScintillaTQt instance.
void QextScintillaBase::handlePopUp(int cmd)
{
	sci -> Command(cmd);
}


// Re-implemented to tell the widget it has the focus.
void QextScintillaBase::focusInEvent(TQFocusEvent *)
{
	sci -> SetFocusState(true);
}


// Re-implemented to tell the widget it has lost the focus.
void QextScintillaBase::focusOutEvent(TQFocusEvent *)
{
	sci -> SetFocusState(false);
}


// Handle a mouse button press.
void QextScintillaBase::mousePress(TQMouseEvent *me)
{
	setFocus();

	Point pt(me -> x(),me -> y());

	switch (me -> button())
	{
	case LeftButton:
		{
			unsigned clickTime;

			// It is a triple click if the timer is running and the
			// mouse hasn't moved too much.
			if (triple_click.isActive() && (me -> globalPos() - triple_click_at).manhattanLength() < TQApplication::startDragDistance())
				clickTime = sci -> lastClickTime + Platform::DoubleClickTime() - 1;
            else
				clickTime = sci -> lastClickTime + Platform::DoubleClickTime() + 1;

			triple_click.stop();

			bool shift = me -> state() & ShiftButton;
			bool ctrl = me -> state() & ControlButton;
			bool alt = me -> state() & AltButton;

			sci -> ButtonDown(pt,clickTime,shift,ctrl,alt);
			break;
		}

	case MidButton:
		{
			TQClipboard *cb = TQApplication::clipboard();

			if (cb -> supportsSelection())
			{
				cb -> setSelectionMode(TRUE);

				int pos = sci -> PositionFromLocation(pt);

				sci -> SetSelection(pos,pos);
				sci -> Paste();

				cb -> setSelectionMode(FALSE);
			}

			break;
		}

	default:
		break;
	}
}


// Handle a context menu event.
void QextScintillaBase::contextMenu(TQContextMenuEvent *cme)
{
	TQApplication::sendEvent(this,cme);

	if (!cme -> isConsumed())
		sci -> ContextMenu(Point(cme -> globalX(),cme -> globalY()));
}


// Handle a mouse button releases.
void QextScintillaBase::mouseRelease(TQMouseEvent *me)
{
	if (sci -> HaveMouseCapture() && me -> button() == LeftButton)
	{
		bool ctrl = me -> state() & ControlButton;

		sci -> ButtonUp(Point(me -> x(),me -> y()),0,ctrl);
	}
}


// Handle a mouse move.
void QextScintillaBase::mouseMove(TQMouseEvent *me)
{
	sci -> ButtonMove(Point(me -> x(),me -> y()));
}


// Handle a mouse wheel event.
void QextScintillaBase::mouseWheel(TQWheelEvent *we)
{
	setFocus();

	if (we -> orientation() == Qt::Horizontal || we -> state() & ShiftButton)
		TQApplication::sendEvent(hsb,we);
	else if (we -> orientation() == Qt::Vertical)
		TQApplication::sendEvent(vsb,we);
}


// Handle a mouse button double click.
void QextScintillaBase::mouseDoubleClick(TQMouseEvent *me)
{
	setFocus();

	if (me -> button() == LeftButton)
	{
		// Make sure Scintilla will interpret this as a double-click.
		unsigned clickTime = sci -> lastClickTime + Platform::DoubleClickTime() - 1;

		bool shift = me -> state() & ShiftButton;
		bool ctrl = me -> state() & ControlButton;
		bool alt = me -> state() & AltButton;

		sci -> ButtonDown(Point(me -> x(),me -> y()),clickTime,shift,ctrl,alt);

		// Remember the current position and time in case it turns into a
		// triple click.
		triple_click_at = me -> globalPos();
		triple_click.start(TQApplication::doubleClickInterval());
	}
}


// Re-implemented to handle key press events.
void QextScintillaBase::keyPressEvent(TQKeyEvent *ke)
{
	unsigned key;

	switch (ke -> key())
	{
	case Key_Down:
		key = SCK_DOWN;
		break;

	case Key_Up:
		key = SCK_UP;
		break;

	case Key_Left:
		key = SCK_LEFT;
		break;

	case Key_Right:
		key = SCK_RIGHT;
		break;

	case Key_Home:
		key = SCK_HOME;
		break;

	case Key_End:
		key = SCK_END;
		break;

	case Key_Prior:
		key = SCK_PRIOR;
		break;

	case Key_Next:
		key = SCK_NEXT;
		break;

	case Key_Delete:
		key = SCK_DELETE;
		break;

	case Key_Insert:
		key = SCK_INSERT;
		break;

	case Key_Escape:
		key = SCK_ESCAPE;
		break;

	case Key_Backspace:
		key = SCK_BACK;
		break;

	case Key_Tab:
		key = SCK_TAB;
		break;

	case Key_Return:
	case Key_Enter:
		key = SCK_RETURN;
		break;

	default:
		if (sci -> IsUnicodeMode())
		{
			// Work out if the original input was a single ASCII
			// key.
			if (ke -> text().length() == 1)
			{
				if ((key = ke -> text()[0].unicode()) >= 0x80)
					key = 0;
			}
			else
				key = 0;
		}
		else
		{
			key = ke -> ascii();

			if (key >= 0x01 && key <= 0x1f)
				key += 0x40;
		}
	}

	bool consumed = FALSE;

	if (key)
	{
		bool shift = ke -> state() & ShiftButton;
		bool ctrl = ke -> state() & ControlButton;
		bool alt = ke -> state() & AltButton;

		// If the character is eventually added by KeyDefault() then
		// the return value of KeyDown() will be true, but consumed
		// will incorrectly be false.
		if (sci -> KeyDown(key,shift,ctrl,alt,&consumed) && !consumed)
			consumed = TRUE;
	}
	else if (sci -> IsUnicodeMode())
	{
		if (ke -> text().length() > 0 && !ke -> text()[0].isNull())
		{
			TQCString s = ke -> text().utf8();

			sci -> AddCharUTF(s.data(),s.length());

			consumed = TRUE;
		}
	}

	if (!consumed)
		ke -> ignore();
}


// Re-implemented to make sure tabs are passed to the editor.
bool QextScintillaBase::focusNextPrevChild(bool)
{
	return false;
}


// Start a drag and allow this to be re-implemented by an application.
void QextScintillaBase::startDrag()
{
	sci -> StartDragImpl();
}


// Handle the vertical scrollbar.
void QextScintillaBase::handleVSb(int val)
{
	sci -> ScrollTo(val);
}


// Handle the horizontal scrollbar.
void QextScintillaBase::handleHSb(int val)
{
	sci ->HorizontalScrollTo(val);
}


// Return the current prefered size.
TQSize QextScintillaBase::sizeHint() const
{
	int height = sci -> vs.lineHeight * sci -> pdoc -> LinesTotal();

	if (sci -> horizontalScrollBarVisible)
		height += hsb -> sizeHint().height();

	return TQSize(sci -> scrollWidth,height);
}


// Handle the selection changing.
void QextScintillaBase::handleSelection()
{
	if (!TQApplication::clipboard() -> ownsSelection())
		sci -> UnclaimSelection();
}