Created
October 12, 2016 04:34
-
-
Save tsekityam/720723ec9a6ab9b0aa83d9b189a0bb98 to your computer and use it in GitHub Desktop.
Demo toggle line with text displayed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mainwindow.cpp | |
// Testing code for port of Scintilla to Qt. | |
// Copyright (c) 2011 Archaeopteryx Software, Inc. d/b/a Wingware | |
#include <QtGui> | |
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0) | |
#include <QFileDialog> | |
#endif | |
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include "SciLexer.h" | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
ui(new Ui::MainWindow) { | |
ui->setupUi(this); | |
ScintillaEditBase *sci = ui->blok; | |
const char *txtInit = | |
"int main(int argc, char **argv) {\n" | |
" // Start up the gnome\n" | |
" gnome_init(\"stest\", \"1.0\", argc, argv);\n}\n"; | |
const char *keywords = | |
"and and_eq asm auto bitand bitor bool break " | |
"case catch char class compl const const_cast continue " | |
"default delete do double dynamic_cast else enum explicit export extern false float for " | |
"friend goto if inline int long mutable namespace new not not_eq " | |
"operator or or_eq private protected public " | |
"register reinterpret_cast return short signed sizeof static static_cast struct switch " | |
"template this throw true try typedef typeid typename union unsigned using " | |
"virtual void volatile wchar_t while xor xor_eq"; | |
Call(SCI_STYLECLEARALL); | |
Call(SCI_SETMARGINWIDTHN, 0, 35); | |
Call(SCI_SETSCROLLWIDTH, 200, 0); | |
Call(SCI_SETSCROLLWIDTHTRACKING, 1, 0); | |
Call(SCI_SETLEXER, SCLEX_CPP, 0); | |
Call(SCI_SETSTYLEBITS, Call(SCI_GETSTYLEBITSNEEDED), 0); | |
Call(SCI_SETKEYWORDS, 0, (sptr_t)keywords); | |
Call(SCI_STYLESETFORE, SCE_C_COMMENT, 0x008000); | |
Call(SCI_STYLESETFORE, SCE_C_COMMENTLINE, 0x008000); | |
Call(SCI_STYLESETFORE, SCE_C_COMMENTDOC, 0x008040); | |
Call(SCI_STYLESETITALIC, SCE_C_COMMENTDOC, 1); | |
Call(SCI_STYLESETFORE, SCE_C_NUMBER, 0x808000); | |
Call(SCI_STYLESETFORE, SCE_C_WORD, 0x800000); | |
Call(SCI_STYLESETBOLD, SCE_C_WORD, 1); | |
Call(SCI_STYLESETFORE, SCE_C_STRING, 0x800080); | |
Call(SCI_STYLESETFORE, SCE_C_PREPROCESSOR, 0x008080); | |
Call(SCI_STYLESETBOLD, SCE_C_OPERATOR, 1); | |
Call(SCI_INSERTTEXT, 0, (sptr_t)(void *)txtInit); | |
Call(SCI_SETSEL, 8, 4); | |
Call(SCI_SETMULTIPLESELECTION, 1); | |
Call(SCI_SETVIRTUALSPACEOPTIONS, | |
SCVS_RECTANGULARSELECTION | SCVS_USERACCESSIBLE); | |
Call(SCI_SETADDITIONALSELECTIONTYPING, 1); | |
Call(SCI_STYLESETFORE, STYLE_INDENTGUIDE, 0x808080); | |
Call(SCI_SETINDENTATIONGUIDES, SC_IV_LOOKBOTH); | |
Call(SCI_SETVIEWWS, SCWS_VISIBLEALWAYS); | |
Call(SCI_SETFOLDMARGINCOLOUR, 1, 0xffD0D0); | |
Call(SCI_SETFOLDMARGINHICOLOUR, 1, 0xD0ffD0); | |
Call(SCI_SETMARGINTYPEN, 2, SC_MARGIN_SYMBOL); | |
Call(SCI_SETMARGINWIDTHN, 2, 18); | |
Call(SCI_SETMARGINMASKN, 2, SC_MASK_FOLDERS); | |
Call(SCI_SETMARGINSENSITIVEN, 2, 1); | |
Call(SCI_SETPROPERTY, (uptr_t)("fold"), (sptr_t)("1")); | |
DefineMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_BOXMINUS, 0xffffff, 0x808080); | |
DefineMarker(SC_MARKNUM_FOLDER, SC_MARK_BOXPLUS, 0xffffff, 0x808080); | |
DefineMarker(SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE, 0xffffff, 0x808080); | |
DefineMarker(SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNER, 0xffffff, 0x808080); | |
DefineMarker(SC_MARKNUM_FOLDEREND, SC_MARK_BOXPLUSCONNECTED, 0xffffff, 0x808080); | |
DefineMarker(SC_MARKNUM_FOLDEROPENMID, SC_MARK_BOXMINUSCONNECTED, 0xffffff, 0x808080); | |
DefineMarker(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNER, 0xffffff, 0x808080); | |
Call(SCI_GRABFOCUS); | |
ui->actionLatin_1->setChecked(true); | |
connect(sci, SIGNAL(notify(SCNotification *)), this, SLOT(receive_notify(SCNotification *))); | |
connect(sci, SIGNAL(command(uptr_t, sptr_t)), this, SLOT(receive_command(uptr_t, sptr_t))); | |
} | |
MainWindow::~MainWindow() { | |
delete ui; | |
} | |
void MainWindow::DefineMarker(int marker, int markerType, int fore, int back) { | |
Call(SCI_MARKERDEFINE, marker, markerType); | |
Call(SCI_MARKERSETFORE, marker, fore); | |
Call(SCI_MARKERSETBACK, marker, back); | |
} | |
sptr_t MainWindow::Call(unsigned int iMessage, uptr_t wParam, sptr_t lParam) { | |
return ui->blok->send(iMessage, wParam, lParam); | |
} | |
QByteArray MainWindow::GetRange(int start, int end) { | |
QByteArray bytes(end-start+1, 0); | |
Sci_TextRange tr; | |
tr.chrg.cpMin = start; | |
tr.chrg.cpMax = end; | |
tr.lpstrText = bytes.data(); | |
Call(SCI_GETTEXTRANGE, 0, (sptr_t)&tr); | |
return bytes; | |
} | |
void MainWindow::on_actionOpen_triggered() { | |
QFileDialog *dlgOpen = new QFileDialog(this, "Open", ".", ""); | |
if (dlgOpen->exec()) { | |
QStringList fileNames = dlgOpen->selectedFiles(); | |
for (int i = 0; i < fileNames.size(); ++i) { | |
Call(SCI_CLEARALL); | |
QFile file(fileNames.at(i)); | |
file.open(QIODevice::ReadOnly); | |
QByteArray contents = file.readAll(); | |
Call(SCI_ADDTEXT, contents.length(), reinterpret_cast<sptr_t>(contents.data())); | |
} | |
} | |
} | |
void MainWindow::on_actionLatin_1_triggered() { | |
Call(SCI_SETCODEPAGE, 0); | |
for (int i=0; i<127;i++) | |
Call(SCI_STYLESETCHARACTERSET, i, 0); | |
ui->actionUTF_8->setChecked(false); | |
ui->actionShift_JIS->setChecked(false); | |
} | |
void MainWindow::on_actionUTF_8_triggered() { | |
Call(SCI_SETCODEPAGE, SC_CP_UTF8); | |
for (int i=0; i<127;i++) | |
Call(SCI_STYLESETCHARACTERSET, i, 0); | |
ui->actionLatin_1->setChecked(false); | |
ui->actionShift_JIS->setChecked(false); | |
} | |
void MainWindow::on_actionShift_JIS_triggered() { | |
Call(SCI_SETCODEPAGE, 932); | |
for (int i=0; i<127;i++) | |
Call(SCI_STYLESETCHARACTERSET, i, SC_CHARSET_SHIFTJIS); | |
ui->actionLatin_1->setChecked(false); | |
ui->actionUTF_8->setChecked(false); | |
} | |
void MainWindow::on_actionFind_Selection_triggered() { | |
int selStart = Call(SCI_GETSELECTIONSTART); | |
int selEnd = Call(SCI_GETSELECTIONEND); | |
int lenDoc = Call(SCI_GETLENGTH); | |
const int indicatorHightlightCurrentWord = INDIC_CONTAINER; | |
Call(SCI_INDICSETSTYLE, indicatorHightlightCurrentWord, INDIC_ROUNDBOX); | |
Call(SCI_INDICSETFORE, indicatorHightlightCurrentWord, 0xff0000); | |
Call(SCI_SETINDICATORCURRENT, indicatorHightlightCurrentWord); | |
Call(SCI_INDICATORCLEARRANGE, 0, lenDoc); | |
Call(SCI_INDICATORFILLRANGE, selStart, selEnd - selStart); | |
QByteArray wordToFind = GetRange(selStart, selEnd); | |
Call(SCI_SETSEARCHFLAGS, 0); | |
Call(SCI_SETTARGETSTART, selEnd); | |
Call(SCI_SETTARGETEND, lenDoc); | |
int indexOf = Call(SCI_SEARCHINTARGET, | |
wordToFind.length()-1, (sptr_t)wordToFind.data()); | |
if (indexOf < 0) { // Wrap around | |
Call(SCI_SETTARGETSTART, 0); | |
Call(SCI_SETTARGETEND, selEnd); | |
indexOf = Call(SCI_SEARCHINTARGET, | |
wordToFind.length()-1, (sptr_t)wordToFind.data()); | |
} | |
if (indexOf >= 0) { | |
Call(SCI_SETSELECTIONSTART, indexOf); | |
Call(SCI_SETSELECTIONEND, indexOf+(selEnd - selStart)); | |
} | |
} | |
void MainWindow::on_actionWrap_triggered() { | |
Call(SCI_SETWRAPMODE, Call(SCI_GETWRAPMODE) ? SC_WRAP_NONE : SC_WRAP_WORD); | |
} | |
void MainWindow::receive_notify(SCNotification *pscn) { | |
Q_UNUSED(pscn) | |
switch (pscn->nmhdr.code) | |
{ | |
case SCN_MARGINCLICK: | |
{ | |
if (pscn->margin == 2) | |
{ | |
int line = Call(SCI_LINEFROMPOSITION, pscn->position); | |
Call(SCI_TOGGLEFOLDSHOWTEXT, line, (sptr_t)"<fold>...</fold>"); | |
} | |
break; | |
}; | |
} | |
} | |
void MainWindow::receive_command(uptr_t wParam, sptr_t lParam) { | |
Q_UNUSED(wParam) | |
Q_UNUSED(lParam) | |
/* | |
int notifyCode = wParam >> 16; | |
if ((notifyCode == SCEN_SETFOCUS) || (notifyCode == SCEN_KILLFOCUS)){ | |
Call(SCI_SETINDICATORCURRENT, INDIC_CONTAINER); | |
Call(SCI_INDICATORCLEARRANGE, 0, Call(SCI_GETLENGTH)); | |
if (notifyCode == SCEN_SETFOCUS) | |
Call(SCI_INDICATORFILLRANGE, 0, 2); | |
} | |
*/ | |
} | |
void MainWindow::on_actionExit_triggered() { | |
QApplication::quit(); | |
} | |
/* XPM */ | |
static const char * arrow_xpm[] = { | |
"12 12 3 1", | |
" c None", | |
". c #000000", | |
"+ c #808080", | |
" ", | |
" .+ ", | |
" .+ ", | |
" +.+ ", | |
" ........+ ", | |
" .........+ ", | |
" .........+ ", | |
" ........+ ", | |
" +.+ ", | |
" .+ ", | |
" .+ ", | |
" "}; | |
/* XPM */ | |
static const char * box_xpm[] = { | |
"12 12 2 1", | |
" c None", | |
". c #000000", | |
" .........", | |
" . . ..", | |
" . . . .", | |
"......... .", | |
". . . .", | |
". . . ..", | |
". . .. .", | |
"......... .", | |
". . . .", | |
". . . . ", | |
". . .. ", | |
"......... "}; | |
void MainWindow::on_actionAutocompletion_triggered() { | |
const char *words = "Babylon-5?1 Battlestar-Galactica Millenium-Falcon?2 Moya?2 Serenity Voyager"; | |
Call(SCI_AUTOCSETIGNORECASE, 1); | |
Call(SCI_REGISTERIMAGE, 1, (sptr_t)arrow_xpm); | |
Call(SCI_REGISTERIMAGE, 2, (sptr_t)box_xpm); | |
Call(SCI_AUTOCSHOW, 0, (sptr_t)words); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment