Pass CFX_WideString further down widget callers

Avoid a couple of places where we copy the raw string back into a
brand-new widestring.

There are a few places where the difference between a null
ptr and an empty string control the logic, and I left these
as-is.  Other places can just take the string by const ref.

Review-Url: https://codereview.chromium.org/2323493002
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index a8e2663..1f182d4 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -323,7 +323,7 @@
 }
 
 void CPDFSDK_InterForm::ResetFieldAppearance(CPDF_FormField* pFormField,
-                                             const FX_WCHAR* sValue,
+                                             const CFX_WideString* sValue,
                                              FX_BOOL bValueChanged) {
   for (int i = 0, sz = pFormField->CountControls(); i < sz; i++) {
     CPDF_FormControl* pFormCtrl = pFormField->GetControl(i);
@@ -634,7 +634,7 @@
     OnCalculate(pField);
     FX_BOOL bFormatted = FALSE;
     CFX_WideString sValue = OnFormat(pField, bFormatted);
-    ResetFieldAppearance(pField, bFormatted ? sValue.c_str() : nullptr, TRUE);
+    ResetFieldAppearance(pField, bFormatted ? &sValue : nullptr, TRUE);
     UpdateField(pField);
   }
 }
diff --git a/fpdfsdk/cpdfsdk_widget.cpp b/fpdfsdk/cpdfsdk_widget.cpp
index d518a56..a8ebef6 100644
--- a/fpdfsdk/cpdfsdk_widget.cpp
+++ b/fpdfsdk/cpdfsdk_widget.cpp
@@ -739,7 +739,7 @@
     case FIELDTYPE_COMBOBOX: {
       FX_BOOL bFormatted = FALSE;
       CFX_WideString sValue = OnFormat(bFormatted);
-      ResetAppearance(bFormatted ? sValue.c_str() : nullptr, TRUE);
+      ResetAppearance(bFormatted ? &sValue : nullptr, TRUE);
       break;
     }
     default:
@@ -749,7 +749,7 @@
 }
 #endif  // PDF_ENABLE_XFA
 
-void CPDFSDK_Widget::ResetAppearance(const FX_WCHAR* sValue,
+void CPDFSDK_Widget::ResetAppearance(const CFX_WideString* sValue,
                                      FX_BOOL bValueChanged) {
   SetAppModified();
 
@@ -1353,7 +1353,7 @@
     SetAppState("Off");
 }
 
-void CPDFSDK_Widget::ResetAppearance_ComboBox(const FX_WCHAR* sValue) {
+void CPDFSDK_Widget::ResetAppearance_ComboBox(const CFX_WideString* sValue) {
   CPDF_FormControl* pControl = GetFormControl();
   CPDF_FormField* pField = pControl->GetField();
   CFX_ByteTextBuf sBody, sLines;
@@ -1387,10 +1387,9 @@
   pEdit->Initialize();
 
   if (sValue) {
-    pEdit->SetText(sValue);
+    pEdit->SetText(*sValue);
   } else {
     int32_t nCurSel = pField->GetSelectedIndex(0);
-
     if (nCurSel < 0)
       pEdit->SetText(pField->GetValue().c_str());
     else
@@ -1512,7 +1511,7 @@
   WriteAppearance("N", GetRotatedRect(), GetMatrix(), sAP);
 }
 
-void CPDFSDK_Widget::ResetAppearance_TextField(const FX_WCHAR* sValue) {
+void CPDFSDK_Widget::ResetAppearance_TextField(const CFX_WideString* sValue) {
   CPDF_FormControl* pControl = GetFormControl();
   CPDF_FormField* pField = pControl->GetField();
   CFX_ByteTextBuf sBody, sLines;
@@ -1554,7 +1553,7 @@
   CFX_WideString sValueTmp;
   if (!sValue && GetMixXFAWidget()) {
     sValueTmp = GetValue(TRUE);
-    sValue = sValueTmp.c_str();
+    sValue = &sValueTmp;
   }
 #endif  // PDF_ENABLE_XFA
 
@@ -1568,7 +1567,7 @@
       }
     } else {
       if (sValue)
-        nMaxLen = wcslen((const wchar_t*)sValue);
+        nMaxLen = sValue->GetLength();
       pEdit->SetLimitChar(nMaxLen);
     }
   }
@@ -1581,7 +1580,7 @@
   pEdit->Initialize();
 
   if (sValue)
-    pEdit->SetText(sValue);
+    pEdit->SetText(*sValue);
   else
     pEdit->SetText(pField->GetValue().c_str());
 
diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp
index 1a9bdac..b490daa 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_widgethandler.cpp
@@ -241,7 +241,7 @@
     FX_BOOL bFormatted = FALSE;
     CFX_WideString sValue = pWidget->OnFormat(bFormatted);
     if (bFormatted && nFieldType == FIELDTYPE_COMBOBOX)
-      pWidget->ResetAppearance(sValue.c_str(), FALSE);
+      pWidget->ResetAppearance(&sValue, FALSE);
   }
 
 #ifdef PDF_ENABLE_XFA
diff --git a/fpdfsdk/formfiller/cffl_iformfiller.cpp b/fpdfsdk/formfiller/cffl_iformfiller.cpp
index db1f538..24a6122 100644
--- a/fpdfsdk/formfiller/cffl_iformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_iformfiller.cpp
@@ -724,8 +724,7 @@
       return;
 
     if (bFormatted) {
-      pInterForm->ResetFieldAppearance(pWidget->GetFormField(), sValue.c_str(),
-                                       TRUE);
+      pInterForm->ResetFieldAppearance(pWidget->GetFormField(), &sValue, TRUE);
       pInterForm->UpdateField(pWidget->GetFormField());
     }
 
diff --git a/fpdfsdk/fxedit/fxet_edit.cpp b/fpdfsdk/fxedit/fxet_edit.cpp
index 7d207a8..7aa8534 100644
--- a/fpdfsdk/fxedit/fxet_edit.cpp
+++ b/fpdfsdk/fxedit/fxet_edit.cpp
@@ -706,7 +706,7 @@
   if (m_pEdit) {
     m_pEdit->SelectNone();
     m_pEdit->SetCaret(m_wrSel.BeginPos);
-    m_pEdit->InsertText(m_swText.c_str(), DEFAULT_CHARSET, FALSE, TRUE);
+    m_pEdit->InsertText(m_swText, DEFAULT_CHARSET, FALSE, TRUE);
     m_pEdit->SetSel(m_wrSel.BeginPos, m_wrSel.EndPos);
   }
 }
@@ -728,7 +728,7 @@
   if (m_pEdit && IsLast()) {
     m_pEdit->SelectNone();
     m_pEdit->SetCaret(m_wpOld);
-    m_pEdit->InsertText(m_swText.c_str(), m_nCharset, FALSE, TRUE);
+    m_pEdit->InsertText(m_swText, m_nCharset, FALSE, TRUE);
   }
 }
 
@@ -1670,9 +1670,9 @@
   return wrRet;
 }
 
-void CFX_Edit::SetText(const FX_WCHAR* text) {
+void CFX_Edit::SetText(const CFX_WideString& sText) {
   Empty();
-  DoInsertText(CPVT_WordPlace(0, 0, -1), text, DEFAULT_CHARSET);
+  DoInsertText(CPVT_WordPlace(0, 0, -1), sText, DEFAULT_CHARSET);
   Paint();
 }
 
@@ -1696,8 +1696,8 @@
   return Clear(TRUE, TRUE);
 }
 
-FX_BOOL CFX_Edit::InsertText(const FX_WCHAR* text, int32_t charset) {
-  return InsertText(text, charset, TRUE, TRUE);
+FX_BOOL CFX_Edit::InsertText(const CFX_WideString& sText, int32_t charset) {
+  return InsertText(sText, charset, TRUE, TRUE);
 }
 
 FX_FLOAT CFX_Edit::GetFontSize() const {
@@ -2685,7 +2685,7 @@
   return TRUE;
 }
 
-FX_BOOL CFX_Edit::InsertText(const FX_WCHAR* text,
+FX_BOOL CFX_Edit::InsertText(const CFX_WideString& sText,
                              int32_t charset,
                              FX_BOOL bAddUndo,
                              FX_BOOL bPaint) {
@@ -2693,24 +2693,23 @@
     return FALSE;
 
   m_pVT->UpdateWordPlace(m_wpCaret);
-  SetCaret(DoInsertText(m_wpCaret, text, charset));
+  SetCaret(DoInsertText(m_wpCaret, sText, charset));
   m_SelState.Set(m_wpCaret, m_wpCaret);
+  if (m_wpCaret == m_wpOldCaret)
+    return FALSE;
 
-  if (m_wpCaret != m_wpOldCaret) {
-    if (bAddUndo && m_bEnableUndo) {
-      AddEditUndoItem(
-          new CFXEU_InsertText(this, m_wpOldCaret, m_wpCaret, text, charset));
-    }
-
-    if (bPaint)
-      PaintInsertText(m_wpOldCaret, m_wpCaret);
-
-    if (m_bOprNotify && m_pOprNotify)
-      m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
-
-    return TRUE;
+  if (bAddUndo && m_bEnableUndo) {
+    AddEditUndoItem(
+        new CFXEU_InsertText(this, m_wpOldCaret, m_wpCaret, sText, charset));
   }
-  return FALSE;
+
+  if (bPaint)
+    PaintInsertText(m_wpOldCaret, m_wpCaret);
+
+  if (m_bOprNotify && m_pOprNotify)
+    m_pOprNotify->OnInsertText(m_wpCaret, m_wpOldCaret);
+
+  return TRUE;
 }
 
 void CFX_Edit::PaintInsertText(const CPVT_WordPlace& wpOld,
@@ -2883,13 +2882,11 @@
 }
 
 CPVT_WordPlace CFX_Edit::DoInsertText(const CPVT_WordPlace& place,
-                                      const FX_WCHAR* text,
+                                      const CFX_WideString& sText,
                                       int32_t charset) {
   CPVT_WordPlace wp = place;
 
   if (m_pVT->IsValid()) {
-    CFX_WideString sText = text;
-
     for (int32_t i = 0, sz = sText.GetLength(); i < sz; i++) {
       uint16_t word = sText[i];
       switch (word) {
diff --git a/fpdfsdk/fxedit/include/fxet_edit.h b/fpdfsdk/fxedit/include/fxet_edit.h
index 9adf172..e0fca92 100644
--- a/fpdfsdk/fxedit/include/fxet_edit.h
+++ b/fpdfsdk/fxedit/include/fxet_edit.h
@@ -470,13 +470,13 @@
   void OnVK_RIGHT(FX_BOOL bShift, FX_BOOL bCtrl);
   void OnVK_HOME(FX_BOOL bShift, FX_BOOL bCtrl);
   void OnVK_END(FX_BOOL bShift, FX_BOOL bCtrl);
-  void SetText(const FX_WCHAR* text);
+  void SetText(const CFX_WideString& sText);
   FX_BOOL InsertWord(uint16_t word, int32_t charset);
   FX_BOOL InsertReturn();
   FX_BOOL Backspace();
   FX_BOOL Delete();
   FX_BOOL Clear();
-  FX_BOOL InsertText(const FX_WCHAR* text, int32_t charset);
+  FX_BOOL InsertText(const CFX_WideString& sText, int32_t charset);
   FX_BOOL Redo();
   FX_BOOL Undo();
   int32_t WordPlaceToWordIndex(const CPVT_WordPlace& place) const;
@@ -525,7 +525,7 @@
   FX_BOOL Empty();
 
   CPVT_WordPlace DoInsertText(const CPVT_WordPlace& place,
-                              const FX_WCHAR* text,
+                              const CFX_WideString& sText,
                               int32_t charset);
   int32_t GetCharSetFromUnicode(uint16_t word, int32_t nOldCharset);
 
@@ -563,7 +563,7 @@
   FX_BOOL Backspace(FX_BOOL bAddUndo, FX_BOOL bPaint);
   FX_BOOL Delete(FX_BOOL bAddUndo, FX_BOOL bPaint);
   FX_BOOL Clear(FX_BOOL bAddUndo, FX_BOOL bPaint);
-  FX_BOOL InsertText(const FX_WCHAR* text,
+  FX_BOOL InsertText(const CFX_WideString& sText,
                      int32_t charset,
                      FX_BOOL bAddUndo,
                      FX_BOOL bPaint);
diff --git a/fpdfsdk/include/cpdfsdk_interform.h b/fpdfsdk/include/cpdfsdk_interform.h
index d900fe2..b4b5db3 100644
--- a/fpdfsdk/include/cpdfsdk_interform.h
+++ b/fpdfsdk/include/cpdfsdk_interform.h
@@ -70,7 +70,7 @@
   CFX_WideString OnFormat(CPDF_FormField* pFormField, FX_BOOL& bFormatted);
 
   void ResetFieldAppearance(CPDF_FormField* pFormField,
-                            const FX_WCHAR* sValue,
+                            const CFX_WideString* sValue,
                             FX_BOOL bValueChanged);
   void UpdateField(CPDF_FormField* pFormField);
 
diff --git a/fpdfsdk/include/cpdfsdk_widget.h b/fpdfsdk/include/cpdfsdk_widget.h
index bf033cc..09f9d94 100644
--- a/fpdfsdk/include/cpdfsdk_widget.h
+++ b/fpdfsdk/include/cpdfsdk_widget.h
@@ -108,7 +108,7 @@
 #ifdef PDF_ENABLE_XFA
   void ResetAppearance(FX_BOOL bValueChanged);
 #endif  // PDF_ENABLE_XFA
-  void ResetAppearance(const FX_WCHAR* sValue, FX_BOOL bValueChanged);
+  void ResetAppearance(const CFX_WideString* sValue, FX_BOOL bValueChanged);
   void ResetFieldAppearance(FX_BOOL bValueChanged);
   void UpdateField();
   CFX_WideString OnFormat(FX_BOOL& bFormatted);
@@ -144,9 +144,9 @@
   void ResetAppearance_PushButton();
   void ResetAppearance_CheckBox();
   void ResetAppearance_RadioButton();
-  void ResetAppearance_ComboBox(const FX_WCHAR* sValue);
+  void ResetAppearance_ComboBox(const CFX_WideString* sValue);
   void ResetAppearance_ListBox();
-  void ResetAppearance_TextField(const FX_WCHAR* sValue);
+  void ResetAppearance_TextField(const CFX_WideString* sValue);
 
   CFX_FloatRect GetClientRect() const;
   CFX_FloatRect GetRotatedRect() const;
diff --git a/fpdfsdk/javascript/Field.cpp b/fpdfsdk/javascript/Field.cpp
index a788fb2..9ecdec6 100644
--- a/fpdfsdk/javascript/Field.cpp
+++ b/fpdfsdk/javascript/Field.cpp
@@ -275,7 +275,7 @@
             static_cast<CPDFSDK_Widget*>(pAnnot)->OnFormat(bFormatted);
         if (pAnnot) {
           static_cast<CPDFSDK_Widget*>(pAnnot)->ResetAppearance(
-              bFormatted ? sValue.c_str() : nullptr, FALSE);
+              bFormatted ? &sValue : nullptr, FALSE);
         }
       }
     } else {
@@ -319,10 +319,7 @@
           nFieldType == FIELDTYPE_TEXTFIELD) {
         FX_BOOL bFormatted = FALSE;
         CFX_WideString sValue = pWidget->OnFormat(bFormatted);
-        if (bFormatted)
-          pWidget->ResetAppearance(sValue.c_str(), FALSE);
-        else
-          pWidget->ResetAppearance(nullptr, FALSE);
+        pWidget->ResetAppearance(bFormatted ? &sValue : nullptr, FALSE);
       } else {
         pWidget->ResetAppearance(nullptr, FALSE);
       }