Quoting Dave Page <[email protected]>:

Yes - in particular the quote settings - I have lots of data containing
newlines for example, which this won't work with for obvious reasons.

Ok, that's done. Seems to work ok for me, but I don't have any real data that would stress it to test with, just a few test rows.

OID columns should be ignored, but serial columns should be optional
imho - just a simple 'The data you are pasting include values that will
be inserted into a serial column. Do you want to skip the serial valules
and allow new ones to be assigned? Y/N' prompt at paste time.

I didn't look too hard, but it looked like OIDs are always going to be the first column when present. Based on that, I updated the patch to skip the first column when there are OIDs.

I'm probably not going to have time to deal with the serial columns part in the next few days. The patch should be usable now if people feel like testing it.

Ed

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

Index: frm/frmEditGrid.cpp
===================================================================
--- frm/frmEditGrid.cpp	(revision 5099)
+++ frm/frmEditGrid.cpp	(working copy)
@@ -44,6 +44,7 @@
 #include "images/sortfilter.xpm"
 #include "images/help.xpm"
 #include "images/clip_copy.xpm"
+#include "images/clip_paste.xpm"
 
 #define CTRLID_LIMITLABEL       4224
 #define CTRLID_LIMITSPACER      4225
@@ -58,6 +59,7 @@
     EVT_MENU(MNU_OPTIONS,   frmEditGrid::OnOptions)
     EVT_MENU(MNU_HELP,      frmEditGrid::OnHelp)
     EVT_MENU(MNU_COPY,      frmEditGrid::OnCopy)
+    EVT_MENU(MNU_PASTE,     frmEditGrid::OnPaste)
     EVT_CLOSE(              frmEditGrid::OnClose)
     EVT_KEY_DOWN(           frmEditGrid::OnKey)
     EVT_GRID_RANGE_SELECT(frmEditGrid::OnGridSelectCells)
@@ -101,6 +103,8 @@
     toolBar->AddSeparator();
     toolBar->AddTool(MNU_COPY, _("Copy"), wxBitmap(clip_copy_xpm), _("Copy selected lines to clipboard"), wxITEM_NORMAL);
     toolBar->AddSeparator();
+    toolBar->AddTool(MNU_PASTE, _("Paste"), wxBitmap(clip_paste_xpm), _("Paste text from the clipboard"), wxITEM_NORMAL);
+    toolBar->AddSeparator();
     toolBar->AddTool(MNU_DELETE, _("Delete"), wxBitmap(delete_xpm), _("Delete selected lines."), wxITEM_NORMAL);
     toolBar->AddSeparator();
 
@@ -128,14 +132,15 @@
     toolBar->EnableTool(MNU_COPY, true);
     toolBar->EnableTool(MNU_DELETE, false);
 
-    wxAcceleratorEntry entries[6];
+    wxAcceleratorEntry entries[7];
 
     entries[0].Set(wxACCEL_CTRL,                (int)'S',      MNU_SAVE);
     entries[1].Set(wxACCEL_NORMAL,              WXK_F5,        MNU_REFRESH);
     entries[2].Set(wxACCEL_CTRL,                (int)'Z',      MNU_UNDO);
     entries[3].Set(wxACCEL_NORMAL,              WXK_F1,        MNU_HELP);
     entries[4].Set(wxACCEL_CTRL,                (int)'C',      MNU_COPY);
-    entries[5].Set(wxACCEL_NORMAL,              WXK_DELETE,    MNU_DELETE);
+    entries[5].Set(wxACCEL_CTRL,                (int)'V',      MNU_PASTE);
+    entries[6].Set(wxACCEL_NORMAL,              WXK_DELETE,    MNU_DELETE);
     
     wxAcceleratorTable accel(6, entries);
     SetAcceleratorTable(accel);
@@ -236,6 +241,84 @@
 }
 
 
+void frmEditGrid::OnPaste(wxCommandEvent &ev)
+{
+    unsigned int row, col, numCols;
+    int start, pos, len;
+    wxArrayString columns;
+    wxString text, quoteChar, colSep;
+    bool inQuotes, inData;
+
+    if (wxTheClipboard->Open())
+    {
+        if (wxTheClipboard->IsSupported(wxDF_TEXT))
+        {
+            wxTextDataObject data;
+            wxTheClipboard->GetData(data);
+            text = data.GetText();
+        }
+        else {
+            wxTheClipboard->Close();
+            return;
+        }
+        wxTheClipboard->Close();
+    }
+    else {
+        return;
+    }
+
+    start = pos = 0;
+    len = text.Len();
+    quoteChar = settings->GetCopyQuoteChar();
+    colSep = settings->GetCopyColSeparator();
+    inQuotes = inData = false;
+
+    while (pos < len && !(text[pos] == '\n' && !inQuotes)) {
+        if (!inData) {
+            if (text[pos] == quoteChar) {
+                inQuotes = inData = true;
+                pos++;
+                start++;
+                continue;
+            }
+            else {
+                inQuotes = false;
+            }
+            inData = true;
+        }
+
+        if (inQuotes && text[pos] == quoteChar &&
+            text[pos+1] == colSep) {
+            columns.Add(text.Mid(start, pos - start));
+            start = (pos += 2);
+            inData = false;
+        }
+        else if (!inQuotes && text[pos] == colSep) {
+            columns.Add(text.Mid(start, pos - start));
+            start = ++pos;
+            inData = false;
+        }
+        else {
+            pos++;
+        }
+    }
+    if (start < pos) {
+        if (inQuotes && text[pos-1] == quoteChar)
+            columns.Add(text.Mid(start, pos - start - 1));
+        else
+            columns.Add(text.Mid(start, pos - start));
+    }
+
+    row = sqlGrid->GetGridCursorRow();
+    numCols = sqlGrid->GetNumberCols();
+
+    for (col = (hasOids ? 1 : 0); col < numCols && col < columns.GetCount(); col++) {
+        sqlGrid->GetTable()->SetValue(row, col, columns.Item(col));
+    }
+    sqlGrid->ForceRefresh();
+}
+
+
 void frmEditGrid::OnHelp(wxCommandEvent &ev)
 {
     DisplayHelp(this, wxT("editgrid"), viewdata_xpm);
Index: include/frmEditGrid.h
===================================================================
--- include/frmEditGrid.h	(revision 5099)
+++ include/frmEditGrid.h	(working copy)
@@ -182,6 +182,7 @@
     void OnEditorShown(wxGridEvent& event);
     void OnKey(wxKeyEvent& event);
     void OnCopy(wxCommandEvent& event);
+    void OnPaste(wxCommandEvent& event);
     void OnLabelDoubleClick(wxGridEvent& event);
     void OnLabelRightClick(wxGridEvent& event);
     void Abort();
---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to