ENH:add "text to image" function in TextInput

jira: none
Change-Id: Ibdb57b74511432e81faa0c556bb6e639d5a174f5
(cherry picked from commit 09323aeed34f29f105b95d3d6a2c7a151e17e42a)
This commit is contained in:
zhou.xu
2025-01-15 12:18:58 +08:00
committed by Noisyfox
parent 2e8a5ecd87
commit 200a811dca
9 changed files with 78 additions and 11 deletions

View File

@@ -121,8 +121,18 @@ void ComboBox::SetLabel(const wxString &value)
{
if (GetTextCtrl()->IsShown() || text_off)
GetTextCtrl()->SetValue(value);
else
TextInput::SetLabel(value);
else {
if (is_replace_text_to_image) {
auto new_value = value;
new_value.Replace(replace_text, "", false);//replace first text
TextInput::SetIcon_1(image_for_text);
TextInput::SetLabel(new_value);
}
else {
TextInput::SetIcon_1("");
TextInput::SetLabel(value);
}
}
}
wxString ComboBox::GetLabel() const
@@ -201,6 +211,13 @@ void ComboBox::DoDeleteOneItem(unsigned int pos)
unsigned int ComboBox::GetCount() const { return items.size(); }
void ComboBox::set_replace_text(wxString text, wxString image_name)
{
replace_text = text;
image_for_text = image_name;
is_replace_text_to_image = true;
}
wxString ComboBox::GetString(unsigned int n) const
{ return n < items.size() ? items[n].text : wxString{}; }

View File

@@ -15,6 +15,9 @@ class ComboBox : public wxWindowWithItems<TextInput, wxItemContainer>
DropDown drop;
bool drop_down = false;
bool text_off = false;
bool is_replace_text_to_image = false;
wxString replace_text;
wxString image_for_text;
public:
ComboBox(wxWindow * parent,
@@ -37,6 +40,7 @@ public:
int Append(const wxString &item, const wxBitmap &bitmap, const wxString &group, void *clientData = nullptr);
void set_replace_text(wxString text, wxString image_name);
unsigned int GetCount() const override;
int GetSelection() const override;

View File

@@ -111,6 +111,17 @@ void TextInput::SetIcon(const wxString &icon)
Rescale();
}
void TextInput::SetIcon_1(const wxString &icon) {
if (this->icon_1.name() == icon.ToStdString())
return;
if (icon.empty()) {
this->icon_1 = ScalableBitmap();
return;
}
this->icon_1 = ScalableBitmap(this, icon.ToStdString(), 16);
Rescale();
}
void TextInput::SetLabelColor(StateColor const &color)
{
label_color = color;
@@ -127,6 +138,8 @@ void TextInput::Rescale()
{
if (!this->icon.name().empty())
this->icon.msw_rescale();
if (!this->icon_1.name().empty())
this->icon_1.msw_rescale();
messureSize();
Refresh();
}
@@ -166,6 +179,10 @@ void TextInput::DoSetSize(int x, int y, int width, int height, int sizeFlags)
wxSize szIcon = this->icon.GetBmpSize();
textPos.x += szIcon.x;
}
if (this->icon_1.bmp().IsOk()) {
wxSize szIcon = this->icon_1.GetBmpSize();
textPos.x += (szIcon.x);
}
bool align_right = GetWindowStyle() & wxALIGN_RIGHT;
if (align_right)
textPos.x += labelSize.x;
@@ -214,6 +231,16 @@ void TextInput::render(wxDC& dc)
dc.DrawBitmap(icon.bmp(), pt);
pt.x += szIcon.x + 0;
}
if (icon_1.bmp().IsOk()) {
wxSize szIcon = icon_1.GetBmpSize();
pt.y = (size.y - szIcon.y) / 2;
if (align_center) {
if (pt.x * 2 + szIcon.x + 0 + labelSize.x < size.x)
pt.x = (size.x - (szIcon.x + 0 + labelSize.x)) / 2;
}
dc.DrawBitmap(icon_1.bmp(), pt);
pt.x += szIcon.x + 0;
}
auto text = wxWindow::GetLabel();
if (!text.IsEmpty()) {
wxSize textSize = text_ctrl->GetSize();

View File

@@ -9,6 +9,7 @@ class TextInput : public wxNavigationEnabled<StaticBox>
wxSize labelSize;
ScalableBitmap icon;
ScalableBitmap icon_1;
StateColor label_color;
StateColor text_color;
wxTextCtrl * text_ctrl;
@@ -44,6 +45,8 @@ public:
void SetIcon(const wxString & icon);
void SetIcon_1(const wxString &icon);
void SetLabelColor(StateColor const &color);
void SetTextColor(StateColor const &color);