summaryrefslogtreecommitdiff
path: root/dbaccess
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2020-08-18 10:35:32 +0100
committerCaolán McNamara <caolanm@redhat.com>2020-08-18 13:50:01 +0200
commit4b97463059e9a616f9eedf142e73d49fc874d4d6 (patch)
treeb816c92831576700528dc8bcb6eb54c39348df7f /dbaccess
parent1b006f8aaa7fd2dd9394f8a74f85999e58fb2310 (diff)
merge OSqlEdit and MultiLineEditSyntaxHighlight
Change-Id: Ie3e38afd976620068c644809c4a88cd3809759a5 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/100907 Tested-by: Jenkins Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Diffstat (limited to 'dbaccess')
-rw-r--r--dbaccess/source/ui/control/sqledit.cxx173
-rw-r--r--dbaccess/source/ui/dlg/directsql.cxx4
-rw-r--r--dbaccess/source/ui/inc/sqledit.hxx23
3 files changed, 181 insertions, 19 deletions
diff --git a/dbaccess/source/ui/control/sqledit.cxx b/dbaccess/source/ui/control/sqledit.cxx
index ccbe210afd5f..aa1745369edd 100644
--- a/dbaccess/source/ui/control/sqledit.cxx
+++ b/dbaccess/source/ui/control/sqledit.cxx
@@ -32,11 +32,14 @@
#include <undosqledit.hxx>
#include <QueryDesignView.hxx>
#include <svx/svxids.hrc>
-#include <vcl/settings.hxx>
#include <cppuhelper/implbase.hxx>
#include <i18nlangtag/languagetag.hxx>
#include <vcl/event.hxx>
+#include <vcl/settings.hxx>
#include <vcl/svapp.hxx>
+#include <vcl/txtattr.hxx>
+#include <vcl/textview.hxx>
+#include <vcl/xtextedt.hxx>
using namespace dbaui;
@@ -65,12 +68,15 @@ private:
OSqlEdit & editor_;
};
-OSqlEdit::OSqlEdit( OQueryTextView* pParent ) :
- MultiLineEditSyntaxHighlight( pParent, WB_LEFT | WB_VSCROLL | WB_BORDER )
- ,m_pView(pParent)
- ,m_bAccelAction( false )
- ,m_bStopTimer(false )
+OSqlEdit::OSqlEdit(OQueryTextView* pParent)
+ : VclMultiLineEdit(pParent, WB_LEFT | WB_VSCROLL | WB_BORDER)
+ , aHighlighter(HighlighterLanguage::SQL)
+ , m_pView(pParent)
+ , m_bAccelAction( false )
+ , m_bStopTimer(false )
{
+ EnableUpdateData(300);
+
SetHelpId( HID_CTL_QRYSQLEDIT );
SetModifyHdl( LINK(this, OSqlEdit, ModifyHdl) );
@@ -103,6 +109,148 @@ OSqlEdit::OSqlEdit( OQueryTextView* pParent ) :
EnableFocusSelectionHide( false );
}
+void OSqlEdit::DoBracketHilight(sal_uInt16 nKey)
+{
+ TextSelection aCurrentPos = GetTextView()->GetSelection();
+ sal_Int32 nStartPos = aCurrentPos.GetStart().GetIndex();
+ const sal_uInt32 nStartPara = aCurrentPos.GetStart().GetPara();
+ sal_uInt16 nCount = 0;
+ int nChar = -1;
+
+ switch (nKey)
+ {
+ case '\'': // no break
+ case '"':
+ {
+ nChar = nKey;
+ break;
+ }
+ case '}' :
+ {
+ nChar = '{';
+ break;
+ }
+ case ')':
+ {
+ nChar = '(';
+ break;
+ }
+ case ']':
+ {
+ nChar = '[';
+ break;
+ }
+ }
+
+ if (nChar == -1)
+ return;
+
+ sal_uInt32 nPara = nStartPara;
+ do
+ {
+ if (nPara == nStartPara && nStartPos == 0)
+ continue;
+
+ OUString aLine( GetTextEngine()->GetText( nPara ) );
+
+ if (aLine.isEmpty())
+ continue;
+
+ for (sal_Int32 i = (nPara==nStartPara) ? nStartPos-1 : aLine.getLength()-1; i>0; --i)
+ {
+ if (aLine[i] == nChar)
+ {
+ if (!nCount)
+ {
+ GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nPara, i, i+1 );
+ GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nPara, i, i+1 );
+ GetTextEngine()->SetAttrib( TextAttribFontWeight( WEIGHT_ULTRABOLD ), nStartPara, nStartPos, nStartPos );
+ GetTextEngine()->SetAttrib( TextAttribFontColor( Color(0,0,0) ), nStartPara, nStartPos, nStartPos );
+ return;
+ }
+ else
+ --nCount;
+ }
+ if (aLine[i] == nKey)
+ ++nCount;
+ }
+ } while (nPara--);
+}
+
+bool OSqlEdit::PreNotify( NotifyEvent& rNEvt )
+{
+ if ( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
+ DoBracketHilight(rNEvt.GetKeyEvent()->GetCharCode());
+
+ return VclMultiLineEdit::PreNotify(rNEvt);
+}
+
+Color OSqlEdit::GetSyntaxHighlightColor(const svtools::ColorConfig& rColorConfig, HighlighterLanguage eLanguage, TokenType aToken)
+{
+ Color aColor;
+ switch (eLanguage)
+ {
+ case HighlighterLanguage::SQL:
+ {
+ switch (aToken)
+ {
+ case TokenType::Identifier: aColor = rColorConfig.GetColorValue(svtools::SQLIDENTIFIER).nColor; break;
+ case TokenType::Number: aColor = rColorConfig.GetColorValue(svtools::SQLNUMBER).nColor; break;
+ case TokenType::String: aColor = rColorConfig.GetColorValue(svtools::SQLSTRING).nColor; break;
+ case TokenType::Operator: aColor = rColorConfig.GetColorValue(svtools::SQLOPERATOR).nColor; break;
+ case TokenType::Keywords: aColor = rColorConfig.GetColorValue(svtools::SQLKEYWORD).nColor; break;
+ case TokenType::Parameter: aColor = rColorConfig.GetColorValue(svtools::SQLPARAMETER).nColor; break;
+ case TokenType::Comment: aColor = rColorConfig.GetColorValue(svtools::SQLCOMMENT).nColor; break;
+ default: aColor = Color(0,0,0);
+ }
+ break;
+ }
+ case HighlighterLanguage::Basic:
+ {
+ switch (aToken)
+ {
+ case TokenType::Identifier: aColor = Color(255,0,0); break;
+ case TokenType::Comment: aColor = Color(0,0,45); break;
+ case TokenType::Number: aColor = Color(204,102,204); break;
+ case TokenType::String: aColor = Color(0,255,45); break;
+ case TokenType::Operator: aColor = Color(0,0,100); break;
+ case TokenType::Keywords: aColor = Color(0,0,255); break;
+ case TokenType::Error : aColor = Color(0,255,255); break;
+ default: aColor = Color(0,0,0);
+ }
+ break;
+ }
+ default: aColor = Color(0,0,0);
+
+ }
+ return aColor;
+}
+
+Color OSqlEdit::GetColorValue(TokenType aToken)
+{
+ return GetSyntaxHighlightColor(m_aColorConfig, aHighlighter.GetLanguage(), aToken);
+}
+
+void OSqlEdit::UpdateData()
+{
+ // syntax highlighting
+ // this must be possible improved by using notifychange correctly
+ bool bTempModified = GetTextEngine()->IsModified();
+ for (sal_uInt32 nLine=0; nLine < GetTextEngine()->GetParagraphCount(); ++nLine)
+ {
+ OUString aLine( GetTextEngine()->GetText( nLine ) );
+ GetTextEngine()->RemoveAttribs( nLine );
+ std::vector<HighlightPortion> aPortions;
+ aHighlighter.getHighlightPortions( aLine, aPortions );
+ for (auto const& portion : aPortions)
+ {
+ GetTextEngine()->SetAttrib( TextAttribFontColor( GetColorValue(portion.tokenType) ), nLine, portion.nBegin, portion.nEnd );
+ }
+ }
+ GetTextView()->ShowCursor( false );
+ GetTextEngine()->SetModified(bTempModified);
+}
+
OSqlEdit::~OSqlEdit()
{
disposeOnce();
@@ -122,7 +270,7 @@ void OSqlEdit::dispose()
}
m_ColorConfig.RemoveListener(this);
m_pView.clear();
- MultiLineEditSyntaxHighlight::dispose();
+ VclMultiLineEdit::dispose();
}
void OSqlEdit::KeyInput( const KeyEvent& rKEvt )
@@ -136,17 +284,16 @@ void OSqlEdit::KeyInput( const KeyEvent& rKEvt )
if( (aKeyFunc==KeyFuncType::CUT)||(aKeyFunc==KeyFuncType::COPY)||(aKeyFunc==KeyFuncType::PASTE) )
m_bAccelAction = true;
- MultiLineEditSyntaxHighlight::KeyInput( rKEvt );
+ VclMultiLineEdit::KeyInput( rKEvt );
if( m_bAccelAction )
m_bAccelAction = false;
}
-
void OSqlEdit::GetFocus()
{
m_strOrigText =GetText();
- MultiLineEditSyntaxHighlight::GetFocus();
+ VclMultiLineEdit::GetFocus();
}
IMPL_LINK_NOARG(OSqlEdit, OnUndoActionTimer, Timer *, void)
@@ -200,7 +347,9 @@ void OSqlEdit::SetText(const OUString& rNewText)
LINK(this, OSqlEdit, OnUndoActionTimer).Call(nullptr);
}
- MultiLineEditSyntaxHighlight::SetText(rNewText);
+ VclMultiLineEdit::SetText(rNewText);
+ UpdateData();
+
m_strOrigText =rNewText;
}
@@ -222,7 +371,7 @@ void OSqlEdit::ConfigurationChanged( utl::ConfigurationBroadcaster* pOption, Con
{
assert( pOption == &m_ColorConfig );
(void) pOption; // avoid warnings
- MultiLineEditSyntaxHighlight::UpdateData();
+ VclMultiLineEdit::UpdateData();
}
void OSqlEdit::ImplSetFont()
diff --git a/dbaccess/source/ui/dlg/directsql.cxx b/dbaccess/source/ui/dlg/directsql.cxx
index 5e7b579df72e..f4d59fea8820 100644
--- a/dbaccess/source/ui/dlg/directsql.cxx
+++ b/dbaccess/source/ui/dlg/directsql.cxx
@@ -19,6 +19,7 @@
#include <core_resource.hxx>
#include <directsql.hxx>
+#include <sqledit.hxx>
#include <strings.hrc>
#include <comphelper/types.hxx>
#include <editeng/colritem.hxx>
@@ -26,7 +27,6 @@
#include <editeng/eeitem.hxx>
#include <osl/mutex.hxx>
#include <svl/itemset.hxx>
-#include <svtools/editsyntaxhighlighter.hxx>
#include <tools/diagnose_ex.h>
#include <vcl/event.hxx>
#include <vcl/svapp.hxx>
@@ -449,7 +449,7 @@ namespace dbaui
Color DirectSQLDialog::GetColorValue(TokenType aToken)
{
- return MultiLineEditSyntaxHighlight::GetSyntaxHighlightColor(m_aColorConfig, m_aHighlighter.GetLanguage(), aToken);
+ return OSqlEdit::GetSyntaxHighlightColor(m_aColorConfig, m_aHighlighter.GetLanguage(), aToken);
}
IMPL_LINK_NOARG(DirectSQLDialog, ImplUpdateDataHdl, Timer*, void)
diff --git a/dbaccess/source/ui/inc/sqledit.hxx b/dbaccess/source/ui/inc/sqledit.hxx
index 8337ae398c64..478a248bf78b 100644
--- a/dbaccess/source/ui/inc/sqledit.hxx
+++ b/dbaccess/source/ui/inc/sqledit.hxx
@@ -21,21 +21,25 @@
#include <sal/config.h>
+#include <comphelper/syntaxhighlight.hxx>
#include <rtl/ref.hxx>
-#include <svtools/editsyntaxhighlighter.hxx>
#include <svtools/colorcfg.hxx>
+#include <vcl/vclmedit.hxx>
namespace com::sun::star::beans { class XMultiPropertySet; }
namespace dbaui
{
class OQueryTextView;
- class OSqlEdit final : public MultiLineEditSyntaxHighlight, public utl::ConfigurationListener
+ class OSqlEdit final : public VclMultiLineEdit, public utl::ConfigurationListener
{
private:
class ChangesListener;
friend class ChangesListener;
+ SyntaxHighlighter aHighlighter;
+ svtools::ColorConfig m_aColorConfig;
+
Timer m_timerInvalidate;
Timer m_timerUndoActionCreation;
OUString m_strOrigText; // is restored on undo
@@ -51,8 +55,10 @@ namespace dbaui
DECL_LINK(OnUndoActionTimer, Timer*, void);
DECL_LINK(OnInvalidateTimer, Timer*, void);
- void ImplSetFont();
+ void ImplSetFont();
+ void DoBracketHilight(sal_uInt16 aKey);
+ virtual bool PreNotify( NotifyEvent& rNEvt ) override;
virtual void KeyInput( const KeyEvent& rKEvt ) override;
virtual void GetFocus() override;
@@ -65,12 +71,19 @@ namespace dbaui
// Edit overridables
virtual void SetText(const OUString& rNewText) override;
- using MultiLineEditSyntaxHighlight::SetText;
+ virtual void SetText( const OUString& rStr, const Selection& rNewSelection ) override
+ { SetText( rStr ); SetSelection( rNewSelection ); }
+
+ virtual void UpdateData() override;
+
+ static Color GetSyntaxHighlightColor(const svtools::ColorConfig& rColorConfig, HighlighterLanguage eLanguage, TokenType aToken);
+
+ Color GetColorValue(TokenType aToken);
// own functionality
// Cut, Copy, Paste by Accel. runs the action in the Edit but also the
// corresponding slot in the View. Therefore, the action occurs twice.
- // To prevent this, SlotExec in View can call this function.
+ // To prevent this, SlotExec in View can call this function.
bool IsInAccelAct() const { return m_bAccelAction; }
void stopTimer();