Gambas France BETA


Pas de compte ? Incription

Exemple : Highlight

À propos de ce code

Un petit exemple pour apprendre à colorer son texte simplement. C'est la réalisation faite lors de ce tuto : http://gambaslinux.forumactif.net/tutoriel-gambas-f3/tuto-l-objet-editor-et-la-coloration-syntaxique-t230.htm

L'exemple est en Gambas 3, mais fonctionne aussi très bien en Gambas 2. ;)

Code source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
'========================FMain.class========================

' Gambas class file
INHERITS Form
PRIVATE textEditor AS Editor
PUBLIC SUB _new()

textEditor = NEW Editor(ME) AS "Editeur"
ME.Arrangement = Arrange.Fill
textEditor.Text = "Salut <couleur valeur=\"rouge\">tout</couleur> le <gras>monde</gras> ! :) "

textEditor.Highlight = Highlight.Custom

textEditor.Flags[Editor.HighlightCurrent] = TRUE

'Styles
textEditor.Styles[Highlight.Symbol].Color = Color.Orange 'Smilies
textEditor.Styles[Highlight.Symbol].Bold = TRUE

textEditor.Styles[Highlight.Keyword].Color = Color.DarkGreen 'Balises
textEditor.Styles[Highlight.Keyword].Bold = TRUE

textEditor.Styles[Highlight.Operator].Color = Color.DarkGreen 'Attributs
textEditor.Styles[Highlight.Operator].Bold = FALSE

textEditor.Styles[Highlight.String].Color = &aa0000 'Valeurs
textEditor.Styles[Highlight.String].Bold = FALSE


END

PUBLIC SUB Editeur_Highlight()


DIM surlignages AS NEW INTEGER[String.Len(Highlight.Text)]
DIM texte AS STRING = Highlight.Text
DIM smilies AS String[] = [" :) ", " :D ", " :p ", " :-° "

DIM i, position AS INTEGER = 0
DIM resultat AS Integer[]

surlignages.Fill(Highlight.Normal)

'Smilies
FOR i = 0 TO smilies.Max
FOR EACH position IN SuperString.InStr(texte, smilies[i])
surlignages.Fill(Highlight.Symbol, position, String.Len(smilies[i]))
NEXT
NEXT

'Balises
FOR EACH resultat IN SuperString.RegexpSearch(texte, "<(.+)>")
surlignages.Fill(Highlight.Keyword, resultat[0], resultat[1])
NEXT

'Valeurs
FOR EACH resultat IN SuperString.RegexpSearch(texte, "\".+\"")
IF surlignages[resultat[0]] = Highlight.Keyword 'Si on est dans une balise
surlignages.Fill(Highlight.String, resultat[0], resultat[1])
ENDIF
NEXT

'Attributs
FOR EACH resultat IN SuperString.RegexpSearch(texte, " ([a-zA-Z]+)=")
IF surlignages[resultat[0]] = Highlight.Keyword
surlignages.Fill(Highlight.Operator, resultat[0], resultat[1])
ENDIF
NEXT

FOR i = 0 TO surlignages.Max
Highlight.Add(surlignages[i])
NEXT


END

'========================SuperString.class========================

' Gambas class file

STATIC PUBLIC FUNCTION InStr(texte AS STRING, pattern AS STRING, OPTIONAL depuis AS INTEGER = 0) AS Integer[]

DIM positions AS NEW Integer[]
DIM position AS INTEGER

position = String.InStr(texte, pattern, depuis)
WHILE position <> 0
positions.Add(position - 1)
position = String.InStr(texte, pattern, position + String.Len(pattern))
WEND

RETURN positions

END

STATIC PUBLIC FUNCTION RegexpSearch(texte AS STRING, pattern AS STRING, OPTIONAL submatch AS INTEGER = 0) AS Integer[][]

DIM resultats AS NEW Integer[][]
DIM tmptexte AS STRING = texte
DIM tmpPos AS INTEGER = 0
DIM regex AS NEW Regexp(texte, pattern, Regexp.Ungreedy)

IF submatch = 0
WHILE regex.Offset <> -1 AND tmptexte <> ""
regex.Exec(tmptexte)
tmpPos += (regex.Offset + String.Len(regex.Text))
resultats.Add([tmpPos - String.Len(regex.Text), String.Len(regex.Text)])
tmptexte = String.Right(texte, tmpPos * -1)
WEND
ELSE
WHILE regex.Offset <> -1 AND tmptexte <> ""
regex.Exec(tmptexte)
tmpPos += (regex.SubMatches[submatch].Offset + String.Len(regex.SubMatches[submatch].Text))
resultats.Add([tmpPos - String.Len(regex.SubMatches[submatch].Text), String.Len(regex.SubMatches[submatch].Text)])
tmptexte = String.Right(texte, tmpPos * -1)
WEND
ENDIF

RETURN resultats

END

Commentaires

Commentaire de thegambasdeur, Le 21/8/2010 à 20:57:21
bon tuto , je ne savais pas l'utiliser :) , trés bon tuto sur le sdz aussi ;)