X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3ec4107dfc11fe843b787ecf602e1f526b19d6b9..4b289591ff4fb45c2e7ec0b3d513512fd35bcb74:/src/msw/textctrl.cpp diff --git a/src/msw/textctrl.cpp b/src/msw/textctrl.cpp index bb243fb4da..f70aa21b7c 100644 --- a/src/msw/textctrl.cpp +++ b/src/msw/textctrl.cpp @@ -13,7 +13,7 @@ // declarations // ============================================================================ -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "textctrl.h" #endif @@ -52,10 +52,14 @@ #include #include "wx/msw/private.h" +#include "wx/msw/winundef.h" #include #include + +#ifndef __WXWINCE__ #include +#endif #if wxUSE_RICHEDIT @@ -112,7 +116,75 @@ IMPLEMENT_DYNAMIC_CLASS(wxRichEditModule, wxModule) // event tables and other macros // ---------------------------------------------------------------------------- +#if wxUSE_EXTENDED_RTTI +WX_DEFINE_FLAGS( wxTextCtrlStyle ) + +wxBEGIN_FLAGS( wxTextCtrlStyle ) + // new style border flags, we put them first to + // use them for streaming out + wxFLAGS_MEMBER(wxBORDER_SIMPLE) + wxFLAGS_MEMBER(wxBORDER_SUNKEN) + wxFLAGS_MEMBER(wxBORDER_DOUBLE) + wxFLAGS_MEMBER(wxBORDER_RAISED) + wxFLAGS_MEMBER(wxBORDER_STATIC) + wxFLAGS_MEMBER(wxBORDER_NONE) + + // old style border flags + wxFLAGS_MEMBER(wxSIMPLE_BORDER) + wxFLAGS_MEMBER(wxSUNKEN_BORDER) + wxFLAGS_MEMBER(wxDOUBLE_BORDER) + wxFLAGS_MEMBER(wxRAISED_BORDER) + wxFLAGS_MEMBER(wxSTATIC_BORDER) + wxFLAGS_MEMBER(wxNO_BORDER) + + // standard window styles + wxFLAGS_MEMBER(wxTAB_TRAVERSAL) + wxFLAGS_MEMBER(wxCLIP_CHILDREN) + wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) + wxFLAGS_MEMBER(wxWANTS_CHARS) + wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE) + wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) + wxFLAGS_MEMBER(wxVSCROLL) + wxFLAGS_MEMBER(wxHSCROLL) + + wxFLAGS_MEMBER(wxTE_PROCESS_ENTER) + wxFLAGS_MEMBER(wxTE_PROCESS_TAB) + wxFLAGS_MEMBER(wxTE_MULTILINE) + wxFLAGS_MEMBER(wxTE_PASSWORD) + wxFLAGS_MEMBER(wxTE_READONLY) + wxFLAGS_MEMBER(wxHSCROLL) + wxFLAGS_MEMBER(wxTE_RICH) + wxFLAGS_MEMBER(wxTE_RICH2) + wxFLAGS_MEMBER(wxTE_AUTO_URL) + wxFLAGS_MEMBER(wxTE_NOHIDESEL) + wxFLAGS_MEMBER(wxTE_LEFT) + wxFLAGS_MEMBER(wxTE_CENTRE) + wxFLAGS_MEMBER(wxTE_RIGHT) + wxFLAGS_MEMBER(wxTE_DONTWRAP) + wxFLAGS_MEMBER(wxTE_LINEWRAP) + wxFLAGS_MEMBER(wxTE_WORDWRAP) + +wxEND_FLAGS( wxTextCtrlStyle ) + +IMPLEMENT_DYNAMIC_CLASS_XTI(wxTextCtrl, wxControl,"wx/textctrl.h") + +wxBEGIN_PROPERTIES_TABLE(wxTextCtrl) + wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent ) + wxEVENT_PROPERTY( TextEnter , wxEVT_COMMAND_TEXT_ENTER , wxCommandEvent ) + + wxPROPERTY( Font , wxFont , SetFont , GetFont ,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") ) + wxPROPERTY( Value , wxString , SetValue, GetValue, wxString() , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) + wxPROPERTY_FLAGS( WindowStyle , wxTextCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style +wxEND_PROPERTIES_TABLE() + +wxBEGIN_HANDLERS_TABLE(wxTextCtrl) +wxEND_HANDLERS_TABLE() + +wxCONSTRUCTOR_6( wxTextCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size , long , WindowStyle) +#else IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) +#endif + BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) EVT_CHAR(wxTextCtrl::OnChar) @@ -140,6 +212,8 @@ BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) #ifdef __WIN16__ EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground) #endif + + EVT_SET_FOCUS(wxTextCtrl::OnSetFocus) END_EVENT_TABLE() // ============================================================================ @@ -158,6 +232,7 @@ void wxTextCtrl::Init() m_privateContextMenu = NULL; m_suppressNextUpdate = FALSE; + m_isNativeCaretShown = true; } wxTextCtrl::~wxTextCtrl() @@ -355,12 +430,6 @@ void wxTextCtrl::AdoptAttributesFromHWND() WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const { - // default border for the text controls is the sunken one - if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) - { - style |= wxBORDER_SUNKEN; - } - long msStyle = wxControl::MSWGetStyle(style, exstyle); // styles which we alaways add by default @@ -395,11 +464,12 @@ WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const msStyle |= ES_AUTOHSCROLL; } - // styles which we add depending on the specified wxWindows styles - if ( style & wxHSCROLL ) + // note that wxTE_DONTWRAP is the same as wxHSCROLL so if we have a horz + // scrollbar, there is no wrapping -- which makes sense + if ( style & wxTE_DONTWRAP ) { // automatically scroll the control horizontally as necessary - msStyle |= WS_HSCROLL;// | ES_AUTOHSCROLL; + msStyle |= WS_HSCROLL; } if ( style & wxTE_READONLY ) @@ -417,16 +487,13 @@ WXDWORD wxTextCtrl::MSWGetStyle(long style, WXDWORD *exstyle) const else if ( style & wxTE_RIGHT ) msStyle |= ES_RIGHT; else - msStyle |= ES_LEFT; // ES_LEFT if 0 as well but for consistency... + msStyle |= ES_LEFT; // ES_LEFT is 0 as well but for consistency... return msStyle; } void wxTextCtrl::SetWindowStyleFlag(long style) { - if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) - style |= wxBORDER_SUNKEN; - #if wxUSE_RICHEDIT // we have to deal with some styles separately because they can't be // changed by simply calling SetWindowLong(GWL_STYLE) but can be changed @@ -470,30 +537,32 @@ wxString wxTextCtrl::GetRange(long from, long to) const int len = GetWindowTextLength(GetHwnd()); if ( len > from ) { - // alloc one extra WORD as needed by the control - wxChar *p = str.GetWriteBuf(++len); + { + // alloc one extra WORD as needed by the control + wxStringBuffer tmp(str, ++len); + wxChar *p = tmp; - TEXTRANGE textRange; - textRange.chrg.cpMin = from; - textRange.chrg.cpMax = to == -1 ? len : to; - textRange.lpstrText = p; + TEXTRANGE textRange; + textRange.chrg.cpMin = from; + textRange.chrg.cpMax = to == -1 ? len : to; + textRange.lpstrText = p; - (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, 0, (LPARAM)&textRange); + (void)SendMessage(GetHwnd(), EM_GETTEXTRANGE, + 0, (LPARAM)&textRange); - if ( m_verRichEdit > 1 ) - { - // RichEdit 2.0 uses just CR ('\r') for the newlines which is - // neither Unix nor Windows style - convert it to something - // reasonable - for ( ; *p; p++ ) + if ( m_verRichEdit > 1 ) { - if ( *p == _T('\r') ) - *p = _T('\n'); + // RichEdit 2.0 uses just CR ('\r') for the + // newlines which is neither Unix nor Windows + // style - convert it to something reasonable + for ( ; *p; p++ ) + { + if ( *p == _T('\r') ) + *p = _T('\n'); + } } } - str.UngetWriteBuf(); - if ( m_verRichEdit == 1 ) { // convert to the canonical form - see comment below @@ -533,6 +602,11 @@ void wxTextCtrl::SetValue(const wxString& value) { DoWriteText(value, FALSE /* not selection only */); } + else // same text + { + // still send an event for consistency + SendUpdateEvent(); + } // we should reset the modified flag even if the value didn't really change @@ -565,7 +639,8 @@ DWORD CALLBACK wxRichEditStreamIn(DWORD dwCookie, BYTE *buf, LONG cb, LONG *pcb) return 0; } -extern long wxEncodingToCodepage(wxFontEncoding encoding); // from utils.cpp +// from utils.cpp +extern WXDLLIMPEXP_BASE long wxEncodingToCodepage(wxFontEncoding encoding); #if wxUSE_UNICODE_MSLU bool wxTextCtrl::StreamIn(const wxString& value, @@ -757,7 +832,7 @@ void wxTextCtrl::AppendText(const wxString& text) void wxTextCtrl::Clear() { - ::SetWindowText(GetHwnd(), wxT("")); + ::SetWindowText(GetHwnd(), wxEmptyString); #if wxUSE_RICHEDIT if ( !IsRich() ) @@ -1072,7 +1147,7 @@ void wxTextCtrl::Replace(long from, long to, const wxString& value) void wxTextCtrl::Remove(long from, long to) { - Replace(from, to, _T("")); + Replace(from, to, wxEmptyString); } bool wxTextCtrl::LoadFile(const wxString& file) @@ -1197,13 +1272,36 @@ wxString wxTextCtrl::GetLineText(long lineNo) const len += sizeof(WORD); wxString str; - wxChar *buf = str.GetWriteBuf(len); + { + wxStringBufferLength tmp(str, len); + wxChar *buf = tmp; - *(WORD *)buf = (WORD)len; - len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); - buf[len] = 0; + *(WORD *)buf = (WORD)len; + len = (size_t)::SendMessage(GetHwnd(), EM_GETLINE, lineNo, (LPARAM)buf); - str.UngetWriteBuf(len); +#if wxUSE_RICHEDIT + if ( IsRich() ) + { + // remove the '\r' returned by the rich edit control, the user code + // should never see it + if ( buf[len - 2] == _T('\r') && buf[len - 1] == _T('\n') ) + { + buf[len - 2] = _T('\n'); + len--; + } + } +#endif // wxUSE_RICHEDIT + + // remove the '\n' at the end, if any (this is how this function is + // supposed to work according to the docs) + if ( buf[len - 1] == _T('\n') ) + { + len--; + } + + buf[len] = 0; + tmp.SetLength(len); + } return str; } @@ -1244,6 +1342,27 @@ bool wxTextCtrl::CanRedo() const return ::SendMessage(GetHwnd(), EM_CANUNDO, 0, 0) != 0; } +// ---------------------------------------------------------------------------- +// caret handling (Windows only) +// ---------------------------------------------------------------------------- + +bool wxTextCtrl::ShowNativeCaret(bool show) +{ + if ( show != m_isNativeCaretShown ) + { + if ( !(show ? ::ShowCaret(GetHwnd()) : ::HideCaret(GetHwnd())) ) + { + // not an error, may simply indicate that it's not shown/hidden + // yet (i.e. it had been hidden/showh 2 times before) + return false; + } + + m_isNativeCaretShown = show; + } + + return true; +} + // ---------------------------------------------------------------------------- // implemenation details // ---------------------------------------------------------------------------- @@ -1491,11 +1610,6 @@ WXHBRUSH wxTextCtrl::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSE #endif // wxUSE_CTL3D HDC hdc = (HDC)pDC; - if (GetParent()->GetTransparentBackground()) - SetBkMode(hdc, TRANSPARENT); - else - SetBkMode(hdc, OPAQUE); - wxColour colBack = GetBackgroundColour(); if (!IsEnabled() && (GetWindowStyle() & wxTE_MULTILINE) == 0) @@ -1644,7 +1758,7 @@ void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) Redo(); } -void wxTextCtrl::OnDelete(wxCommandEvent& event) +void wxTextCtrl::OnDelete(wxCommandEvent& WXUNUSED(event)) { long from, to; GetSelection(& from, & to); @@ -1652,7 +1766,7 @@ void wxTextCtrl::OnDelete(wxCommandEvent& event) Remove(from, to); } -void wxTextCtrl::OnSelectAll(wxCommandEvent& event) +void wxTextCtrl::OnSelectAll(wxCommandEvent& WXUNUSED(event)) { SetSelection(-1, -1); } @@ -1720,6 +1834,15 @@ void wxTextCtrl::OnRightClick(wxMouseEvent& event) event.Skip(); } +void wxTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) +{ + // be sure the caret remains invisible if the user had hidden it + if ( !m_isNativeCaretShown ) + { + ::HideCaret(GetHwnd()); + } +} + // the rest of the file only deals with the rich edit controls #if wxUSE_RICHEDIT @@ -1815,7 +1938,7 @@ bool wxTextCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) } return TRUE; } - + // not processed, leave it to the base class return wxTextCtrlBase::MSWOnNotify(idCtrl, lParam, result); } @@ -2093,10 +2216,135 @@ bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style) return TRUE; } -bool wxTextCtrl::GetStyle(long WXUNUSED(position), wxTextAttr& WXUNUSED(style)) +bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) { - // TODO - return FALSE; + if ( !IsRich() ) + { + // can't do it with normal text control + return FALSE; + } + + // initialize CHARFORMAT struct +#if wxUSE_RICHEDIT2 + CHARFORMAT2 cf; +#else + CHARFORMAT cf; +#endif + + wxZeroMemory(cf); + + // we can't use CHARFORMAT2 with RichEdit 1.0, so pretend it is a simple + // CHARFORMAT in that case +#if wxUSE_RICHEDIT2 + if ( m_verRichEdit == 1 ) + { + // this is the only thing the control is going to grok + cf.cbSize = sizeof(CHARFORMAT); + } + else +#endif + { + // CHARFORMAT or CHARFORMAT2 + cf.cbSize = sizeof(cf); + } + // we can only change the format of the selection, so select the range we + // want and restore the old selection later + long startOld, endOld; + GetSelection(&startOld, &endOld); + + // but do we really have to change the selection? + bool changeSel = position != startOld || position != endOld; + + if ( changeSel ) + { + DoSetSelection(position, position, FALSE /* don't scroll caret into view */); + } + + // get the selection formatting + (void) ::SendMessage(GetHwnd(), EM_GETCHARFORMAT, + SCF_SELECTION, (LPARAM)&cf) ; + + LOGFONT lf; + lf.lfHeight = cf.yHeight; + lf.lfWidth = 0; + lf.lfCharSet = ANSI_CHARSET; // FIXME: how to get correct charset? + lf.lfClipPrecision = 0; + lf.lfEscapement = 0; + wxStrcpy(lf.lfFaceName, cf.szFaceName); + if (cf.dwEffects & CFE_ITALIC) + lf.lfItalic = TRUE; + lf.lfOrientation = 0; + lf.lfPitchAndFamily = cf.bPitchAndFamily; + lf.lfQuality = 0; + if (cf.dwEffects & CFE_STRIKEOUT) + lf.lfStrikeOut = TRUE; + if (cf.dwEffects & CFE_UNDERLINE) + lf.lfUnderline = TRUE; + if (cf.dwEffects & CFE_BOLD) + lf.lfWeight = FW_BOLD; + + wxFont font = wxCreateFontFromLogFont(& lf); + if (font.Ok()) + { + style.SetFont(font); + } + style.SetTextColour(wxColour(cf.crTextColor)); + +#if wxUSE_RICHEDIT2 + if ( m_verRichEdit != 1 ) + { + // cf.dwMask |= CFM_BACKCOLOR; + style.SetBackgroundColour(wxColour(cf.crBackColor)); + } +#endif // wxUSE_RICHEDIT2 + + // now get the paragraph formatting + PARAFORMAT2 pf; + wxZeroMemory(pf); + // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple + // PARAFORMAT in that case +#if wxUSE_RICHEDIT2 + if ( m_verRichEdit == 1 ) + { + // this is the only thing the control is going to grok + pf.cbSize = sizeof(PARAFORMAT); + } + else +#endif + { + // PARAFORMAT or PARAFORMAT2 + pf.cbSize = sizeof(pf); + } + + // do format the selection + (void) ::SendMessage(GetHwnd(), EM_GETPARAFORMAT, 0, (LPARAM) &pf) ; + + style.SetLeftIndent( (int) ((double) pf.dxStartIndent * twips2mm * 10.0) ); + style.SetRightIndent( (int) ((double) pf.dxRightIndent * twips2mm * 10.0) ); + + if (pf.wAlignment == PFA_CENTER) + style.SetAlignment(wxTEXT_ALIGNMENT_CENTRE); + else if (pf.wAlignment == PFA_RIGHT) + style.SetAlignment(wxTEXT_ALIGNMENT_RIGHT); + else if (pf.wAlignment == PFA_JUSTIFY) + style.SetAlignment(wxTEXT_ALIGNMENT_JUSTIFIED); + else + style.SetAlignment(wxTEXT_ALIGNMENT_LEFT); + + wxArrayInt tabStops; + size_t i; + for (i = 0; i < (size_t) pf.cTabCount; i++) + { + tabStops[i] = (int) ((double) (pf.rgxTabs[i] & 0xFFFF) * twips2mm * 10.0) ; + } + + if ( changeSel ) + { + // restore the original selection + DoSetSelection(startOld, endOld, FALSE); + } + + return TRUE; } #endif @@ -2118,6 +2366,7 @@ void wxRichEditModule::OnExit() if ( ms_hRichEdit[i] ) { ::FreeLibrary(ms_hRichEdit[i]); + ms_hRichEdit[i] = NULL; } } }