1 /*******************************************************
2 Copyright (C) 2006-2012
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 ********************************************************/
18 
19 #pragma once
20 #include <wx/textctrl.h>
21 #include <wx/string.h>
22 #include "mmCalculator.h"
23 #include "model/Model_Currency.h"
24 #include "model/Model_Account.h"
25 #include <wx/richtooltip.h>
26 
27 class mmTextCtrl : public wxTextCtrl
28 {
29 public:
30     using wxTextCtrl::SetValue;
31 
mmTextCtrl()32     mmTextCtrl() : currency_(0) {}
33     mmTextCtrl(wxWindow *parent, wxWindowID id
34         , const wxString &value = wxEmptyString
35         , const wxPoint &pos = wxDefaultPosition
36         , const wxSize &size = wxDefaultSize
37         , long style = 0
38         , const wxValidator &validator = wxDefaultValidator
39         , const wxString &name = wxTextCtrlNameStr)
wxTextCtrl(parent,id,value,pos,size,style,validator,name)40         : wxTextCtrl(parent, id, value, pos, size, style, validator, name)
41         , currency_(Model_Currency::GetBaseCurrency())
42     {}
SetValue(double value)43     void SetValue(double value)
44     {
45         this->SetValue(Model_Currency::toString(value, currency_));
46     }
SetValue(double value,int precision)47     void SetValue(double value, int precision)
48     {
49         this->SetValue(Model_Currency::toString(value, currency_, precision));
50     }
51     void SetValue(double value, const Model_Account::Data* account, int precision = -1)
52     {
53         if (account) currency_ = Model_Currency::instance().get(account->CURRENCYID);
54         this->SetValue(value, precision > -1 ? precision : log10(currency_->SCALE));
55     }
56     void SetValue(double value, const Model_Currency::Data* currency, int precision = -1)
57     {
58         currency_ = (currency ? currency : Model_Currency::GetBaseCurrency());
59         this->SetValue(value, precision > -1 ? precision : log10(currency_->SCALE));
60     }
61     bool Calculate(const Model_Currency::Data* currency, int alt_precision = -1)
62     {
63         if (!currency || this->GetValue().empty()) return false;
64         mmCalculator calc;
65         int precision = alt_precision >= 0 ? alt_precision : log10(currency->SCALE);
66         const wxString str = Model_Currency::fromString2Default(this->GetValue(), currency);
67         if (calc.is_ok(str)) {
68             this->SetValue(Model_Currency::toString(calc.get_result(), currency, precision));
69             this->SetInsertionPoint(this->GetValue().Len());
70             return true;
71         }
72         return false;
73     }
GetValue()74     wxString GetValue() const
75     {
76         // Remove prefix and suffix characters from value
77         // Base class handles the thousands seperator
78         return /*Model_Currency::fromString(*/wxTextCtrl::GetValue()/*, currency_)*/;
79     }
80     bool GetDouble(double &amount, const Model_Currency::Data* currency = nullptr) const
81     {
82         wxString amountStr = this->GetValue().Trim();
83         return Model_Currency::fromString(amountStr, amount, (currency ? currency : currency_));
84     }
85     bool checkValue(double &amount, const Model_Currency::Data* currency = nullptr, bool positive_only = true)
86     {
87         if (!GetDouble(amount, currency) || (positive_only && amount < 0))
88         {
89             wxRichToolTip tip(_("Invalid Amount."),
90                 _("Please enter a positive or calculated value.")
91                 + "\n\n"
92                 + _("Tip: For calculations, enter expressions like (2+2)*(2+2)\nCalculations will be evaluated and the result used as the entry."));
93             tip.SetIcon(wxICON_WARNING);
94             tip.ShowFor(this);
95             SetFocus();
96             return false;
97         }
98         return true;
99     }
100 private:
101     const Model_Currency::Data* currency_;
102 };
103 
104 
105