Exemplo n.º 1
0
void NumValidatorBase::OnPaste(wxClipboardTextEvent& event)
{
   event.Skip(false);

   wxTextEntry * const control = GetTextEntry();
   if ( !control )
   {
      return;
   }

   wxClipboardLocker cb;
//    if (!wxClipboard::Get()->IsSupported(wxDataFormat(wxDF_TEXT)))
   if (!wxClipboard::Get()->IsSupported(wxDF_TEXT))
   {
      return;
   }

   wxTextDataObject data;
   if (!wxClipboard::Get()->GetData( data ))
   {
      return;
   }

   wxString toPaste = data.GetText();
   wxString val;
   int pos;
   GetCurrentValueAndInsertionPoint(val, pos);

   for (size_t i = 0, cnt = toPaste.Length(); i < cnt; i++)
   {
      const wxChar ch = toPaste[i];

      // Check if this character is allowed in the current state.
      if ( IsCharOk(val, pos, ch) )
      {
         val = GetValueAfterInsertingChar(val, pos++, ch);
      }
      else if ( !wxValidator::IsSilent() )
      {
               wxBell();
      }
   }

   // When we change the control value below, its "modified" status is reset
   // so we need to explicitly keep it marked as modified if it was so in the
   // first place.
   //
   // Notice that only wxTextCtrl (and not wxTextEntry) has
   // IsModified()/MarkDirty() methods hence the need for dynamic cast.
   wxTextCtrl * const text = wxDynamicCast(m_validatorWindow, wxTextCtrl);
   const bool wasModified = text ? text->IsModified() : false;

   // Use SetValue because effect still needs EVT_TEXT (bug 1357)
   control->SetValue(NormalizeString(val));

   if ( wasModified )
   {
      text->MarkDirty();
   }
}
Exemplo n.º 2
0
bool
wxIntegerValidatorBase::IsCharOk(const wxString& val, int pos, wxChar ch) const
{
    // We may accept minus sign if we can represent negative numbers at all.
    if ( ch == '-' )
    {
        // Notice that entering '-' can make our value invalid, for example if
        // we're limited to -5..15 range and the current value is 12, then the
        // new value would be (invalid) -12. We consider it better to let the
        // user do this because perhaps he is going to press Delete key next to
        // make it -2 and forcing him to delete 1 first would be unnatural.
        //
        // TODO: It would be nice to indicate that the current control contents
        //       is invalid (if it's indeed going to be the case) once
        //       wxValidator supports doing this non-intrusively.
        return m_min < 0 && IsMinusOk(val, pos);
    }

    // We only accept digits here (remember that '-' is taken care of by the
    // base class already).
    if ( ch < '0' || ch > '9' )
        return false;

    // And the value after insertion needs to be in the defined range.
    LongestValueType value;
    if ( !FromString(GetValueAfterInsertingChar(val, pos, ch), &value) )
        return false;

    wxString smin = ToString(m_min);
    wxString smax = ToString(m_max);
    if ( pos < (int) smin.Length() )
       return true;
    if ( pos < (int) smax.Length() - 1 )
       return true;

    return IsInRange(value);
}