Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 154 additions & 1 deletion docs/Controls/NumberEdit.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
| :--- | :--- | :--- | :--- |
| intonly | false | BOOL | 是否仅支持输入整数,如(true) |
| allownegative | false | BOOL | 是否允许输入负数,如(true) |
| maxvalueenabled | false | BOOL | 是否开启最大值控制,如(true) |
| minvalueenabled | false | BOOL | 是否开启最小值控制,如(true) |
| maxvalue | 0 | INT | 允许输入的最大值 |
| minvalue | 0 | INT | 允许输入的最小值 |

## 可用接口

Expand All @@ -17,6 +21,18 @@
| [SetIntOnly](#SetIntOnly) | 设置是否仅支持输入整数 |
| [IsAllowNegative](#IsAllowNegative) | 判断是否允许输入负数 |
| [SetAllowNegative](#SetAllowNegative) | 设置是否允许输入负数 |
| [GetIntValue](#GetIntValue) | 获取控件中的整数 |
| [GetValue](#GetValue) | 获取控件中的实数 |
| [SetValue](#SetValue) | 设置控件的数值(整数) |
| [SetValue](#SetValue-1) | 设置控件的数值(实数) |
| [IsMaxValueEnabled](#IsMaxValueEnabled) | 判断是否开启最大值控制 |
| [SetMaxValueEnabled](#SetMaxValueEnabled) | 设置是否是否开启最大值控制 |
| [IsMinValueEnabled](#IsMinValueEnabled) | 判断是否开启最小值控制 |
| [SetMinValueEnabled](#SetMinValueEnabled) | 设置是否是否开启最小值控制 |
| [GetMaxValue](#GetMaxValue) | 获取可输入的最大值 |
| [SetMaxValue](#SetMaxValue) | 设置可输入的最大值 |
| [GetMinValue](#GetMinValue) | 获取可输入的最小值 |
| [SetMinValue](#SetMinValue) | 设置可输入的最小值 |

### IsIntOnly

Expand All @@ -31,7 +47,7 @@ bool IsIntOnly()

### SetIntOnly

判断是否仅支持输入整数
设置是否仅支持输入整数

```cpp
void SetIntOnly(bool bIntOnly = true)
Expand Down Expand Up @@ -64,5 +80,142 @@ void SetAllowNegative(bool bAllowNegative = true)
- `bAllowNegative` 为 true 则允许进行负数输入,false 为不允许
- 返回值:无

### GetIntValue

获取控件中的整数

```cpp
int GetIntValue(int def = 0)
```

- 参 数:
- `def` 取值失败时返回的缺省值
- 返回值:返回控件中的整数

### GetValue

获取控件中的实数

```cpp
double GetValue(double def = 0)
```

- 参 数:
- `def` 取值失败时返回的缺省值
- 返回值:返回控件中的实数

### SetValue

设置控件的数值(整数)

```cpp
void SetValue(int value)
```

- 参 数:
- `value` 要设置的整数值
- 返回值:无

### SetValue

设置控件的数值(实数)

```cpp
void SetValue(double value)
```

- 参 数:
- `value` 要设置的实数值
- 返回值:无

### IsMaxValueEnabled

判断是否开启最大值控制

```cpp
bool IsMaxValueEnabled()
```

- 参 数:无
- 返回值:返回 true 表示开启

### SetMaxValueEnabled

设置是否是否开启最大值控制

```cpp
void SetMaxValueEnabled(bool bMaxValueEnabled = false)
```

- 参 数:
- `bMaxValueEnabled` 为 true 开启
- 返回值:无

### IsMinValueEnabled

判断是否开启最小值控制

```cpp
bool IsMinValueEnabled()
```

- 参 数:无
- 返回值:返回 true 表示开启

### SetMinValueEnabled

设置是否是否开启最小值控制

```cpp
void SetMinValueEnabled(bool bMinValueEnabled = false)
```

- 参 数:
- `bMinValueEnabled` 为 true 开启
- 返回值:无

### GetMaxValue

获取可输入的最大值

```cpp
int GetMaxValue()
```

- 参 数:无
- 返回值:返回可输入的最大值

### SetMaxValue

设置可输入的最大值

```cpp
void SetMaxValue(int value)
```

- 参 数:
- `value` 要设置的最大可输入值
- 返回值:无

### GetMinValue

获取可输入的最小值

```cpp
int GetMinValue()
```

- 参 数:无
- 返回值:返回可输入的最小值

### SetMinValue

设置可输入的最小值

```cpp
void SetMinValue(int value)
```

- 参 数:
- `value` 要设置的最小可输入值
- 返回值:无
104 changes: 102 additions & 2 deletions duilib/Control/NumberEdit.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
#include "stdafx.h"
#include "base/thread/thread_manager.h"

#include <sstream>
// These constants are for backward compatibility. They are the
// sizes used for initialization and reset in RichEdit 1.0

namespace ui {

template <typename T1>
T1 StringToNum(const std::string& input, const T1 def) {
std::istringstream iss(input);
T1 num;
iss >> num;
if (iss.fail()) {
return def;
}
else {
return num;
}
}

NumberEdit::NumberEdit() :
RichEdit(),
m_bIntOnly(false),
m_bAllowNegative(false)
m_bAllowNegative(false),
m_bMaxValueEnabled(false),
m_iMaxValue(0),
m_bMinValueEnabled(false),
m_iMinValue(0) //��Сֵ
{
m_bWantTab = false;
m_lTwhStyle &= ~ES_MULTILINE;
Expand All @@ -35,6 +52,65 @@ namespace ui {
m_bAllowNegative = bAllowNegative;
}

int NumberEdit::GetIntValue(int def)
{
return StringToNum<int>(GetUTF8Text(), def);
}

double NumberEdit::GetValue(double def)
{
return StringToNum<double>(GetUTF8Text(), def);
}

void NumberEdit::SetValue(int value)
{
SetText(std::to_wstring(value));
}

void NumberEdit::SetValue(double value)
{
SetText(std::to_wstring(value));
}

bool NumberEdit::IsMaxValueEnabled()
{
return m_bMaxValueEnabled;
}

void NumberEdit::SetMaxValueEnabled(bool bMaxValueEnabled)
{
m_bMaxValueEnabled = bMaxValueEnabled;
}

bool NumberEdit::IsMinValueEnabled()
{
return m_bMinValueEnabled;
}

void NumberEdit::SetMinValueEnabled(bool bMinValueEnabled)
{
m_bMinValueEnabled = bMinValueEnabled;
}

int NumberEdit::GetMaxValue()
{
return m_iMaxValue;
}

void NumberEdit::SetMaxValue(int value)
{
m_iMaxValue = value;
}

int NumberEdit::GetMinValue()
{
return m_iMinValue;
}

void NumberEdit::SetMinValue(int value)
{
m_iMinValue = value;
}
void NumberEdit::SetText(const std::wstring & strText)
{
std::wstring strNum = _ToNumberStr(strText);
Expand Down Expand Up @@ -136,13 +212,37 @@ namespace ui {
TxSendMessage(WM_KEYDOWN, event.wParam, event.lParam, NULL);
}

bool NumberEdit::OnTxTextChanged()
{
if (m_pWindow != NULL) {
if (m_bMaxValueEnabled) {
if (GetValue() > m_iMaxValue) {
SetValue(m_iMaxValue);
}
}

if (m_bMinValueEnabled) {
if (GetValue() < m_iMinValue) {
SetValue(m_iMinValue);
}
}

m_pWindow->SendNotify(this, kEventTextChange);
}

return true;
}
void NumberEdit::SetAttribute(const std::wstring& strName, const std::wstring& strValue)
{
if (strName == _T("number")) {}
else if (strName == _T("wanttab")) {}
else if (strName == _T("multiline")) {}
else if (strName == _T("intonly")) SetIntOnly(strValue == _T("true"));
else if (strName == _T("allownegative")) SetAllowNegative(strValue == _T("true"));
else if (strName == _T("maxvalueenabled")) SetMaxValueEnabled(strValue == _T("true"));
else if (strName == _T("minvalueenabled")) SetMinValueEnabled(strValue == _T("true"));
else if (strName == _T("maxvalue")) SetMaxValue(std::stoi(strValue));
else if (strName == _T("minvalue")) SetMinValue(std::stoi(strValue));
else RichEdit::SetAttribute(strName, strValue);
}

Expand Down
Loading