]> git.saurik.com Git - wxWidgets.git/commitdiff
No changes, just refactor wxTextCtrl::SetStyle() in wxMSW.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 18 Sep 2010 16:26:30 +0000 (16:26 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 18 Sep 2010 16:26:30 +0000 (16:26 +0000)
Split this overly long function into MSWSetCharFormat() and MSWSetParaFormat().

No real changes otherwise except for the use of PARAFORMAT instead of
PARAFORMAT2 if wxUSE_RICHEDIT2 is not set as it was certainly intended (but
the fact that nobody complained about this problem means that nobody must be
compiling without wxUSE_RICHEDIT2 by now so arguably we should just remove it
completely).

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65564 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/msw/textctrl.h
src/msw/textctrl.cpp

index af6ce10eaf6ebfe9e6e3be78075bb6828f8df940..888ea4c76b8e0e461f334ba1060100f918857e62 100644 (file)
@@ -236,6 +236,19 @@ protected:
     virtual wxSize DoGetBestSize() const;
 
 #if wxUSE_RICHEDIT
+    // Apply the character-related parts of wxTextAttr to the given selection
+    // or the entire control if start == end == -1.
+    //
+    // This function is private and should only be called for rich edit
+    // controls and with (from, to) already in correct order, i.e. from <= to.
+    bool MSWSetCharFormat(const wxTextAttr& attr, long from = -1, long to = -1);
+
+    // Same as above for paragraph-related parts of wxTextAttr. Note that this
+    // can only be applied to the selection as RichEdit doesn't support setting
+    // the paragraph styles globally.
+    bool MSWSetParaFormat(const wxTextAttr& attr, long from, long to);
+
+
     // we're using RICHEDIT (and not simple EDIT) control if this field is not
     // 0, it also gives the version of the RICHEDIT control being used
     // (although not directly: 1 is for 1.0, 2 is for either 2.0 or 3.0 as we
index c941de8b57303ef7be3a09b891abfb1992725c91..b4bdd92822b39d5de5227755a5527589913fce03 100644 (file)
@@ -2384,46 +2384,8 @@ bool wxTextCtrl::SetFont(const wxFont& font)
 // styling support for rich edit controls
 // ----------------------------------------------------------------------------
 
-bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
+bool wxTextCtrl::MSWSetCharFormat(const wxTextAttr& style, long start, long end)
 {
-    if ( !IsRich() )
-    {
-        // can't do it with normal text control
-        return false;
-    }
-
-    // the richedit 1.0 doesn't handle setting background colour, so don't
-    // even try to do anything if it's the only thing we want to change
-    if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() &&
-        !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() &&
-        !style.HasTabs() )
-    {
-        // nothing to do: return true if there was really nothing to do and
-        // false if we failed to set bg colour
-        return !style.HasBackgroundColour();
-    }
-
-    // order the range if needed
-    if ( start > end )
-    {
-        long tmp = start;
-        start = end;
-        end = tmp;
-    }
-
-    // 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 = start != startOld || end != endOld;
-
-    if ( changeSel )
-    {
-        DoSetSelection(start, end, SetSel_NoScroll);
-    }
-
     // initialize CHARFORMAT struct
 #if wxUSE_RICHEDIT2
     CHARFORMAT2 cf;
@@ -2505,17 +2467,29 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
     }
 #endif // wxUSE_RICHEDIT2
 
-    // do format the selection
-    bool ok = ::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
-                            SCF_SELECTION, (LPARAM)&cf) != 0;
-    if ( !ok )
+    // Apply the style to the selection.
+    DoSetSelection(start, end, SetSel_NoScroll);
+
+    if ( !::SendMessage(GetHwnd(), EM_SETCHARFORMAT,
+                        SCF_SELECTION, (LPARAM)&cf) )
     {
-        wxLogDebug(wxT("SendMessage(EM_SETCHARFORMAT, SCF_SELECTION) failed"));
+        wxLogLastError(wxT("SendMessage(EM_SETCHARFORMAT)"));
+        return false;
     }
 
-    // now do the paragraph formatting
+    return true;
+}
+
+bool wxTextCtrl::MSWSetParaFormat(const wxTextAttr& style, long start, long end)
+{
+#if wxUSE_RICHEDIT2
     PARAFORMAT2 pf;
+#else
+    PARAFORMAT pf;
+#endif
+
     wxZeroMemory(pf);
+
     // we can't use PARAFORMAT2 with RichEdit 1.0, so pretend it is a simple
     // PARAFORMAT in that case
 #if wxUSE_RICHEDIT2
@@ -2590,16 +2564,54 @@ bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
 
     if ( pf.dwMask )
     {
-        // do format the selection
-        bool ok = ::SendMessage(GetHwnd(), EM_SETPARAFORMAT,
-                                0, (LPARAM) &pf) != 0;
-        if ( !ok )
+        // Do format the selection.
+        DoSetSelection(start, end, SetSel_NoScroll);
+
+        if ( !::SendMessage(GetHwnd(), EM_SETPARAFORMAT, 0, (LPARAM) &pf) )
         {
-            wxLogDebug(wxT("SendMessage(EM_SETPARAFORMAT, 0) failed"));
+            wxLogLastError(wxT("SendMessage(EM_SETPARAFORMAT)"));
+
+            return false;
         }
     }
 
-    if ( changeSel )
+    return true;
+}
+
+bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
+{
+    if ( !IsRich() )
+    {
+        // can't do it with normal text control
+        return false;
+    }
+
+    // the richedit 1.0 doesn't handle setting background colour, so don't
+    // even try to do anything if it's the only thing we want to change
+    if ( m_verRichEdit == 1 && !style.HasFont() && !style.HasTextColour() &&
+        !style.HasLeftIndent() && !style.HasRightIndent() && !style.HasAlignment() &&
+        !style.HasTabs() )
+    {
+        // nothing to do: return true if there was really nothing to do and
+        // false if we failed to set bg colour
+        return !style.HasBackgroundColour();
+    }
+
+    // order the range if needed
+    if ( start > end )
+        wxSwap(start, end);
+
+    // we can only change the format of the selection, so select the range we
+    // want and restore the old selection later, after MSWSetXXXFormat()
+    // functions (possibly) change it.
+    long startOld, endOld;
+    GetSelection(&startOld, &endOld);
+
+    bool ok = MSWSetCharFormat(style, start, end);
+    if ( !MSWSetParaFormat(style, start, end) )
+        ok = false;
+
+    if ( start != startOld || end != endOld )
     {
         // restore the original selection
         DoSetSelection(startOld, endOld, SetSel_NoScroll);