basctl/source/basicide/BasicColorConfig.cxx                  |   31 ++++--
 basctl/source/basicide/baside2.cxx                           |    1 
 basctl/source/basicide/baside2.hxx                           |    7 +
 basctl/source/basicide/baside2b.cxx                          |   51 +++++++++++
 basctl/source/basicide/linenumberwindow.cxx                  |   26 +++++
 basctl/source/basicide/linenumberwindow.hxx                  |    1 
 basctl/source/inc/colorscheme.hxx                            |    1 
 officecfg/registry/data/org/openoffice/Office/BasicIDE.xcu   |   30 ++++++
 officecfg/registry/schema/org/openoffice/Office/BasicIDE.xcs |   10 ++
 9 files changed, 149 insertions(+), 9 deletions(-)

New commits:
commit bf410be4502c2d78e38856c455833681c6b8a151
Author:     Rafael Lima <rafael.palma.l...@gmail.com>
AuthorDate: Wed Jan 10 20:34:55 2024 +0100
Commit:     Andreas Heinisch <andreas.heini...@yahoo.de>
CommitDate: Thu Jan 11 19:56:08 2024 +0100

    tdf#158750 Highlight the line where the cursor is positioned (Basic IDE)
    
    This patch implements support for highlighting the selected line in the 
code editor, similar to what Kate, VSCode, etc do.
    
    If the cursor is positioned in a single line and nothing is selected, then 
a highlight color is applied to the line. The line number window also 
highlights the selected line.
    
    Change-Id: I2047d79500cd783b122b6752bb00996de0a7c702
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/161861
    Tested-by: Jenkins
    Reviewed-by: Andreas Heinisch <andreas.heini...@yahoo.de>

diff --git a/basctl/source/basicide/BasicColorConfig.cxx 
b/basctl/source/basicide/BasicColorConfig.cxx
index 21ff95bc0f8e..ab17b164934b 100644
--- a/basctl/source/basicide/BasicColorConfig.cxx
+++ b/basctl/source/basicide/BasicColorConfig.cxx
@@ -49,13 +49,16 @@ ColorScheme BasicColorConfig::GetColorScheme(const 
OUString& rScheme)
         return GetAutomaticColorScheme();
     }
 
-    std::vector<OUString> aVecPropNames = {
-        OUString(rScheme + "/GenericColor/Color"),   OUString(rScheme + 
"/IdentifierColor/Color"),
-        OUString(rScheme + "/NumberColor/Color"),    OUString(rScheme + 
"/StringColor/Color"),
-        OUString(rScheme + "/CommentColor/Color"),   OUString(rScheme + 
"/ErrorColor/Color"),
-        OUString(rScheme + "/OperatorColor/Color"),  OUString(rScheme + 
"/KeywordColor/Color"),
-        OUString(rScheme + "/BackgroundColor/Color")
-    };
+    std::vector<OUString> aVecPropNames = { OUString(rScheme + 
"/GenericColor/Color"),
+                                            OUString(rScheme + 
"/IdentifierColor/Color"),
+                                            OUString(rScheme + 
"/NumberColor/Color"),
+                                            OUString(rScheme + 
"/StringColor/Color"),
+                                            OUString(rScheme + 
"/CommentColor/Color"),
+                                            OUString(rScheme + 
"/ErrorColor/Color"),
+                                            OUString(rScheme + 
"/OperatorColor/Color"),
+                                            OUString(rScheme + 
"/KeywordColor/Color"),
+                                            OUString(rScheme + 
"/BackgroundColor/Color"),
+                                            OUString(rScheme + 
"/LineHighlightColor/Color") };
 
     css::uno::Sequence<OUString> aPropNames(aVecPropNames.size());
     OUString* pPropNames = aPropNames.getArray();
@@ -77,12 +80,22 @@ ColorScheme BasicColorConfig::GetColorScheme(const 
OUString& rScheme)
     aColors[6] >>= aColorScheme.m_aOperatorColor;
     aColors[7] >>= aColorScheme.m_aKeywordColor;
     aColors[8] >>= aColorScheme.m_aBackgroundColor;
+    aColors[9] >>= aColorScheme.m_aLineHighlightColor;
 
     return aColorScheme;
 }
 
 ColorScheme BasicColorConfig::GetAutomaticColorScheme()
 {
+    // Application Colors do not define a line highlight color, so here we 
adjust
+    // the background color to get a highlight color
+    Color aBackgroundColor = 
aColorConfig.GetColorValue(svtools::BASICEDITOR).nColor;
+    Color aHighlightColor(aBackgroundColor);
+    if (aBackgroundColor.IsDark())
+        aHighlightColor.ApplyTintOrShade(1000);
+    else
+        aHighlightColor.ApplyTintOrShade(-1000);
+
     ColorScheme aScheme = { DEFAULT_SCHEME,
                             false,
                             
aColorConfig.GetColorValue(svtools::FONTCOLOR).nColor,
@@ -93,7 +106,9 @@ ColorScheme BasicColorConfig::GetAutomaticColorScheme()
                             
aColorConfig.GetColorValue(svtools::BASICERROR).nColor,
                             
aColorConfig.GetColorValue(svtools::BASICOPERATOR).nColor,
                             
aColorConfig.GetColorValue(svtools::BASICKEYWORD).nColor,
-                            
aColorConfig.GetColorValue(svtools::BASICEDITOR).nColor };
+                            aBackgroundColor,
+                            aHighlightColor };
+
     return aScheme;
 }
 
diff --git a/basctl/source/basicide/baside2.cxx 
b/basctl/source/basicide/baside2.cxx
index a36cec6fe245..74c25d6f4631 100644
--- a/basctl/source/basicide/baside2.cxx
+++ b/basctl/source/basicide/baside2.cxx
@@ -1618,6 +1618,7 @@ void 
ModulWindowLayout::SyntaxColors::ApplyColorScheme(OUString aSchemeId, bool
     {
         pEditor->ChangeFontColor(aFontColor);
         pEditor->SetBackground(Wallpaper(aDocColor));
+        pEditor->SetLineHighlightColor(aColorScheme.m_aLineHighlightColor);
         pEditor->Invalidate();
     }
 
diff --git a/basctl/source/basicide/baside2.hxx 
b/basctl/source/basicide/baside2.hxx
index c07141b7079f..cd0485c46b4b 100644
--- a/basctl/source/basicide/baside2.hxx
+++ b/basctl/source/basicide/baside2.hxx
@@ -105,6 +105,11 @@ private:
     bool            bDoSyntaxHighlight;
     bool            bDelayHighlight;
 
+    // Used to determine if the highlighted line has changed, which would 
require redrawing the highlight
+    sal_uInt32      m_nLastHighlightPara;
+
+    Color           m_aLineHighlightColor;
+
     virtual css::uno::Reference< css::awt::XVclWindowPeer > 
GetComponentInterface(bool bCreate = true) override;
     CodeCompleteDataCache aCodeCompleteCache;
     VclPtr<CodeCompleteWindow> pCodeCompleteWnd;
@@ -130,6 +135,7 @@ private:
     void            DoSyntaxHighlight( sal_uInt32 nPara );
     OUString        GetWordAtCursor();
     bool            ImpCanModify();
+    void            HighlightCurrentLine(vcl::RenderContext& rRenderContext);
 
 public:
                     EditorWindow (vcl::Window* pParent, ModulWindow*);
@@ -156,6 +162,7 @@ public:
 
     void            ChangeFontColor( Color aColor );
     void            UpdateSyntaxHighlighting ();
+    void            SetLineHighlightColor(Color aColor);
 
     void            SetEditorZoomLevel(sal_uInt16 nNewZoomLevel);
     sal_uInt16      GetCurrentZoom() { return nCurrentZoomLevel; }
diff --git a/basctl/source/basicide/baside2b.cxx 
b/basctl/source/basicide/baside2b.cxx
index 0cb13161178b..52b2194a93d9 100644
--- a/basctl/source/basicide/baside2b.cxx
+++ b/basctl/source/basicide/baside2b.cxx
@@ -71,6 +71,7 @@
 #include "uiobject.hxx"
 #include <basegfx/utils/zoomtools.hxx>
 #include <svl/itemset.hxx>
+#include <BasicColorConfig.hxx>
 
 namespace basctl
 {
@@ -248,12 +249,14 @@ EditorWindow::EditorWindow (vcl::Window* pParent, 
ModulWindow* pModulWindow) :
     bHighlighting(false),
     bDoSyntaxHighlight(true),
     bDelayHighlight(true),
+    m_nLastHighlightPara(0),
     pCodeCompleteWnd(VclPtr<CodeCompleteWindow>::Create(this))
 {
     set_id("EditorWindow");
     const Wallpaper 
aBackground(rModulWindow.GetLayout().GetSyntaxBackgroundColor());
     SetBackground(aBackground);
     GetWindow(GetWindowType::Border)->SetBackground(aBackground);
+    
SetLineHighlightColor(GetShell()->GetColorConfig()->GetCurrentColorScheme().m_aLineHighlightColor);
     SetPointer( PointerStyle::Text );
     SetHelpId( HID_BASICIDE_EDITORWINDOW );
 
@@ -729,6 +732,11 @@ void EditorWindow::HandleAutoCorrect()
     }
 }
 
+void EditorWindow::SetLineHighlightColor(Color aColor)
+{
+    m_aLineHighlightColor = aColor;
+}
+
 TextSelection EditorWindow::GetLastHighlightPortionTextSelection() const
 {//creates a text selection from the highlight portion on the cursor
     const sal_uInt32 nLine = 
GetEditView()->GetSelection().GetStart().GetPara();
@@ -976,9 +984,34 @@ void EditorWindow::Paint(vcl::RenderContext& 
rRenderContext, const tools::Rectan
     if (!pEditEngine)     // We need it now at latest
         CreateEditEngine();
 
+    HighlightCurrentLine(rRenderContext);
+
     pEditView->Paint(rRenderContext, rRect);
 }
 
+void EditorWindow::HighlightCurrentLine(vcl::RenderContext& rRenderContext)
+{
+    // If the cursor is in a single line and nothing is selected, then a 
highlight color
+    // is applied to the background of the current line
+    TextPaM aStartPaM = pEditView->GetSelection().GetStart();
+    TextPaM aEndPaM = pEditView->GetSelection().GetEnd();
+    if (aStartPaM == aEndPaM)
+    {
+        Size aWinSize(GetOutputSizePixel());
+        sal_Int16 nDocPosY = pEditView->GetStartDocPos().Y();
+        sal_Int16 nY1 = pEditEngine->PaMtoEditCursor(aStartPaM).TopLeft().Y();
+        sal_Int16 nY2 = 
pEditEngine->PaMtoEditCursor(aStartPaM).BottomRight().Y();
+        // Only draw if the cursor is in a visible position
+        if ((nY1 >= nDocPosY && nY1 <= nDocPosY + aWinSize.Height())
+            || (nY2 >= nDocPosY && nY2 <= nDocPosY + aWinSize.Height()))
+        {
+            tools::Rectangle aRect(Point(0, nY1 - nDocPosY), 
Point(aWinSize.Width(), nY2 - nDocPosY));
+            rRenderContext.SetFillColor(m_aLineHighlightColor);
+            rRenderContext.DrawRect(aRect);
+        }
+    }
+}
+
 void EditorWindow::LoseFocus()
 {
     // tdf#114258 wait until the next event loop cycle to do this so it doesn't
@@ -1169,6 +1202,24 @@ void EditorWindow::Notify( SfxBroadcaster& /*rBC*/, 
const SfxHint& rHint )
             pBindings->Invalidate( SID_COPY );
         }
     }
+    else if( rTextHint.GetId() == SfxHintId::TextViewCaretChanged )
+    {
+        // Check whether the line number where the caret is has changed and the
+        // highlight needs to be redrawn
+        sal_uInt32 nStartPara = pEditView->GetSelection().GetStart().GetPara();
+        sal_uInt32 nEndPara = pEditView->GetSelection().GetEnd().GetPara();
+        if (nStartPara == nEndPara && nStartPara != m_nLastHighlightPara)
+        {
+            m_nLastHighlightPara = nStartPara;
+            Invalidate();
+            rModulWindow.GetLineNumberWindow().Invalidate();
+        }
+        else if (nStartPara != nEndPara)
+        {
+            // If multiple lines are selected, then update the line number 
window
+            rModulWindow.GetLineNumberWindow().Invalidate();
+        }
+    }
 }
 
 OUString EditorWindow::GetActualSubName( sal_uInt32 nLine )
diff --git a/basctl/source/basicide/linenumberwindow.cxx 
b/basctl/source/basicide/linenumberwindow.cxx
index 18420199e2a9..7b4b07da726f 100644
--- a/basctl/source/basicide/linenumberwindow.cxx
+++ b/basctl/source/basicide/linenumberwindow.cxx
@@ -25,6 +25,7 @@ LineNumberWindow::LineNumberWindow(vcl::Window* pParent, 
ModulWindow* pModulWind
     SetBackground(aBackground);
     GetWindow(GetWindowType::Border)->SetBackground(aBackground);
     m_FontColor = GetSettings().GetStyleSettings().GetWindowTextColor();
+    m_HighlightColor = GetSettings().GetStyleSettings().GetFaceColor();
     m_nBaseWidth = GetTextWidth("8");
     m_nWidth = m_nBaseWidth * 3 + m_nBaseWidth / 2;
 }
@@ -51,6 +52,7 @@ void LineNumberWindow::Paint(vcl::RenderContext& 
rRenderContext, const tools::Re
         return;
 
     int windowHeight = rRenderContext.GetOutputSize().Height();
+    int windowWidth = rRenderContext.GetOutputSize().Width();
     int nLineHeight = rRenderContext.GetTextHeight();
     if (!nLineHeight)
     {
@@ -78,16 +80,38 @@ void LineNumberWindow::Paint(vcl::RenderContext& 
rRenderContext, const tools::Re
         m_nWidth += m_nBaseWidth;
     }
 
+    vcl::Font aNormalFont = rRenderContext.GetFont();
+    vcl::Font aBoldFont(aNormalFont);
+    aBoldFont.SetWeight(FontWeight::WEIGHT_BOLD);
+
+    sal_uInt32 nParaEnd = txtView->GetSelection().GetEnd().GetPara() + 1;
     sal_Int64 y = (nStartLine - 1) * static_cast<sal_Int64>(nLineHeight);
-    rRenderContext.SetTextColor(m_FontColor);
+
     for (sal_uInt32 n = nStartLine; n <= nEndLine; ++n, y += nLineHeight)
     {
+        // Font weight for the selected lines is bold
+        if (n == nParaEnd)
+        {
+            tools::Rectangle aRect(Point(0, y - m_nCurYOffset),
+                                   Point(windowWidth, y - m_nCurYOffset + 
nLineHeight));
+            rRenderContext.SetFillColor(m_HighlightColor);
+            rRenderContext.DrawRect(aRect);
+            rRenderContext.SetFont(aBoldFont);
+        }
+        else
+        {
+            rRenderContext.SetFont(aNormalFont);
+        }
+
+        rRenderContext.SetTextColor(m_FontColor);
         const OUString aLineNumber = OUString::number(n);
         // tdf#153798 - align line numbers to the right
         rRenderContext.DrawText(
             Point(m_nWidth - GetTextWidth(aLineNumber) - m_nBaseWidth / 2, y - 
m_nCurYOffset),
             aLineNumber);
     }
+    // Restore the original font
+    rRenderContext.SetFont(aNormalFont);
 
     // Resize the parent after calculating the new width and height values
     GetParent()->Resize();
diff --git a/basctl/source/basicide/linenumberwindow.hxx 
b/basctl/source/basicide/linenumberwindow.hxx
index a2e457f71103..5f5ab744f0f0 100644
--- a/basctl/source/basicide/linenumberwindow.hxx
+++ b/basctl/source/basicide/linenumberwindow.hxx
@@ -23,6 +23,7 @@ private:
     tools::Long m_nCurYOffset;
     int m_nBaseWidth;
     Color m_FontColor;
+    Color m_HighlightColor;
     virtual void DataChanged(DataChangedEvent const& rDCEvt) override;
 
 protected:
diff --git a/basctl/source/inc/colorscheme.hxx 
b/basctl/source/inc/colorscheme.hxx
index 5f7514febe3c..96567f79f7ff 100644
--- a/basctl/source/inc/colorscheme.hxx
+++ b/basctl/source/inc/colorscheme.hxx
@@ -37,6 +37,7 @@ typedef struct
     Color m_aOperatorColor;
     Color m_aKeywordColor;
     Color m_aBackgroundColor;
+    Color m_aLineHighlightColor;
 } ColorScheme;
 
 } // namespace basctl
diff --git a/officecfg/registry/data/org/openoffice/Office/BasicIDE.xcu 
b/officecfg/registry/data/org/openoffice/Office/BasicIDE.xcu
index 0448b5570987..c62a2d6af7dd 100644
--- a/officecfg/registry/data/org/openoffice/Office/BasicIDE.xcu
+++ b/officecfg/registry/data/org/openoffice/Office/BasicIDE.xcu
@@ -65,6 +65,11 @@
           <value>16777215</value>
         </prop>
       </node>
+      <node oor:name="LineHighlightColor">
+        <prop oor:name="Color">
+          <value>15658734</value>
+        </prop>
+      </node>
     </node>
     <node oor:name="COLORSCHEME_LIBREOFFICE_DARK" oor:op="replace">
       <node oor:name="GenericColor">
@@ -112,6 +117,11 @@
           <value>0</value>
         </prop>
       </node>
+      <node oor:name="LineHighlightColor">
+        <prop oor:name="Color">
+          <value>2236962</value>
+        </prop>
+      </node>
     </node>
     <node oor:name="COLORSCHEME_BREEZE_LIGHT" oor:op="replace">
       <node oor:name="GenericColor">
@@ -159,6 +169,11 @@
           <value>16777215</value>
         </prop>
       </node>
+      <node oor:name="LineHighlightColor">
+        <prop oor:name="Color">
+          <value>16316406</value>
+        </prop>
+      </node>
     </node>
     <node oor:name="COLORSCHEME_BREEZE_DARK" oor:op="replace">
       <node oor:name="GenericColor">
@@ -206,6 +221,11 @@
           <value>2303529</value>
         </prop>
       </node>
+      <node oor:name="LineHighlightColor">
+        <prop oor:name="Color">
+          <value>2764338</value>
+        </prop>
+      </node>
     </node>
     <node oor:name="COLORSCHEME_SOLARIZED_LIGHT" oor:op="replace">
       <node oor:name="GenericColor">
@@ -253,6 +273,11 @@
           <value>16643811</value>
         </prop>
       </node>
+      <node oor:name="LineHighlightColor">
+        <prop oor:name="Color">
+          <value>15657173</value>
+        </prop>
+      </node>
     </node>
     <node oor:name="COLORSCHEME_SOLARIZED_DARK" oor:op="replace">
       <node oor:name="GenericColor">
@@ -300,6 +325,11 @@
           <value>11062</value>
         </prop>
       </node>
+      <node oor:name="LineHighlightColor">
+        <prop oor:name="Color">
+          <value>472642</value>
+        </prop>
+      </node>
     </node>
   </node>
 </oor:component-data>
diff --git a/officecfg/registry/schema/org/openoffice/Office/BasicIDE.xcs 
b/officecfg/registry/schema/org/openoffice/Office/BasicIDE.xcs
index 1fe9af67f78f..cc783b837046 100644
--- a/officecfg/registry/schema/org/openoffice/Office/BasicIDE.xcs
+++ b/officecfg/registry/schema/org/openoffice/Office/BasicIDE.xcs
@@ -116,6 +116,16 @@
           </info>
         </prop>
       </group>
+      <group oor:name="LineHighlightColor">
+        <info>
+          <desc>Specifies the formatting to highlight the selected line.</desc>
+        </info>
+        <prop oor:name="Color" oor:type="xs:int">
+          <info>
+            <desc>Specifies the color used to highlight the selected 
line.</desc>
+          </info>
+        </prop>
+      </group>
     </group>
     </templates>
     <component>

Reply via email to