tdf#42897 Warn the user if string without quote is entered as condition value.

Change-Id: I5b30b608c0192b434ff237513ed7fbbf5af43f11
diff --git a/sc/inc/globstr.hrc b/sc/inc/globstr.hrc
index eab98cc..9ef50d5 100644
--- a/sc/inc/globstr.hrc
+++ b/sc/inc/globstr.hrc
@@ -695,7 +695,10 @@
#define STR_UNDO_CONDFORMAT             531
#define STR_UNDO_FORMULA_TO_VALUE       532

#define SC_GLOBSTR_STR_COUNT            533     /**< the count of permanently resident strings */
#define STR_UNQUOTED_STRING             533
#define STR_ENTER_VALUE                 534

#define SC_GLOBSTR_STR_COUNT            535     /**< the count of permanently resident strings */

#endif

diff --git a/sc/source/ui/condformat/condformatdlgentry.cxx b/sc/source/ui/condformat/condformatdlgentry.cxx
index 87c7f45..4fd949d 100644
--- a/sc/source/ui/condformat/condformatdlgentry.cxx
+++ b/sc/source/ui/condformat/condformatdlgentry.cxx
@@ -23,9 +23,11 @@
#include <svx/drawitem.hxx>
#include <vcl/msgbox.hxx>
#include <vcl/settings.hxx>
#include <formula/token.hxx>
#include "tokenarray.hxx"
#include "stlpool.hxx"
#include "tabvwsh.hxx"
#include "simpleformulacalc.hxx"

#include "colorformat.hxx"

@@ -124,23 +126,6 @@ void ScCondFrmtEntry::Deselect()
    SetHeight();
}

IMPL_LINK(ScCondFrmtEntry, EdModifyHdl, Edit*, pEdit)
{
    OUString aFormula = pEdit->GetText();
    ScCompiler aComp( mpDoc, maPos );
    aComp.SetGrammar( mpDoc->GetGrammar() );
    boost::scoped_ptr<ScTokenArray> mpCode(aComp.CompileString(aFormula));
    if(mpCode->GetCodeError())
    {
        pEdit->SetControlBackground(COL_LIGHTRED);
    }
    else
    {
        pEdit->SetControlBackground(GetSettings().GetStyleSettings().GetWindowColor());
    }
    return 0;
}

//condition

namespace {
@@ -197,6 +182,7 @@ ScConditionFrmtEntry::ScConditionFrmtEntry( vcl::Window* pParent, ScDocument* pD
    maLbCondType( this, ScResId( LB_CELLIS_TYPE ) ),
    maEdVal1( this, NULL, NULL, ScResId( ED_VAL1 ) ),
    maEdVal2( this, NULL, NULL, ScResId( ED_VAL2 ) ),
    maFtVal( this, ScResId( FT_VAL ) ),
    maFtStyle( this, ScResId( FT_STYLE ) ),
    maLbStyle( this, ScResId( LB_STYLE ) ),
    maWdPreview( this, ScResId( WD_PREVIEW ) ),
@@ -256,8 +242,8 @@ void ScConditionFrmtEntry::Init(ScCondFormatDlg* pDialogParent)
    maEdVal1.SetStyle( maEdVal1.GetStyle() | WB_FORCECTRLBACKGROUND );
    maEdVal2.SetStyle( maEdVal2.GetStyle() | WB_FORCECTRLBACKGROUND );

    maEdVal1.SetModifyHdl( LINK( this, ScCondFrmtEntry, EdModifyHdl ) );
    maEdVal2.SetModifyHdl( LINK( this, ScCondFrmtEntry, EdModifyHdl ) );
    maEdVal1.SetModifyHdl( LINK( this, ScConditionFrmtEntry, OnEdChanged ) );
    maEdVal2.SetModifyHdl( LINK( this, ScConditionFrmtEntry, OnEdChanged ) );

    FillStyleListBox( mpDoc, maLbStyle );
    maLbStyle.SetSelectHdl( LINK( this, ScConditionFrmtEntry, StyleSelectHdl ) );
@@ -283,6 +269,59 @@ ScFormatEntry* ScConditionFrmtEntry::createConditionEntry() const
    return pEntry;
}

IMPL_LINK(ScConditionFrmtEntry, OnEdChanged, Edit*, pEdit)
{
    OUString aFormula = pEdit->GetText();

    if( aFormula.isEmpty() )
    {
        maFtVal.SetText(ScGlobal::GetRscString(STR_ENTER_VALUE));
        return 0;
    }

    ScCompiler aComp( mpDoc, maPos );
    aComp.SetGrammar( mpDoc->GetGrammar() );
    boost::scoped_ptr<ScTokenArray> ta(aComp.CompileString(aFormula));

    // Error, warn the user
    if( ta->GetCodeError() )
    {
        pEdit->SetControlBackground(COL_LIGHTRED);
        maFtVal.SetText(ScGlobal::GetRscString(STR_VALID_DEFERROR));
        return 0;
    }

    // Recognized col/row name or string token, warn the user
    formula::FormulaToken* token = ta->First();
    formula::StackVar t = token->GetType();
    OpCode op = token->GetOpCode();
    if( ( op == ocColRowName ) ||
        ( ( op == ocBad ) && ( t == formula::svString ) )
      )
    {
        pEdit->SetControlBackground(COL_LIGHTRED);
        maFtVal.SetText(ScGlobal::GetRscString(STR_UNQUOTED_STRING));
        return 0;
    }

    pEdit->SetControlBackground(GetSettings().GetStyleSettings().GetWindowColor());
    maFtVal.SetText("");
    return 0;
}

void ScConditionFrmtEntry::Select()
{
    maFtVal.Show();
    ScCondFrmtEntry::Select();
}

void ScConditionFrmtEntry::Deselect()
{
    maFtVal.Hide();
    ScCondFrmtEntry::Deselect();
}


sal_Int32 ScConditionFrmtEntry::ConditionModeToEntryPos( ScConditionMode eMode ) const
{
    for ( sal_Int32 i = 0; i < NUM_COND_ENTRIES; ++i )
@@ -994,14 +1033,17 @@ IMPL_LINK_NOARG( ScConditionFrmtEntry, ConditionTypeSelectHdl )
        case 0:
            maEdVal1.Hide();
            maEdVal2.Hide();
            maFtVal.Hide();
            break;
        case 1:
            maEdVal1.Show();
            maEdVal2.Hide();
            maFtVal.Show();
            break;
        case 2:
            maEdVal1.Show();
            maEdVal2.Show();
            maFtVal.Show();
            break;
    }

diff --git a/sc/source/ui/inc/condformatdlg.hrc b/sc/source/ui/inc/condformatdlg.hrc
index 5aa27e6..af801f5 100644
--- a/sc/source/ui/inc/condformatdlg.hrc
+++ b/sc/source/ui/inc/condformatdlg.hrc
@@ -55,4 +55,6 @@
#define FT_ICON_SET_ENTRY_TEXT 49
#define ED_ICON_SET_ENTRY_VALUE 50

#define FT_VAL 51

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/inc/condformatdlgentry.hxx b/sc/source/ui/inc/condformatdlgentry.hxx
index 99b3c7f..0ed7a5b 100644
--- a/sc/source/ui/inc/condformatdlgentry.hxx
+++ b/sc/source/ui/inc/condformatdlgentry.hxx
@@ -59,8 +59,8 @@ protected:

    DECL_LINK( EdModifyHdl, Edit* );

    void Select();
    void Deselect();
    virtual void Select();
    virtual void Deselect();

    virtual OUString GetExpressionString() = 0;

@@ -88,6 +88,7 @@ class ScConditionFrmtEntry : public ScCondFrmtEntry, public SfxListener
    ListBox maLbCondType;
    formula::RefEdit maEdVal1;
    formula::RefEdit maEdVal2;
    FixedText maFtVal;
    FixedText maFtStyle;
    ListBox maLbStyle;
    SvxFontPrevWindow maWdPreview;
@@ -103,6 +104,7 @@ class ScConditionFrmtEntry : public ScCondFrmtEntry, public SfxListener
    void Init(ScCondFormatDlg* pDialogParent);
    DECL_LINK( StyleSelectHdl, void* );
    DECL_LINK( ConditionTypeSelectHdl, void* );
    DECL_LINK( OnEdChanged, Edit* );

    // Searches the lookup table for the entry position, given condition mode
    sal_Int32 ConditionModeToEntryPos( ScConditionMode eMode ) const;
@@ -111,6 +113,10 @@ class ScConditionFrmtEntry : public ScCondFrmtEntry, public SfxListener
    // Returns the number of edit fields used for a given condition mode
    sal_Int32 GetNumberEditFields( ScConditionMode eMode ) const;

protected:
    virtual void Select() SAL_OVERRIDE;
    virtual void Deselect() SAL_OVERRIDE;

public:
    ScConditionFrmtEntry( vcl::Window* pParent, ScDocument* pDoc, ScCondFormatDlg* pDialogParent,
            const ScAddress& rPos, const ScCondFormatEntry* pFormatEntry = NULL );
diff --git a/sc/source/ui/src/condformatdlg.src b/sc/source/ui/src/condformatdlg.src
index 68e91e9..d086dca 100644
--- a/sc/source/ui/src/condformatdlg.src
+++ b/sc/source/ui/src/condformatdlg.src
@@ -190,6 +190,12 @@ Control RID_COND_ENTRY
        Text [ en-US ] = "Example";
        Border = TRUE;
    };
    FixedText FT_VAL
    {
        Pos = MAP_APPFONT( 5, 48 );
        Size = MAP_APPFONT( 300, 12 );
        Text[ en-US ] = "Enter a value!";
    };
    Edit ED_COL_SCALE_MIN
    {
        Pos = MAP_APPFONT( 5, 48 );
diff --git a/sc/source/ui/src/globstr.src b/sc/source/ui/src/globstr.src
index 94be7b8..616342b 100644
--- a/sc/source/ui/src/globstr.src
+++ b/sc/source/ui/src/globstr.src
@@ -2081,6 +2081,14 @@ Resource RID_GLOBSTR
    {
        Text [ en-US ] = "Convert Formula To Value";
    };
    String STR_UNQUOTED_STRING
    {
        Text [ en-US ] = "Strings must be quoted, otherwise they might be interpreted as an address or col/row name!";
    };
    String STR_ENTER_VALUE
    {
        Text[ en-US ] = "Enter a value!";
    };
};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */