Page 1 sur 1

[Exemple] Scintilla(coloration syntaxique, pliage ....)

Publié : ven. 05/déc./2008 16:32
par cha0s
Voila un petit exemple pour utiliser scintilla
Marche sur windows&linux
testé sur la version 4.30 b4&5

c'est pas commenté et codé via la méthode ancestrale de LaRache

Code : Tout sélectionner

;===========================================;
;                                           ;
;   Scintilla example                       ;
;                                           ;
;===========================================;

Global KeyWordUp.s, KeyWordDown.s, KeyWordNone.s
KeyWordUp = "Repeat If Procedure"
KeyWordDown = "ForEver EndIf EndProcedure"
KeyWordNone = "Else"
Enumeration 0
      #LexerState_Space
      #LexerState_Comment
      #LexerState_NonKeyword
      #LexerState_Keyword
      #LexerState_FoldKeyword
      #LexerState_Constant
      #LexerState_String
      #LexerState_FoldKeywordUp
      #LexerState_FoldKeywordDown
EndEnumeration

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      InitScintilla("Scintilla.dll")
CompilerEndIf

Procedure SCI_GetLineEndPosition(gadget, line)
      ProcedureReturn ScintillaSendMessage(gadget,#SCI_GETLINEENDPOSITION,line)
EndProcedure

Procedure SCI_LineFromPosition(gadget, Pos)
      ProcedureReturn ScintillaSendMessage(gadget,#SCI_LINEFROMPOSITION,Pos)
EndProcedure

Procedure KeyWordIs(key.s)
      Protected n
      If key=""
            ProcedureReturn -1
      EndIf
      
      For n=1 To CountString(KeyWordUp, " ") + 1
            If LCase(StringField(KeyWordUp, n, " ")) = LCase(key)
                  ProcedureReturn #LexerState_FoldKeywordUp
            EndIf
      Next n
      For n=1 To CountString(KeyWordDown, " ") + 1
            If LCase(StringField(KeyWordDown, n, " ")) = LCase(key)
                  ProcedureReturn #LexerState_FoldKeywordDown
            EndIf
      Next n
      For n=1 To CountString(KeyWordNone, " ") + 1
            If LCase(StringField(KeyWordNone, n, " ")) = LCase(key)
                  ProcedureReturn #LexerState_Keyword
            EndIf
      Next n
      ProcedureReturn -1
EndProcedure


Procedure Highlight(sciptr.l, endpos.l)
      Protected level = #SC_FOLDLEVELBASE, Char.l, keyword.s, state.i, linenumber.l
      Protected thislevel.l = level
      Protected nextlevel.l = level
      Protected CurrentPos.l = 0, endlinepos.l, startkeyword
      Protected currentline.l = 0
      endpos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, endpos))
      ScintillaSendMessage(sciptr, #SCI_STARTSTYLING, CurrentPos, $1F | #INDICS_MASK)
      
      While CurrentPos <= endpos
            Char = ScintillaSendMessage(sciptr, #SCI_GETCHARAT, CurrentPos)
            Select Char
                  Case 10
                        ScintillaSendMessage(sciptr, #SCI_SETSTYLING, 1, #LexerState_NonKeyword)
                        ScintillaSendMessage(sciptr, #SCI_SETFOLDLEVEL, linenumber , thislevel)
                        thislevel = nextlevel
                        linenumber + 1
                  Case 'a' To 'z', 'A' To 'Z'
                        endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
                        
                        keyword = Chr(char)
                        While currentpos < endlinepos
                              currentpos + 1
                              char = ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos)
                              If Not ((char >= 'a' And char <= 'z') Or (char >= 'A' And char <= 'Z') Or char = '_'Or (char >= '0' And char <= '9'))
                                    currentpos-1
                                    Break
                              EndIf
                              keyword + Chr(char)
                        Wend
                        Select KeyWordIs(keyword)
                              Case #LexerState_FoldKeywordUp
                                    state = #LexerState_Keyword
                                    thislevel | #SC_FOLDLEVELHEADERFLAG
                                    nextlevel + 1
                              Case #LexerState_FoldKeywordDown
                                    state = #LexerState_Keyword
                                    nextlevel - 1
                                    If nextlevel < #SC_FOLDLEVELBASE
                                          nextlevel = #SC_FOLDLEVELBASE
                                    EndIf
                              Case #LexerState_Keyword
                                    state = #LexerState_Keyword
                              Default
                                    state = #LexerState_NonKeyword
                        EndSelect
                        
                        
                        ScintillaSendMessage(sciptr, #SCI_SETSTYLING, Len(keyword), state)
                  Case '"'
                        endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
                        startkeyword = 1
                        While currentpos < endlinepos
                              currentpos + 1
                              startkeyword + 1
                              If ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos) = '"'
                                    Break
                              EndIf
                        Wend
                        ScintillaSendMessage(sciptr, #SCI_SETSTYLING, startkeyword, #LexerState_String)
                  Case ';'
                        endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
                        startkeyword = 1
                        While currentpos < endlinepos
                              currentpos + 1
                              startkeyword + 1
                        Wend
                        ScintillaSendMessage(sciptr, #SCI_SETSTYLING, startkeyword, #LexerState_Comment)
                  Case 9, ' '
                        ScintillaSendMessage(sciptr, #SCI_SETSTYLING, 1, #LexerState_Space)
                  Case '#'
                        endlinepos = SCI_GetLineEndPosition(sciptr, SCI_LineFromPosition(sciptr, currentpos))
                        startkeyword = 1
                        While currentpos < endlinepos
                              currentpos + 1
                              char = ScintillaSendMessage(sciptr, #SCI_GETCHARAT, currentpos)
                              If Not ((char >= 'a' And char <= 'z') Or (char >= 'A' And char <= 'Z') Or char = '_' Or (char >= '0' And char <= '9'))
                                    currentpos-1
                                    Break
                              EndIf
                              startkeyword + 1
                        Wend
                        ScintillaSendMessage(sciptr, #SCI_SETSTYLING, startkeyword, #LexerState_Constant)
                  Default
                        ScintillaSendMessage(sciptr, #SCI_SETSTYLING, 1, #LexerState_NonKeyword)
            EndSelect
            currentpos+1
      Wend 
EndProcedure

ProcedureDLL ScintillaCallBack(Gadget, *scinotify.SCNotification)
      Select *scinotify\nmhdr\code
            Case #SCN_STYLENEEDED
                  Highlight(Gadget, *scinotify\position)
            Case #SCN_MARGINCLICK
                  ScintillaSendMessage(Gadget, #SCI_TOGGLEFOLD, ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, *scinotify\Position))
      EndSelect
EndProcedure


If OpenWindow(0, 0, 0, 800, 600, "Scintilla exemple", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
      
      If UseGadgetList(WindowID(0))
            ScintillaGadget(0, 0, 0, 800, 600, @ScintillaCallBack())
            ; Choose a lexer
            ScintillaSendMessage(0, #SCI_SETLEXER, #SCLEX_CONTAINER, 0)
            
            ; Set default font
            ScintillaSendMessage(0, #SCI_STYLESETFONT, #STYLE_DEFAULT, @"Courier New")
            ScintillaSendMessage(0, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 12)
            ScintillaSendMessage(0, #SCI_STYLECLEARALL)
            
            ; Set caret line colour
            ScintillaSendMessage(0, #SCI_SETCARETLINEBACK, $eeeeff)
            ScintillaSendMessage(0, #SCI_SETCARETLINEVISIBLE, #True)
            
            ; Set styles for custom lexer
            ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Comment, $bb00)
            ScintillaSendMessage(0, #SCI_STYLESETITALIC, #LexerState_Comment, 1)
            ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_NonKeyword, 0)
            ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Keyword, RGB(0, 102, 102))
            ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_Constant, RGB(169, 64, 147))
            ScintillaSendMessage(0, #SCI_STYLESETFORE, #LexerState_String, RGB(255, 139, 37))
            ; Margins
            ScintillaSendMessage(0, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)
            ScintillaSendMessage(0, #SCI_SETMARGINMASKN, 2, #SC_MASK_FOLDERS)
            ScintillaSendMessage(0, #SCI_SETMARGINWIDTHN, 0, 50)
            ScintillaSendMessage(0, #SCI_SETMARGINWIDTHN, 2, 20)
            ScintillaSendMessage(0, #SCI_SETMARGINSENSITIVEN, 2, #True)
            
            ; Choose folding icons
            ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDEROPEN, #SC_MARK_CIRCLEMINUS)
            ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDER, #SC_MARK_CIRCLEPLUS)
            ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDERSUB, #SC_MARK_VLINE)
            ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDERTAIL, #SC_MARK_LCORNERCURVE)
            ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDEREND, #SC_MARK_CIRCLEPLUSCONNECTED)
            ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDEROPENMID, #SC_MARK_CIRCLEMINUSCONNECTED)
            ScintillaSendMessage(0, #SCI_MARKERDEFINE, #SC_MARKNUM_FOLDERMIDTAIL, #SC_MARK_TCORNERCURVE)
            
            ; Choose folding icon colours
            ScintillaSendMessage(0, #SCI_MARKERSETFORE, #SC_MARKNUM_FOLDER, $FFFFFF)
            ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDER, 0)
            ScintillaSendMessage(0, #SCI_MARKERSETFORE, #SC_MARKNUM_FOLDEROPEN, $FFFFFF)
            ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDEROPEN, 0)
            ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDEROPENMID, 0)
            ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDERSUB, 0)
            ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDERTAIL, 0)
            ScintillaSendMessage(0, #SCI_MARKERSETBACK, #SC_MARKNUM_FOLDERMIDTAIL, 0)
            
            txt.s = "procedure test()"+#LF$
            txt.s + " #test = "+ Chr(34)+ "chaine 1"+ Chr(34)+#LF$
            txt.s + " If #test = 24"+#LF$
            txt.s + "   ;test "+#LF$
            txt.s + " Else"+#LF$
            txt.s + ""+#LF$
            txt.s + " EndIf"+#LF$
            txt.s + "EndProcedure"+#LF$
            ScintillaSendMessage(0, #SCI_SETTEXT, #Null, @txt)
      EndIf
      
      
      Repeat
            Event = WaitWindowEvent()
            
            If Event = #PB_Event_CloseWindow 
                  Quit = 1
            EndIf
            
      Until Quit = 1
      
EndIf

End   
  
  

Publié : ven. 05/déc./2008 16:49
par Backup
Merci :)

je viens de légèrement corriger ton code pour la compilation en windows
avec purebasic 4.20

Code : Tout sélectionner

   InitScintilla("Scintilla.dll")

Publié : ven. 05/déc./2008 19:06
par comtois
sympa ce code.
Pour la coloration syntaxique, il y a une raison particulière pour que tu n'utilises pas la dll du sdk ? tu n'as pas à te casser la tête , c'est la même qu'utilise l'IDE officiel.

Publié : ven. 05/déc./2008 20:47
par cha0s
InitScintilla() marche très bien chez moi je me prend pas la tête et de toute façon je programme mon IDE principalement sous nux qui utilise une lib statique.

Publié : ven. 05/déc./2008 20:58
par comtois
je ne parlais pas de scintilla , mais de cette dll , je n'ai pas regardé si elle existe sous linux ?
;
; The SyntaxHilighting.dll provides the syntax parser of the
; PureBasic IDE in form of a dll, so it can be easily reused
; to do other tasks as well.
;
; Some notes:
;
; - For speed reasons, the dll is not threadsave.
;
; - The hilighter does not handle unicode. It does however handle UTF8, so if
; Unicode text should be parsed, convert it to UTF8 first.
;
; The dll exports only one function:
;
; SyntaxHighlight(*Buffer, Length, @Callback(), EnableAsm)
;
; *Buffer and Length specify the text buffer to parse.
;
; Callback() must have the parameters as in the example below and will be called
; for each parsed token.
;
; If EnableAsm is set to nonzero, the parser will report asm keywords also outside of
; the special ! lines, just as the InlineAsm parser does.

Publié : ven. 05/déc./2008 21:46
par cha0s
non justement sous linux c'est linké de façon statique donc pas besoin de fichier scintilla.so externe sinon pour windows j'utilise celle fournit par pb.

Publié : ven. 05/déc./2008 22:41
par comtois
je n'insiste pas !



Allez si, encore un petit peu ....



Lis bien



The SyntaxHilighting.dll



Je ne parle pas de scintilla , enfin bref ...

Publié : sam. 06/déc./2008 0:45
par Anonyme
@comtois

Une Dll n'existe pas sous linux , je regarde demain sur mon poste si je l'ai.


Boudjoux ++

Publié : sam. 06/déc./2008 10:05
par comtois
maintenant que j'ai linux, je peux aussi regarder mais j'avais la flemme de rebooter hier soir pour vérifier :)

Je le ferai sûrement dans la journée.

Merci.

Publié : sam. 06/déc./2008 11:03
par Kwai chang caine
Je m'excuse d'etre a la ramasse comme dab.
J'entend parler de scintilla par ci et par la.
J'ai fait F1 et j'ai vu qu'il etait inclus dans PB maintenant.
Mais ça sert a quoi vraiment ???
Juste pour les IDE ???
Ou alors ça peut servir a d'autres choses ??? :roll:

Publié : sam. 06/déc./2008 14:11
par Backup
Kwai chang caine a écrit :Je m'excuse d'etre a la ramasse comme dab.
J'entend parler de scintilla par ci et par la.
J'ai fait F1 et j'ai vu qu'il etait inclus dans PB maintenant.
Mais ça sert a quoi vraiment ???
Google est ton ami :)

http://fr.wikipedia.org/wiki/Scintilla

Publié : sam. 06/déc./2008 15:11
par comtois
voila je suis sous linux, effectivement l'utilitaire SyntaxHightLighting n'existe pas sous linux.

Publié : sam. 06/déc./2008 16:58
par Anonyme
De toute manière tu peut tout faire avec Scintilla. ( en matière d'ide)

Publié : sam. 06/déc./2008 19:23
par Kwai chang caine
@DOBRO et CPL

Merci pour l'info
C'est dingue que "quinquin" il est créé une DLL rien que pour ça 8O
Apparement en C, decidement j'aurais toujours du mal a imaginer toute la portée de certain domaine informatique.
C'est meme utilisable en visual basic.

Bon si tout le monde en parle, c'est que ce doit etre genial :D

Re: [Exemple] Scintilla(coloration syntaxique, pliage ....)

Publié : sam. 05/déc./2009 15:47
par dayvid
mois j'aurais besoin d'une aide très clair et complètte sur comment utiliser scintilla
réponder mois en mp