From 9e35736a04ca93426ecc9ee582177e269a760a13 Mon Sep 17 00:00:00 2001 From: gambas Date: Fri, 5 Mar 2021 17:54:15 +0100 Subject: [PATCH] Code editor: Color insertion automatically detects the color under the cursor and highlights it, so that it will be replaced if the dialog inserts a new color. [DEVELOPMENT ENVIRONMENT] * NEW: Code editor: Color insertion automatically detects the color under the cursor and highlights it, so that it will be replaced if the dialog inserts a new color. --- .../gambas3/.src/Dialog/FColorChooser.class | 12 +- .../gambas3/.src/Editor/CInsertColor.class | 141 +++++++++++------- app/src/gambas3/.src/Editor/Code/FEditor.form | 3 +- .../gambas3/.src/Editor/Code/FTextEditor.form | 3 +- .../.src/VersionControl/FVersionControl.form | 4 - 5 files changed, 96 insertions(+), 67 deletions(-) diff --git a/app/src/gambas3/.src/Dialog/FColorChooser.class b/app/src/gambas3/.src/Dialog/FColorChooser.class index c15445ca4..775a8fb4b 100644 --- a/app/src/gambas3/.src/Dialog/FColorChooser.class +++ b/app/src/gambas3/.src/Dialog/FColorChooser.class @@ -131,8 +131,10 @@ Public Sub Form_Open() aColor.Sort(gb.Natural) $aColorName.Insert(aColor) - iSep = $aColorName.Count - $aColorName.Add("-") + If aColor.Count Then + iSep = $aColorName.Count + $aColorName.Add("-") + Endif aColor = New String[] For Each hSym In hClass.Symbols @@ -150,8 +152,10 @@ Public Sub Form_Open() gvwColor.Rows.H = Desktop.Scale * 4 gvwColor.Rows[2].H = 1 gvwColor[2, 0].ColumnSpan = gvwColor.Columns.Count - gvwColor.Rows[iSep].H = 1 - gvwColor[iSep, 0].ColumnSpan = gvwColor.Columns.Count + If iSep Then + gvwColor.Rows[iSep].H = 1 + gvwColor[iSep, 0].ColumnSpan = gvwColor.Columns.Count + Endif Endif diff --git a/app/src/gambas3/.src/Editor/CInsertColor.class b/app/src/gambas3/.src/Editor/CInsertColor.class index 140da9a25..146264878 100644 --- a/app/src/gambas3/.src/Editor/CInsertColor.class +++ b/app/src/gambas3/.src/Editor/CInsertColor.class @@ -75,9 +75,7 @@ Static Private Sub GetColorFromString(sRgb As String) As Integer Return Val("&H" & Mid$(sRgb, 2, 6)) Endif Endif - -Catch - + End Public Sub _new(hModule As Object, hMenuButton As MenuButton) @@ -90,7 +88,7 @@ Public Sub _new(hModule As Object, hMenuButton As MenuButton) Case "gambas" $aPattern = ["&HRxGxBx", "#RxGxBx", "&RxGxBx"] $aPatternSearch = ["&H", "#", "&"] - Case "css", "html" + Case "css", "html", "xml" $aPattern = ["#RxGxBx", "rgb(Ri,Gi,Bi)"] $aPatternSearch = ["#", "rgb(", "rgba("] Case "webpage" @@ -117,6 +115,55 @@ Private Sub GetEditor() As TextEditor End +Private Sub ReadColor(sText As String, iPos As Integer, sPattern As String, Optional ByRef iLen As Integer) As String + + Dim aScan As String[] + Dim sColor As String + Dim iStart As Integer + + If sPattern Begins "rgb" Then + + If sPattern Begins "rgba(" Then + aScan = Scan(Mid$(sText, iPos), sPattern & "*,*,*,*)*") + If aScan.Count <> 5 Then Return + Else + aScan = Scan(Mid$(sText, iPos), sPattern & "*,*,*)*") + If aScan.Count <> 4 Then Return + Endif + aScan.Pop() + sColor = sPattern & aScan.Join() & ")" + + If Not IsMissing(iLen) Then + iLen = String.Len(Mid$(sText, iPos, InStr(sText, ")", iPos) - iPos + 1)) + Endif + + Else + + If Mid$(sText, iPos, 1) Not Begins sPattern Then Return + + iStart = iPos + iPos += Len(sPattern) + sColor = "" + While IsHexa(Mid$(sText, iPos, 1)) And Len(sColor) < 6 + sColor &= Mid$(sText, iPos, 1) + Inc iPos + Wend + If Len(sColor) = 0 Then Return + If IsLetter(Mid$(sText, iPos, 1)) Then Return + If Len(sColor) < 3 Then Return + + 'sColor = String$(6 - Len(sColor), "0") & sColor + sColor = sPattern & sColor + + If Not IsMissing(iLen) Then + iLen = String.Len(Mid$(sText, iStart, iPos - iStart)) + Endif + + Endif + + Return sColor + +End Public Sub Menu_Show() @@ -127,7 +174,6 @@ Public Sub Menu_Show() Dim aColor As New String[] Dim hMenu As Menu Dim hImage As Image - Dim aScan As String[] Dim I As Integer Dim iColor As Integer Dim hPattern As Image @@ -140,36 +186,10 @@ Public Sub Menu_Show() iPos = InStr(sText, sPattern, iPos + 1) If iPos = 0 Then Break - If sPattern Begins "rgb" Then - - If sPattern Begins "rgba(" Then - aScan = Scan(Mid$(sText, iPos), sPattern & "*,*,*,*)*") - If aScan.Count <> 5 Then Continue - Else - aScan = Scan(Mid$(sText, iPos), sPattern & "*,*,*)*") - If aScan.Count <> 4 Then Continue - Endif - aScan.Pop() - sColor = sPattern & aScan.Join() & ")" - - Else - - iPos += Len(sPattern) - sColor = "" - While IsHexa(Mid$(sText, iPos, 1)) And Len(sColor) < 6 - sColor &= Mid$(sText, iPos, 1) - Inc iPos - Wend - If Len(sColor) = 0 Then Continue - If IsLetter(Mid$(sText, iPos, 1)) Then Continue - If Len(sColor) < 3 Then Continue - - 'sColor = String$(6 - Len(sColor), "0") & sColor - sColor = sPattern & sColor - - Endif - - iColor = GetColorFromString(sColor) + sColor = ReadColor(sText, iPos, sPattern) + If Not sColor Then Continue + Try iColor = GetColorFromString(sColor) + If Error Then Continue GoSub ADD_COLOR If aColor.Count >= 100 Then Break @@ -191,19 +211,18 @@ Public Sub Menu_Show() $hMenu.Children.Clear - hPattern = Image.Load("img/16/tile.png") - hPattern.FillRect(0, 0, 16, 1, Color.Gray) - hPattern.FillRect(0, 0, 1, 16, Color.Gray) - hPattern.FillRect(0, 15, 16, 1, Color.Gray) - hPattern.FillRect(15, 0, 1, 16, Color.Gray) + hPattern = Image.Load("img/32/tile.png") + Paint.Begin(hPattern) + Paint.DrawRect(0, 0, 32, 32, Color.LightForeground, 2) + Paint.End For Each sColor In aColor hMenu = New Menu($hMenu) As "MenuColor" hMenu.Text = Replace(sColor, "&", "&&") hMenu.Tag = sColor hImage = hPattern.Copy() - hImage.PaintRect(1, 1, hImage.W - 2, hImage.H - 2, GetColorFromString(sColor)) - hMenu.Picture = hImage.Picture + hImage.PaintRect(2, 2, 28, 28, GetColorFromString(sColor)) + hMenu.Picture = hImage.Stretch(Desktop.Scale * 2, Desktop.Scale * 2).Picture Next Return @@ -238,9 +257,9 @@ Public Sub Button_Click() Dim sLine As String Dim X As Integer Dim XD As Integer - Dim sCar As String - Dim XF As Integer Dim iLen As Integer + Dim sPattern As String + Dim iColor As Integer hEditor = GetEditor() sLine = hEditor[hEditor.Line].Text @@ -249,23 +268,31 @@ Public Sub Button_Click() XD = X While XD > 0 - sCar = String.Mid$(sLine, XD, 1) - If Not IsHexa(sCar) Then Break + + For Each sPattern In $aPatternSearch + + sColor = ReadColor(sLine, String.Pos(sLine, XD + 1), sPattern, ByRef iLen) + If Not sColor Then Continue + Try iColor = GetColorFromString(sColor) + If Error Then Continue + + If XD + iLen >= X Then + hEditor.Select(XD, hEditor.Line, XD + iLen, hEditor.Line) + Else + sColor = "" + Endif + + Goto OPEN_DIALOG + + Next + Dec XD + Wend - XF = X - While XF < iLen - sCar = String.Mid$(sLine, XF + 1, 1) - If Not IsHexa(sCar) Then Break - Inc XF - Wend +OPEN_DIALOG: - If XF - XD = 6 Then - sColor = "&H" & String.Mid$(sLine, XD + 1, XF - XD) - Endif - - If FColorChooser.Insert(False, ("Insert color"), $aPattern, sColor) Then Return + If FColorChooser.Insert(False, ("Insert color"), $aPattern, "&H" & Hex$(iColor, 6)) Then Return With GetEditor() .Insert(FColorChooser.Pattern) diff --git a/app/src/gambas3/.src/Editor/Code/FEditor.form b/app/src/gambas3/.src/Editor/Code/FEditor.form index ddab9bc3d..c01a0fc94 100644 --- a/app/src/gambas3/.src/Editor/Code/FEditor.form +++ b/app/src/gambas3/.src/Editor/Code/FEditor.form @@ -91,6 +91,7 @@ Action = ".color" Text = ("Insert color") & "..." Picture = Picture["icon:/small/color"] + Shortcut = "Ctrl+Shift+C" } } { Menu3 Menu @@ -566,7 +567,7 @@ } { Action color Text = "Insert color" - Shortcut = "" + Shortcut = "Ctrl+Shift+C" Picture = "icon:/small/color" } { Action comment diff --git a/app/src/gambas3/.src/Editor/Code/FTextEditor.form b/app/src/gambas3/.src/Editor/Code/FTextEditor.form index 0245e21fb..ffeb081e0 100644 --- a/app/src/gambas3/.src/Editor/Code/FTextEditor.form +++ b/app/src/gambas3/.src/Editor/Code/FTextEditor.form @@ -91,6 +91,7 @@ Action = ".color" Text = ("Insert color") & "..." Picture = Picture["icon:/small/color"] + Shortcut = "Ctrl+Shift+C" } { mnuPretty Menu Action = ".pretty" @@ -617,7 +618,7 @@ } { Action color Text = "Insert color" - Shortcut = "" + Shortcut = "Ctrl+Shift+C" Picture = "icon:/small/color" } { Action comment diff --git a/app/src/gambas3/.src/VersionControl/FVersionControl.form b/app/src/gambas3/.src/VersionControl/FVersionControl.form index 62d9677c9..25de4fd83 100644 --- a/app/src/gambas3/.src/VersionControl/FVersionControl.form +++ b/app/src/gambas3/.src/VersionControl/FVersionControl.form @@ -13,10 +13,6 @@ Count = 4 Index = 0 Text = ("Journal") - { ToolBar1 ToolBar - MoveScaled(2,1,36,4) - Separator = True - } { edtJournal TextEditor MoveScaled(13.25,11.125,16,16) Expand = True