]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/richedit/wxlwindow.cpp
Fix for VA 4.0
[wxWidgets.git] / samples / richedit / wxlwindow.cpp
index 3780f8ccabd2ba0e448fd33c8883418c3ea79e99..0993e2ca99f2ecc1a2193826672fd668bacb1b43 100644 (file)
@@ -86,6 +86,8 @@
 static const int X_SCROLL_PAGE = 10;
 static const int Y_SCROLL_PAGE = 20;
 
+
+
 // ----------------------------------------------------------------------------
 // event tables
 // ----------------------------------------------------------------------------
@@ -394,7 +396,9 @@ wxLayoutWindow::OnMouse(int eventId, wxMouseEvent& event)
    case WXLOWIN_MENU_LUP:
       if ( m_Selecting )
       {
-         m_llist->EndSelection();
+         // end selection at the cursor position corresponding to the
+         // current mouse position, but don´t move cursor there.
+         m_llist->EndSelection(cursorPos,m_ClickPosition);
          m_Selecting = false;
 
          RequestUpdate();     // TODO: we don't have to redraw everything!
@@ -490,21 +494,18 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
    if ( IsDirectionKey(keyCode) )
    {
       // just continue the old selection
-      if ( m_Selecting )
+      if ( m_Selecting && event.ShiftDown() )
+            m_llist->ContinueSelection();
+      else
       {
+         m_llist->DiscardSelection();
+         m_Selecting = false;
          if( event.ShiftDown() )
-            m_llist->ContinueSelection();
-         else
          {
-            m_llist->DiscardSelection();
-            m_Selecting = false;
+            m_Selecting = true;
+            m_llist->StartSelection();
          }
       }
-      else if( event.ShiftDown() )
-      {
-         m_Selecting = true;
-         m_llist->StartSelection();
-      }
    }
    
    // If needed, make cursor visible:
@@ -561,17 +562,17 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
          {
          case 'c':
             // this should work even in read-only mode
-            Copy();
+            Copy(TRUE, TRUE);
             break;
-#ifdef M_BASEDIR
-        case 's': // search
+         case 's': // search
             Find("");
             break;
          case 't': // search again
             FindAgain();
             break;
-#endif
-        default:
+         default:
+            // we don't handle it, maybe an accelerator?
+            event.Skip();
             ;
          }
       else if( IsEditable() )
@@ -611,15 +612,13 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
                   SetDirty();
                }
                break;
-#ifdef M_BASEDIR
             case 's': // search
                Find("");
                break;
             case 't': // search again
                FindAgain();
                break;
-#endif
-           case 'u':
+            case 'u':
                m_llist->DeleteToBeginOfLine();
                SetDirty();
                break;
@@ -627,8 +626,11 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
                m_llist->DeleteToEndOfLine();
                SetDirty();
                break;
+            case 'c':
+               Copy(TRUE, TRUE);
+               break;
             case 'v':
-               Paste();
+               Paste( TRUE );
                break;
             case 'x':
                Cut();
@@ -639,7 +641,8 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
                break;
 #endif
             default:
-               ;
+            // we don't handle it, maybe an accelerator?
+            event.Skip();
             }
          }
          // ALT only:
@@ -653,7 +656,8 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
                SetDirty();
                break;
             default:
-               ;
+               // we don't handle it, maybe an accelerator?
+               event.Skip();
             }
          }
          // no control keys:
@@ -705,23 +709,32 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
 
             default:
                if((!(event.ControlDown() || event.AltDown()
-//#if 0
-                     ///FIXME: wxGTK reports MetaDown always
-                     || event.MetaDown()
-//#endif
                   ))
                   && (keyCode < 256 && keyCode >= 32)
                   )
                {
                   if(m_WrapMargin > 0 && isspace(keyCode))
-                     m_llist->WrapLine(m_WrapMargin);
-                  m_llist->Insert((char)keyCode);
+                  {
+                     bool wrapped = m_llist->WrapLine(m_WrapMargin);
+                     // don´t insert space as first thing in line
+                     // after wrapping:
+                     if(! wrapped || m_llist->GetCursorPos().x != 0)
+                        m_llist->Insert((char)keyCode);
+                  }
+                  else
+                     m_llist->Insert((char)keyCode);
                   SetDirty();
                }
+               else
+                  // we don't handle it, maybe an accelerator?
+                  event.Skip();
                break;
             }
          }
       }// if(IsEditable())
+      else
+         // we don't handle it, maybe an accelerator?
+         event.Skip();
    }// first switch()
 
    if ( m_Selecting )
@@ -732,7 +745,6 @@ wxLayoutWindow::OnChar(wxKeyEvent& event)
    ScrollToCursor();
    // refresh the screen
    RequestUpdate(m_llist->GetUpdateRect());
-   event.Skip();
 }
 
 void
@@ -851,19 +863,9 @@ wxLayoutWindow::InternalPaint(const wxRect *updateRect)
                   updateRect->x+updateRect->width,
                   updateRect->y+updateRect->height));
    }
-   if(IsDirty())
-   {
-      WXLO_DEBUG(("InternalPaint, isdirty, list size: %ld,%ld",
-                  (unsigned long) m_llist->GetSize().x,
-                  (unsigned long) m_llist->GetSize().y));
-//      m_llist->ForceTotalLayout();
-      m_llist->Layout(dc);
-      WXLO_DEBUG(("InternalPaint, isdirty, list size after layout: %ld,%ld",
-                  (unsigned long) m_llist->GetSize().x,
-                  (unsigned long) m_llist->GetSize().y));
-      ResizeScrollbars();
-      ResetDirty();
-   }
+
+   ResizeScrollbars();
+
    
    /* Check whether the window has grown, if so, we need to reallocate
       the bitmap to be larger. */
@@ -988,20 +990,24 @@ wxLayoutWindow::OnSize(wxSizeEvent &event)
    event.Skip();
 }
 
-// change the range and position of scrollbars
+/*
+Change the range and position of scrollbars. Has evolved into a
+generic Update function which will at some time later cause a repaint
+as needed. 
+*/
+
 void
 wxLayoutWindow::ResizeScrollbars(bool exact)
 {
 
-   if(IsDirty())
-   {
-      wxClientDC dc( this );
-      PrepareDC( dc );
+   if(! IsDirty())
+      return;
+   
+   wxClientDC dc( this );
+   PrepareDC( dc );
 //      m_llist->ForceTotalLayout();
-      m_llist->Layout(dc);
-      ResetDirty();
-      RequestUpdate();
-   }
+   m_llist->Layout(dc);
+   ResetDirty();
    
    wxPoint max = m_llist->GetSize();
    wxSize size = GetClientSize();
@@ -1017,7 +1023,11 @@ wxLayoutWindow::ResizeScrollbars(bool exact)
 
    // check if the text hasn't become too big
    // TODO why do we set both at once? they're independent...
-   if( max.x > m_maxx - WXLO_ROFFSET || max.y > m_maxy - WXLO_BOFFSET || exact )
+   if( max.x > m_maxx - WXLO_ROFFSET
+       || max.y > m_maxy - WXLO_BOFFSET
+       || max.x < m_maxx - X_SCROLL_PAGE
+       || max.y < m_maxy - Y_SCROLL_PAGE
+       || exact )
    {
       // text became too large
       if ( !exact )
@@ -1027,89 +1037,102 @@ wxLayoutWindow::ResizeScrollbars(bool exact)
          max.y += WXLO_BOFFSET;
       }
 
-      ViewStart(&m_ViewStartX, &m_ViewStartY);
-      SetScrollbars(X_SCROLL_PAGE, Y_SCROLL_PAGE,
-                    max.x / X_SCROLL_PAGE + 1, max.y / Y_SCROLL_PAGE + 1,
-                    m_ViewStartX, m_ViewStartY,
-                    true);
-
-      m_hasHScrollbar =
-      m_hasVScrollbar = true;
-
-      m_maxx = max.x + X_SCROLL_PAGE;
-      m_maxy = max.y + Y_SCROLL_PAGE;
-   }
-#if 0
-   //FIXME: this code is pretty broken, producing "arithmetic
-   //exception" crashes (div by 0??)
-   else
-   {
-      // check if the window hasn't become too big, thus making the scrollbars
-      // unnecessary
-      if ( !exact )
+      bool done = FALSE;
+      if(max.x < X_SCROLL_PAGE)
       {
-         // add an extra bit to the sizes to avoid future updates
-         max.x -= WXLO_ROFFSET;
-         max.y -= WXLO_BOFFSET;
+         SetScrollbars(0,-1,0,-1,0,-1,true);
+         m_hasHScrollbar = FALSE;
+         done = TRUE;
       }
-
-      if ( m_hasHScrollbar && (max.x < m_maxx) )
+      if(max.y < Y_SCROLL_PAGE)
       {
-         // remove the horizontal scrollbar
-         SetScrollbars(0, -1, 0, -1, 0, -1, true);
-         m_hasHScrollbar = false;
+         SetScrollbars(-1,0,-1,0,-1,0,true);
+         m_hasVScrollbar = FALSE;
+         done = TRUE;
       }
-
-      if ( m_hasVScrollbar && (max.y < m_maxy) )
+      if(! done)
       {
-         // remove the vertical scrollbar
-         SetScrollbars(-1, 0, -1, 0, -1, 0, true);
-         m_hasVScrollbar = false;
+         ViewStart(&m_ViewStartX, &m_ViewStartY);
+         SetScrollbars(X_SCROLL_PAGE,
+                       Y_SCROLL_PAGE,
+                       max.x / X_SCROLL_PAGE + 1,
+                       max.y / Y_SCROLL_PAGE + 1,
+                       m_ViewStartX, m_ViewStartY,
+                       true);
+         m_hasHScrollbar =
+            m_hasVScrollbar = true;
       }
+
+      m_maxx = max.x + X_SCROLL_PAGE;
+      m_maxy = max.y + Y_SCROLL_PAGE;
    }
-#endif
 }
 
 // ----------------------------------------------------------------------------
+//
 // clipboard operations
 //
 // ----------------------------------------------------------------------------
 
 void
-wxLayoutWindow::Paste(bool primary)
+wxLayoutWindow::Paste(bool usePrivate, bool primary)
 {
+   // this only has an effect under X11:
+   wxTheClipboard->UsePrimarySelection(primary);
    // Read some text
    if (wxTheClipboard->Open())
    {
-      if(primary)
-         wxTheClipboard->UsePrimarySelection();
-
-      wxLayoutDataObject wxldo;
-      if (wxTheClipboard->IsSupported( wxldo.GetFormat() ))
+      if(usePrivate)
       {
-         wxTheClipboard->GetData(wxldo);
-
-         // now we can access the data we had put into wxLayoutDataObject in
-         // wxLayoutList::GetSelection by calling its GetLayoutData() - the
-         // trouble is that I don't know what to do with it! (VZ)
+         wxLayoutDataObject wxldo;
+         if (wxTheClipboard->IsSupported( wxldo.GetFormat() ))
+         {
+            if(wxTheClipboard->GetData(wxldo))
+            {
+               wxTheClipboard->Close();
+               wxString str = wxldo.GetLayoutData();
+               m_llist->Read(str);
+               SetDirty();
+               RequestUpdate();
+               return;
+            }
+         }
       }
-      else
+      wxTextDataObject data;
+      if (wxTheClipboard->IsSupported( data.GetFormat() )
+          && wxTheClipboard->GetData(data) )
+      {
+         wxTheClipboard->Close();
+         wxString text = data.GetText();
+         wxLayoutImportText( m_llist, text);
+         SetDirty();
+         RequestUpdate();
+         return;
+      }
+   }
+   // if everything failed we can still try the primary:
+   wxTheClipboard->Close();
+   if(! primary) // not tried before
+   {
+      wxTheClipboard->UsePrimarySelection();
+      if (wxTheClipboard->Open())
       {
          wxTextDataObject data;
-         if (wxTheClipboard->IsSupported( data.GetFormat() ))
+         if (wxTheClipboard->IsSupported( data.GetFormat() )
+             && wxTheClipboard->GetData(data) )
          {
-            wxTheClipboard->GetData(data);
             wxString text = data.GetText();
             wxLayoutImportText( m_llist, text);
             SetDirty();
+            RequestUpdate();
          }
+         wxTheClipboard->Close();
       }
-      wxTheClipboard->Close();
    }
 }
 
 bool
-wxLayoutWindow::Copy(bool invalidate)
+wxLayoutWindow::Copy(bool invalidate, bool privateFormat, bool primary)
 {
    // Calling GetSelection() will automatically do an EndSelection()
    // on the list, but we need to take a note of it, too:
@@ -1145,26 +1168,41 @@ wxLayoutWindow::Copy(bool invalidate)
          text = text.Mid(0,len-1);
    }
 
+#if 0
+if(! primary) // always copy as text-only to primary selection
+   {
+      wxTheClipboard->UsePrimarySelection();
+      if (wxTheClipboard->Open())
+      {
+         wxTextDataObject *data = new wxTextDataObject( text );
+         wxTheClipboard->SetData( data );
+         wxTheClipboard->Close();
+      }
+   }
+#endif
 
+   wxTheClipboard->UsePrimarySelection(primary);
    if (wxTheClipboard->Open())
    {
-      wxDataObjectComposite *dobj = new wxDataObjectComposite;
-      dobj->Add(new wxTextDataObject(text));
-      dobj->Add(wldo);
-
-      bool rc = wxTheClipboard->SetData(dobj);
+      wxTextDataObject *data = new wxTextDataObject( text );
+      bool rc;
 
+         rc = wxTheClipboard->SetData( data );
+      if(privateFormat)
+         rc |= wxTheClipboard->SetData( wldo );
       wxTheClipboard->Close();
       return rc;
    }
-
+   else
+      delete wldo;
+   
    return FALSE;
 }
 
 bool
-wxLayoutWindow::Cut(void)
+wxLayoutWindow::Cut(bool privateFormat, bool usePrimary)
 {
-   if(Copy(false)) // do not invalidate selection after copy
+   if(Copy(false, privateFormat, usePrimary)) // do not invalidate selection after copy
    {
       m_llist->DeleteSelection();
       SetDirty();
@@ -1178,12 +1216,12 @@ wxLayoutWindow::Cut(void)
 // searching
 // ----------------------------------------------------------------------------
 
-#ifdef M_BASEDIR
 bool
 wxLayoutWindow::Find(const wxString &needle,
                      wxPoint * fromWhere,
                      const wxString &configPath)
 {
+#ifdef M_BASEDIR
    wxPoint found;
    
    if(needle.Length() == 0)
@@ -1215,6 +1253,7 @@ wxLayoutWindow::Find(const wxString &needle,
       RequestUpdate();
       return true;
    }
+#endif
    return false;
 }
 
@@ -1225,7 +1264,6 @@ wxLayoutWindow::FindAgain(void)
    bool rc = Find(m_FindString);
    return rc;
 }
-#endif
 
 // ----------------------------------------------------------------------------
 // popup menu stuff