Beispiel #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();
   }
}
void NumValidatorBase::OnChar(wxKeyEvent& event)
{
   // By default we just validate this key so don't prevent the normal
   // handling from taking place.
   event.Skip();

   if ( !m_validatorWindow )
      return;

#if wxUSE_UNICODE
   const int ch = event.GetUnicodeKey();
   const int c = event.GetKeyCode();
   if ( c > WXK_START )
   {
      // It's a character without any Unicode equivalent at all, e.g. cursor
      // arrow or function key, we never filter those.
      return;
   }
#else // !wxUSE_UNICODE
   const int ch = event.GetKeyCode();
   const int c = ch;
   if ( ch > WXK_DELETE )
   {
      // Not a character neither.
      return;
   }
#endif // wxUSE_UNICODE/!wxUSE_UNICODE

   // Space is an allowed thousands separator. But we don't allow user to type
   // it. We will add it at formatting time in OnKillFocus().
   if ( c < WXK_SPACE || c == WXK_DELETE )
   {
      // Allow ASCII control characters and Delete.
      return;
   }

   // Check if this character is allowed in the current state.
   wxString val;
   int pos;
   GetCurrentValueAndInsertionPoint(val, pos);

   if ( !IsCharOk(val, pos, ch) )
   {
      if ( !wxValidator::IsSilent() )
         wxBell();

      // Do not skip the event in this case, stop handling it here.
      event.Skip(false);
   }
}