Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[a7e785]: qt / ScintillaEditBase / ScintillaEditBase.cpp Maximize Restore History

Download this file

ScintillaEditBase.cpp    640 lines (532 with data), 17.4 kB

  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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
//
// Copyright (c) 1990-2011, Scientific Toolworks, Inc.
//
// The License.txt file describes the conditions under which this software may be distributed.
//
// Author: Jason Haslam
//
// Additions Copyright (c) 2011 Archaeopteryx Software, Inc. d/b/a Wingware
// ScintillaEditBase.cpp - Qt widget that wraps ScintillaQt and provides events and scrolling
#include "ScintillaEditBase.h"
#include "ScintillaQt.h"
#include "PlatQt.h"
#include <QApplication>
#include <QInputContext>
#include <QPainter>
#include <QScrollBar>
#include <QTextFormat>
#include <QVarLengthArray>
#define INDIC_INPUTMETHOD 24
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif
ScintillaEditBase::ScintillaEditBase(QWidget *parent)
: QAbstractScrollArea(parent), sqt(0), preeditPos(-1), wheelDelta(0)
{
sqt = new ScintillaQt(this);
time.start();
// Set Qt defaults.
setAcceptDrops(true);
setMouseTracking(true);
setAutoFillBackground(false);
setFrameStyle(QFrame::NoFrame);
setFocusPolicy(Qt::StrongFocus);
setAttribute(Qt::WA_StaticContents);
setAttribute(Qt::WA_OpaquePaintEvent);
setAttribute(Qt::WA_KeyCompression);
setAttribute(Qt::WA_InputMethodEnabled);
connect(sqt, SIGNAL(notifyParent(SCNotification)),
this, SLOT(notifyParent(SCNotification)));
// Connect scroll bars.
connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
this, SLOT(scrollVertical(int)));
connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
this, SLOT(scrollHorizontal(int)));
// Connect pass-through signals.
connect(sqt, SIGNAL(horizontalRangeChanged(int,int)),
this, SIGNAL(horizontalRangeChanged(int,int)));
connect(sqt, SIGNAL(verticalRangeChanged(int,int)),
this, SIGNAL(verticalRangeChanged(int,int)));
connect(sqt, SIGNAL(horizontalScrolled(int)),
this, SIGNAL(horizontalScrolled(int)));
connect(sqt, SIGNAL(verticalScrolled(int)),
this, SIGNAL(verticalScrolled(int)));
connect(sqt, SIGNAL(notifyChange()),
this, SIGNAL(notifyChange()));
connect(sqt, SIGNAL(command(uptr_t, sptr_t)),
this, SLOT(event_command(uptr_t, sptr_t)));
connect(sqt, SIGNAL(aboutToCopy(QMimeData *)),
this, SIGNAL(aboutToCopy(QMimeData *)));
}
ScintillaEditBase::~ScintillaEditBase() {}
sptr_t ScintillaEditBase::send(
unsigned int iMessage,
uptr_t wParam,
sptr_t lParam) const
{
return sqt->WndProc(iMessage, wParam, lParam);
}
sptr_t ScintillaEditBase::sends(
unsigned int iMessage,
uptr_t wParam,
const char *s) const
{
return sqt->WndProc(iMessage, wParam, (sptr_t)s);
}
void ScintillaEditBase::scrollHorizontal(int value)
{
sqt->HorizontalScrollTo(value);
}
void ScintillaEditBase::scrollVertical(int value)
{
sqt->ScrollTo(value);
}
bool ScintillaEditBase::event(QEvent *event)
{
bool result = false;
if (event->type() == QEvent::KeyPress) {
// Circumvent the tab focus convention.
keyPressEvent(static_cast<QKeyEvent *>(event));
result = event->isAccepted();
} else {
result = QAbstractScrollArea::event(event);
}
return result;
}
void ScintillaEditBase::paintEvent(QPaintEvent *event)
{
sqt->PartialPaint(PRectFromQRect(event->rect()));
}
void ScintillaEditBase::wheelEvent(QWheelEvent *event)
{
if (event->orientation() == Qt::Horizontal) {
if (horizontalScrollBarPolicy() == Qt::ScrollBarAlwaysOff)
event->ignore();
else
QAbstractScrollArea::wheelEvent(event);
} else {
if (event->modifiers() & Qt::ControlModifier) {
// Zoom! We play with the font sizes in the styles.
// Number of steps/line is ignored, we just care if sizing up or down
if (event->delta() > 0) {
sqt->KeyCommand(SCI_ZOOMIN);
} else {
sqt->KeyCommand(SCI_ZOOMOUT);
}
} else {
// Ignore wheel events when the scroll bars are disabled.
if (verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOff) {
event->ignore();
} else {
// Scroll
QAbstractScrollArea::wheelEvent(event);
}
}
}
}
void ScintillaEditBase::focusInEvent(QFocusEvent *event)
{
sqt->SetFocusState(true);
emit updateUi();
QAbstractScrollArea::focusInEvent(event);
}
void ScintillaEditBase::focusOutEvent(QFocusEvent *event)
{
sqt->SetFocusState(false);
QAbstractScrollArea::focusOutEvent(event);
}
void ScintillaEditBase::resizeEvent(QResizeEvent *)
{
sqt->ChangeSize();
emit resized();
}
void ScintillaEditBase::keyPressEvent(QKeyEvent *event)
{
// All keystrokes containing the meta modifier are
// assumed to be shortcuts not handled by scintilla.
if (event->modifiers() & Qt::MetaModifier) {
QAbstractScrollArea::keyPressEvent(event);
emit keyPressed(event);
return;
}
int key = 0;
switch (event->key()) {
case Qt::Key_Down: key = SCK_DOWN; break;
case Qt::Key_Up: key = SCK_UP; break;
case Qt::Key_Left: key = SCK_LEFT; break;
case Qt::Key_Right: key = SCK_RIGHT; break;
case Qt::Key_Home: key = SCK_HOME; break;
case Qt::Key_End: key = SCK_END; break;
case Qt::Key_PageUp: key = SCK_PRIOR; break;
case Qt::Key_PageDown: key = SCK_NEXT; break;
case Qt::Key_Delete: key = SCK_DELETE; break;
case Qt::Key_Insert: key = SCK_INSERT; break;
case Qt::Key_Escape: key = SCK_ESCAPE; break;
case Qt::Key_Backspace: key = SCK_BACK; break;
case Qt::Key_Plus: key = SCK_ADD; break;
case Qt::Key_Minus: key = SCK_SUBTRACT; break;
case Qt::Key_Backtab: // fall through
case Qt::Key_Tab: key = SCK_TAB; break;
case Qt::Key_Enter: // fall through
case Qt::Key_Return: key = SCK_RETURN; break;
case Qt::Key_Control: key = 0; break;
case Qt::Key_Alt: key = 0; break;
case Qt::Key_Shift: key = 0; break;
case Qt::Key_Meta: key = 0; break;
default: key = event->key(); break;
}
bool shift = event->modifiers() & Qt::ShiftModifier;
bool ctrl = event->modifiers() & Qt::ControlModifier;
bool alt = event->modifiers() & Qt::AltModifier;
bool consumed = false;
bool added = sqt->KeyDown(key, shift, ctrl, alt, &consumed) != 0;
if (!consumed)
consumed = added;
if (!consumed) {
// Don't insert text if the control key was pressed unless
// it was pressed in conjunction with alt for AltGr emulation.
bool input = (!ctrl || alt);
// Additionally, on non-mac platforms, don't insert text
// if the alt key was pressed unless control is also present.
// On mac alt can be used to insert special characters.
#ifndef Q_WS_MAC
input &= (!alt || ctrl);
#endif
QString text = event->text();
if (input && !text.isEmpty() && text[0].isPrint()) {
QByteArray utext = sqt->BytesForDocument(text);
sqt->AddCharUTF(utext.data(), utext.size());
} else {
event->ignore();
}
}
emit keyPressed(event);
}
static int modifierTranslated(int sciModifier)
{
switch (sciModifier) {
case SCMOD_SHIFT:
return Qt::ShiftModifier;
case SCMOD_CTRL:
return Qt::ControlModifier;
case SCMOD_ALT:
return Qt::AltModifier;
case SCMOD_SUPER:
return Qt::MetaModifier;
default:
return 0;
}
}
void ScintillaEditBase::mousePressEvent(QMouseEvent *event)
{
Point pos = PointFromQPoint(event->pos());
emit buttonPressed(event);
if (event->button() == Qt::MidButton &&
QApplication::clipboard()->supportsSelection()) {
SelectionPosition selPos = sqt->SPositionFromLocation(
pos, false, false, sqt->UserVirtualSpace());
sqt->sel.Clear();
sqt->SetSelection(selPos, selPos);
sqt->PasteFromMode(QClipboard::Selection);
return;
}
bool button = event->button() == Qt::LeftButton;
if (button) {
bool shift = event->modifiers() & Qt::ShiftModifier;
bool ctrl = event->modifiers() & Qt::ControlModifier;
#ifdef Q_WS_X11
// On X allow choice of rectangular modifier since most window
// managers grab alt + click for moving windows.
bool alt = event->modifiers() & modifierTranslated(sqt->rectangularSelectionModifier);
#else
bool alt = event->modifiers() & Qt::AltModifier;
#endif
sqt->ButtonDown(pos, time.elapsed(), shift, ctrl, alt);
}
}
void ScintillaEditBase::mouseReleaseEvent(QMouseEvent *event)
{
Point point = PointFromQPoint(event->pos());
bool ctrl = event->modifiers() & Qt::ControlModifier;
if (event->button() == Qt::LeftButton)
sqt->ButtonUp(point, time.elapsed(), ctrl);
int pos = send(SCI_POSITIONFROMPOINT, point.x, point.y);
int line = send(SCI_LINEFROMPOSITION, pos);
int modifiers = event->modifiers();
emit textAreaClicked(line, modifiers);
emit buttonReleased(event);
}
void ScintillaEditBase::mouseDoubleClickEvent(QMouseEvent *event)
{
// Scintilla does its own double-click detection.
mousePressEvent(event);
}
void ScintillaEditBase::mouseMoveEvent(QMouseEvent *event)
{
Point pos = PointFromQPoint(event->pos());
sqt->ButtonMove(pos);
}
void ScintillaEditBase::contextMenuEvent(QContextMenuEvent *event)
{
Point pos = PointFromQPoint(event->globalPos());
Point pt = PointFromQPoint(event->pos());
if (!sqt->PointInSelection(pt))
sqt->SetEmptySelection(sqt->PositionFromLocation(pt));
sqt->ContextMenu(pos);
}
void ScintillaEditBase::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasText()) {
event->acceptProposedAction();
Point point = PointFromQPoint(event->pos());
sqt->DragEnter(point);
} else {
event->ignore();
}
}
void ScintillaEditBase::dragLeaveEvent(QDragLeaveEvent * /* event */)
{
sqt->DragLeave();
}
void ScintillaEditBase::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasText()) {
event->acceptProposedAction();
Point point = PointFromQPoint(event->pos());
sqt->DragMove(point);
} else {
event->ignore();
}
}
void ScintillaEditBase::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasText()) {
event->acceptProposedAction();
Point point = PointFromQPoint(event->pos());
bool move = (event->source() == this &&
event->proposedAction() == Qt::MoveAction);
sqt->Drop(point, event->mimeData(), move);
} else {
event->ignore();
}
}
void ScintillaEditBase::inputMethodEvent(QInputMethodEvent *event)
{
// Clear the current selection.
sqt->ClearSelection();
if (preeditPos >= 0)
sqt->SetSelection(preeditPos, preeditPos);
// Insert the commit string.
if (!event->commitString().isEmpty() || event->replacementLength()) {
// Select the text to be removed.
int commitPos = send(SCI_GETCURRENTPOS);
int start = commitPos + event->replacementStart();
int end = start + event->replacementLength();
sqt->SetSelection(start, end);
// Replace the selection with the commit string.
QByteArray commitBytes = sqt->BytesForDocument(event->commitString());
char *commitData = commitBytes.data();
sqt->AddCharUTF(commitData, strlen(commitData));
}
// Select the previous preedit string.
int pos = send(SCI_GETCURRENTPOS);
int length = sqt->BytesForDocument(preeditString).length();
sqt->SetSelection(pos, pos + length);
// Replace the selection with the new preedit string.
QByteArray bytes = sqt->BytesForDocument(event->preeditString());
char *data = bytes.data();
bool recording = sqt->recordingMacro;
sqt->recordingMacro = false;
send(SCI_SETUNDOCOLLECTION, false);
sqt->AddCharUTF(data, strlen(data));
send(SCI_SETUNDOCOLLECTION, true);
sqt->recordingMacro = recording;
sqt->SetSelection(pos, pos);
// Store the state of the current preedit string.
preeditString = event->preeditString();
preeditPos = !preeditString.isEmpty() ? send(SCI_GETCURRENTPOS) : -1;
if (!preeditString.isEmpty()) {
// Apply attributes to the preedit string.
int indicNum = 0;
sqt->ShowCaretAtCurrentPosition();
foreach (QInputMethodEvent::Attribute a, event->attributes()) {
QString prefix = preeditString.left(a.start);
QByteArray prefixBytes = sqt->BytesForDocument(prefix);
int prefixLength = prefixBytes.length();
int caretPos = preeditPos + prefixLength;
if (a.type == QInputMethodEvent::Cursor) {
sqt->SetSelection(caretPos, caretPos);
if (!a.length)
sqt->DropCaret();
} else if (a.type == QInputMethodEvent::TextFormat) {
Q_ASSERT(a.value.canConvert(QVariant::TextFormat));
QTextFormat format = a.value.value<QTextFormat>();
Q_ASSERT(format.isCharFormat());
QTextCharFormat charFormat = format.toCharFormat();
QString sub = preeditString.mid(a.start, a.length);
QByteArray subBytes = sqt->BytesForDocument(sub);
int subLength = subBytes.length();
if (charFormat.underlineStyle() != QTextCharFormat::NoUnderline) {
// Set temporary indicator for underline style.
QColor uc = charFormat.underlineColor();
int style = INDIC_PLAIN;
if (charFormat.underlineStyle() == QTextCharFormat::DashUnderline)
style = INDIC_DASH;
send(SCI_INDICSETSTYLE, INDIC_INPUTMETHOD + indicNum, style);
send(SCI_INDICSETFORE, INDIC_INPUTMETHOD + indicNum, uc.rgb());
send(SCI_SETINDICATORCURRENT, INDIC_INPUTMETHOD + indicNum);
send(SCI_INDICATORFILLRANGE, caretPos, subLength);
indicNum++;
}
}
}
}
}
QVariant ScintillaEditBase::inputMethodQuery(Qt::InputMethodQuery query) const
{
int pos = send(SCI_GETCURRENTPOS);
int line = send(SCI_LINEFROMPOSITION, pos);
switch (query) {
case Qt::ImMicroFocus:
{
int startPos = (preeditPos >= 0) ? preeditPos : pos;
Point pt = sqt->LocationFromPosition(startPos);
int width = send(SCI_GETCARETWIDTH);
int height = send(SCI_TEXTHEIGHT, line);
return QRect(pt.x, pt.y, width, height);
}
case Qt::ImFont:
{
char fontName[64];
int style = send(SCI_GETSTYLEAT, pos);
int len = send(SCI_STYLEGETFONT, style, (sptr_t)fontName);
int size = send(SCI_STYLEGETSIZE, style);
bool italic = send(SCI_STYLEGETITALIC, style);
int weight = send(SCI_STYLEGETBOLD, style) ? QFont::Bold : -1;
return QFont(QString::fromUtf8(fontName, len), size, weight, italic);
}
case Qt::ImCursorPosition:
{
int paraStart = sqt->pdoc->ParaUp(pos);
return pos - paraStart;
}
case Qt::ImSurroundingText:
{
int paraStart = sqt->pdoc->ParaUp(pos);
int paraEnd = sqt->pdoc->ParaDown(pos);
QVarLengthArray<char,1024> buffer(paraEnd - paraStart + 1);
Sci_CharacterRange charRange;
charRange.cpMin = paraStart;
charRange.cpMax = paraEnd;
Sci_TextRange textRange;
textRange.chrg = charRange;
textRange.lpstrText = buffer.data();
send(SCI_GETTEXTRANGE, 0, (sptr_t)&textRange);
return sqt->StringFromDocument(buffer.constData());
}
case Qt::ImCurrentSelection:
{
QVarLengthArray<char,1024> buffer(send(SCI_GETSELTEXT));
send(SCI_GETSELTEXT, 0, (sptr_t)buffer.data());
return sqt->StringFromDocument(buffer.constData());
}
default:
return QVariant();
}
}
void ScintillaEditBase::notifyParent(SCNotification scn)
{
emit notify(&scn);
switch (scn.nmhdr.code) {
case SCN_STYLENEEDED:
emit styleNeeded(scn.position);
break;
case SCN_CHARADDED:
emit charAdded(scn.ch);
break;
case SCN_SAVEPOINTREACHED:
emit savePointChanged(false);
break;
case SCN_SAVEPOINTLEFT:
emit savePointChanged(true);
break;
case SCN_MODIFYATTEMPTRO:
emit modifyAttemptReadOnly();
break;
case SCN_KEY:
emit key(scn.ch);
break;
case SCN_DOUBLECLICK:
emit doubleClick(scn.position, scn.line);
break;
case SCN_UPDATEUI:
emit updateUi();
break;
case SCN_MODIFIED:
{
bool added = scn.modificationType & SC_MOD_INSERTTEXT;
bool deleted = scn.modificationType & SC_MOD_DELETETEXT;
int length = send(SCI_GETTEXTLENGTH);
bool firstLineAdded = (added && length == 1) ||
(deleted && length == 0);
if (scn.linesAdded != 0) {
emit linesAdded(scn.linesAdded);
} else if (firstLineAdded) {
emit linesAdded(added ? 1 : -1);
}
const QByteArray bytes = QByteArray::fromRawData(scn.text, scn.length);
emit modified(scn.modificationType, scn.position, scn.length,
scn.linesAdded, bytes, scn.line,
scn.foldLevelNow, scn.foldLevelPrev);
break;
}
case SCN_MACRORECORD:
emit macroRecord(scn.message, scn.wParam, scn.lParam);
break;
case SCN_MARGINCLICK:
emit marginClicked(scn.position, scn.modifiers, scn.margin);
break;
case SCN_NEEDSHOWN:
emit needShown(scn.position, scn.length);
break;
case SCN_PAINTED:
emit painted();
break;
case SCN_USERLISTSELECTION:
emit userListSelection();
break;
case SCN_URIDROPPED:
emit uriDropped();
break;
case SCN_DWELLSTART:
emit dwellStart(scn.x, scn.y);
break;
case SCN_DWELLEND:
emit dwellEnd(scn.x, scn.y);
break;
case SCN_ZOOM:
emit zoom(send(SCI_GETZOOM));
break;
case SCN_HOTSPOTCLICK:
emit hotSpotClick(scn.position, scn.modifiers);
break;
case SCN_HOTSPOTDOUBLECLICK:
emit hotSpotDoubleClick(scn.position, scn.modifiers);
break;
case SCN_CALLTIPCLICK:
emit callTipClick();
break;
case SCN_AUTOCSELECTION:
emit autoCompleteSelection(scn.lParam, QString::fromUtf8(scn.text));
break;
case SCN_AUTOCCANCELLED:
emit autoCompleteCancelled();
break;
default:
return;
}
}
void ScintillaEditBase::event_command(uptr_t wParam, sptr_t lParam)
{
emit command(wParam, lParam);
}