SyntaxHilighting.dll & Scintilla en quelques lignes

Sujets variés concernant le développement en PureBasic
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

SyntaxHilighting.dll & Scintilla en quelques lignes

Message par falsam »

Un embryon d'éditeur Pure Basic qui, associé à une base de données SQlite, peut servir de base pour créer par exemple une bibliothèque de codes Pure Basic avec système de tags ou alors un RAD Pure Basic avec possibilité d'éditer du code Pure Basic.

Ce code reprend ce que j'ai expliqué sur ce post.

La coloration du gadget scintilla est issu du fichier préférence de l'éditeur officiel de Pure Basic.

Le gadget scintilla est à peine traité. Juste le minimum pour mettre en avant la dll SyntaxHilighting.dll.

Je ne traite pas l'indentation du texte, ni du zoom, ni de la recherche, etc....
EnableExplicit

Enumeration Syntax
  #SYNTAX_Text
  #SYNTAX_Keyword
  #SYNTAX_Comment
  #SYNTAX_Constant
  #SYNTAX_String
  #SYNTAX_Function
  #SYNTAX_Asm
  #SYNTAX_Operator
  #SYNTAX_Structure
  #SYNTAX_Number
  #SYNTAX_Pointer
  #SYNTAX_Separator
  #SYNTAX_Label
  #SYNTAX_Module
EndEnumeration

Enumeration File
  #Dll
EndEnumeration

Enumeration Window
  #Mainform
EndEnumeration

Enumeration Gadget
  #Editor
EndEnumeration

Global SciPositionStart, *Buffer

Declare CallbackHighlight(*Position, Length, IdColor)
Declare ScintillaCallBack(Gadget.i, *scinotify.SCNotification)

Declare MainForm_Open()
Declare MainForm_Resize()


Procedure CallbackHighlight(*Position, Length, IdColor)
  Shared SciPositionStart, *Buffer

  ScintillaSendMessage(#Editor, #SCI_STARTSTYLING, (*Position - *Buffer) + SciPositionStart, $1F)
  ScintillaSendMessage(#Editor, #SCI_SETSTYLING, Length, IdColor)
EndProcedure

Procedure ScintillaCallBack(Gadget.i, *scinotify.SCNotification)
  Protected SciLineNumber
  Protected txtr.TEXTRANGE
  Protected SciPositionEnd
  Shared SciPositionStart, *Buffer
 
  Select *scinotify\nmhdr\code      
      
    Case #SCN_STYLENEEDED
      SciLineNumber = ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, ScintillaSendMessage(Gadget, #SCI_GETENDSTYLED))
      SciPositionStart = ScintillaSendMessage(Gadget, #SCI_POSITIONFROMLINE, SciLineNumber)
      SciPositionEnd     = *scinotify\Position

      txtr\chrg\cpMin = SciPositionStart
      txtr\chrg\cpMax = SciPositionEnd
      txtr\lpstrText  = AllocateMemory(SciPositionEnd - SciPositionStart + 1)
    
      *Buffer          = txtr\lpstrText

      ScintillaSendMessage(Gadget, #SCI_GETTEXTRANGE, 0, txtr)
        
      CallFunction(#Dll, "SyntaxHighlight", *Buffer, SciPositionEnd - SciPositionStart, @CallbackHighlight(), #True)
    
      FreeMemory(*Buffer)
      
  EndSelect
  
EndProcedure


Procedure MainForm_Open()
  Protected Font.s
  
  If OpenWindow(#Mainform, 0, 0, 800, 600, "SyntaxHilighting & Scintilla", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
    
    StickyWindow(#MainForm, #True)
       
    ScintillaGadget(#Editor, 10, 10, 780, 580, @ScintillaCallBack())
    
    ;Initialisation du gaget scintilla
    ScintillaSendMessage(#Editor, #SCI_SETLEXER, #SCLEX_CONTAINER)
   
    ;Ouverture du fichier préférence de l'éditeur officiel de Pure Basic
    OpenPreferences(GetEnvironmentVariable("APPDATA") + "\PureBasic\PureBasic.prefs")
    
    PreferenceGroup("Editor")
  
    Font = ReadPreferenceString("EditorFontName", "Courier New")
    
    ;Initialisation de la coloration syntaxique du gadget scintilla
    ;les couleurs seront celles que vous avez définies dans le menu préférence
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE,#STYLE_DEFAULT, $000000)
    ScintillaSendMessage(#Editor, #SCI_STYLESETBACK, #STYLE_DEFAULT, ReadPreferenceLong("BackgroundColor", $FFFFFF))
  
    ScintillaSendMessage(#Editor, #SCI_STYLESETFONT, #STYLE_DEFAULT, @Font)
    ScintillaSendMessage(#Editor, #SCI_STYLESETSIZE, #STYLE_DEFAULT, ReadPreferenceLong("EditorFontSize", 10))
    ScintillaSendMessage(#Editor, #SCI_STYLECLEARALL)

    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Text, ReadPreferenceLong("NormalTextColor", 0))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Keyword, ReadPreferenceLong("BasicKeywordColor", $9D030D))
    ScintillaSendMessage(#Editor, #SCI_STYLESETBOLD, #SYNTAX_Keyword, #True)
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Comment, ReadPreferenceLong("CommentColor", $6F884A))
    ScintillaSendMessage(#Editor, #SCI_STYLESETITALIC, #SYNTAX_Comment, #True)
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Constant, ReadPreferenceLong("ConstantColor", $0B46AE))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_String, ReadPreferenceLong("StringColor", $413C33))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Function, ReadPreferenceLong("PureKeywordColor", $EF303D))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Asm, ReadPreferenceLong("ASMKeywordColor", $400080))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Operator,  ReadPreferenceLong("OperatorColor", $0000FF))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Structure, ReadPreferenceLong("StructureColor", $0548B4))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Number, ReadPreferenceLong("NumberColor", $0000FF))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Pointer,  ReadPreferenceLong("PointerColor", $0507B4))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Separator,  ReadPreferenceLong("SeparatorColor", $000000))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Label,  ReadPreferenceLong("LabelColor", $804000))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #SYNTAX_Module,  ReadPreferenceLong("ModuleColor", $9D030D))
        
    ;Affichage de la colone de numérotation des lignes    
    ScintillaSendMessage(#Editor, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)
    ScintillaSendMessage(#Editor, #SCI_SETMARGINWIDTHN, 0, 50)
    ScintillaSendMessage(#Editor, #SCI_STYLESETBACK, #STYLE_LINENUMBER, ReadPreferenceLong("LineNumberBackColor", GetSysColor_(15)))
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #STYLE_LINENUMBER, ReadPreferenceLong("LineNumberColor", 0))
    ScintillaSendMessage(#Editor, #SCI_SETMARGINWIDTHN, 1, 5)
    
    ;Affichage et couleur de la ligne active
    ScintillaSendMessage(#Editor, #SCI_SETCARETLINEVISIBLE, #True)
    ScintillaSendMessage(#Editor, #SCI_SETCARETLINEBACK, ReadPreferenceLong("CurrentLineColor", 0))
  
    ;Les tabulations sont remplacées par des espaces 
    ScintillaSendMessage(#Editor, #SCI_SETUSETABS, #False)
  
    ;Nombre d'espaces pour une tabulation
    ScintillaSendMessage(#Editor, #SCI_SETINDENT, 4)
  
    ;Affichage du guide d'indentation et affectation d'une couleur
    ScintillaSendMessage(#Editor, #SCI_SETINDENTATIONGUIDES, #SC_IV_LOOKFORWARD)
    ScintillaSendMessage(#Editor, #SCI_STYLESETFORE, #STYLE_INDENTGUIDE, RGB(154, 205, 50))
    
    RemoveKeyboardShortcut(#Mainform, #PB_Shortcut_Tab)
    
    ClosePreferences()
    
    BindEvent(#PB_Event_SizeWindow, @MainForm_Resize(), #Mainform)
    
    Repeat : Until WaitWindowEvent(100) = #PB_Event_CloseWindow
    
  EndIf
EndProcedure

Procedure MainForm_Resize()
  ResizeGadget(#Editor,#PB_Ignore, #PB_Ignore, WindowWidth(#Mainform)-20, WindowHeight(#Mainform)-20)  
EndProcedure

;-Start
If Not OpenLibrary(#Dll, #PB_Compiler_Home+"SDK/Syntax Highlighting/SyntaxHilighting.dll") 
  MessageRequester("Erreur", "Impossible d'ouvir SyntaxHilighting.dll")
  End
EndIf

If Not InitScintilla()
  MessageRequester("Erreur", "Impossible d'initialiser Scintilla")
  End
EndIf

MainForm_Open()
On peut bien sur se lancer dans un vrai éditeur Pure Basic en se passant du fichier préférence de l'éditeur officiel.
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Avatar de l’utilisateur
graph100
Messages : 1318
Inscription : sam. 21/mai/2005 17:50

Re: SyntaxHilighting.dll & Scintilla en quelques lignes

Message par graph100 »

pourquoi tu posts du code dans une citation ?
C'est dommage car l'outil de Erix14 permet de mieux voir le code.
_________________________________________________
Mon site : CeriseCode (Attention Chantier perpétuel ;))
Avatar de l’utilisateur
falsam
Messages : 7244
Inscription : dim. 22/août/2010 15:24
Localisation : IDF (Yvelines)
Contact :

Re: SyntaxHilighting.dll & Scintilla en quelques lignes

Message par falsam »

graph100 a écrit :pourquoi tu posts du code dans une citation ?
C'est dommage car l'outil de Erix14 permet de mieux voir le code.
Je poste le code dans une citation parce que j'utilise la colorisation syntaxique BBcode. Le BBcode ne fonctionnant pas dans une balise

Code : Tout sélectionner

[/i], je passe donc par la balise [i][Quote][/i].

L'addon d'Erix14 permet effectivement de voir du code posté dans la balise [code] comme le montre l'image ci-dessous mais c'est une extension qui fonctionne uniquement avec FireFox. 

[img]http://s242132022.onlinehome.fr/Download/PureBasic/capture/erix14codeview[/img]

La plupart du temps je poste du code sans utiliser la coloration syntaxique BBCode, mais là j'avais envie :)

PS : [i]Cette extension pour Firefox n'est apparemment plus disponible en téléchargement depuis le site de Mozilla.[/i]

Le lien sur le site d'Erix14
 :arrow:  [url]http://www.rx14.info/pbforumcode/PureBasicForumCode.xpi[/url]
Configuration : Windows 11 Famille 64-bit - PB 6.03 x64 - AMD Ryzen 7 - 16 GO RAM
Vidéo NVIDIA GeForce GTX 1650 Ti - Résolution 1920x1080 - Mise à l'échelle 125%
Répondre