qsciscintilla.cpp 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479
  1. // This module implements the "official" high-level API of the Qt port of
  2. // Scintilla. It is modelled on QTextEdit - a method of the same name should
  3. // behave in the same way.
  4. //
  5. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  6. //
  7. // This file is part of QScintilla.
  8. //
  9. // This file may be used under the terms of the GNU General Public License
  10. // version 3.0 as published by the Free Software Foundation and appearing in
  11. // the file LICENSE included in the packaging of this file. Please review the
  12. // following information to ensure the GNU General Public License version 3.0
  13. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  14. //
  15. // If you do not wish to use this file under the terms of the GPL version 3.0
  16. // then you may purchase a commercial license. For more information contact
  17. // info@riverbankcomputing.com.
  18. //
  19. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  20. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21. #include "Qsci/qsciscintilla.h"
  22. #include <string.h>
  23. #include <QAction>
  24. #include <QApplication>
  25. #include <QColor>
  26. #include <QEvent>
  27. #include <QImage>
  28. #include <QIODevice>
  29. #include <QKeyEvent>
  30. #include <QKeySequence>
  31. #include <QMenu>
  32. #include <QPoint>
  33. #include "Qsci/qsciabstractapis.h"
  34. #include "Qsci/qscicommandset.h"
  35. #include "Qsci/qscilexer.h"
  36. #include "Qsci/qscistyle.h"
  37. #include "Qsci/qscistyledtext.h"
  38. // Make sure these match the values in Scintilla.h. We don't #include that
  39. // file because it just causes more clashes.
  40. #define KEYWORDSET_MAX 8
  41. #define MARKER_MAX 31
  42. // The list separators for auto-completion and user lists.
  43. const char acSeparator = '\x03';
  44. const char userSeparator = '\x04';
  45. // The default fold margin width.
  46. static const int defaultFoldMarginWidth = 14;
  47. // The default set of characters that make up a word.
  48. static const char *defaultWordChars = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  49. // Forward declarations.
  50. static QColor asQColor(long sci_colour);
  51. // The ctor.
  52. QsciScintilla::QsciScintilla(QWidget *parent)
  53. : QsciScintillaBase(parent),
  54. allocatedMarkers(0), allocatedIndicators(7), oldPos(-1), selText(false),
  55. fold(FoldStyle::NoFoldStyle), foldmargin(2), autoInd(false),
  56. braceMode(BraceMatch::NoBraceMatch), acSource(AutoCompletionSource::AcsNone), acThresh(-1),
  57. wchars(defaultWordChars), call_tips_position(CallTipsPosition::CallTipsBelowText),
  58. call_tips_style(CallTipsStyle::CallTipsNoContext), maxCallTips(-1),
  59. use_single(AcusNever), explicit_fillups(""), fillups_enabled(false)
  60. {
  61. connect(this,SIGNAL(SCN_MODIFYATTEMPTRO()),
  62. SIGNAL(modificationAttempted()));
  63. connect(this,SIGNAL(SCN_MODIFIED(int,int,const char *,int,int,int,int,int,int,int)),
  64. SLOT(handleModified(int,int,const char *,int,int,int,int,int,int,int)));
  65. connect(this,SIGNAL(SCN_CALLTIPCLICK(int)),
  66. SLOT(handleCallTipClick(int)));
  67. connect(this,SIGNAL(SCN_CHARADDED(int)),
  68. SLOT(handleCharAdded(int)));
  69. connect(this,SIGNAL(SCN_INDICATORCLICK(int,int)),
  70. SLOT(handleIndicatorClick(int,int)));
  71. connect(this,SIGNAL(SCN_INDICATORRELEASE(int,int)),
  72. SLOT(handleIndicatorRelease(int,int)));
  73. connect(this,SIGNAL(SCN_MARGINCLICK(int,int,int)),
  74. SLOT(handleMarginClick(int,int,int)));
  75. connect(this,SIGNAL(SCN_MARGINRIGHTCLICK(int,int,int)),
  76. SLOT(handleMarginRightClick(int,int,int)));
  77. connect(this,SIGNAL(SCN_SAVEPOINTREACHED()),
  78. SLOT(handleSavePointReached()));
  79. connect(this,SIGNAL(SCN_SAVEPOINTLEFT()),
  80. SLOT(handleSavePointLeft()));
  81. connect(this,SIGNAL(SCN_UPDATEUI(int)),
  82. SLOT(handleUpdateUI(int)));
  83. connect(this,SIGNAL(QSCN_SELCHANGED(bool)),
  84. SLOT(handleSelectionChanged(bool)));
  85. connect(this,SIGNAL(SCN_AUTOCSELECTION(const char *,int)),
  86. SLOT(handleAutoCompletionSelection()));
  87. connect(this,SIGNAL(SCN_USERLISTSELECTION(const char *,int)),
  88. SLOT(handleUserListSelection(const char *,int)));
  89. // Set the default font.
  90. setFont(QApplication::font());
  91. // Set the default fore and background colours.
  92. QPalette pal = QApplication::palette();
  93. setColor(pal.text().color());
  94. setPaper(pal.base().color());
  95. setSelectionForegroundColor(pal.highlightedText().color());
  96. setSelectionBackgroundColor(pal.highlight().color());
  97. #if defined(Q_OS_WIN)
  98. setEolMode(EolMode::EolWindows);
  99. #else
  100. // Note that EolMac is pre-OS/X.
  101. setEolMode(EolMode::EolUnix);
  102. #endif
  103. // Capturing the mouse seems to cause problems on multi-head systems. Qt
  104. // should do the right thing anyway.
  105. SendScintilla(SCI_SETMOUSEDOWNCAPTURES, 0UL);
  106. setMatchedBraceForegroundColor(Qt::blue);
  107. setUnmatchedBraceForegroundColor(Qt::red);
  108. setAnnotationDisplay(AnnotationStandard);
  109. setLexer();
  110. // Set the visible policy. These are the same as SciTE's defaults
  111. // which, presumably, are sensible.
  112. SendScintilla(SCI_SETVISIBLEPOLICY, VISIBLE_STRICT | VISIBLE_SLOP, 4);
  113. // The default behaviour is unexpected.
  114. SendScintilla(SCI_AUTOCSETCASEINSENSITIVEBEHAVIOUR,
  115. SC_CASEINSENSITIVEBEHAVIOUR_IGNORECASE);
  116. // Create the standard command set.
  117. stdCmds = new QsciCommandSet(this);
  118. doc.display(this,0);
  119. }
  120. // The dtor.
  121. QsciScintilla::~QsciScintilla()
  122. {
  123. // Detach any current lexer.
  124. detachLexer();
  125. doc.undisplay(this);
  126. delete stdCmds;
  127. }
  128. // Return the current text colour.
  129. QColor QsciScintilla::color() const
  130. {
  131. return nl_text_colour;
  132. }
  133. // Set the text colour.
  134. void QsciScintilla::setColor(const QColor &c)
  135. {
  136. if (lex.isNull())
  137. {
  138. // Assume style 0 applies to everything so that we don't need to use
  139. // SCI_STYLECLEARALL which clears everything.
  140. SendScintilla(SCI_STYLESETFORE, 0, c);
  141. nl_text_colour = c;
  142. }
  143. }
  144. // Return the overwrite mode.
  145. bool QsciScintilla::overwriteMode() const
  146. {
  147. return SendScintilla(SCI_GETOVERTYPE);
  148. }
  149. // Set the overwrite mode.
  150. void QsciScintilla::setOverwriteMode(bool overwrite)
  151. {
  152. SendScintilla(SCI_SETOVERTYPE, overwrite);
  153. }
  154. // Return the current paper colour.
  155. QColor QsciScintilla::paper() const
  156. {
  157. return nl_paper_colour;
  158. }
  159. // Set the paper colour.
  160. void QsciScintilla::setPaper(const QColor &c)
  161. {
  162. if (lex.isNull())
  163. {
  164. // Assume style 0 applies to everything so that we don't need to use
  165. // SCI_STYLECLEARALL which clears everything. We still have to set the
  166. // default style as well for the background without any text.
  167. SendScintilla(SCI_STYLESETBACK, 0, c);
  168. SendScintilla(SCI_STYLESETBACK, STYLE_DEFAULT, c);
  169. nl_paper_colour = c;
  170. }
  171. }
  172. // Set the default font.
  173. void QsciScintilla::setFont(const QFont &f)
  174. {
  175. if (lex.isNull())
  176. {
  177. // Assume style 0 applies to everything so that we don't need to use
  178. // SCI_STYLECLEARALL which clears everything.
  179. setStylesFont(f, 0);
  180. QWidget::setFont(f);
  181. }
  182. }
  183. // Enable/disable auto-indent.
  184. void QsciScintilla::setAutoIndent(bool autoindent)
  185. {
  186. autoInd = autoindent;
  187. }
  188. // Set the brace matching mode.
  189. void QsciScintilla::setBraceMatching(BraceMatch bm)
  190. {
  191. braceMode = bm;
  192. }
  193. // Handle the addition of a character.
  194. void QsciScintilla::handleCharAdded(int ch)
  195. {
  196. // Ignore if there is a selection.
  197. long pos = SendScintilla(SCI_GETSELECTIONSTART);
  198. if (pos != SendScintilla(SCI_GETSELECTIONEND) || pos == 0)
  199. return;
  200. // If auto-completion is already active then see if this character is a
  201. // start character. If it is then create a new list which will be a subset
  202. // of the current one. The case where it isn't a start character seems to
  203. // be handled correctly elsewhere.
  204. if (isListActive() && isStartChar(ch))
  205. {
  206. cancelList();
  207. startAutoCompletion(acSource, false, use_single == AcusAlways);
  208. return;
  209. }
  210. // Handle call tips.
  211. if (call_tips_style != CallTipsStyle::CallTipsNone && !lex.isNull() && strchr("(),", ch) != NULL)
  212. callTip();
  213. // Handle auto-indentation.
  214. if (autoInd)
  215. if (lex.isNull() || (lex->autoIndentStyle() & AiMaintain))
  216. maintainIndentation(ch, pos);
  217. else
  218. autoIndentation(ch, pos);
  219. // See if we might want to start auto-completion.
  220. if (!isCallTipActive() && acSource != AutoCompletionSource::AcsNone)
  221. if (isStartChar(ch))
  222. startAutoCompletion(acSource, false, use_single == AcusAlways);
  223. else if (acThresh >= 1 && isWordCharacter(ch))
  224. startAutoCompletion(acSource, true, use_single == AcusAlways);
  225. }
  226. // See if a call tip is active.
  227. bool QsciScintilla::isCallTipActive() const
  228. {
  229. return SendScintilla(SCI_CALLTIPACTIVE);
  230. }
  231. // Handle a possible change to any current call tip.
  232. void QsciScintilla::callTip()
  233. {
  234. QsciAbstractAPIs *apis = lex->apis();
  235. if (!apis)
  236. return;
  237. int pos, commas = 0;
  238. bool found = false;
  239. char ch;
  240. pos = SendScintilla(SCI_GETCURRENTPOS);
  241. // Move backwards through the line looking for the start of the current
  242. // call tip and working out which argument it is.
  243. while ((ch = getCharacter(pos)) != '\0')
  244. {
  245. if (ch == ',')
  246. ++commas;
  247. else if (ch == ')')
  248. {
  249. int depth = 1;
  250. // Ignore everything back to the start of the corresponding
  251. // parenthesis.
  252. while ((ch = getCharacter(pos)) != '\0')
  253. {
  254. if (ch == ')')
  255. ++depth;
  256. else if (ch == '(' && --depth == 0)
  257. break;
  258. }
  259. }
  260. else if (ch == '(')
  261. {
  262. found = true;
  263. break;
  264. }
  265. }
  266. // Cancel any existing call tip.
  267. SendScintilla(SCI_CALLTIPCANCEL);
  268. // Done if there is no new call tip to set.
  269. if (!found)
  270. return;
  271. QStringList context = apiContext(pos, pos, ctPos);
  272. if (context.isEmpty())
  273. return;
  274. // The last word is complete, not partial.
  275. context << QString();
  276. ct_cursor = 0;
  277. ct_shifts.clear();
  278. ct_entries = apis->callTips(context, commas, call_tips_style, ct_shifts);
  279. int nr_entries = ct_entries.count();
  280. if (nr_entries == 0)
  281. return;
  282. if (maxCallTips > 0 && maxCallTips < nr_entries)
  283. {
  284. ct_entries = ct_entries.mid(0, maxCallTips);
  285. nr_entries = maxCallTips;
  286. }
  287. int shift;
  288. QString ct;
  289. int nr_shifts = ct_shifts.count();
  290. if (maxCallTips < 0 && nr_entries > 1)
  291. {
  292. shift = (nr_shifts > 0 ? ct_shifts.first() : 0);
  293. ct = ct_entries[0];
  294. ct.prepend('\002');
  295. }
  296. else
  297. {
  298. if (nr_shifts > nr_entries)
  299. nr_shifts = nr_entries;
  300. // Find the biggest shift.
  301. shift = 0;
  302. for (int i = 0; i < nr_shifts; ++i)
  303. {
  304. int sh = ct_shifts[i];
  305. if (shift < sh)
  306. shift = sh;
  307. }
  308. ct = ct_entries.join("\n");
  309. }
  310. QByteArray ct_ba = ct.toLatin1();
  311. const char *cts = ct_ba.data();
  312. SendScintilla(SCI_CALLTIPSHOW, adjustedCallTipPosition(shift), cts);
  313. // Done if there is more than one call tip.
  314. if (nr_entries > 1)
  315. return;
  316. // Highlight the current argument.
  317. const char *astart;
  318. if (commas == 0)
  319. astart = strchr(cts, '(');
  320. else
  321. for (astart = strchr(cts, ','); astart && --commas > 0; astart = strchr(astart + 1, ','))
  322. ;
  323. if (!astart || !*++astart)
  324. return;
  325. // The end is at the next comma or unmatched closing parenthesis.
  326. const char *aend;
  327. int depth = 0;
  328. for (aend = astart; *aend; ++aend)
  329. {
  330. char ch = *aend;
  331. if (ch == ',' && depth == 0)
  332. break;
  333. else if (ch == '(')
  334. ++depth;
  335. else if (ch == ')')
  336. {
  337. if (depth == 0)
  338. break;
  339. --depth;
  340. }
  341. }
  342. if (astart != aend)
  343. SendScintilla(SCI_CALLTIPSETHLT, astart - cts, aend - cts);
  344. }
  345. // Handle a call tip click.
  346. void QsciScintilla::handleCallTipClick(int dir)
  347. {
  348. int nr_entries = ct_entries.count();
  349. // Move the cursor while bounds checking.
  350. if (dir == 1)
  351. {
  352. if (ct_cursor - 1 < 0)
  353. return;
  354. --ct_cursor;
  355. }
  356. else if (dir == 2)
  357. {
  358. if (ct_cursor + 1 >= nr_entries)
  359. return;
  360. ++ct_cursor;
  361. }
  362. else
  363. return;
  364. int shift = (ct_shifts.count() > ct_cursor ? ct_shifts[ct_cursor] : 0);
  365. QString ct = ct_entries[ct_cursor];
  366. // Add the arrows.
  367. if (ct_cursor < nr_entries - 1)
  368. ct.prepend('\002');
  369. if (ct_cursor > 0)
  370. ct.prepend('\001');
  371. SendScintilla(SCI_CALLTIPSHOW, adjustedCallTipPosition(shift), ct.toLatin1().data());
  372. }
  373. // Shift the position of the call tip (to take any context into account) but
  374. // don't go before the start of the line.
  375. int QsciScintilla::adjustedCallTipPosition(int ctshift) const
  376. {
  377. int ct = ctPos;
  378. if (ctshift)
  379. {
  380. int ctmin = SendScintilla(SCI_POSITIONFROMLINE, SendScintilla(SCI_LINEFROMPOSITION, ct));
  381. if (ct - ctshift < ctmin)
  382. ct = ctmin;
  383. }
  384. return ct;
  385. }
  386. // Return the list of words that make up the context preceding the given
  387. // position. The list will only have more than one element if there is a lexer
  388. // set and it defines start strings. If so, then the last element might be
  389. // empty if a start string has just been typed. On return pos is at the start
  390. // of the context.
  391. QStringList QsciScintilla::apiContext(int pos, int &context_start,
  392. int &last_word_start)
  393. {
  394. enum {
  395. Either,
  396. Separator,
  397. Word
  398. };
  399. QStringList words;
  400. int good_pos = pos, expecting = Either;
  401. last_word_start = -1;
  402. while (pos > 0)
  403. {
  404. if (getSeparator(pos))
  405. {
  406. if (expecting == Either)
  407. words.prepend(QString());
  408. else if (expecting == Word)
  409. break;
  410. good_pos = pos;
  411. expecting = Word;
  412. }
  413. else
  414. {
  415. QString word = getWord(pos);
  416. if (word.isEmpty() || expecting == Separator)
  417. break;
  418. words.prepend(word);
  419. good_pos = pos;
  420. expecting = Separator;
  421. // Return the position of the start of the last word if required.
  422. if (last_word_start < 0)
  423. last_word_start = pos;
  424. }
  425. // Strip any preceding spaces (mainly around operators).
  426. char ch;
  427. while ((ch = getCharacter(pos)) != '\0')
  428. {
  429. // This is the same definition of space that Scintilla uses.
  430. if (ch != ' ' && (ch < 0x09 || ch > 0x0d))
  431. {
  432. ++pos;
  433. break;
  434. }
  435. }
  436. }
  437. // A valid sequence always starts with a word and so should be expecting a
  438. // separator.
  439. if (expecting != Separator)
  440. words.clear();
  441. context_start = good_pos;
  442. return words;
  443. }
  444. // Try and get a lexer's word separator from the text before the current
  445. // position. Return true if one was found and adjust the position accordingly.
  446. bool QsciScintilla::getSeparator(int &pos) const
  447. {
  448. int opos = pos;
  449. // Go through each separator.
  450. for (int i = 0; i < wseps.count(); ++i)
  451. {
  452. const QString &ws = wseps[i];
  453. // Work backwards.
  454. uint l;
  455. for (l = ws.length(); l; --l)
  456. {
  457. char ch = getCharacter(pos);
  458. if (ch == '\0' || ws.at(l - 1) != ch)
  459. break;
  460. }
  461. if (!l)
  462. return true;
  463. // Reset for the next separator.
  464. pos = opos;
  465. }
  466. return false;
  467. }
  468. // Try and get a word from the text before the current position. Return the
  469. // word if one was found and adjust the position accordingly.
  470. QString QsciScintilla::getWord(int &pos) const
  471. {
  472. QString word;
  473. bool numeric = true;
  474. char ch;
  475. while ((ch = getCharacter(pos)) != '\0')
  476. {
  477. if (!isWordCharacter(ch))
  478. {
  479. ++pos;
  480. break;
  481. }
  482. if (ch < '0' || ch > '9')
  483. numeric = false;
  484. word.prepend(ch);
  485. }
  486. // We don't auto-complete numbers.
  487. if (numeric)
  488. word.truncate(0);
  489. return word;
  490. }
  491. // Get the "next" character (ie. the one before the current position) in the
  492. // current line. The character will be '\0' if there are no more.
  493. char QsciScintilla::getCharacter(int &pos) const
  494. {
  495. if (pos <= 0)
  496. return '\0';
  497. char ch = SendScintilla(SCI_GETCHARAT, --pos);
  498. // Don't go past the end of the previous line.
  499. if (ch == '\n' || ch == '\r')
  500. {
  501. ++pos;
  502. return '\0';
  503. }
  504. return ch;
  505. }
  506. // See if a character is an auto-completion start character, ie. the last
  507. // character of a word separator.
  508. bool QsciScintilla::isStartChar(char ch) const
  509. {
  510. QString s = QChar(ch);
  511. for (int i = 0; i < wseps.count(); ++i)
  512. if (wseps[i].endsWith(s))
  513. return true;
  514. return false;
  515. }
  516. // Possibly start auto-completion.
  517. void QsciScintilla::startAutoCompletion(AutoCompletionSource acs,
  518. bool checkThresh, bool choose_single)
  519. {
  520. int start, ignore;
  521. QStringList context = apiContext(SendScintilla(SCI_GETCURRENTPOS), start,
  522. ignore);
  523. if (context.isEmpty())
  524. return;
  525. // Get the last word's raw data and length.
  526. ScintillaBytes s = textAsBytes(context.last());
  527. const char *last_data = ScintillaBytesConstData(s);
  528. int last_len = s.length();
  529. if (checkThresh && last_len < acThresh)
  530. return;
  531. // Generate the string representing the valid words to select from.
  532. QStringList wlist;
  533. if ((acs == AutoCompletionSource::AcsAll || acs == AutoCompletionSource::AcsAPIs) && !lex.isNull())
  534. {
  535. QsciAbstractAPIs *apis = lex->apis();
  536. if (apis)
  537. apis->updateAutoCompletionList(context, wlist);
  538. }
  539. if (acs == AutoCompletionSource::AcsAll || acs == AutoCompletionSource::AcsDocument)
  540. {
  541. int sflags = SCFIND_WORDSTART;
  542. if (!SendScintilla(SCI_AUTOCGETIGNORECASE))
  543. sflags |= SCFIND_MATCHCASE;
  544. SendScintilla(SCI_SETSEARCHFLAGS, sflags);
  545. int pos = 0;
  546. int dlen = SendScintilla(SCI_GETLENGTH);
  547. int caret = SendScintilla(SCI_GETCURRENTPOS);
  548. int clen = caret - start;
  549. char *orig_context = new char[clen + 1];
  550. SendScintilla(SCI_GETTEXTRANGE, start, caret, orig_context);
  551. for (;;)
  552. {
  553. int fstart;
  554. SendScintilla(SCI_SETTARGETSTART, pos);
  555. SendScintilla(SCI_SETTARGETEND, dlen);
  556. if ((fstart = SendScintilla(SCI_SEARCHINTARGET, clen, orig_context)) < 0)
  557. break;
  558. // Move past the root part.
  559. pos = fstart + clen;
  560. // Skip if this is the context we are auto-completing.
  561. if (pos == caret)
  562. continue;
  563. // Get the rest of this word.
  564. QString w = last_data;
  565. while (pos < dlen)
  566. {
  567. char ch = SendScintilla(SCI_GETCHARAT, pos);
  568. if (!isWordCharacter(ch))
  569. break;
  570. w += ch;
  571. ++pos;
  572. }
  573. // Add the word if it isn't already there.
  574. if (!w.isEmpty())
  575. {
  576. bool keep;
  577. // If there are APIs then check if the word is already present
  578. // as an API word (i.e. with a trailing space).
  579. if (acs == AutoCompletionSource::AcsAll)
  580. {
  581. QString api_w = w;
  582. api_w.append(' ');
  583. keep = !wlist.contains(api_w);
  584. }
  585. else
  586. {
  587. keep = true;
  588. }
  589. if (keep && !wlist.contains(w))
  590. wlist.append(w);
  591. }
  592. }
  593. delete []orig_context;
  594. }
  595. if (wlist.isEmpty())
  596. return;
  597. wlist.sort();
  598. SendScintilla(SCI_AUTOCSETCHOOSESINGLE, choose_single);
  599. SendScintilla(SCI_AUTOCSETSEPARATOR, acSeparator);
  600. ScintillaBytes wlist_s = textAsBytes(wlist.join(QChar(acSeparator)));
  601. SendScintilla(SCI_AUTOCSHOW, last_len, ScintillaBytesConstData(wlist_s));
  602. }
  603. // Maintain the indentation of the previous line.
  604. void QsciScintilla::maintainIndentation(char ch, long pos)
  605. {
  606. if (ch != '\r' && ch != '\n')
  607. return;
  608. int curr_line = SendScintilla(SCI_LINEFROMPOSITION, pos);
  609. // Get the indentation of the preceding non-zero length line.
  610. int ind = 0;
  611. for (int line = curr_line - 1; line >= 0; --line)
  612. {
  613. if (SendScintilla(SCI_GETLINEENDPOSITION, line) >
  614. SendScintilla(SCI_POSITIONFROMLINE, line))
  615. {
  616. ind = indentation(line);
  617. break;
  618. }
  619. }
  620. if (ind > 0)
  621. autoIndentLine(pos, curr_line, ind);
  622. }
  623. // Implement auto-indentation.
  624. void QsciScintilla::autoIndentation(char ch, long pos)
  625. {
  626. int curr_line = SendScintilla(SCI_LINEFROMPOSITION, pos);
  627. int ind_width = indentWidth();
  628. long curr_line_start = SendScintilla(SCI_POSITIONFROMLINE, curr_line);
  629. const char *block_start = lex->blockStart();
  630. bool start_single = (block_start && qstrlen(block_start) == 1);
  631. const char *block_end = lex->blockEnd();
  632. bool end_single = (block_end && qstrlen(block_end) == 1);
  633. if (end_single && block_end[0] == ch)
  634. {
  635. if (!(lex->autoIndentStyle() & AiClosing) && rangeIsWhitespace(curr_line_start, pos - 1))
  636. autoIndentLine(pos, curr_line, blockIndent(curr_line - 1) - ind_width);
  637. }
  638. else if (start_single && block_start[0] == ch)
  639. {
  640. // De-indent if we have already indented because the previous line was
  641. // a start of block keyword.
  642. if (!(lex->autoIndentStyle() & AiOpening) && curr_line > 0 && getIndentState(curr_line - 1) == isKeywordStart && rangeIsWhitespace(curr_line_start, pos - 1))
  643. autoIndentLine(pos, curr_line, blockIndent(curr_line - 1) - ind_width);
  644. }
  645. else if (ch == '\r' || ch == '\n')
  646. {
  647. // Don't auto-indent the line (ie. preserve its existing indentation)
  648. // if we have inserted a new line above by pressing return at the start
  649. // of this line - in other words, if the previous line is empty.
  650. long prev_line_length = SendScintilla(SCI_GETLINEENDPOSITION, curr_line - 1) - SendScintilla(SCI_POSITIONFROMLINE, curr_line - 1);
  651. if (prev_line_length != 0)
  652. autoIndentLine(pos, curr_line, blockIndent(curr_line - 1));
  653. }
  654. }
  655. // Set the indentation for a line.
  656. void QsciScintilla::autoIndentLine(long pos, int line, int indent)
  657. {
  658. if (indent < 0)
  659. return;
  660. long pos_before = SendScintilla(SCI_GETLINEINDENTPOSITION, line);
  661. SendScintilla(SCI_SETLINEINDENTATION, line, indent);
  662. long pos_after = SendScintilla(SCI_GETLINEINDENTPOSITION, line);
  663. long new_pos = -1;
  664. if (pos_after > pos_before)
  665. new_pos = pos + (pos_after - pos_before);
  666. else if (pos_after < pos_before && pos >= pos_after)
  667. if (pos >= pos_before)
  668. new_pos = pos + (pos_after - pos_before);
  669. else
  670. new_pos = pos_after;
  671. if (new_pos >= 0)
  672. SendScintilla(SCI_SETSEL, new_pos, new_pos);
  673. }
  674. // Return the indentation of the block defined by the given line (or something
  675. // significant before).
  676. int QsciScintilla::blockIndent(int line)
  677. {
  678. if (line < 0)
  679. return 0;
  680. // Handle the trvial case.
  681. if (!lex->blockStartKeyword() && !lex->blockStart() && !lex->blockEnd())
  682. return indentation(line);
  683. int line_limit = line - lex->blockLookback();
  684. if (line_limit < 0)
  685. line_limit = 0;
  686. for (int l = line; l >= line_limit; --l)
  687. {
  688. IndentState istate = getIndentState(l);
  689. if (istate != isNone)
  690. {
  691. int ind_width = indentWidth();
  692. int ind = indentation(l);
  693. if (istate == isBlockStart)
  694. {
  695. if (!(lex -> autoIndentStyle() & AiOpening))
  696. ind += ind_width;
  697. }
  698. else if (istate == isBlockEnd)
  699. {
  700. if (lex -> autoIndentStyle() & AiClosing)
  701. ind -= ind_width;
  702. if (ind < 0)
  703. ind = 0;
  704. }
  705. else if (line == l)
  706. ind += ind_width;
  707. return ind;
  708. }
  709. }
  710. return indentation(line);
  711. }
  712. // Return true if all characters starting at spos up to, but not including
  713. // epos, are spaces or tabs.
  714. bool QsciScintilla::rangeIsWhitespace(long spos, long epos)
  715. {
  716. while (spos < epos)
  717. {
  718. char ch = SendScintilla(SCI_GETCHARAT, spos);
  719. if (ch != ' ' && ch != '\t')
  720. return false;
  721. ++spos;
  722. }
  723. return true;
  724. }
  725. // Returns the indentation state of a line.
  726. QsciScintilla::IndentState QsciScintilla::getIndentState(int line)
  727. {
  728. IndentState istate;
  729. // Get the styled text.
  730. long spos = SendScintilla(SCI_POSITIONFROMLINE, line);
  731. long epos = SendScintilla(SCI_POSITIONFROMLINE, line + 1);
  732. char *text = new char[(epos - spos + 1) * 2];
  733. SendScintilla(SCI_GETSTYLEDTEXT, spos, epos, text);
  734. int style, bstart_off, bend_off;
  735. // Block start/end takes precedence over keywords.
  736. const char *bstart_words = lex->blockStart(&style);
  737. bstart_off = findStyledWord(text, style, bstart_words);
  738. const char *bend_words = lex->blockEnd(&style);
  739. bend_off = findStyledWord(text, style, bend_words);
  740. // If there is a block start but no block end characters then ignore it
  741. // unless the block start is the last significant thing on the line, ie.
  742. // assume Python-like blocking.
  743. if (bstart_off >= 0 && !bend_words)
  744. for (int i = bstart_off * 2; text[i] != '\0'; i += 2)
  745. if (!QChar(text[i]).isSpace())
  746. return isNone;
  747. if (bstart_off > bend_off)
  748. istate = isBlockStart;
  749. else if (bend_off > bstart_off)
  750. istate = isBlockEnd;
  751. else
  752. {
  753. const char *words = lex->blockStartKeyword(&style);
  754. istate = (findStyledWord(text,style,words) >= 0) ? isKeywordStart : isNone;
  755. }
  756. delete[] text;
  757. return istate;
  758. }
  759. // text is a pointer to some styled text (ie. a character byte followed by a
  760. // style byte). style is a style number. words is a space separated list of
  761. // words. Returns the position in the text immediately after the last one of
  762. // the words with the style. The reason we are after the last, and not the
  763. // first, occurance is that we are looking for words that start and end a block
  764. // where the latest one is the most significant.
  765. int QsciScintilla::findStyledWord(const char *text, int style,
  766. const char *words)
  767. {
  768. if (!words)
  769. return -1;
  770. // Find the range of text with the style we are looking for.
  771. const char *stext;
  772. for (stext = text; stext[1] != style; stext += 2)
  773. if (stext[0] == '\0')
  774. return -1;
  775. // Move to the last character.
  776. const char *etext = stext;
  777. while (etext[2] != '\0')
  778. etext += 2;
  779. // Backtrack until we find the style. There will be one.
  780. while (etext[1] != style)
  781. etext -= 2;
  782. // Look for each word in turn.
  783. while (words[0] != '\0')
  784. {
  785. // Find the end of the word.
  786. const char *eword = words;
  787. while (eword[1] != ' ' && eword[1] != '\0')
  788. ++eword;
  789. // Now search the text backwards.
  790. const char *wp = eword;
  791. for (const char *tp = etext; tp >= stext; tp -= 2)
  792. {
  793. if (tp[0] != wp[0] || tp[1] != style)
  794. {
  795. // Reset the search.
  796. wp = eword;
  797. continue;
  798. }
  799. // See if all the word has matched.
  800. if (wp-- == words)
  801. return ((tp - text) / 2) + (eword - words) + 1;
  802. }
  803. // Move to the start of the next word if there is one.
  804. words = eword + 1;
  805. if (words[0] == ' ')
  806. ++words;
  807. }
  808. return -1;
  809. }
  810. // Return true if the code page is UTF8.
  811. bool QsciScintilla::isUtf8() const
  812. {
  813. return (SendScintilla(SCI_GETCODEPAGE) == SC_CP_UTF8);
  814. }
  815. // Set the code page.
  816. void QsciScintilla::setUtf8(bool cp)
  817. {
  818. SendScintilla(SCI_SETCODEPAGE, (cp ? SC_CP_UTF8 : 0));
  819. }
  820. // Return the end-of-line mode.
  821. QsciScintilla::EolMode QsciScintilla::eolMode() const
  822. {
  823. return (QsciScintilla::EolMode)SendScintilla(SCI_GETEOLMODE);
  824. }
  825. // Set the end-of-line mode.
  826. void QsciScintilla::setEolMode(QsciScintilla::EolMode mode)
  827. {
  828. SendScintilla(SCI_SETEOLMODE, mode);
  829. }
  830. // Convert the end-of-lines to a particular mode.
  831. void QsciScintilla::convertEols(EolMode mode)
  832. {
  833. SendScintilla(SCI_CONVERTEOLS, mode);
  834. }
  835. // Add an edge column.
  836. void QsciScintilla::addEdgeColumn(int colnr, const QColor &col)
  837. {
  838. SendScintilla(SCI_MULTIEDGEADDLINE, colnr, col);
  839. }
  840. // Clear all multi-edge columns.
  841. void QsciScintilla::clearEdgeColumns()
  842. {
  843. SendScintilla(SCI_MULTIEDGECLEARALL);
  844. }
  845. // Return the edge colour.
  846. QColor QsciScintilla::edgeColor() const
  847. {
  848. return asQColor(SendScintilla(SCI_GETEDGECOLOUR));
  849. }
  850. // Set the edge colour.
  851. void QsciScintilla::setEdgeColor(const QColor &col)
  852. {
  853. SendScintilla(SCI_SETEDGECOLOUR, col);
  854. }
  855. // Return the edge column.
  856. int QsciScintilla::edgeColumn() const
  857. {
  858. return SendScintilla(SCI_GETEDGECOLUMN);
  859. }
  860. // Set the edge column.
  861. void QsciScintilla::setEdgeColumn(int colnr)
  862. {
  863. SendScintilla(SCI_SETEDGECOLUMN, colnr);
  864. }
  865. // Return the edge mode.
  866. QsciScintilla::EdgeMode QsciScintilla::edgeMode() const
  867. {
  868. return (EdgeMode)SendScintilla(SCI_GETEDGEMODE);
  869. }
  870. // Set the edge mode.
  871. void QsciScintilla::setEdgeMode(EdgeMode mode)
  872. {
  873. SendScintilla(SCI_SETEDGEMODE, mode);
  874. }
  875. // Return the end-of-line visibility.
  876. bool QsciScintilla::eolVisibility() const
  877. {
  878. return SendScintilla(SCI_GETVIEWEOL);
  879. }
  880. // Set the end-of-line visibility.
  881. void QsciScintilla::setEolVisibility(bool visible)
  882. {
  883. SendScintilla(SCI_SETVIEWEOL, visible);
  884. }
  885. // Return the extra ascent.
  886. int QsciScintilla::extraAscent() const
  887. {
  888. return SendScintilla(SCI_GETEXTRAASCENT);
  889. }
  890. // Set the extra ascent.
  891. void QsciScintilla::setExtraAscent(int extra)
  892. {
  893. SendScintilla(SCI_SETEXTRAASCENT, extra);
  894. }
  895. // Return the extra descent.
  896. int QsciScintilla::extraDescent() const
  897. {
  898. return SendScintilla(SCI_GETEXTRADESCENT);
  899. }
  900. // Set the extra descent.
  901. void QsciScintilla::setExtraDescent(int extra)
  902. {
  903. SendScintilla(SCI_SETEXTRADESCENT, extra);
  904. }
  905. // Return the whitespace size.
  906. int QsciScintilla::whitespaceSize() const
  907. {
  908. return SendScintilla(SCI_GETWHITESPACESIZE);
  909. }
  910. // Set the whitespace size.
  911. void QsciScintilla::setWhitespaceSize(int size)
  912. {
  913. SendScintilla(SCI_SETWHITESPACESIZE, size);
  914. }
  915. // Set the whitespace background colour.
  916. void QsciScintilla::setWhitespaceBackgroundColor(const QColor &col)
  917. {
  918. SendScintilla(SCI_SETWHITESPACEBACK, col.isValid(), col);
  919. }
  920. // Set the whitespace foreground colour.
  921. void QsciScintilla::setWhitespaceForegroundColor(const QColor &col)
  922. {
  923. SendScintilla(SCI_SETWHITESPACEFORE, col.isValid(), col);
  924. }
  925. // Return the whitespace visibility.
  926. QsciScintilla::WhitespaceVisibility QsciScintilla::whitespaceVisibility() const
  927. {
  928. return (WhitespaceVisibility)SendScintilla(SCI_GETVIEWWS);
  929. }
  930. // Set the whitespace visibility.
  931. void QsciScintilla::setWhitespaceVisibility(WhitespaceVisibility mode)
  932. {
  933. SendScintilla(SCI_SETVIEWWS, mode);
  934. }
  935. // Return the tab draw mode.
  936. QsciScintilla::TabDrawMode QsciScintilla::tabDrawMode() const
  937. {
  938. return (TabDrawMode)SendScintilla(SCI_GETTABDRAWMODE);
  939. }
  940. // Set the tab draw mode.
  941. void QsciScintilla::setTabDrawMode(TabDrawMode mode)
  942. {
  943. SendScintilla(SCI_SETTABDRAWMODE, mode);
  944. }
  945. // Return the line wrap mode.
  946. QsciScintilla::WrapMode QsciScintilla::wrapMode() const
  947. {
  948. return (WrapMode)SendScintilla(SCI_GETWRAPMODE);
  949. }
  950. // Set the line wrap mode.
  951. void QsciScintilla::setWrapMode(WrapMode mode)
  952. {
  953. SendScintilla(SCI_SETLAYOUTCACHE,
  954. (mode == WrapNone ? SC_CACHE_CARET : SC_CACHE_DOCUMENT));
  955. SendScintilla(SCI_SETWRAPMODE, mode);
  956. }
  957. // Return the line wrap indent mode.
  958. QsciScintilla::WrapIndentMode QsciScintilla::wrapIndentMode() const
  959. {
  960. return (WrapIndentMode)SendScintilla(SCI_GETWRAPINDENTMODE);
  961. }
  962. // Set the line wrap indent mode.
  963. void QsciScintilla::setWrapIndentMode(WrapIndentMode mode)
  964. {
  965. SendScintilla(SCI_SETWRAPINDENTMODE, mode);
  966. }
  967. // Set the line wrap visual flags.
  968. void QsciScintilla::setWrapVisualFlags(WrapVisualFlag endFlag,
  969. WrapVisualFlag startFlag, int indent)
  970. {
  971. int flags = SC_WRAPVISUALFLAG_NONE;
  972. int loc = SC_WRAPVISUALFLAGLOC_DEFAULT;
  973. switch (endFlag)
  974. {
  975. case WrapFlagByText:
  976. flags |= SC_WRAPVISUALFLAG_END;
  977. loc |= SC_WRAPVISUALFLAGLOC_END_BY_TEXT;
  978. break;
  979. case WrapFlagByBorder:
  980. flags |= SC_WRAPVISUALFLAG_END;
  981. break;
  982. case WrapFlagInMargin:
  983. flags |= SC_WRAPVISUALFLAG_MARGIN;
  984. break;
  985. }
  986. switch (startFlag)
  987. {
  988. case WrapFlagByText:
  989. flags |= SC_WRAPVISUALFLAG_START;
  990. loc |= SC_WRAPVISUALFLAGLOC_START_BY_TEXT;
  991. break;
  992. case WrapFlagByBorder:
  993. flags |= SC_WRAPVISUALFLAG_START;
  994. break;
  995. case WrapFlagInMargin:
  996. flags |= SC_WRAPVISUALFLAG_MARGIN;
  997. break;
  998. }
  999. SendScintilla(SCI_SETWRAPVISUALFLAGS, flags);
  1000. SendScintilla(SCI_SETWRAPVISUALFLAGSLOCATION, loc);
  1001. SendScintilla(SCI_SETWRAPSTARTINDENT, indent);
  1002. }
  1003. // Set the folding style.
  1004. void QsciScintilla::setFolding(FoldStyle folding, int margin)
  1005. {
  1006. fold = folding;
  1007. foldmargin = margin;
  1008. if (folding == FoldStyle::NoFoldStyle)
  1009. {
  1010. SendScintilla(SCI_SETMARGINWIDTHN, margin, 0L);
  1011. return;
  1012. }
  1013. int mask = SendScintilla(SCI_GETMODEVENTMASK);
  1014. SendScintilla(SCI_SETMODEVENTMASK, mask | SC_MOD_CHANGEFOLD);
  1015. SendScintilla(SCI_SETFOLDFLAGS, SC_FOLDFLAG_LINEAFTER_CONTRACTED);
  1016. SendScintilla(SCI_SETMARGINTYPEN, margin, (long)SC_MARGIN_SYMBOL);
  1017. SendScintilla(SCI_SETMARGINMASKN, margin, SC_MASK_FOLDERS);
  1018. SendScintilla(SCI_SETMARGINSENSITIVEN, margin, 1);
  1019. // Set the marker symbols to use.
  1020. switch (folding)
  1021. {
  1022. case FoldStyle::PlainFoldStyle:
  1023. setFoldMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_MINUS);
  1024. setFoldMarker(SC_MARKNUM_FOLDER, SC_MARK_PLUS);
  1025. setFoldMarker(SC_MARKNUM_FOLDERSUB);
  1026. setFoldMarker(SC_MARKNUM_FOLDERTAIL);
  1027. setFoldMarker(SC_MARKNUM_FOLDEREND);
  1028. setFoldMarker(SC_MARKNUM_FOLDEROPENMID);
  1029. setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL);
  1030. break;
  1031. case FoldStyle::CircledFoldStyle:
  1032. setFoldMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_CIRCLEMINUS);
  1033. setFoldMarker(SC_MARKNUM_FOLDER, SC_MARK_CIRCLEPLUS);
  1034. setFoldMarker(SC_MARKNUM_FOLDERSUB);
  1035. setFoldMarker(SC_MARKNUM_FOLDERTAIL);
  1036. setFoldMarker(SC_MARKNUM_FOLDEREND);
  1037. setFoldMarker(SC_MARKNUM_FOLDEROPENMID);
  1038. setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL);
  1039. break;
  1040. case FoldStyle::BoxedFoldStyle:
  1041. setFoldMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_BOXMINUS);
  1042. setFoldMarker(SC_MARKNUM_FOLDER, SC_MARK_BOXPLUS);
  1043. setFoldMarker(SC_MARKNUM_FOLDERSUB);
  1044. setFoldMarker(SC_MARKNUM_FOLDERTAIL);
  1045. setFoldMarker(SC_MARKNUM_FOLDEREND);
  1046. setFoldMarker(SC_MARKNUM_FOLDEROPENMID);
  1047. setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL);
  1048. break;
  1049. case FoldStyle::CircledTreeFoldStyle:
  1050. setFoldMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_CIRCLEMINUS);
  1051. setFoldMarker(SC_MARKNUM_FOLDER, SC_MARK_CIRCLEPLUS);
  1052. setFoldMarker(SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE);
  1053. setFoldMarker(SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNERCURVE);
  1054. setFoldMarker(SC_MARKNUM_FOLDEREND, SC_MARK_CIRCLEPLUSCONNECTED);
  1055. setFoldMarker(SC_MARKNUM_FOLDEROPENMID, SC_MARK_CIRCLEMINUSCONNECTED);
  1056. setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNERCURVE);
  1057. break;
  1058. case FoldStyle::BoxedTreeFoldStyle:
  1059. setFoldMarker(SC_MARKNUM_FOLDEROPEN, SC_MARK_BOXMINUS);
  1060. setFoldMarker(SC_MARKNUM_FOLDER, SC_MARK_BOXPLUS);
  1061. setFoldMarker(SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE);
  1062. setFoldMarker(SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNER);
  1063. setFoldMarker(SC_MARKNUM_FOLDEREND, SC_MARK_BOXPLUSCONNECTED);
  1064. setFoldMarker(SC_MARKNUM_FOLDEROPENMID, SC_MARK_BOXMINUSCONNECTED);
  1065. setFoldMarker(SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNER);
  1066. break;
  1067. }
  1068. SendScintilla(SCI_SETMARGINWIDTHN, margin, defaultFoldMarginWidth);
  1069. }
  1070. // Clear all current folds.
  1071. void QsciScintilla::clearFolds()
  1072. {
  1073. recolor();
  1074. int maxLine = SendScintilla(SCI_GETLINECOUNT);
  1075. for (int line = 0; line < maxLine; line++)
  1076. {
  1077. int level = SendScintilla(SCI_GETFOLDLEVEL, line);
  1078. if (level & SC_FOLDLEVELHEADERFLAG)
  1079. {
  1080. SendScintilla(SCI_SETFOLDEXPANDED, line, 1);
  1081. foldExpand(line, true, false, 0, level);
  1082. line--;
  1083. }
  1084. }
  1085. }
  1086. // Set up a folder marker.
  1087. void QsciScintilla::setFoldMarker(int marknr, int mark)
  1088. {
  1089. SendScintilla(SCI_MARKERDEFINE, marknr, mark);
  1090. if (mark != SC_MARK_EMPTY)
  1091. {
  1092. SendScintilla(SCI_MARKERSETFORE, marknr, QColor(Qt::white));
  1093. SendScintilla(SCI_MARKERSETBACK, marknr, QColor(Qt::black));
  1094. }
  1095. }
  1096. // Handle a click in the fold margin. This is mostly taken from SciTE.
  1097. void QsciScintilla::foldClick(int lineClick, int bstate)
  1098. {
  1099. bool shift = bstate & Qt::ShiftModifier;
  1100. bool ctrl = bstate & Qt::ControlModifier;
  1101. if (shift && ctrl)
  1102. {
  1103. foldAll();
  1104. return;
  1105. }
  1106. int levelClick = SendScintilla(SCI_GETFOLDLEVEL, lineClick);
  1107. if (levelClick & SC_FOLDLEVELHEADERFLAG)
  1108. {
  1109. if (shift)
  1110. {
  1111. // Ensure all children are visible.
  1112. SendScintilla(SCI_SETFOLDEXPANDED, lineClick, 1);
  1113. foldExpand(lineClick, true, true, 100, levelClick);
  1114. }
  1115. else if (ctrl)
  1116. {
  1117. if (SendScintilla(SCI_GETFOLDEXPANDED, lineClick))
  1118. {
  1119. // Contract this line and all its children.
  1120. SendScintilla(SCI_SETFOLDEXPANDED, lineClick, 0L);
  1121. foldExpand(lineClick, false, true, 0, levelClick);
  1122. }
  1123. else
  1124. {
  1125. // Expand this line and all its children.
  1126. SendScintilla(SCI_SETFOLDEXPANDED, lineClick, 1);
  1127. foldExpand(lineClick, true, true, 100, levelClick);
  1128. }
  1129. }
  1130. else
  1131. {
  1132. // Toggle this line.
  1133. SendScintilla(SCI_TOGGLEFOLD, lineClick);
  1134. }
  1135. }
  1136. }
  1137. // Do the hard work of hiding and showing lines. This is mostly taken from
  1138. // SciTE.
  1139. void QsciScintilla::foldExpand(int &line, bool doExpand, bool force,
  1140. int visLevels, int level)
  1141. {
  1142. int lineMaxSubord = SendScintilla(SCI_GETLASTCHILD, line,
  1143. level & SC_FOLDLEVELNUMBERMASK);
  1144. line++;
  1145. while (line <= lineMaxSubord)
  1146. {
  1147. if (force)
  1148. {
  1149. if (visLevels > 0)
  1150. SendScintilla(SCI_SHOWLINES, line, line);
  1151. else
  1152. SendScintilla(SCI_HIDELINES, line, line);
  1153. }
  1154. else if (doExpand)
  1155. SendScintilla(SCI_SHOWLINES, line, line);
  1156. int levelLine = level;
  1157. if (levelLine == -1)
  1158. levelLine = SendScintilla(SCI_GETFOLDLEVEL, line);
  1159. if (levelLine & SC_FOLDLEVELHEADERFLAG)
  1160. {
  1161. if (force)
  1162. {
  1163. if (visLevels > 1)
  1164. SendScintilla(SCI_SETFOLDEXPANDED, line, 1);
  1165. else
  1166. SendScintilla(SCI_SETFOLDEXPANDED, line, 0L);
  1167. foldExpand(line, doExpand, force, visLevels - 1);
  1168. }
  1169. else if (doExpand)
  1170. {
  1171. if (!SendScintilla(SCI_GETFOLDEXPANDED, line))
  1172. SendScintilla(SCI_SETFOLDEXPANDED, line, 1);
  1173. foldExpand(line, true, force, visLevels - 1);
  1174. }
  1175. else
  1176. foldExpand(line, false, force, visLevels - 1);
  1177. }
  1178. else
  1179. line++;
  1180. }
  1181. }
  1182. // Fully expand (if there is any line currently folded) all text. Otherwise,
  1183. // fold all text. This is mostly taken from SciTE.
  1184. void QsciScintilla::foldAll(bool children)
  1185. {
  1186. recolor();
  1187. int maxLine = SendScintilla(SCI_GETLINECOUNT);
  1188. bool expanding = true;
  1189. for (int lineSeek = 0; lineSeek < maxLine; lineSeek++)
  1190. {
  1191. if (SendScintilla(SCI_GETFOLDLEVEL,lineSeek) & SC_FOLDLEVELHEADERFLAG)
  1192. {
  1193. expanding = !SendScintilla(SCI_GETFOLDEXPANDED, lineSeek);
  1194. break;
  1195. }
  1196. }
  1197. for (int line = 0; line < maxLine; line++)
  1198. {
  1199. int level = SendScintilla(SCI_GETFOLDLEVEL, line);
  1200. if (!(level & SC_FOLDLEVELHEADERFLAG))
  1201. continue;
  1202. if (children ||
  1203. (SC_FOLDLEVELBASE == (level & SC_FOLDLEVELNUMBERMASK)))
  1204. {
  1205. if (expanding)
  1206. {
  1207. SendScintilla(SCI_SETFOLDEXPANDED, line, 1);
  1208. foldExpand(line, true, false, 0, level);
  1209. line--;
  1210. }
  1211. else
  1212. {
  1213. int lineMaxSubord = SendScintilla(SCI_GETLASTCHILD, line, -1);
  1214. SendScintilla(SCI_SETFOLDEXPANDED, line, 0L);
  1215. if (lineMaxSubord > line)
  1216. SendScintilla(SCI_HIDELINES, line + 1, lineMaxSubord);
  1217. }
  1218. }
  1219. }
  1220. }
  1221. // Handle a fold change. This is mostly taken from SciTE.
  1222. void QsciScintilla::foldChanged(int line,int levelNow,int levelPrev)
  1223. {
  1224. if (levelNow & SC_FOLDLEVELHEADERFLAG)
  1225. {
  1226. if (!(levelPrev & SC_FOLDLEVELHEADERFLAG))
  1227. SendScintilla(SCI_SETFOLDEXPANDED, line, 1);
  1228. }
  1229. else if (levelPrev & SC_FOLDLEVELHEADERFLAG)
  1230. {
  1231. if (!SendScintilla(SCI_GETFOLDEXPANDED, line))
  1232. {
  1233. // Removing the fold from one that has been contracted so should
  1234. // expand. Otherwise lines are left invisible with no way to make
  1235. // them visible.
  1236. foldExpand(line, true, false, 0, levelPrev);
  1237. }
  1238. }
  1239. }
  1240. // Toggle the fold for a line if it contains a fold marker.
  1241. void QsciScintilla::foldLine(int line)
  1242. {
  1243. SendScintilla(SCI_TOGGLEFOLD, line);
  1244. }
  1245. // Return the list of folded lines.
  1246. QList<int> QsciScintilla::contractedFolds() const
  1247. {
  1248. QList<int> folds;
  1249. int linenr = 0, fold_line;
  1250. while ((fold_line = SendScintilla(SCI_CONTRACTEDFOLDNEXT, linenr)) >= 0)
  1251. {
  1252. folds.append(fold_line);
  1253. linenr = fold_line + 1;
  1254. }
  1255. return folds;
  1256. }
  1257. // Set the fold state from a list.
  1258. void QsciScintilla::setContractedFolds(const QList<int> &folds)
  1259. {
  1260. for (int i = 0; i < folds.count(); ++i)
  1261. {
  1262. int line = folds[i];
  1263. int last_line = SendScintilla(SCI_GETLASTCHILD, line, -1);
  1264. SendScintilla(SCI_SETFOLDEXPANDED, line, 0L);
  1265. SendScintilla(SCI_HIDELINES, line + 1, last_line);
  1266. }
  1267. }
  1268. // Handle the SCN_MODIFIED notification.
  1269. void QsciScintilla::handleModified(int pos, int mtype, const char *text,
  1270. int len, int added, int line, int foldNow, int foldPrev, int token,
  1271. int annotationLinesAdded)
  1272. {
  1273. if (mtype & SC_MOD_CHANGEFOLD)
  1274. {
  1275. if (fold)
  1276. foldChanged(line, foldNow, foldPrev);
  1277. }
  1278. if (mtype & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT))
  1279. {
  1280. emit textChanged();
  1281. if (added != 0)
  1282. emit linesChanged();
  1283. }
  1284. }
  1285. // Zoom in a number of points.
  1286. void QsciScintilla::zoomIn(int range)
  1287. {
  1288. zoomTo(SendScintilla(SCI_GETZOOM) + range);
  1289. }
  1290. // Zoom in a single point.
  1291. void QsciScintilla::zoomIn()
  1292. {
  1293. SendScintilla(SCI_ZOOMIN);
  1294. }
  1295. // Zoom out a number of points.
  1296. void QsciScintilla::zoomOut(int range)
  1297. {
  1298. zoomTo(SendScintilla(SCI_GETZOOM) - range);
  1299. }
  1300. // Zoom out a single point.
  1301. void QsciScintilla::zoomOut()
  1302. {
  1303. SendScintilla(SCI_ZOOMOUT);
  1304. }
  1305. // Set the zoom to a number of points.
  1306. void QsciScintilla::zoomTo(int size)
  1307. {
  1308. if (size < -10)
  1309. size = -10;
  1310. else if (size > 20)
  1311. size = 20;
  1312. SendScintilla(SCI_SETZOOM, size);
  1313. }
  1314. // Find the first occurrence of a string.
  1315. bool QsciScintilla::findFirst(const QString &expr, bool re, bool cs, bool wo,
  1316. bool wrap, bool forward, int line, int index, bool show, bool posix)
  1317. {
  1318. if (expr.isEmpty())
  1319. {
  1320. findState.status = FindState::Idle;
  1321. return false;
  1322. }
  1323. findState.status = FindState::Finding;
  1324. findState.expr = expr;
  1325. findState.wrap = wrap;
  1326. findState.forward = forward;
  1327. findState.flags =
  1328. (cs ? SCFIND_MATCHCASE : 0) |
  1329. (wo ? SCFIND_WHOLEWORD : 0) |
  1330. (re ? SCFIND_REGEXP : 0) |
  1331. (posix ? SCFIND_POSIX : 0);
  1332. if (line < 0 || index < 0)
  1333. findState.startpos = SendScintilla(SCI_GETCURRENTPOS);
  1334. else
  1335. findState.startpos = positionFromLineIndex(line, index);
  1336. if (forward)
  1337. findState.endpos = SendScintilla(SCI_GETLENGTH);
  1338. else
  1339. findState.endpos = 0;
  1340. findState.show = show;
  1341. return doFind();
  1342. }
  1343. // Find the first occurrence of a string in the current selection.
  1344. bool QsciScintilla::findFirstInSelection(const QString &expr, bool re, bool cs,
  1345. bool wo, bool forward, bool show, bool posix)
  1346. {
  1347. if (expr.isEmpty())
  1348. {
  1349. findState.status = FindState::Idle;
  1350. return false;
  1351. }
  1352. findState.status = FindState::FindingInSelection;
  1353. findState.expr = expr;
  1354. findState.wrap = false;
  1355. findState.forward = forward;
  1356. findState.flags =
  1357. (cs ? SCFIND_MATCHCASE : 0) |
  1358. (wo ? SCFIND_WHOLEWORD : 0) |
  1359. (re ? SCFIND_REGEXP : 0) |
  1360. (posix ? SCFIND_POSIX : 0);
  1361. findState.startpos_orig = SendScintilla(SCI_GETSELECTIONSTART);
  1362. findState.endpos_orig = SendScintilla(SCI_GETSELECTIONEND);
  1363. if (forward)
  1364. {
  1365. findState.startpos = findState.startpos_orig;
  1366. findState.endpos = findState.endpos_orig;
  1367. }
  1368. else
  1369. {
  1370. findState.startpos = findState.endpos_orig;
  1371. findState.endpos = findState.startpos_orig;
  1372. }
  1373. findState.show = show;
  1374. return doFind();
  1375. }
  1376. // Find the next occurrence of a string.
  1377. bool QsciScintilla::findNext()
  1378. {
  1379. if (findState.status == FindState::Idle)
  1380. return false;
  1381. return doFind();
  1382. }
  1383. // Do the hard work of the find methods.
  1384. bool QsciScintilla::doFind()
  1385. {
  1386. SendScintilla(SCI_SETSEARCHFLAGS, findState.flags);
  1387. int pos = simpleFind();
  1388. // See if it was found. If not and wraparound is wanted, try again.
  1389. if (pos == -1 && findState.wrap)
  1390. {
  1391. if (findState.forward)
  1392. {
  1393. findState.startpos = 0;
  1394. findState.endpos = SendScintilla(SCI_GETLENGTH);
  1395. }
  1396. else
  1397. {
  1398. findState.startpos = SendScintilla(SCI_GETLENGTH);
  1399. findState.endpos = 0;
  1400. }
  1401. pos = simpleFind();
  1402. }
  1403. if (pos == -1)
  1404. {
  1405. // Restore the original selection.
  1406. if (findState.status == FindState::FindingInSelection)
  1407. SendScintilla(SCI_SETSEL, findState.startpos_orig,
  1408. findState.endpos_orig);
  1409. findState.status = FindState::Idle;
  1410. return false;
  1411. }
  1412. // It was found.
  1413. long targstart = SendScintilla(SCI_GETTARGETSTART);
  1414. long targend = SendScintilla(SCI_GETTARGETEND);
  1415. // Ensure the text found is visible if required.
  1416. if (findState.show)
  1417. {
  1418. int startLine = SendScintilla(SCI_LINEFROMPOSITION, targstart);
  1419. int endLine = SendScintilla(SCI_LINEFROMPOSITION, targend);
  1420. for (int i = startLine; i <= endLine; ++i)
  1421. SendScintilla(SCI_ENSUREVISIBLEENFORCEPOLICY, i);
  1422. }
  1423. // Now set the selection.
  1424. SendScintilla(SCI_SETSEL, targstart, targend);
  1425. // Finally adjust the start position so that we don't find the same one
  1426. // again.
  1427. if (findState.forward)
  1428. findState.startpos = targend;
  1429. else if ((findState.startpos = targstart - 1) < 0)
  1430. findState.startpos = 0;
  1431. return true;
  1432. }
  1433. // Do a simple find between the start and end positions.
  1434. int QsciScintilla::simpleFind()
  1435. {
  1436. if (findState.startpos == findState.endpos)
  1437. return -1;
  1438. SendScintilla(SCI_SETTARGETSTART, findState.startpos);
  1439. SendScintilla(SCI_SETTARGETEND, findState.endpos);
  1440. ScintillaBytes s = textAsBytes(findState.expr);
  1441. return SendScintilla(SCI_SEARCHINTARGET, s.length(),
  1442. ScintillaBytesConstData(s));
  1443. }
  1444. // Replace the text found with the previous find method.
  1445. void QsciScintilla::replace(const QString &replaceStr)
  1446. {
  1447. if (findState.status == FindState::Idle)
  1448. return;
  1449. long start = SendScintilla(SCI_GETSELECTIONSTART);
  1450. long orig_len = SendScintilla(SCI_GETSELECTIONEND) - start;
  1451. SendScintilla(SCI_TARGETFROMSELECTION);
  1452. int cmd = (findState.flags & SCFIND_REGEXP) ? SCI_REPLACETARGETRE : SCI_REPLACETARGET;
  1453. ScintillaBytes s = textAsBytes(replaceStr);
  1454. long len = SendScintilla(cmd, -1, ScintillaBytesConstData(s));
  1455. // Reset the selection.
  1456. SendScintilla(SCI_SETSELECTIONSTART, start);
  1457. SendScintilla(SCI_SETSELECTIONEND, start + len);
  1458. // Fix the original selection.
  1459. findState.endpos_orig += (len - orig_len);
  1460. if (findState.forward)
  1461. findState.startpos = start + len;
  1462. }
  1463. // Query the modified state.
  1464. bool QsciScintilla::isModified() const
  1465. {
  1466. return doc.isModified();
  1467. }
  1468. // Set the modified state.
  1469. void QsciScintilla::setModified(bool m)
  1470. {
  1471. if (!m)
  1472. SendScintilla(SCI_SETSAVEPOINT);
  1473. }
  1474. // Handle the SCN_INDICATORCLICK notification.
  1475. void QsciScintilla::handleIndicatorClick(int pos, int modifiers)
  1476. {
  1477. int state = mapModifiers(modifiers);
  1478. int line, index;
  1479. lineIndexFromPosition(pos, &line, &index);
  1480. emit indicatorClicked(line, index, Qt::KeyboardModifiers(state));
  1481. }
  1482. // Handle the SCN_INDICATORRELEASE notification.
  1483. void QsciScintilla::handleIndicatorRelease(int pos, int modifiers)
  1484. {
  1485. int state = mapModifiers(modifiers);
  1486. int line, index;
  1487. lineIndexFromPosition(pos, &line, &index);
  1488. emit indicatorReleased(line, index, Qt::KeyboardModifiers(state));
  1489. }
  1490. // Handle the SCN_MARGINCLICK notification.
  1491. void QsciScintilla::handleMarginClick(int pos, int modifiers, int margin)
  1492. {
  1493. int state = mapModifiers(modifiers);
  1494. int line = SendScintilla(SCI_LINEFROMPOSITION, pos);
  1495. if (fold && margin == foldmargin)
  1496. foldClick(line, state);
  1497. else
  1498. emit marginClicked(margin, line, Qt::KeyboardModifiers(state));
  1499. }
  1500. // Handle the SCN_MARGINRIGHTCLICK notification.
  1501. void QsciScintilla::handleMarginRightClick(int pos, int modifiers, int margin)
  1502. {
  1503. int state = mapModifiers(modifiers);
  1504. int line = SendScintilla(SCI_LINEFROMPOSITION, pos);
  1505. emit marginRightClicked(margin, line, Qt::KeyboardModifiers(state));
  1506. }
  1507. // Handle the SCN_SAVEPOINTREACHED notification.
  1508. void QsciScintilla::handleSavePointReached()
  1509. {
  1510. doc.setModified(false);
  1511. emit modificationChanged(false);
  1512. }
  1513. // Handle the SCN_SAVEPOINTLEFT notification.
  1514. void QsciScintilla::handleSavePointLeft()
  1515. {
  1516. doc.setModified(true);
  1517. emit modificationChanged(true);
  1518. }
  1519. // Handle the QSCN_SELCHANGED signal.
  1520. void QsciScintilla::handleSelectionChanged(bool yes)
  1521. {
  1522. selText = yes;
  1523. emit copyAvailable(yes);
  1524. emit selectionChanged();
  1525. }
  1526. // Get the current selection.
  1527. void QsciScintilla::getSelection(int *lineFrom, int *indexFrom, int *lineTo,
  1528. int *indexTo) const
  1529. {
  1530. if (selText)
  1531. {
  1532. lineIndexFromPosition(SendScintilla(SCI_GETSELECTIONSTART), lineFrom,
  1533. indexFrom);
  1534. lineIndexFromPosition(SendScintilla(SCI_GETSELECTIONEND), lineTo,
  1535. indexTo);
  1536. }
  1537. else
  1538. *lineFrom = *indexFrom = *lineTo = *indexTo = -1;
  1539. }
  1540. // Sets the current selection.
  1541. void QsciScintilla::setSelection(int lineFrom, int indexFrom, int lineTo,
  1542. int indexTo)
  1543. {
  1544. SendScintilla(SCI_SETSEL, positionFromLineIndex(lineFrom, indexFrom),
  1545. positionFromLineIndex(lineTo, indexTo));
  1546. }
  1547. // Set the background colour of selected text.
  1548. void QsciScintilla::setSelectionBackgroundColor(const QColor &col)
  1549. {
  1550. int alpha = col.alpha();
  1551. if (alpha == 255)
  1552. alpha = SC_ALPHA_NOALPHA;
  1553. SendScintilla(SCI_SETSELBACK, 1, col);
  1554. SendScintilla(SCI_SETSELALPHA, alpha);
  1555. }
  1556. // Set the foreground colour of selected text.
  1557. void QsciScintilla::setSelectionForegroundColor(const QColor &col)
  1558. {
  1559. SendScintilla(SCI_SETSELFORE, 1, col);
  1560. }
  1561. // Reset the background colour of selected text to the default.
  1562. void QsciScintilla::resetSelectionBackgroundColor()
  1563. {
  1564. SendScintilla(SCI_SETSELALPHA, SC_ALPHA_NOALPHA);
  1565. SendScintilla(SCI_SETSELBACK, 0UL);
  1566. }
  1567. // Reset the foreground colour of selected text to the default.
  1568. void QsciScintilla::resetSelectionForegroundColor()
  1569. {
  1570. SendScintilla(SCI_SETSELFORE, 0UL);
  1571. }
  1572. // Set the fill to the end-of-line for the selection.
  1573. void QsciScintilla::setSelectionToEol(bool filled)
  1574. {
  1575. SendScintilla(SCI_SETSELEOLFILLED, filled);
  1576. }
  1577. // Return the fill to the end-of-line for the selection.
  1578. bool QsciScintilla::selectionToEol() const
  1579. {
  1580. return SendScintilla(SCI_GETSELEOLFILLED);
  1581. }
  1582. // Set the width of the caret.
  1583. void QsciScintilla::setCaretWidth(int width)
  1584. {
  1585. SendScintilla(SCI_SETCARETWIDTH, width);
  1586. }
  1587. // Set the foreground colour of the caret.
  1588. void QsciScintilla::setCaretForegroundColor(const QColor &col)
  1589. {
  1590. SendScintilla(SCI_SETCARETFORE, col);
  1591. }
  1592. // Set the background colour of the line containing the caret.
  1593. void QsciScintilla::setCaretLineBackgroundColor(const QColor &col)
  1594. {
  1595. int alpha = col.alpha();
  1596. if (alpha == 255)
  1597. alpha = SC_ALPHA_NOALPHA;
  1598. SendScintilla(SCI_SETCARETLINEBACK, col);
  1599. SendScintilla(SCI_SETCARETLINEBACKALPHA, alpha);
  1600. }
  1601. // Set the state of the background colour of the line containing the caret.
  1602. void QsciScintilla::setCaretLineVisible(bool enable)
  1603. {
  1604. SendScintilla(SCI_SETCARETLINEVISIBLE, enable);
  1605. }
  1606. // Set the background colour of a hotspot area.
  1607. void QsciScintilla::setHotspotBackgroundColor(const QColor &col)
  1608. {
  1609. SendScintilla(SCI_SETSELBACK, 1, col);
  1610. }
  1611. // Set the foreground colour of a hotspot area.
  1612. void QsciScintilla::setHotspotForegroundColor(const QColor &col)
  1613. {
  1614. SendScintilla(SCI_SETHOTSPOTACTIVEFORE, 1, col);
  1615. }
  1616. // Reset the background colour of a hotspot area to the default.
  1617. void QsciScintilla::resetHotspotBackgroundColor()
  1618. {
  1619. SendScintilla(SCI_SETSELBACK, 0UL);
  1620. }
  1621. // Reset the foreground colour of a hotspot area to the default.
  1622. void QsciScintilla::resetHotspotForegroundColor()
  1623. {
  1624. SendScintilla(SCI_SETHOTSPOTACTIVEFORE, 0UL);
  1625. }
  1626. // Set the underline of a hotspot area.
  1627. void QsciScintilla::setHotspotUnderline(bool enable)
  1628. {
  1629. SendScintilla(SCI_SETHOTSPOTACTIVEUNDERLINE, enable);
  1630. }
  1631. // Set the wrapping of a hotspot area.
  1632. void QsciScintilla::setHotspotWrap(bool enable)
  1633. {
  1634. SendScintilla(SCI_SETHOTSPOTSINGLELINE, !enable);
  1635. }
  1636. // Query the read-only state.
  1637. bool QsciScintilla::isReadOnly() const
  1638. {
  1639. return SendScintilla(SCI_GETREADONLY);
  1640. }
  1641. // Set the read-only state.
  1642. void QsciScintilla::setReadOnly(bool ro)
  1643. {
  1644. setAttribute(Qt::WA_InputMethodEnabled, !ro);
  1645. SendScintilla(SCI_SETREADONLY, ro);
  1646. }
  1647. // Append the given text.
  1648. void QsciScintilla::append(const QString &text)
  1649. {
  1650. bool ro = ensureRW();
  1651. ScintillaBytes s = textAsBytes(text);
  1652. SendScintilla(SCI_APPENDTEXT, s.length(), ScintillaBytesConstData(s));
  1653. SendScintilla(SCI_EMPTYUNDOBUFFER);
  1654. setReadOnly(ro);
  1655. }
  1656. // Insert the given text at the current position.
  1657. void QsciScintilla::insert(const QString &text)
  1658. {
  1659. insertAtPos(text, -1);
  1660. }
  1661. // Insert the given text at the given line and offset.
  1662. void QsciScintilla::insertAt(const QString &text, int line, int index)
  1663. {
  1664. insertAtPos(text, positionFromLineIndex(line, index));
  1665. }
  1666. // Insert the given text at the given position.
  1667. void QsciScintilla::insertAtPos(const QString &text, int pos)
  1668. {
  1669. bool ro = ensureRW();
  1670. SendScintilla(SCI_BEGINUNDOACTION);
  1671. SendScintilla(SCI_INSERTTEXT, pos,
  1672. ScintillaBytesConstData(textAsBytes(text)));
  1673. SendScintilla(SCI_ENDUNDOACTION);
  1674. setReadOnly(ro);
  1675. }
  1676. // Begin a sequence of undoable actions.
  1677. void QsciScintilla::beginUndoAction()
  1678. {
  1679. SendScintilla(SCI_BEGINUNDOACTION);
  1680. }
  1681. // End a sequence of undoable actions.
  1682. void QsciScintilla::endUndoAction()
  1683. {
  1684. SendScintilla(SCI_ENDUNDOACTION);
  1685. }
  1686. // Redo a sequence of actions.
  1687. void QsciScintilla::redo()
  1688. {
  1689. SendScintilla(SCI_REDO);
  1690. }
  1691. // Undo a sequence of actions.
  1692. void QsciScintilla::undo()
  1693. {
  1694. SendScintilla(SCI_UNDO);
  1695. }
  1696. // See if there is something to redo.
  1697. bool QsciScintilla::isRedoAvailable() const
  1698. {
  1699. return SendScintilla(SCI_CANREDO);
  1700. }
  1701. // See if there is something to undo.
  1702. bool QsciScintilla::isUndoAvailable() const
  1703. {
  1704. return SendScintilla(SCI_CANUNDO);
  1705. }
  1706. // Return the number of lines.
  1707. int QsciScintilla::lines() const
  1708. {
  1709. return SendScintilla(SCI_GETLINECOUNT);
  1710. }
  1711. // Return the line at a position.
  1712. int QsciScintilla::lineAt(const QPoint &pos) const
  1713. {
  1714. long chpos = SendScintilla(SCI_POSITIONFROMPOINTCLOSE, pos.x(), pos.y());
  1715. if (chpos < 0)
  1716. return -1;
  1717. return SendScintilla(SCI_LINEFROMPOSITION, chpos);
  1718. }
  1719. // Return the length of a line.
  1720. int QsciScintilla::lineLength(int line) const
  1721. {
  1722. if (line < 0 || line >= SendScintilla(SCI_GETLINECOUNT))
  1723. return -1;
  1724. return SendScintilla(SCI_LINELENGTH, line);
  1725. }
  1726. // Return the length of the current text.
  1727. int QsciScintilla::length() const
  1728. {
  1729. return SendScintilla(SCI_GETTEXTLENGTH);
  1730. }
  1731. // Remove any selected text.
  1732. void QsciScintilla::removeSelectedText()
  1733. {
  1734. SendScintilla(SCI_REPLACESEL, "");
  1735. }
  1736. // Replace any selected text.
  1737. void QsciScintilla::replaceSelectedText(const QString &text)
  1738. {
  1739. SendScintilla(SCI_REPLACESEL, ScintillaBytesConstData(textAsBytes(text)));
  1740. }
  1741. // Return the current selected text.
  1742. QString QsciScintilla::selectedText() const
  1743. {
  1744. if (!selText)
  1745. return QString();
  1746. char *buf = new char[SendScintilla(SCI_GETSELECTIONEND) - SendScintilla(SCI_GETSELECTIONSTART) + 1];
  1747. SendScintilla(SCI_GETSELTEXT, buf);
  1748. QString qs = bytesAsText(buf);
  1749. delete[] buf;
  1750. return qs;
  1751. }
  1752. // Return the current text.
  1753. QString QsciScintilla::text() const
  1754. {
  1755. int buflen = length() + 1;
  1756. char *buf = new char[buflen];
  1757. SendScintilla(SCI_GETTEXT, buflen, buf);
  1758. QString qs = bytesAsText(buf);
  1759. delete[] buf;
  1760. return qs;
  1761. }
  1762. // Return the text of a line.
  1763. QString QsciScintilla::text(int line) const
  1764. {
  1765. int line_len = lineLength(line);
  1766. if (line_len < 1)
  1767. return QString();
  1768. char *buf = new char[line_len + 1];
  1769. SendScintilla(SCI_GETLINE, line, buf);
  1770. buf[line_len] = '\0';
  1771. QString qs = bytesAsText(buf);
  1772. delete[] buf;
  1773. return qs;
  1774. }
  1775. // Return the text between two positions.
  1776. QString QsciScintilla::text(int start, int end) const
  1777. {
  1778. char *buf = new char[end - start + 1];
  1779. SendScintilla(SCI_GETTEXTRANGE, start, end, buf);
  1780. QString text = bytesAsText(buf);
  1781. delete[] buf;
  1782. return text;
  1783. }
  1784. // Return the text as encoded bytes between two positions.
  1785. QByteArray QsciScintilla::bytes(int start, int end) const
  1786. {
  1787. QByteArray bytes(end - start + 1, '\0');
  1788. SendScintilla(SCI_GETTEXTRANGE, start, end, bytes.data());
  1789. return bytes;
  1790. }
  1791. // Set the given text.
  1792. void QsciScintilla::setText(const QString &text)
  1793. {
  1794. bool ro = ensureRW();
  1795. SendScintilla(SCI_SETTEXT, ScintillaBytesConstData(textAsBytes(text)));
  1796. SendScintilla(SCI_EMPTYUNDOBUFFER);
  1797. setReadOnly(ro);
  1798. }
  1799. // Get the cursor position
  1800. void QsciScintilla::getCursorPosition(int *line, int *index) const
  1801. {
  1802. lineIndexFromPosition(SendScintilla(SCI_GETCURRENTPOS), line, index);
  1803. }
  1804. // Set the cursor position
  1805. void QsciScintilla::setCursorPosition(int line, int index)
  1806. {
  1807. SendScintilla(SCI_GOTOPOS, positionFromLineIndex(line, index));
  1808. }
  1809. // Ensure the cursor is visible.
  1810. void QsciScintilla::ensureCursorVisible()
  1811. {
  1812. SendScintilla(SCI_SCROLLCARET);
  1813. }
  1814. // Ensure a line is visible.
  1815. void QsciScintilla::ensureLineVisible(int line)
  1816. {
  1817. SendScintilla(SCI_ENSUREVISIBLEENFORCEPOLICY, line);
  1818. }
  1819. // Copy text to the clipboard.
  1820. void QsciScintilla::copy()
  1821. {
  1822. SendScintilla(SCI_COPY);
  1823. }
  1824. // Cut text to the clipboard.
  1825. void QsciScintilla::cut()
  1826. {
  1827. SendScintilla(SCI_CUT);
  1828. }
  1829. // Paste text from the clipboard.
  1830. void QsciScintilla::paste()
  1831. {
  1832. SendScintilla(SCI_PASTE);
  1833. }
  1834. // Select all text, or deselect any selected text.
  1835. void QsciScintilla::selectAll(bool select)
  1836. {
  1837. if (select)
  1838. SendScintilla(SCI_SELECTALL);
  1839. else
  1840. SendScintilla(SCI_SETANCHOR, SendScintilla(SCI_GETCURRENTPOS));
  1841. }
  1842. // Delete all text.
  1843. void QsciScintilla::clear()
  1844. {
  1845. bool ro = ensureRW();
  1846. SendScintilla(SCI_BEGINUNDOACTION);
  1847. SendScintilla(SCI_CLEARALL);
  1848. SendScintilla(SCI_ENDUNDOACTION);
  1849. setReadOnly(ro);
  1850. }
  1851. // Return the indentation of a line.
  1852. int QsciScintilla::indentation(int line) const
  1853. {
  1854. return SendScintilla(SCI_GETLINEINDENTATION, line);
  1855. }
  1856. // Set the indentation of a line.
  1857. void QsciScintilla::setIndentation(int line, int indentation)
  1858. {
  1859. SendScintilla(SCI_BEGINUNDOACTION);
  1860. SendScintilla(SCI_SETLINEINDENTATION, line, indentation);
  1861. SendScintilla(SCI_ENDUNDOACTION);
  1862. }
  1863. // Indent a line.
  1864. void QsciScintilla::indent(int line)
  1865. {
  1866. setIndentation(line, indentation(line) + indentWidth());
  1867. }
  1868. // Unindent a line.
  1869. void QsciScintilla::unindent(int line)
  1870. {
  1871. int newIndent = indentation(line) - indentWidth();
  1872. if (newIndent < 0)
  1873. newIndent = 0;
  1874. setIndentation(line, newIndent);
  1875. }
  1876. // Return the indentation of the current line.
  1877. int QsciScintilla::currentIndent() const
  1878. {
  1879. return indentation(SendScintilla(SCI_LINEFROMPOSITION,
  1880. SendScintilla(SCI_GETCURRENTPOS)));
  1881. }
  1882. // Return the current indentation width.
  1883. int QsciScintilla::indentWidth() const
  1884. {
  1885. int w = indentationWidth();
  1886. if (w == 0)
  1887. w = tabWidth();
  1888. return w;
  1889. }
  1890. // Return the state of indentation guides.
  1891. bool QsciScintilla::indentationGuides() const
  1892. {
  1893. return (SendScintilla(SCI_GETINDENTATIONGUIDES) != SC_IV_NONE);
  1894. }
  1895. // Enable and disable indentation guides.
  1896. void QsciScintilla::setIndentationGuides(bool enable)
  1897. {
  1898. int iv;
  1899. if (!enable)
  1900. iv = SC_IV_NONE;
  1901. else if (lex.isNull())
  1902. iv = SC_IV_REAL;
  1903. else
  1904. iv = lex->indentationGuideView();
  1905. SendScintilla(SCI_SETINDENTATIONGUIDES, iv);
  1906. }
  1907. // Set the background colour of indentation guides.
  1908. void QsciScintilla::setIndentationGuidesBackgroundColor(const QColor &col)
  1909. {
  1910. SendScintilla(SCI_STYLESETBACK, STYLE_INDENTGUIDE, col);
  1911. }
  1912. // Set the foreground colour of indentation guides.
  1913. void QsciScintilla::setIndentationGuidesForegroundColor(const QColor &col)
  1914. {
  1915. SendScintilla(SCI_STYLESETFORE, STYLE_INDENTGUIDE, col);
  1916. }
  1917. // Return the indentation width.
  1918. int QsciScintilla::indentationWidth() const
  1919. {
  1920. return SendScintilla(SCI_GETINDENT);
  1921. }
  1922. // Set the indentation width.
  1923. void QsciScintilla::setIndentationWidth(int width)
  1924. {
  1925. SendScintilla(SCI_SETINDENT, width);
  1926. }
  1927. // Return the tab width.
  1928. int QsciScintilla::tabWidth() const
  1929. {
  1930. return SendScintilla(SCI_GETTABWIDTH);
  1931. }
  1932. // Set the tab width.
  1933. void QsciScintilla::setTabWidth(int width)
  1934. {
  1935. SendScintilla(SCI_SETTABWIDTH, width);
  1936. }
  1937. // Return the effect of the backspace key.
  1938. bool QsciScintilla::backspaceUnindents() const
  1939. {
  1940. return SendScintilla(SCI_GETBACKSPACEUNINDENTS);
  1941. }
  1942. // Set the effect of the backspace key.
  1943. void QsciScintilla::setBackspaceUnindents(bool unindents)
  1944. {
  1945. SendScintilla(SCI_SETBACKSPACEUNINDENTS, unindents);
  1946. }
  1947. // Return the effect of the tab key.
  1948. bool QsciScintilla::tabIndents() const
  1949. {
  1950. return SendScintilla(SCI_GETTABINDENTS);
  1951. }
  1952. // Set the effect of the tab key.
  1953. void QsciScintilla::setTabIndents(bool indents)
  1954. {
  1955. SendScintilla(SCI_SETTABINDENTS, indents);
  1956. }
  1957. // Return the indentation use of tabs.
  1958. bool QsciScintilla::indentationsUseTabs() const
  1959. {
  1960. return SendScintilla(SCI_GETUSETABS);
  1961. }
  1962. // Set the indentation use of tabs.
  1963. void QsciScintilla::setIndentationsUseTabs(bool tabs)
  1964. {
  1965. SendScintilla(SCI_SETUSETABS, tabs);
  1966. }
  1967. // Return the number of margins.
  1968. int QsciScintilla::margins() const
  1969. {
  1970. return SendScintilla(SCI_GETMARGINS);
  1971. }
  1972. // Set the number of margins.
  1973. void QsciScintilla::setMargins(int margins)
  1974. {
  1975. SendScintilla(SCI_SETMARGINS, margins);
  1976. }
  1977. // Return the margin background colour.
  1978. QColor QsciScintilla::marginBackgroundColor(int margin) const
  1979. {
  1980. return asQColor(SendScintilla(SCI_GETMARGINBACKN, margin));
  1981. }
  1982. // Set the margin background colour.
  1983. void QsciScintilla::setMarginBackgroundColor(int margin, const QColor &col)
  1984. {
  1985. SendScintilla(SCI_SETMARGINBACKN, margin, col);
  1986. }
  1987. // Return the margin options.
  1988. int QsciScintilla::marginOptions() const
  1989. {
  1990. return SendScintilla(SCI_GETMARGINOPTIONS);
  1991. }
  1992. // Set the margin options.
  1993. void QsciScintilla::setMarginOptions(int options)
  1994. {
  1995. SendScintilla(SCI_SETMARGINOPTIONS, options);
  1996. }
  1997. // Return the margin type.
  1998. QsciScintilla::MarginType QsciScintilla::marginType(int margin) const
  1999. {
  2000. return (MarginType)SendScintilla(SCI_GETMARGINTYPEN, margin);
  2001. }
  2002. // Set the margin type.
  2003. void QsciScintilla::setMarginType(int margin, MarginType type)
  2004. {
  2005. SendScintilla(SCI_SETMARGINTYPEN, margin, type);
  2006. }
  2007. // Clear margin text.
  2008. void QsciScintilla::clearMarginText(int line)
  2009. {
  2010. if (line < 0)
  2011. SendScintilla(SCI_MARGINTEXTCLEARALL);
  2012. else
  2013. SendScintilla(SCI_MARGINSETTEXT, line, (const char *)0);
  2014. }
  2015. // Annotate a line.
  2016. void QsciScintilla::setMarginText(int line, const QString &text, int style)
  2017. {
  2018. int style_offset = SendScintilla(SCI_MARGINGETSTYLEOFFSET);
  2019. SendScintilla(SCI_MARGINSETTEXT, line,
  2020. ScintillaBytesConstData(textAsBytes(text)));
  2021. SendScintilla(SCI_MARGINSETSTYLE, line, style - style_offset);
  2022. }
  2023. // Annotate a line.
  2024. void QsciScintilla::setMarginText(int line, const QString &text, const QsciStyle &style)
  2025. {
  2026. style.apply(this);
  2027. setMarginText(line, text, style.style());
  2028. }
  2029. // Annotate a line.
  2030. void QsciScintilla::setMarginText(int line, const QsciStyledText &text)
  2031. {
  2032. text.apply(this);
  2033. setMarginText(line, text.text(), text.style());
  2034. }
  2035. // Annotate a line.
  2036. void QsciScintilla::setMarginText(int line, const QList<QsciStyledText> &text)
  2037. {
  2038. char *styles;
  2039. ScintillaBytes styled_text = styleText(text, &styles,
  2040. SendScintilla(SCI_MARGINGETSTYLEOFFSET));
  2041. SendScintilla(SCI_MARGINSETTEXT, line,
  2042. ScintillaBytesConstData(styled_text));
  2043. SendScintilla(SCI_MARGINSETSTYLES, line, styles);
  2044. delete[] styles;
  2045. }
  2046. // Return the state of line numbers in a margin.
  2047. bool QsciScintilla::marginLineNumbers(int margin) const
  2048. {
  2049. return SendScintilla(SCI_GETMARGINTYPEN, margin);
  2050. }
  2051. // Enable and disable line numbers in a margin.
  2052. void QsciScintilla::setMarginLineNumbers(int margin, bool lnrs)
  2053. {
  2054. SendScintilla(SCI_SETMARGINTYPEN, margin,
  2055. lnrs ? SC_MARGIN_NUMBER : SC_MARGIN_SYMBOL);
  2056. }
  2057. // Return the marker mask of a margin.
  2058. int QsciScintilla::marginMarkerMask(int margin) const
  2059. {
  2060. return SendScintilla(SCI_GETMARGINMASKN, margin);
  2061. }
  2062. // Set the marker mask of a margin.
  2063. void QsciScintilla::setMarginMarkerMask(int margin,int mask)
  2064. {
  2065. SendScintilla(SCI_SETMARGINMASKN, margin, mask);
  2066. }
  2067. // Return the state of a margin's sensitivity.
  2068. bool QsciScintilla::marginSensitivity(int margin) const
  2069. {
  2070. return SendScintilla(SCI_GETMARGINSENSITIVEN, margin);
  2071. }
  2072. // Enable and disable a margin's sensitivity.
  2073. void QsciScintilla::setMarginSensitivity(int margin,bool sens)
  2074. {
  2075. SendScintilla(SCI_SETMARGINSENSITIVEN, margin, sens);
  2076. }
  2077. // Return the width of a margin.
  2078. int QsciScintilla::marginWidth(int margin) const
  2079. {
  2080. return SendScintilla(SCI_GETMARGINWIDTHN, margin);
  2081. }
  2082. // Set the width of a margin.
  2083. void QsciScintilla::setMarginWidth(int margin, int width)
  2084. {
  2085. SendScintilla(SCI_SETMARGINWIDTHN, margin, width);
  2086. }
  2087. // Set the width of a margin to the width of some text.
  2088. void QsciScintilla::setMarginWidth(int margin, const QString &s)
  2089. {
  2090. int width = SendScintilla(SCI_TEXTWIDTH, STYLE_LINENUMBER,
  2091. ScintillaBytesConstData(textAsBytes(s)));
  2092. setMarginWidth(margin, width);
  2093. }
  2094. // Set the background colour of all margins.
  2095. void QsciScintilla::setMarginsBackgroundColor(const QColor &col)
  2096. {
  2097. handleStylePaperChange(col, STYLE_LINENUMBER);
  2098. }
  2099. // Set the foreground colour of all margins.
  2100. void QsciScintilla::setMarginsForegroundColor(const QColor &col)
  2101. {
  2102. handleStyleColorChange(col, STYLE_LINENUMBER);
  2103. }
  2104. // Set the font of all margins.
  2105. void QsciScintilla::setMarginsFont(const QFont &f)
  2106. {
  2107. setStylesFont(f, STYLE_LINENUMBER);
  2108. }
  2109. // Define an indicator.
  2110. int QsciScintilla::indicatorDefine(IndicatorStyle style, int indicatorNumber)
  2111. {
  2112. checkIndicator(indicatorNumber);
  2113. if (indicatorNumber >= 0)
  2114. SendScintilla(SCI_INDICSETSTYLE, indicatorNumber,
  2115. static_cast<long>(style));
  2116. return indicatorNumber;
  2117. }
  2118. // Return the state of an indicator being drawn under the text.
  2119. bool QsciScintilla::indicatorDrawUnder(int indicatorNumber) const
  2120. {
  2121. if (indicatorNumber < 0 || indicatorNumber >= INDIC_IME)
  2122. return false;
  2123. return SendScintilla(SCI_INDICGETUNDER, indicatorNumber);
  2124. }
  2125. // Set the state of indicators being drawn under the text.
  2126. void QsciScintilla::setIndicatorDrawUnder(bool under, int indicatorNumber)
  2127. {
  2128. if (indicatorNumber < INDIC_IME)
  2129. {
  2130. // We ignore allocatedIndicators to allow any indicators defined
  2131. // elsewhere (e.g. in lexers) to be set.
  2132. if (indicatorNumber < 0)
  2133. {
  2134. for (int i = 0; i < INDIC_IME; ++i)
  2135. SendScintilla(SCI_INDICSETUNDER, i, under);
  2136. }
  2137. else
  2138. {
  2139. SendScintilla(SCI_INDICSETUNDER, indicatorNumber, under);
  2140. }
  2141. }
  2142. }
  2143. // Set the indicator foreground colour.
  2144. void QsciScintilla::setIndicatorForegroundColor(const QColor &col,
  2145. int indicatorNumber)
  2146. {
  2147. if (indicatorNumber < INDIC_IME)
  2148. {
  2149. int alpha = col.alpha();
  2150. // We ignore allocatedIndicators to allow any indicators defined
  2151. // elsewhere (e.g. in lexers) to be set.
  2152. if (indicatorNumber < 0)
  2153. {
  2154. for (int i = 0; i < INDIC_IME; ++i)
  2155. {
  2156. SendScintilla(SCI_INDICSETFORE, i, col);
  2157. SendScintilla(SCI_INDICSETALPHA, i, alpha);
  2158. }
  2159. }
  2160. else
  2161. {
  2162. SendScintilla(SCI_INDICSETFORE, indicatorNumber, col);
  2163. SendScintilla(SCI_INDICSETALPHA, indicatorNumber, alpha);
  2164. }
  2165. }
  2166. }
  2167. // Set the indicator hover foreground colour.
  2168. void QsciScintilla::setIndicatorHoverForegroundColor(const QColor &col,
  2169. int indicatorNumber)
  2170. {
  2171. if (indicatorNumber < INDIC_IME)
  2172. {
  2173. // We ignore allocatedIndicators to allow any indicators defined
  2174. // elsewhere (e.g. in lexers) to be set.
  2175. if (indicatorNumber < 0)
  2176. {
  2177. for (int i = 0; i < INDIC_IME; ++i)
  2178. SendScintilla(SCI_INDICSETHOVERFORE, i, col);
  2179. }
  2180. else
  2181. {
  2182. SendScintilla(SCI_INDICSETHOVERFORE, indicatorNumber, col);
  2183. }
  2184. }
  2185. }
  2186. // Set the indicator hover style.
  2187. void QsciScintilla::setIndicatorHoverStyle(IndicatorStyle style,
  2188. int indicatorNumber)
  2189. {
  2190. if (indicatorNumber < INDIC_IME)
  2191. {
  2192. // We ignore allocatedIndicators to allow any indicators defined
  2193. // elsewhere (e.g. in lexers) to be set.
  2194. if (indicatorNumber < 0)
  2195. {
  2196. for (int i = 0; i < INDIC_IME; ++i)
  2197. SendScintilla(SCI_INDICSETHOVERSTYLE, i,
  2198. static_cast<long>(style));
  2199. }
  2200. else
  2201. {
  2202. SendScintilla(SCI_INDICSETHOVERSTYLE, indicatorNumber,
  2203. static_cast<long>(style));
  2204. }
  2205. }
  2206. }
  2207. // Set the indicator outline colour.
  2208. void QsciScintilla::setIndicatorOutlineColor(const QColor &col, int indicatorNumber)
  2209. {
  2210. if (indicatorNumber < INDIC_IME)
  2211. {
  2212. int alpha = col.alpha();
  2213. // We ignore allocatedIndicators to allow any indicators defined
  2214. // elsewhere (e.g. in lexers) to be set.
  2215. if (indicatorNumber < 0)
  2216. {
  2217. for (int i = 0; i < INDIC_IME; ++i)
  2218. SendScintilla(SCI_INDICSETOUTLINEALPHA, i, alpha);
  2219. }
  2220. else
  2221. {
  2222. SendScintilla(SCI_INDICSETOUTLINEALPHA, indicatorNumber, alpha);
  2223. }
  2224. }
  2225. }
  2226. // Fill a range with an indicator.
  2227. void QsciScintilla::fillIndicatorRange(int lineFrom, int indexFrom,
  2228. int lineTo, int indexTo, int indicatorNumber)
  2229. {
  2230. if (indicatorNumber < INDIC_IME)
  2231. {
  2232. int start = positionFromLineIndex(lineFrom, indexFrom);
  2233. int finish = positionFromLineIndex(lineTo, indexTo);
  2234. // We ignore allocatedIndicators to allow any indicators defined
  2235. // elsewhere (e.g. in lexers) to be set.
  2236. if (indicatorNumber < 0)
  2237. {
  2238. for (int i = 0; i < INDIC_IME; ++i)
  2239. {
  2240. SendScintilla(SCI_SETINDICATORCURRENT, i);
  2241. SendScintilla(SCI_INDICATORFILLRANGE, start, finish - start);
  2242. }
  2243. }
  2244. else
  2245. {
  2246. SendScintilla(SCI_SETINDICATORCURRENT, indicatorNumber);
  2247. SendScintilla(SCI_INDICATORFILLRANGE, start, finish - start);
  2248. }
  2249. }
  2250. }
  2251. // Clear a range with an indicator.
  2252. void QsciScintilla::clearIndicatorRange(int lineFrom, int indexFrom,
  2253. int lineTo, int indexTo, int indicatorNumber)
  2254. {
  2255. if (indicatorNumber < INDIC_IME)
  2256. {
  2257. int start = positionFromLineIndex(lineFrom, indexFrom);
  2258. int finish = positionFromLineIndex(lineTo, indexTo);
  2259. // We ignore allocatedIndicators to allow any indicators defined
  2260. // elsewhere (e.g. in lexers) to be set.
  2261. if (indicatorNumber < 0)
  2262. {
  2263. for (int i = 0; i < INDIC_IME; ++i)
  2264. {
  2265. SendScintilla(SCI_SETINDICATORCURRENT, i);
  2266. SendScintilla(SCI_INDICATORCLEARRANGE, start, finish - start);
  2267. }
  2268. }
  2269. else
  2270. {
  2271. SendScintilla(SCI_SETINDICATORCURRENT, indicatorNumber);
  2272. SendScintilla(SCI_INDICATORCLEARRANGE, start, finish - start);
  2273. }
  2274. }
  2275. }
  2276. // Define a marker based on a symbol.
  2277. int QsciScintilla::markerDefine(MarkerSymbol sym, int markerNumber)
  2278. {
  2279. checkMarker(markerNumber);
  2280. if (markerNumber >= 0)
  2281. SendScintilla(SCI_MARKERDEFINE, markerNumber, static_cast<long>(sym));
  2282. return markerNumber;
  2283. }
  2284. // Define a marker based on a character.
  2285. int QsciScintilla::markerDefine(char ch, int markerNumber)
  2286. {
  2287. checkMarker(markerNumber);
  2288. if (markerNumber >= 0)
  2289. SendScintilla(SCI_MARKERDEFINE, markerNumber,
  2290. static_cast<long>(SC_MARK_CHARACTER) + ch);
  2291. return markerNumber;
  2292. }
  2293. // Define a marker based on a QPixmap.
  2294. int QsciScintilla::markerDefine(const QPixmap &pm, int markerNumber)
  2295. {
  2296. checkMarker(markerNumber);
  2297. if (markerNumber >= 0)
  2298. SendScintilla(SCI_MARKERDEFINEPIXMAP, markerNumber, pm);
  2299. return markerNumber;
  2300. }
  2301. // Define a marker based on a QImage.
  2302. int QsciScintilla::markerDefine(const QImage &im, int markerNumber)
  2303. {
  2304. checkMarker(markerNumber);
  2305. if (markerNumber >= 0)
  2306. {
  2307. SendScintilla(SCI_RGBAIMAGESETHEIGHT, im.height());
  2308. SendScintilla(SCI_RGBAIMAGESETWIDTH, im.width());
  2309. SendScintilla(SCI_MARKERDEFINERGBAIMAGE, markerNumber, im);
  2310. }
  2311. return markerNumber;
  2312. }
  2313. // Add a marker to a line.
  2314. int QsciScintilla::markerAdd(int linenr, int markerNumber)
  2315. {
  2316. if (markerNumber < 0 || markerNumber > MARKER_MAX || (allocatedMarkers & (1 << markerNumber)) == 0)
  2317. return -1;
  2318. return SendScintilla(SCI_MARKERADD, linenr, markerNumber);
  2319. }
  2320. // Get the marker mask for a line.
  2321. unsigned QsciScintilla::markersAtLine(int linenr) const
  2322. {
  2323. return SendScintilla(SCI_MARKERGET, linenr);
  2324. }
  2325. // Delete a marker from a line.
  2326. void QsciScintilla::markerDelete(int linenr, int markerNumber)
  2327. {
  2328. if (markerNumber <= MARKER_MAX)
  2329. {
  2330. if (markerNumber < 0)
  2331. {
  2332. unsigned am = allocatedMarkers;
  2333. for (int m = 0; m <= MARKER_MAX; ++m)
  2334. {
  2335. if (am & 1)
  2336. SendScintilla(SCI_MARKERDELETE, linenr, m);
  2337. am >>= 1;
  2338. }
  2339. }
  2340. else if (allocatedMarkers & (1 << markerNumber))
  2341. SendScintilla(SCI_MARKERDELETE, linenr, markerNumber);
  2342. }
  2343. }
  2344. // Delete a marker from the text.
  2345. void QsciScintilla::markerDeleteAll(int markerNumber)
  2346. {
  2347. if (markerNumber <= MARKER_MAX)
  2348. {
  2349. if (markerNumber < 0)
  2350. SendScintilla(SCI_MARKERDELETEALL, -1);
  2351. else if (allocatedMarkers & (1 << markerNumber))
  2352. SendScintilla(SCI_MARKERDELETEALL, markerNumber);
  2353. }
  2354. }
  2355. // Delete a marker handle from the text.
  2356. void QsciScintilla::markerDeleteHandle(int mhandle)
  2357. {
  2358. SendScintilla(SCI_MARKERDELETEHANDLE, mhandle);
  2359. }
  2360. // Return the line containing a marker instance.
  2361. int QsciScintilla::markerLine(int mhandle) const
  2362. {
  2363. return SendScintilla(SCI_MARKERLINEFROMHANDLE, mhandle);
  2364. }
  2365. // Search forwards for a marker.
  2366. int QsciScintilla::markerFindNext(int linenr, unsigned mask) const
  2367. {
  2368. return SendScintilla(SCI_MARKERNEXT, linenr, mask);
  2369. }
  2370. // Search backwards for a marker.
  2371. int QsciScintilla::markerFindPrevious(int linenr, unsigned mask) const
  2372. {
  2373. return SendScintilla(SCI_MARKERPREVIOUS, linenr, mask);
  2374. }
  2375. // Set the marker background colour.
  2376. void QsciScintilla::setMarkerBackgroundColor(const QColor &col, int markerNumber)
  2377. {
  2378. if (markerNumber <= MARKER_MAX)
  2379. {
  2380. int alpha = col.alpha();
  2381. // An opaque background would make the text invisible.
  2382. if (alpha == 255)
  2383. alpha = SC_ALPHA_NOALPHA;
  2384. if (markerNumber < 0)
  2385. {
  2386. unsigned am = allocatedMarkers;
  2387. for (int m = 0; m <= MARKER_MAX; ++m)
  2388. {
  2389. if (am & 1)
  2390. {
  2391. SendScintilla(SCI_MARKERSETBACK, m, col);
  2392. SendScintilla(SCI_MARKERSETALPHA, m, alpha);
  2393. }
  2394. am >>= 1;
  2395. }
  2396. }
  2397. else if (allocatedMarkers & (1 << markerNumber))
  2398. {
  2399. SendScintilla(SCI_MARKERSETBACK, markerNumber, col);
  2400. SendScintilla(SCI_MARKERSETALPHA, markerNumber, alpha);
  2401. }
  2402. }
  2403. }
  2404. // Set the marker foreground colour.
  2405. void QsciScintilla::setMarkerForegroundColor(const QColor &col, int markerNumber)
  2406. {
  2407. if (markerNumber <= MARKER_MAX)
  2408. {
  2409. if (markerNumber < 0)
  2410. {
  2411. unsigned am = allocatedMarkers;
  2412. for (int m = 0; m <= MARKER_MAX; ++m)
  2413. {
  2414. if (am & 1)
  2415. SendScintilla(SCI_MARKERSETFORE, m, col);
  2416. am >>= 1;
  2417. }
  2418. }
  2419. else if (allocatedMarkers & (1 << markerNumber))
  2420. {
  2421. SendScintilla(SCI_MARKERSETFORE, markerNumber, col);
  2422. }
  2423. }
  2424. }
  2425. // Check a marker, allocating a marker number if necessary.
  2426. void QsciScintilla::checkMarker(int &markerNumber)
  2427. {
  2428. allocateId(markerNumber, allocatedMarkers, 0, MARKER_MAX);
  2429. }
  2430. // Check an indicator, allocating an indicator number if necessary.
  2431. void QsciScintilla::checkIndicator(int &indicatorNumber)
  2432. {
  2433. allocateId(indicatorNumber, allocatedIndicators, INDIC_CONTAINER,
  2434. INDIC_IME - 1);
  2435. }
  2436. // Make sure an identifier is valid and allocate it if necessary.
  2437. void QsciScintilla::allocateId(int &id, unsigned &allocated, int min, int max)
  2438. {
  2439. if (id >= 0)
  2440. {
  2441. // Note that we allow existing identifiers to be explicitly redefined.
  2442. if (id > max)
  2443. id = -1;
  2444. }
  2445. else
  2446. {
  2447. unsigned aids = allocated >> min;
  2448. // Find the smallest unallocated identifier.
  2449. for (id = min; id <= max; ++id)
  2450. {
  2451. if ((aids & 1) == 0)
  2452. break;
  2453. aids >>= 1;
  2454. }
  2455. }
  2456. // Allocate the identifier if it is valid.
  2457. if (id >= 0)
  2458. allocated |= (1 << id);
  2459. }
  2460. // Reset the fold margin colours.
  2461. void QsciScintilla::resetFoldMarginColors()
  2462. {
  2463. SendScintilla(SCI_SETFOLDMARGINHICOLOUR, 0, 0L);
  2464. SendScintilla(SCI_SETFOLDMARGINCOLOUR, 0, 0L);
  2465. }
  2466. // Set the fold margin colours.
  2467. void QsciScintilla::setFoldMarginColors(const QColor &fore, const QColor &back)
  2468. {
  2469. SendScintilla(SCI_SETFOLDMARGINHICOLOUR, 1, fore);
  2470. SendScintilla(SCI_SETFOLDMARGINCOLOUR, 1, back);
  2471. }
  2472. // Set the call tips background colour.
  2473. void QsciScintilla::setCallTipsBackgroundColor(const QColor &col)
  2474. {
  2475. SendScintilla(SCI_CALLTIPSETBACK, col);
  2476. }
  2477. // Set the call tips foreground colour.
  2478. void QsciScintilla::setCallTipsForegroundColor(const QColor &col)
  2479. {
  2480. SendScintilla(SCI_CALLTIPSETFORE, col);
  2481. }
  2482. // Set the call tips highlight colour.
  2483. void QsciScintilla::setCallTipsHighlightColor(const QColor &col)
  2484. {
  2485. SendScintilla(SCI_CALLTIPSETFOREHLT, col);
  2486. }
  2487. // Set the matched brace background colour.
  2488. void QsciScintilla::setMatchedBraceBackgroundColor(const QColor &col)
  2489. {
  2490. SendScintilla(SCI_STYLESETBACK, STYLE_BRACELIGHT, col);
  2491. }
  2492. // Set the matched brace foreground colour.
  2493. void QsciScintilla::setMatchedBraceForegroundColor(const QColor &col)
  2494. {
  2495. SendScintilla(SCI_STYLESETFORE, STYLE_BRACELIGHT, col);
  2496. }
  2497. // Set the matched brace indicator.
  2498. void QsciScintilla::setMatchedBraceIndicator(int indicatorNumber)
  2499. {
  2500. SendScintilla(SCI_BRACEHIGHLIGHTINDICATOR, 1, indicatorNumber);
  2501. }
  2502. // Reset the matched brace indicator.
  2503. void QsciScintilla::resetMatchedBraceIndicator()
  2504. {
  2505. SendScintilla(SCI_BRACEHIGHLIGHTINDICATOR, 0, 0L);
  2506. }
  2507. // Set the unmatched brace background colour.
  2508. void QsciScintilla::setUnmatchedBraceBackgroundColor(const QColor &col)
  2509. {
  2510. SendScintilla(SCI_STYLESETBACK, STYLE_BRACEBAD, col);
  2511. }
  2512. // Set the unmatched brace foreground colour.
  2513. void QsciScintilla::setUnmatchedBraceForegroundColor(const QColor &col)
  2514. {
  2515. SendScintilla(SCI_STYLESETFORE, STYLE_BRACEBAD, col);
  2516. }
  2517. // Set the unmatched brace indicator.
  2518. void QsciScintilla::setUnmatchedBraceIndicator(int indicatorNumber)
  2519. {
  2520. SendScintilla(SCI_BRACEBADLIGHTINDICATOR, 1, indicatorNumber);
  2521. }
  2522. // Reset the unmatched brace indicator.
  2523. void QsciScintilla::resetUnmatchedBraceIndicator()
  2524. {
  2525. SendScintilla(SCI_BRACEBADLIGHTINDICATOR, 0, 0L);
  2526. }
  2527. // Detach any lexer.
  2528. void QsciScintilla::detachLexer()
  2529. {
  2530. if (!lex.isNull())
  2531. {
  2532. lex->setEditor(0);
  2533. lex->disconnect(this);
  2534. SendScintilla(SCI_STYLERESETDEFAULT);
  2535. SendScintilla(SCI_STYLECLEARALL);
  2536. }
  2537. }
  2538. // Set the lexer.
  2539. void QsciScintilla::setLexer(QsciLexer *lexer)
  2540. {
  2541. // Detach any current lexer.
  2542. detachLexer();
  2543. // Connect up the new lexer.
  2544. lex = lexer;
  2545. if (lex)
  2546. {
  2547. SendScintilla(SCI_CLEARDOCUMENTSTYLE);
  2548. if (lex->lexer())
  2549. SendScintilla(SCI_SETLEXERLANGUAGE, lex->lexer());
  2550. else
  2551. SendScintilla(SCI_SETLEXER, lex->lexerId());
  2552. lex->setEditor(this);
  2553. connect(lex,SIGNAL(colorChanged(const QColor &, int)),
  2554. SLOT(handleStyleColorChange(const QColor &, int)));
  2555. connect(lex,SIGNAL(eolFillChanged(bool, int)),
  2556. SLOT(handleStyleEolFillChange(bool, int)));
  2557. connect(lex,SIGNAL(fontChanged(const QFont &, int)),
  2558. SLOT(handleStyleFontChange(const QFont &, int)));
  2559. connect(lex,SIGNAL(paperChanged(const QColor &, int)),
  2560. SLOT(handleStylePaperChange(const QColor &, int)));
  2561. connect(lex,SIGNAL(propertyChanged(const char *, const char *)),
  2562. SLOT(handlePropertyChange(const char *, const char *)));
  2563. SendScintilla(SCI_SETPROPERTY, "fold", "1");
  2564. SendScintilla(SCI_SETPROPERTY, "fold.html", "1");
  2565. // Set the keywords. Scintilla allows for sets numbered 0 to
  2566. // KEYWORDSET_MAX (although the lexers only seem to exploit 0 to
  2567. // KEYWORDSET_MAX - 1). We number from 1 in line with SciTE's property
  2568. // files.
  2569. for (int k = 0; k <= KEYWORDSET_MAX; ++k)
  2570. {
  2571. const char *kw = lex -> keywords(k + 1);
  2572. if (!kw)
  2573. kw = "";
  2574. SendScintilla(SCI_SETKEYWORDS, k, kw);
  2575. }
  2576. // Initialise each style. Do the default first so its (possibly
  2577. // incorrect) font setting gets reset when style 0 is set.
  2578. setLexerStyle(STYLE_DEFAULT);
  2579. int nrStyles = 1 << SendScintilla(SCI_GETSTYLEBITS);
  2580. for (int s = 0; s < nrStyles; ++s)
  2581. if (!lex->description(s).isEmpty())
  2582. setLexerStyle(s);
  2583. // Initialise the properties.
  2584. lex->refreshProperties();
  2585. // Set the auto-completion fillups and word separators.
  2586. setAutoCompletionFillupsEnabled(fillups_enabled);
  2587. wseps = lex->autoCompletionWordSeparators();
  2588. wchars = lex->wordCharacters();
  2589. if (!wchars)
  2590. wchars = defaultWordChars;
  2591. SendScintilla(SCI_AUTOCSETIGNORECASE, !lex->caseSensitive());
  2592. recolor();
  2593. }
  2594. else
  2595. {
  2596. SendScintilla(SCI_SETLEXER, SCLEX_CONTAINER);
  2597. setColor(nl_text_colour);
  2598. setPaper(nl_paper_colour);
  2599. SendScintilla(SCI_AUTOCSETFILLUPS, "");
  2600. SendScintilla(SCI_AUTOCSETIGNORECASE, false);
  2601. wseps.clear();
  2602. wchars = defaultWordChars;
  2603. }
  2604. }
  2605. // Set a particular style of the current lexer.
  2606. void QsciScintilla::setLexerStyle(int style)
  2607. {
  2608. handleStyleColorChange(lex->color(style), style);
  2609. handleStyleEolFillChange(lex->eolFill(style), style);
  2610. handleStyleFontChange(lex->font(style), style);
  2611. handleStylePaperChange(lex->paper(style), style);
  2612. }
  2613. // Get the current lexer.
  2614. QsciLexer *QsciScintilla::lexer() const
  2615. {
  2616. return lex;
  2617. }
  2618. // Handle a change in lexer style foreground colour.
  2619. void QsciScintilla::handleStyleColorChange(const QColor &c, int style)
  2620. {
  2621. SendScintilla(SCI_STYLESETFORE, style, c);
  2622. }
  2623. // Handle a change in lexer style end-of-line fill.
  2624. void QsciScintilla::handleStyleEolFillChange(bool eolfill, int style)
  2625. {
  2626. SendScintilla(SCI_STYLESETEOLFILLED, style, eolfill);
  2627. }
  2628. // Handle a change in lexer style font.
  2629. void QsciScintilla::handleStyleFontChange(const QFont &f, int style)
  2630. {
  2631. setStylesFont(f, style);
  2632. if (style == lex->braceStyle())
  2633. {
  2634. setStylesFont(f, STYLE_BRACELIGHT);
  2635. setStylesFont(f, STYLE_BRACEBAD);
  2636. }
  2637. }
  2638. // Set the font for a style.
  2639. void QsciScintilla::setStylesFont(const QFont &f, int style)
  2640. {
  2641. SendScintilla(SCI_STYLESETFONT, style, f.family().toLatin1().data());
  2642. SendScintilla(SCI_STYLESETSIZEFRACTIONAL, style,
  2643. long(f.pointSizeF() * SC_FONT_SIZE_MULTIPLIER));
  2644. // Pass the Qt weight via the back door.
  2645. SendScintilla(SCI_STYLESETWEIGHT, style, -f.weight());
  2646. SendScintilla(SCI_STYLESETITALIC, style, f.italic());
  2647. SendScintilla(SCI_STYLESETUNDERLINE, style, f.underline());
  2648. // Tie the font settings of the default style to that of style 0 (the style
  2649. // conventionally used for whitespace by lexers). This is needed so that
  2650. // fold marks, indentations, edge columns etc are set properly.
  2651. if (style == 0)
  2652. setStylesFont(f, STYLE_DEFAULT);
  2653. }
  2654. // Handle a change in lexer style background colour.
  2655. void QsciScintilla::handleStylePaperChange(const QColor &c, int style)
  2656. {
  2657. SendScintilla(SCI_STYLESETBACK, style, c);
  2658. }
  2659. // Handle a change in lexer property.
  2660. void QsciScintilla::handlePropertyChange(const char *prop, const char *val)
  2661. {
  2662. SendScintilla(SCI_SETPROPERTY, prop, val);
  2663. }
  2664. // Handle a change to the user visible user interface.
  2665. void QsciScintilla::handleUpdateUI(int)
  2666. {
  2667. int newPos = SendScintilla(SCI_GETCURRENTPOS);
  2668. if (newPos != oldPos)
  2669. {
  2670. oldPos = newPos;
  2671. int line = SendScintilla(SCI_LINEFROMPOSITION, newPos);
  2672. int col = SendScintilla(SCI_GETCOLUMN, newPos);
  2673. emit cursorPositionChanged(line, col);
  2674. }
  2675. if (braceMode != BraceMatch::NoBraceMatch)
  2676. braceMatch();
  2677. }
  2678. // Handle brace matching.
  2679. void QsciScintilla::braceMatch()
  2680. {
  2681. long braceAtCaret, braceOpposite;
  2682. findMatchingBrace(braceAtCaret, braceOpposite, braceMode);
  2683. if (braceAtCaret >= 0 && braceOpposite < 0)
  2684. {
  2685. SendScintilla(SCI_BRACEBADLIGHT, braceAtCaret);
  2686. SendScintilla(SCI_SETHIGHLIGHTGUIDE, 0UL);
  2687. }
  2688. else
  2689. {
  2690. char chBrace = SendScintilla(SCI_GETCHARAT, braceAtCaret);
  2691. SendScintilla(SCI_BRACEHIGHLIGHT, braceAtCaret, braceOpposite);
  2692. long columnAtCaret = SendScintilla(SCI_GETCOLUMN, braceAtCaret);
  2693. long columnOpposite = SendScintilla(SCI_GETCOLUMN, braceOpposite);
  2694. if (chBrace == ':')
  2695. {
  2696. long lineStart = SendScintilla(SCI_LINEFROMPOSITION, braceAtCaret);
  2697. long indentPos = SendScintilla(SCI_GETLINEINDENTPOSITION,
  2698. lineStart);
  2699. long indentPosNext = SendScintilla(SCI_GETLINEINDENTPOSITION,
  2700. lineStart + 1);
  2701. columnAtCaret = SendScintilla(SCI_GETCOLUMN, indentPos);
  2702. long columnAtCaretNext = SendScintilla(SCI_GETCOLUMN,
  2703. indentPosNext);
  2704. long indentSize = SendScintilla(SCI_GETINDENT);
  2705. if (columnAtCaretNext - indentSize > 1)
  2706. columnAtCaret = columnAtCaretNext - indentSize;
  2707. if (columnOpposite == 0)
  2708. columnOpposite = columnAtCaret;
  2709. }
  2710. long column = columnAtCaret;
  2711. if (column > columnOpposite)
  2712. column = columnOpposite;
  2713. SendScintilla(SCI_SETHIGHLIGHTGUIDE, column);
  2714. }
  2715. }
  2716. // Check if the character at a position is a brace.
  2717. long QsciScintilla::checkBrace(long pos, int brace_style, bool &colonMode)
  2718. {
  2719. long brace_pos = -1;
  2720. char ch = SendScintilla(SCI_GETCHARAT, pos);
  2721. if (ch == ':')
  2722. {
  2723. // A bit of a hack, we should really use a virtual.
  2724. if (!lex.isNull() && qstrcmp(lex->lexer(), "python") == 0)
  2725. {
  2726. brace_pos = pos;
  2727. colonMode = true;
  2728. }
  2729. }
  2730. else if (ch && strchr("[](){}<>", ch))
  2731. {
  2732. if (brace_style < 0)
  2733. brace_pos = pos;
  2734. else
  2735. {
  2736. int style = SendScintilla(SCI_GETSTYLEAT, pos) & 0x1f;
  2737. if (style == brace_style)
  2738. brace_pos = pos;
  2739. }
  2740. }
  2741. return brace_pos;
  2742. }
  2743. // Find a brace and it's match. Return true if the current position is inside
  2744. // a pair of braces.
  2745. bool QsciScintilla::findMatchingBrace(long &brace, long &other,BraceMatch mode)
  2746. {
  2747. bool colonMode = false;
  2748. int brace_style = (lex.isNull() ? -1 : lex->braceStyle());
  2749. brace = -1;
  2750. other = -1;
  2751. long caretPos = SendScintilla(SCI_GETCURRENTPOS);
  2752. if (caretPos > 0)
  2753. brace = checkBrace(caretPos - 1, brace_style, colonMode);
  2754. bool isInside = false;
  2755. if (brace < 0 && mode == BraceMatch::SloppyBraceMatch)
  2756. {
  2757. brace = checkBrace(caretPos, brace_style, colonMode);
  2758. if (brace >= 0 && !colonMode)
  2759. isInside = true;
  2760. }
  2761. if (brace >= 0)
  2762. {
  2763. if (colonMode)
  2764. {
  2765. // Find the end of the Python indented block.
  2766. long lineStart = SendScintilla(SCI_LINEFROMPOSITION, brace);
  2767. long lineMaxSubord = SendScintilla(SCI_GETLASTCHILD, lineStart, -1);
  2768. other = SendScintilla(SCI_GETLINEENDPOSITION, lineMaxSubord);
  2769. }
  2770. else
  2771. other = SendScintilla(SCI_BRACEMATCH, brace, 0L);
  2772. if (other > brace)
  2773. isInside = !isInside;
  2774. }
  2775. return isInside;
  2776. }
  2777. // Move to the matching brace.
  2778. void QsciScintilla::moveToMatchingBrace()
  2779. {
  2780. gotoMatchingBrace(false);
  2781. }
  2782. // Select to the matching brace.
  2783. void QsciScintilla::selectToMatchingBrace()
  2784. {
  2785. gotoMatchingBrace(true);
  2786. }
  2787. // Move to the matching brace and optionally select the text.
  2788. void QsciScintilla::gotoMatchingBrace(bool select)
  2789. {
  2790. long braceAtCaret;
  2791. long braceOpposite;
  2792. bool isInside = findMatchingBrace(braceAtCaret, braceOpposite,
  2793. BraceMatch::SloppyBraceMatch);
  2794. if (braceOpposite >= 0)
  2795. {
  2796. // Convert the character positions into caret positions based on
  2797. // whether the caret position was inside or outside the braces.
  2798. if (isInside)
  2799. {
  2800. if (braceOpposite > braceAtCaret)
  2801. braceAtCaret++;
  2802. else
  2803. braceOpposite++;
  2804. }
  2805. else
  2806. {
  2807. if (braceOpposite > braceAtCaret)
  2808. braceOpposite++;
  2809. else
  2810. braceAtCaret++;
  2811. }
  2812. ensureLineVisible(SendScintilla(SCI_LINEFROMPOSITION, braceOpposite));
  2813. if (select)
  2814. SendScintilla(SCI_SETSEL, braceAtCaret, braceOpposite);
  2815. else
  2816. SendScintilla(SCI_SETSEL, braceOpposite, braceOpposite);
  2817. }
  2818. }
  2819. // Return a position from a line number and an index within the line.
  2820. int QsciScintilla::positionFromLineIndex(int line, int index) const
  2821. {
  2822. int pos = SendScintilla(SCI_POSITIONFROMLINE, line);
  2823. // Allow for multi-byte characters.
  2824. for(int i = 0; i < index; i++)
  2825. pos = SendScintilla(SCI_POSITIONAFTER, pos);
  2826. return pos;
  2827. }
  2828. // Return a line number and an index within the line from a position.
  2829. void QsciScintilla::lineIndexFromPosition(int position, int *line, int *index) const
  2830. {
  2831. int lin = SendScintilla(SCI_LINEFROMPOSITION, position);
  2832. int linpos = SendScintilla(SCI_POSITIONFROMLINE, lin);
  2833. int indx = 0;
  2834. // Allow for multi-byte characters.
  2835. while (linpos < position)
  2836. {
  2837. int new_linpos = SendScintilla(SCI_POSITIONAFTER, linpos);
  2838. // If the position hasn't moved then we must be at the end of the text
  2839. // (which implies that the position passed was beyond the end of the
  2840. // text).
  2841. if (new_linpos == linpos)
  2842. break;
  2843. linpos = new_linpos;
  2844. ++indx;
  2845. }
  2846. *line = lin;
  2847. *index = indx;
  2848. }
  2849. // Set the source of the automatic auto-completion list.
  2850. void QsciScintilla::setAutoCompletionSource(AutoCompletionSource source)
  2851. {
  2852. acSource = source;
  2853. }
  2854. // Set the threshold for automatic auto-completion.
  2855. void QsciScintilla::setAutoCompletionThreshold(int thresh)
  2856. {
  2857. acThresh = thresh;
  2858. }
  2859. // Set the auto-completion word separators if there is no current lexer.
  2860. void QsciScintilla::setAutoCompletionWordSeparators(const QStringList &separators)
  2861. {
  2862. if (lex.isNull())
  2863. wseps = separators;
  2864. }
  2865. // Explicitly auto-complete from all sources.
  2866. void QsciScintilla::autoCompleteFromAll()
  2867. {
  2868. startAutoCompletion(AutoCompletionSource::AcsAll, false, use_single != AcusNever);
  2869. }
  2870. // Explicitly auto-complete from the APIs.
  2871. void QsciScintilla::autoCompleteFromAPIs()
  2872. {
  2873. startAutoCompletion(AutoCompletionSource::AcsAPIs, false, use_single != AcusNever);
  2874. }
  2875. // Explicitly auto-complete from the document.
  2876. void QsciScintilla::autoCompleteFromDocument()
  2877. {
  2878. startAutoCompletion(AutoCompletionSource::AcsDocument, false, use_single != AcusNever);
  2879. }
  2880. // Check if a character can be in a word.
  2881. bool QsciScintilla::isWordCharacter(char ch) const
  2882. {
  2883. return (strchr(wchars, ch) != NULL);
  2884. }
  2885. // Return the set of valid word characters.
  2886. const char *QsciScintilla::wordCharacters() const
  2887. {
  2888. return wchars;
  2889. }
  2890. // Recolour the document.
  2891. void QsciScintilla::recolor(int start, int end)
  2892. {
  2893. SendScintilla(SCI_COLOURISE, start, end);
  2894. }
  2895. // Registered a QPixmap image.
  2896. void QsciScintilla::registerImage(int id, const QPixmap &pm)
  2897. {
  2898. SendScintilla(SCI_REGISTERIMAGE, id, pm);
  2899. }
  2900. // Registered a QImage image.
  2901. void QsciScintilla::registerImage(int id, const QImage &im)
  2902. {
  2903. SendScintilla(SCI_RGBAIMAGESETHEIGHT, im.height());
  2904. SendScintilla(SCI_RGBAIMAGESETWIDTH, im.width());
  2905. SendScintilla(SCI_REGISTERRGBAIMAGE, id, im);
  2906. }
  2907. // Clear all registered images.
  2908. void QsciScintilla::clearRegisteredImages()
  2909. {
  2910. SendScintilla(SCI_CLEARREGISTEREDIMAGES);
  2911. }
  2912. // Enable auto-completion fill-ups.
  2913. void QsciScintilla::setAutoCompletionFillupsEnabled(bool enable)
  2914. {
  2915. const char *fillups;
  2916. if (!enable)
  2917. fillups = "";
  2918. else if (!lex.isNull())
  2919. fillups = lex->autoCompletionFillups();
  2920. else
  2921. fillups = explicit_fillups.data();
  2922. SendScintilla(SCI_AUTOCSETFILLUPS, fillups);
  2923. fillups_enabled = enable;
  2924. }
  2925. // See if auto-completion fill-ups are enabled.
  2926. bool QsciScintilla::autoCompletionFillupsEnabled() const
  2927. {
  2928. return fillups_enabled;
  2929. }
  2930. // Set the fill-up characters for auto-completion if there is no current lexer.
  2931. void QsciScintilla::setAutoCompletionFillups(const char *fillups)
  2932. {
  2933. explicit_fillups = fillups;
  2934. setAutoCompletionFillupsEnabled(fillups_enabled);
  2935. }
  2936. // Set the case sensitivity for auto-completion.
  2937. void QsciScintilla::setAutoCompletionCaseSensitivity(bool cs)
  2938. {
  2939. SendScintilla(SCI_AUTOCSETIGNORECASE, !cs);
  2940. }
  2941. // Return the case sensitivity for auto-completion.
  2942. bool QsciScintilla::autoCompletionCaseSensitivity() const
  2943. {
  2944. return !SendScintilla(SCI_AUTOCGETIGNORECASE);
  2945. }
  2946. // Set the replace word mode for auto-completion.
  2947. void QsciScintilla::setAutoCompletionReplaceWord(bool replace)
  2948. {
  2949. SendScintilla(SCI_AUTOCSETDROPRESTOFWORD, replace);
  2950. }
  2951. // Return the replace word mode for auto-completion.
  2952. bool QsciScintilla::autoCompletionReplaceWord() const
  2953. {
  2954. return SendScintilla(SCI_AUTOCGETDROPRESTOFWORD);
  2955. }
  2956. // Set the single item mode for auto-completion.
  2957. void QsciScintilla::setAutoCompletionUseSingle(AutoCompletionUseSingle single)
  2958. {
  2959. use_single = single;
  2960. }
  2961. // Return the single item mode for auto-completion.
  2962. QsciScintilla::AutoCompletionUseSingle QsciScintilla::autoCompletionUseSingle() const
  2963. {
  2964. return use_single;
  2965. }
  2966. // Set the single item mode for auto-completion (deprecated).
  2967. void QsciScintilla::setAutoCompletionShowSingle(bool single)
  2968. {
  2969. use_single = (single ? AcusExplicit : AcusNever);
  2970. }
  2971. // Return the single item mode for auto-completion (deprecated).
  2972. bool QsciScintilla::autoCompletionShowSingle() const
  2973. {
  2974. return (use_single != AcusNever);
  2975. }
  2976. // Set current call tip position.
  2977. void QsciScintilla::setCallTipsPosition(CallTipsPosition position)
  2978. {
  2979. SendScintilla(SCI_CALLTIPSETPOSITION, (position == CallTipsPosition::CallTipsAboveText));
  2980. call_tips_position = position;
  2981. }
  2982. // Set current call tip style.
  2983. void QsciScintilla::setCallTipsStyle(CallTipsStyle style)
  2984. {
  2985. call_tips_style = style;
  2986. }
  2987. // Set maximum number of call tips displayed.
  2988. void QsciScintilla::setCallTipsVisible(int nr)
  2989. {
  2990. maxCallTips = nr;
  2991. }
  2992. // Set the document to display.
  2993. void QsciScintilla::setDocument(const QsciDocument &document)
  2994. {
  2995. if (doc.pdoc != document.pdoc)
  2996. {
  2997. doc.undisplay(this);
  2998. doc.attach(document);
  2999. doc.display(this,&document);
  3000. }
  3001. }
  3002. // Ensure the document is read-write and return true if was was read-only.
  3003. bool QsciScintilla::ensureRW()
  3004. {
  3005. bool ro = isReadOnly();
  3006. if (ro)
  3007. setReadOnly(false);
  3008. return ro;
  3009. }
  3010. // Return the number of the first visible line.
  3011. int QsciScintilla::firstVisibleLine() const
  3012. {
  3013. return SendScintilla(SCI_GETFIRSTVISIBLELINE);
  3014. }
  3015. // Set the number of the first visible line.
  3016. void QsciScintilla::setFirstVisibleLine(int linenr)
  3017. {
  3018. SendScintilla(SCI_SETFIRSTVISIBLELINE, linenr);
  3019. }
  3020. // Return the height in pixels of the text in a particular line.
  3021. int QsciScintilla::textHeight(int linenr) const
  3022. {
  3023. return SendScintilla(SCI_TEXTHEIGHT, linenr);
  3024. }
  3025. // See if auto-completion or user list is active.
  3026. bool QsciScintilla::isListActive() const
  3027. {
  3028. return SendScintilla(SCI_AUTOCACTIVE);
  3029. }
  3030. // Cancel any current auto-completion or user list.
  3031. void QsciScintilla::cancelList()
  3032. {
  3033. SendScintilla(SCI_AUTOCCANCEL);
  3034. }
  3035. // Handle a selection from the auto-completion list.
  3036. void QsciScintilla::handleAutoCompletionSelection()
  3037. {
  3038. if (!lex.isNull())
  3039. {
  3040. QsciAbstractAPIs *apis = lex->apis();
  3041. if (apis)
  3042. apis->autoCompletionSelected(acSelection);
  3043. }
  3044. }
  3045. // Display a user list.
  3046. void QsciScintilla::showUserList(int id, const QStringList &list)
  3047. {
  3048. // Sanity check to make sure auto-completion doesn't get confused.
  3049. if (id <= 0)
  3050. return;
  3051. SendScintilla(SCI_AUTOCSETSEPARATOR, userSeparator);
  3052. ScintillaBytes s = textAsBytes(list.join(QChar(userSeparator)));
  3053. SendScintilla(SCI_USERLISTSHOW, id, ScintillaBytesConstData(s));
  3054. }
  3055. // Translate the SCN_USERLISTSELECTION notification into something more useful.
  3056. void QsciScintilla::handleUserListSelection(const char *text, int id)
  3057. {
  3058. emit userListActivated(id, QString(text));
  3059. // Make sure the editor hasn't been deactivated as a side effect.
  3060. activateWindow();
  3061. }
  3062. // Return the case sensitivity of any lexer.
  3063. bool QsciScintilla::caseSensitive() const
  3064. {
  3065. return lex.isNull() ? true : lex->caseSensitive();
  3066. }
  3067. // Return true if the current list is an auto-completion list rather than a
  3068. // user list.
  3069. bool QsciScintilla::isAutoCompletionList() const
  3070. {
  3071. return (SendScintilla(SCI_AUTOCGETSEPARATOR) == acSeparator);
  3072. }
  3073. // Read the text from a QIODevice.
  3074. bool QsciScintilla::read(QIODevice *io)
  3075. {
  3076. const int min_size = 1024 * 8;
  3077. int buf_size = min_size;
  3078. char *buf = new char[buf_size];
  3079. int data_len = 0;
  3080. bool ok = true;
  3081. qint64 part;
  3082. // Read the whole lot in so we don't have to worry about character
  3083. // boundaries.
  3084. do
  3085. {
  3086. // Make sure there is a minimum amount of room.
  3087. if (buf_size - data_len < min_size)
  3088. {
  3089. buf_size *= 2;
  3090. char *new_buf = new char[buf_size * 2];
  3091. memcpy(new_buf, buf, data_len);
  3092. delete[] buf;
  3093. buf = new_buf;
  3094. }
  3095. part = io->read(buf + data_len, buf_size - data_len - 1);
  3096. data_len += part;
  3097. }
  3098. while (part > 0);
  3099. if (part < 0)
  3100. ok = false;
  3101. else
  3102. {
  3103. buf[data_len] = '\0';
  3104. bool ro = ensureRW();
  3105. SendScintilla(SCI_SETTEXT, buf);
  3106. SendScintilla(SCI_EMPTYUNDOBUFFER);
  3107. setReadOnly(ro);
  3108. }
  3109. delete[] buf;
  3110. return ok;
  3111. }
  3112. // Write the text to a QIODevice.
  3113. bool QsciScintilla::write(QIODevice *io) const
  3114. {
  3115. const char *buf = reinterpret_cast<const char *>(SendScintillaPtrResult(SCI_GETCHARACTERPOINTER));
  3116. const char *bp = buf;
  3117. uint buflen = qstrlen(buf);
  3118. while (buflen > 0)
  3119. {
  3120. qint64 part = io->write(bp, buflen);
  3121. if (part < 0)
  3122. return false;
  3123. bp += part;
  3124. buflen -= part;
  3125. }
  3126. return true;
  3127. }
  3128. // Return the word at the given coordinates.
  3129. QString QsciScintilla::wordAtLineIndex(int line, int index) const
  3130. {
  3131. return wordAtPosition(positionFromLineIndex(line, index));
  3132. }
  3133. // Return the word at the given coordinates.
  3134. QString QsciScintilla::wordAtPoint(const QPoint &point) const
  3135. {
  3136. long close_pos = SendScintilla(SCI_POSITIONFROMPOINTCLOSE, point.x(),
  3137. point.y());
  3138. return wordAtPosition(close_pos);
  3139. }
  3140. // Return the word at the given position.
  3141. QString QsciScintilla::wordAtPosition(int position) const
  3142. {
  3143. if (position < 0)
  3144. return QString();
  3145. long start_pos = SendScintilla(SCI_WORDSTARTPOSITION, position, true);
  3146. long end_pos = SendScintilla(SCI_WORDENDPOSITION, position, true);
  3147. if (start_pos >= end_pos)
  3148. return QString();
  3149. return text(start_pos, end_pos);
  3150. }
  3151. // Return the display style for annotations.
  3152. QsciScintilla::AnnotationDisplay QsciScintilla::annotationDisplay() const
  3153. {
  3154. return (AnnotationDisplay)SendScintilla(SCI_ANNOTATIONGETVISIBLE);
  3155. }
  3156. // Set the display style for annotations.
  3157. void QsciScintilla::setAnnotationDisplay(QsciScintilla::AnnotationDisplay display)
  3158. {
  3159. SendScintilla(SCI_ANNOTATIONSETVISIBLE, display);
  3160. setScrollBars();
  3161. }
  3162. // Clear annotations.
  3163. void QsciScintilla::clearAnnotations(int line)
  3164. {
  3165. if (line >= 0)
  3166. SendScintilla(SCI_ANNOTATIONSETTEXT, line, (const char *)0);
  3167. else
  3168. SendScintilla(SCI_ANNOTATIONCLEARALL);
  3169. setScrollBars();
  3170. }
  3171. // Annotate a line.
  3172. void QsciScintilla::annotate(int line, const QString &text, int style)
  3173. {
  3174. int style_offset = SendScintilla(SCI_ANNOTATIONGETSTYLEOFFSET);
  3175. ScintillaBytes s = textAsBytes(text);
  3176. SendScintilla(SCI_ANNOTATIONSETTEXT, line, ScintillaBytesConstData(s));
  3177. SendScintilla(SCI_ANNOTATIONSETSTYLE, line, style - style_offset);
  3178. setScrollBars();
  3179. }
  3180. // Annotate a line.
  3181. void QsciScintilla::annotate(int line, const QString &text, const QsciStyle &style)
  3182. {
  3183. style.apply(this);
  3184. annotate(line, text, style.style());
  3185. }
  3186. // Annotate a line.
  3187. void QsciScintilla::annotate(int line, const QsciStyledText &text)
  3188. {
  3189. text.apply(this);
  3190. annotate(line, text.text(), text.style());
  3191. }
  3192. // Annotate a line.
  3193. void QsciScintilla::annotate(int line, const QList<QsciStyledText> &text)
  3194. {
  3195. char *styles;
  3196. ScintillaBytes styled_text = styleText(text, &styles,
  3197. SendScintilla(SCI_ANNOTATIONGETSTYLEOFFSET));
  3198. SendScintilla(SCI_ANNOTATIONSETTEXT, line,
  3199. ScintillaBytesConstData(styled_text));
  3200. SendScintilla(SCI_ANNOTATIONSETSTYLES, line, styles);
  3201. delete[] styles;
  3202. }
  3203. // Get the annotation for a line, if any.
  3204. QString QsciScintilla::annotation(int line) const
  3205. {
  3206. char *buf = new char[SendScintilla(SCI_ANNOTATIONGETTEXT, line, (const char *)0) + 1];
  3207. buf[SendScintilla(SCI_ANNOTATIONGETTEXT, line, buf)] = '\0';
  3208. QString qs = bytesAsText(buf);
  3209. delete[] buf;
  3210. return qs;
  3211. }
  3212. // Convert a list of styled text to the low-level arrays.
  3213. QsciScintillaBase::ScintillaBytes QsciScintilla::styleText(const QList<QsciStyledText> &styled_text, char **styles, int style_offset)
  3214. {
  3215. QString text;
  3216. int i;
  3217. // Build the full text.
  3218. for (i = 0; i < styled_text.count(); ++i)
  3219. {
  3220. const QsciStyledText &st = styled_text[i];
  3221. st.apply(this);
  3222. text.append(st.text());
  3223. }
  3224. ScintillaBytes s = textAsBytes(text);
  3225. // There is a style byte for every byte.
  3226. char *sp = *styles = new char[s.length()];
  3227. for (i = 0; i < styled_text.count(); ++i)
  3228. {
  3229. const QsciStyledText &st = styled_text[i];
  3230. ScintillaBytes part = textAsBytes(st.text());
  3231. int part_length = part.length();
  3232. for (int c = 0; c < part_length; ++c)
  3233. *sp++ = (char)(st.style() - style_offset);
  3234. }
  3235. return s;
  3236. }
  3237. // Convert Scintilla modifiers to the Qt equivalent.
  3238. int QsciScintilla::mapModifiers(int modifiers)
  3239. {
  3240. int state = 0;
  3241. if (modifiers & SCMOD_SHIFT)
  3242. state |= Qt::ShiftModifier;
  3243. if (modifiers & SCMOD_CTRL)
  3244. state |= Qt::ControlModifier;
  3245. if (modifiers & SCMOD_ALT)
  3246. state |= Qt::AltModifier;
  3247. if (modifiers & (SCMOD_SUPER | SCMOD_META))
  3248. state |= Qt::MetaModifier;
  3249. return state;
  3250. }
  3251. // Re-implemented to handle shortcut overrides.
  3252. bool QsciScintilla::event(QEvent *e)
  3253. {
  3254. if (e->type() == QEvent::ShortcutOverride && !isReadOnly())
  3255. {
  3256. QKeyEvent *ke = static_cast<QKeyEvent *>(e);
  3257. if (ke->key())
  3258. {
  3259. // We want ordinary characters.
  3260. if ((ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier || ke->modifiers() == Qt::KeypadModifier) && ke->key() < Qt::Key_Escape)
  3261. {
  3262. ke->accept();
  3263. return true;
  3264. }
  3265. // We want any key that is bound.
  3266. QsciCommand *cmd = stdCmds->boundTo(ke->key() | (ke->modifiers() & ~Qt::KeypadModifier));
  3267. if (cmd)
  3268. {
  3269. ke->accept();
  3270. return true;
  3271. }
  3272. }
  3273. }
  3274. return QsciScintillaBase::event(e);
  3275. }
  3276. // Re-implemented to handle chenges to the enabled state.
  3277. void QsciScintilla::changeEvent(QEvent *e)
  3278. {
  3279. QsciScintillaBase::changeEvent(e);
  3280. if (e->type() != QEvent::EnabledChange)
  3281. return;
  3282. if (isEnabled())
  3283. SendScintilla(SCI_SETCARETSTYLE, CARETSTYLE_LINE);
  3284. else
  3285. SendScintilla(SCI_SETCARETSTYLE, CARETSTYLE_INVISIBLE);
  3286. QColor fore = palette().color(QPalette::Disabled, QPalette::Text);
  3287. QColor back = palette().color(QPalette::Disabled, QPalette::Base);
  3288. if (lex.isNull())
  3289. {
  3290. if (isEnabled())
  3291. {
  3292. fore = nl_text_colour;
  3293. back = nl_paper_colour;
  3294. }
  3295. SendScintilla(SCI_STYLESETFORE, 0, fore);
  3296. // Assume style 0 applies to everything so that we don't need to use
  3297. // SCI_STYLECLEARALL which clears everything. We still have to set the
  3298. // default style as well for the background without any text.
  3299. SendScintilla(SCI_STYLESETBACK, 0, back);
  3300. SendScintilla(SCI_STYLESETBACK, STYLE_DEFAULT, back);
  3301. }
  3302. else
  3303. {
  3304. setEnabledColors(STYLE_DEFAULT, fore, back);
  3305. int nrStyles = 1 << SendScintilla(SCI_GETSTYLEBITS);
  3306. for (int s = 0; s < nrStyles; ++s)
  3307. if (!lex->description(s).isNull())
  3308. setEnabledColors(s, fore, back);
  3309. }
  3310. }
  3311. // Set the foreground and background colours for a style.
  3312. void QsciScintilla::setEnabledColors(int style, QColor &fore, QColor &back)
  3313. {
  3314. if (isEnabled())
  3315. {
  3316. fore = lex->color(style);
  3317. back = lex->paper(style);
  3318. }
  3319. handleStyleColorChange(fore, style);
  3320. handleStylePaperChange(back, style);
  3321. }
  3322. // Re-implemented to implement a more Qt-like context menu.
  3323. void QsciScintilla::contextMenuEvent(QContextMenuEvent *e)
  3324. {
  3325. QMenu *menu = createStandardContextMenu();
  3326. if (menu)
  3327. {
  3328. menu->setAttribute(Qt::WA_DeleteOnClose);
  3329. menu->popup(e->globalPos());
  3330. }
  3331. }
  3332. // Create an instance of the standard context menu.
  3333. QMenu *QsciScintilla::createStandardContextMenu()
  3334. {
  3335. bool read_only = isReadOnly();
  3336. bool has_selection = hasSelectedText();
  3337. QMenu *menu = new QMenu(this);
  3338. QAction *action;
  3339. if (!read_only)
  3340. {
  3341. action = menu->addAction(tr("&Undo"), this, SLOT(undo()));
  3342. set_shortcut(action, QsciCommand::Undo);
  3343. action->setEnabled(isUndoAvailable());
  3344. action = menu->addAction(tr("&Redo"), this, SLOT(redo()));
  3345. set_shortcut(action, QsciCommand::Redo);
  3346. action->setEnabled(isRedoAvailable());
  3347. menu->addSeparator();
  3348. action = menu->addAction(tr("Cu&t"), this, SLOT(cut()));
  3349. set_shortcut(action, QsciCommand::SelectionCut);
  3350. action->setEnabled(has_selection);
  3351. }
  3352. action = menu->addAction(tr("&Copy"), this, SLOT(copy()));
  3353. set_shortcut(action, QsciCommand::SelectionCopy);
  3354. action->setEnabled(has_selection);
  3355. if (!read_only)
  3356. {
  3357. action = menu->addAction(tr("&Paste"), this, SLOT(paste()));
  3358. set_shortcut(action, QsciCommand::Paste);
  3359. action->setEnabled(SendScintilla(SCI_CANPASTE));
  3360. action = menu->addAction(tr("Delete"), this, SLOT(delete_selection()));
  3361. action->setEnabled(has_selection);
  3362. }
  3363. if (!menu->isEmpty())
  3364. menu->addSeparator();
  3365. action = menu->addAction(tr("Select All"), this, SLOT(selectAll()));
  3366. set_shortcut(action, QsciCommand::SelectAll);
  3367. action->setEnabled(length() != 0);
  3368. return menu;
  3369. }
  3370. // Set the shortcut for an action using any current key binding.
  3371. void QsciScintilla::set_shortcut(QAction *action, QsciCommand::Command cmd_id) const
  3372. {
  3373. QsciCommand *cmd = stdCmds->find(cmd_id);
  3374. if (cmd && cmd->key())
  3375. action->setShortcut(QKeySequence(cmd->key()));
  3376. }
  3377. // Delete the current selection.
  3378. void QsciScintilla::delete_selection()
  3379. {
  3380. SendScintilla(SCI_CLEAR);
  3381. }
  3382. // Convert a Scintilla colour to a QColor.
  3383. static QColor asQColor(long sci_colour)
  3384. {
  3385. return QColor(
  3386. ((int)sci_colour) & 0x00ff,
  3387. ((int)(sci_colour >> 8)) & 0x00ff,
  3388. ((int)(sci_colour >> 16)) & 0x00ff);
  3389. }