mirror of
https://github.com/wxWidgets/wxWidgets.git
synced 2026-09-27 19:17:06 +08:00
Merge branch 'gtk-treectrl-text-size'
Improve size and handling of in-place editor in wxGenericTreeCtrl. See #23001.
This commit is contained in:
@@ -291,7 +291,7 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
|
|||||||
tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
|
tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
|
||||||
|
|
||||||
item_menu->Append(TreeTest_Dump, "&Dump item children");
|
item_menu->Append(TreeTest_Dump, "&Dump item children");
|
||||||
item_menu->Append(TreeTest_Rename, "&Rename item...");
|
item_menu->Append(TreeTest_Rename, "&Rename item...\tF2");
|
||||||
|
|
||||||
item_menu->AppendSeparator();
|
item_menu->AppendSeparator();
|
||||||
item_menu->Append(TreeTest_SetBold, "Make item &bold");
|
item_menu->Append(TreeTest_SetBold, "Make item &bold");
|
||||||
|
|||||||
+52
-25
@@ -100,9 +100,10 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void OnChar( wxKeyEvent &event );
|
void OnChar( wxKeyEvent &event );
|
||||||
void OnKeyUp( wxKeyEvent &event );
|
|
||||||
void OnKillFocus( wxFocusEvent &event );
|
void OnKillFocus( wxFocusEvent &event );
|
||||||
|
|
||||||
|
void IncreaseSizeForText( const wxString& text );
|
||||||
|
|
||||||
bool AcceptChanges();
|
bool AcceptChanges();
|
||||||
void Finish( bool setfocus );
|
void Finish( bool setfocus );
|
||||||
|
|
||||||
@@ -417,7 +418,6 @@ void wxTreeRenameTimer::Notify()
|
|||||||
|
|
||||||
wxBEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
|
wxBEGIN_EVENT_TABLE(wxTreeTextCtrl,wxTextCtrl)
|
||||||
EVT_CHAR (wxTreeTextCtrl::OnChar)
|
EVT_CHAR (wxTreeTextCtrl::OnChar)
|
||||||
EVT_KEY_UP (wxTreeTextCtrl::OnKeyUp)
|
|
||||||
EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
|
EVT_KILL_FOCUS (wxTreeTextCtrl::OnKillFocus)
|
||||||
wxEND_EVENT_TABLE()
|
wxEND_EVENT_TABLE()
|
||||||
|
|
||||||
@@ -428,6 +428,11 @@ wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
|
|||||||
m_owner = owner;
|
m_owner = owner;
|
||||||
m_aboutToFinish = false;
|
m_aboutToFinish = false;
|
||||||
|
|
||||||
|
// Create the text hidden to show it with the correct size -- which we
|
||||||
|
// can't determine before creating it.
|
||||||
|
Hide();
|
||||||
|
Create(m_owner, wxID_ANY, m_startValue);
|
||||||
|
|
||||||
wxRect rect;
|
wxRect rect;
|
||||||
m_owner->GetBoundingRect(m_itemEdited, rect, true);
|
m_owner->GetBoundingRect(m_itemEdited, rect, true);
|
||||||
|
|
||||||
@@ -438,15 +443,27 @@ wxTreeTextCtrl::wxTreeTextCtrl(wxGenericTreeCtrl *owner,
|
|||||||
rect.x -= 5;
|
rect.x -= 5;
|
||||||
#endif // platforms
|
#endif // platforms
|
||||||
|
|
||||||
(void)Create(m_owner, wxID_ANY, m_startValue,
|
const wxSize textSize = rect.GetSize();
|
||||||
rect.GetPosition(), rect.GetSize());
|
wxSize fullSize = GetSizeFromTextSize(textSize);
|
||||||
|
if ( fullSize.y > textSize.y )
|
||||||
|
{
|
||||||
|
// It's ok to extend the rect to the right horizontally, which happens
|
||||||
|
// when we just change its size without changing its position below,
|
||||||
|
// but when extending it vertically, we need to keep it centered.
|
||||||
|
rect.y -= (fullSize.y - textSize.y + 1) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also check that the control fits into the parent window.
|
||||||
|
const int totalWidth = m_owner->GetClientSize().x;
|
||||||
|
if ( rect.x + fullSize.x > totalWidth )
|
||||||
|
{
|
||||||
|
fullSize.x = totalWidth - rect.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
rect.SetSize(fullSize);
|
||||||
|
|
||||||
int w;
|
|
||||||
GetTextExtent(m_startValue, &w, nullptr);
|
|
||||||
const wxSize size(GetSizeFromTextSize(w));
|
|
||||||
rect.y += (rect.height - size.y) / 2;
|
|
||||||
rect.SetSize(size);
|
|
||||||
SetSize(rect);
|
SetSize(rect);
|
||||||
|
Show();
|
||||||
|
|
||||||
SelectAll();
|
SelectAll();
|
||||||
}
|
}
|
||||||
@@ -536,28 +553,38 @@ void wxTreeTextCtrl::OnChar( wxKeyEvent &event )
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
if ( !m_aboutToFinish )
|
||||||
|
{
|
||||||
|
wxChar ch = event.GetUnicodeKey();
|
||||||
|
if ( ch != WXK_NONE )
|
||||||
|
{
|
||||||
|
wxString value = GetValue();
|
||||||
|
|
||||||
|
long from, to;
|
||||||
|
GetSelection( &from, &to );
|
||||||
|
if ( from != to )
|
||||||
|
{
|
||||||
|
value.Remove( from, to - from );
|
||||||
|
}
|
||||||
|
|
||||||
|
IncreaseSizeForText( value + ch );
|
||||||
|
}
|
||||||
|
}
|
||||||
event.Skip();
|
event.Skip();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxTreeTextCtrl::OnKeyUp( wxKeyEvent &event )
|
void wxTreeTextCtrl::IncreaseSizeForText( const wxString& text )
|
||||||
{
|
{
|
||||||
if ( !m_aboutToFinish )
|
// auto-grow the textctrl:
|
||||||
{
|
wxSize parentSize = m_owner->GetClientSize();
|
||||||
// auto-grow the textctrl:
|
wxPoint myPos = GetPosition();
|
||||||
wxSize parentSize = m_owner->GetSize();
|
wxSize mySize = GetSize();
|
||||||
wxPoint myPos = GetPosition();
|
int sx = GetSizeFromText(text).x;
|
||||||
wxSize mySize = GetSize();
|
if (myPos.x + sx > parentSize.x)
|
||||||
int sx, sy;
|
sx = parentSize.x - myPos.x;
|
||||||
GetTextExtent(GetValue() + wxT("M"), &sx, &sy);
|
if (sx > mySize.x)
|
||||||
if (myPos.x + sx > parentSize.x)
|
|
||||||
sx = parentSize.x - myPos.x;
|
|
||||||
if (mySize.x > sx)
|
|
||||||
sx = mySize.x;
|
|
||||||
SetSize(sx, wxDefaultCoord);
|
SetSize(sx, wxDefaultCoord);
|
||||||
}
|
|
||||||
|
|
||||||
event.Skip();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
|
void wxTreeTextCtrl::OnKillFocus( wxFocusEvent &event )
|
||||||
|
|||||||
@@ -373,6 +373,13 @@ wxSize wxControl::GTKGetEntryMargins(GtkEntry* entry) const
|
|||||||
GtkStyleContext* sc = gtk_widget_get_style_context(GTK_WIDGET(entry));
|
GtkStyleContext* sc = gtk_widget_get_style_context(GTK_WIDGET(entry));
|
||||||
gtk_style_context_get_padding(sc, gtk_style_context_get_state(sc), &border);
|
gtk_style_context_get_padding(sc, gtk_style_context_get_state(sc), &border);
|
||||||
#else
|
#else
|
||||||
|
if (gtk_entry_get_has_frame(entry))
|
||||||
|
{
|
||||||
|
GtkStyle* style = GTK_WIDGET(entry)->style;
|
||||||
|
size.x += 2 * style->xthickness;
|
||||||
|
size.y += 2 * style->ythickness;
|
||||||
|
}
|
||||||
|
|
||||||
// Equivalent to the GTK2 private function _gtk_entry_effective_inner_border()
|
// Equivalent to the GTK2 private function _gtk_entry_effective_inner_border()
|
||||||
|
|
||||||
GtkBorder border = { 2, 2, 2, 2 };
|
GtkBorder border = { 2, 2, 2, 2 };
|
||||||
|
|||||||
+14
-11
@@ -2133,14 +2133,13 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
|
|||||||
{
|
{
|
||||||
wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
|
wxASSERT_MSG( m_widget, wxS("GetSizeFromTextSize called before creation") );
|
||||||
|
|
||||||
wxSize tsize(xlen, 0);
|
|
||||||
int cHeight = GetCharHeight();
|
int cHeight = GetCharHeight();
|
||||||
|
wxSize tsize(xlen, cHeight);
|
||||||
|
|
||||||
if ( IsSingleLine() )
|
if ( IsSingleLine() )
|
||||||
{
|
{
|
||||||
if ( HasFlag(wxBORDER_NONE) )
|
if ( HasFlag(wxBORDER_NONE) )
|
||||||
{
|
{
|
||||||
tsize.y = cHeight;
|
|
||||||
#ifdef __WXGTK3__
|
#ifdef __WXGTK3__
|
||||||
tsize.IncBy(9, 0);
|
tsize.IncBy(9, 0);
|
||||||
#else
|
#else
|
||||||
@@ -2151,10 +2150,16 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
|
|||||||
{
|
{
|
||||||
// default height
|
// default height
|
||||||
tsize.y = GTKGetPreferredSize(m_widget).y;
|
tsize.y = GTKGetPreferredSize(m_widget).y;
|
||||||
// Add the margins we have previously set, but only the horizontal border
|
#ifdef __WXGTK3__
|
||||||
// as vertical one has been taken account at GTKGetPreferredSize().
|
// Add the margins we have previously set.
|
||||||
// Also get other GTK+ margins.
|
tsize.IncBy( GTKGetEntryMargins(GetEntry()) );
|
||||||
tsize.IncBy( GTKGetEntryMargins(GetEntry()).x, 0);
|
#else
|
||||||
|
// For GTK 2 these margins are too big, so hard code something more
|
||||||
|
// reasonable, this is not great but should be fine considering
|
||||||
|
// that it's very unlikely that GTK 2 is going to evolve, making
|
||||||
|
// this inappropriate.
|
||||||
|
tsize.IncBy(20, 0);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2166,7 +2171,6 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
|
|||||||
tsize.IncBy(GTKGetPreferredSize(GTK_WIDGET(m_scrollBar[1])).x + 3, 0);
|
tsize.IncBy(GTKGetPreferredSize(GTK_WIDGET(m_scrollBar[1])).x + 3, 0);
|
||||||
|
|
||||||
// height
|
// height
|
||||||
tsize.y = cHeight;
|
|
||||||
if ( ylen <= 0 )
|
if ( ylen <= 0 )
|
||||||
{
|
{
|
||||||
tsize.y = 1 + cHeight * wxMax(wxMin(GetNumberOfLines(), 10), 2);
|
tsize.y = 1 + cHeight * wxMax(wxMin(GetNumberOfLines(), 10), 2);
|
||||||
@@ -2182,10 +2186,9 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perhaps the user wants something different from CharHeight, or ylen
|
// We should always use at least the specified height if it's valid.
|
||||||
// is used as the height of a multiline text.
|
if ( ylen > tsize.y )
|
||||||
if ( ylen > 0 )
|
tsize.y = ylen;
|
||||||
tsize.IncBy(0, ylen - cHeight);
|
|
||||||
|
|
||||||
return tsize;
|
return tsize;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2555,10 +2555,9 @@ wxSize wxTextCtrl::DoGetSizeFromTextSize(int xlen, int ylen) const
|
|||||||
hText += EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) - cy;
|
hText += EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy) - cy;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perhaps the user wants something different from CharHeight, or ylen
|
// We should always use at least the specified height if it's valid.
|
||||||
// is used as the height of a multiline text.
|
if ( ylen > hText )
|
||||||
if ( ylen > 0 )
|
hText = ylen;
|
||||||
hText += ylen - GetCharHeight();
|
|
||||||
|
|
||||||
return wxSize(wText, hText);
|
return wxSize(wText, hText);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user