From 85c0b45bb11830c61f4c6b9ac9614d58cefe8656 Mon Sep 17 00:00:00 2001
From: Jasmin Dizdarevic <jasmin.dizdarevic@vvb.at>
Date: Sat, 27 Apr 2013 22:25:09 +0200
Subject: [PATCH 2/2] Added option to copy column names from result grid

---
 pgadmin/ctl/ctlSQLGrid.cpp          | 52 +++++++++++++++++++++++++++++++++++++
 pgadmin/frm/frmOptions.cpp          |  3 +++
 pgadmin/include/ctl/ctlSQLGrid.h    |  4 +++
 pgadmin/include/utils/sysSettings.h | 10 +++++++
 pgadmin/ui/frmOptions.xrc           | 15 +++++++++++
 5 files changed, 84 insertions(+)

diff --git a/pgadmin/ctl/ctlSQLGrid.cpp b/pgadmin/ctl/ctlSQLGrid.cpp
index ba45729..dce2936 100644
--- a/pgadmin/ctl/ctlSQLGrid.cpp
+++ b/pgadmin/ctl/ctlSQLGrid.cpp
@@ -149,14 +149,60 @@ wxString ctlSQLGrid::GetExportLine(int row, wxArrayInt cols)
 	return str;
 }
 
+wxString ctlSQLGrid::GetColumnName(int colNum)
+{
+	wxString columnName = GetColLabelValue(colNum);
+	columnName = columnName.Left(columnName.find(wxT("\n")));	
+	return columnName;
+}
+void ctlSQLGrid::AppendColumnHeader(wxString &str, int start, int end) {
+	size_t i, arrsize;
+	arrsize = (end - start + 1);
+	wxArrayInt columns = wxArrayInt(arrsize);
+	
+	for(i = 0; i < arrsize; i++)
+	{
+		columns[i] = (start + i);
+	}
+
+	AppendColumnHeader(str, columns);
+}
+void ctlSQLGrid::AppendColumnHeader(wxString &str, wxArrayInt columns)
+{
+	if(settings->GetColumnNames())
+	{
+		bool CopyQuoting = (settings->GetCopyQuoting() == 1 || settings->GetCopyQuoting() == 2);
+		size_t i;
+
+		for(i = 0; i < columns.Count() ; i++)
+		{
+			long columnPos = columns.Item(i);
+			if(i > 0)
+				str.Append(settings->GetCopyColSeparator());
+
+			if(CopyQuoting)
+				str.Append(settings->GetCopyQuoteChar());
+			str.Append(GetColumnName(columnPos));
+			if(CopyQuoting)
+				str.Append(settings->GetCopyQuoteChar());		
+		
+		}
+		str.Append(END_OF_LINE);
+	}
+}
+
 int ctlSQLGrid::Copy()
 {
 	wxString str;
 	int copied = 0;
 	size_t i;
 
+	
+
 	if (GetSelectedRows().GetCount())
 	{
+		AppendColumnHeader(str, 0, (GetNumberCols() - 1));		
+		
 		wxArrayInt rows = GetSelectedRows();
 
 		for (i = 0 ; i < rows.GetCount() ; i++)
@@ -174,6 +220,8 @@ int ctlSQLGrid::Copy()
 		wxArrayInt cols = GetSelectedCols();
 		size_t numRows = GetNumberRows();
 
+		AppendColumnHeader(str, cols);
+
 		for (i = 0 ; i < numRows ; i++)
 		{
 			str.Append(GetExportLine(i, cols));
@@ -194,6 +242,8 @@ int ctlSQLGrid::Copy()
 		y1 = GetSelectionBlockTopLeft()[0].GetRow();
 		y2 = GetSelectionBlockBottomRight()[0].GetRow();
 
+		AppendColumnHeader(str, x1, x2);
+
 		for (i = y1; i <= y2; i++)
 		{
 			str.Append(GetExportLine(i, x1, x2));
@@ -211,6 +261,8 @@ int ctlSQLGrid::Copy()
 		row = GetGridCursorRow();
 		col = GetGridCursorCol();
 
+		AppendColumnHeader(str, col, col);
+
 		str.Append(GetExportLine(row, col, col));
 		copied = 1;
 	}
diff --git a/pgadmin/frm/frmOptions.cpp b/pgadmin/frm/frmOptions.cpp
index c1fcd14..9ca9c66 100644
--- a/pgadmin/frm/frmOptions.cpp
+++ b/pgadmin/frm/frmOptions.cpp
@@ -86,6 +86,7 @@
 #define chkStickySql                CTRL_CHECKBOX("chkStickySql")
 #define chkIndicateNull             CTRL_CHECKBOX("chkIndicateNull")
 #define txtDecimalMark	            CTRL_TEXT("txtDecimalMark")
+#define chkColumnNames				CTRL_CHECKBOX("chkColumnNames")
 #define txtThousandsSeparator       CTRL_TEXT("txtThousandsSeparator")
 #define chkAutoRollback             CTRL_CHECKBOX("chkAutoRollback")
 #define chkDoubleClickProperties    CTRL_CHECKBOX("chkDoubleClickProperties")
@@ -304,6 +305,7 @@ frmOptions::frmOptions(frmMain *parent)
 	chkAutoRollback->SetValue(settings->GetAutoRollback());
 	chkDoubleClickProperties->SetValue(settings->GetDoubleClickProperties());
 	txtDecimalMark->SetValue(settings->GetDecimalMark());
+	chkColumnNames->SetValue(settings->GetColumnNames());
 	chkShowNotices->SetValue(settings->GetShowNotices());
 
 	txtPgHelpPath->SetValue(settings->GetPgHelpPath());
@@ -671,6 +673,7 @@ void frmOptions::OnOK(wxCommandEvent &ev)
 	settings->SetStickySql(chkStickySql->GetValue());
 	settings->SetIndicateNull(chkIndicateNull->GetValue());
 	settings->SetDecimalMark(txtDecimalMark->GetValue());
+	settings->SetColumnNames(chkColumnNames->GetValue());
 	settings->SetThousandsSeparator(txtThousandsSeparator->GetValue());
 	settings->SetAutoRollback(chkAutoRollback->GetValue());
 	settings->SetDoubleClickProperties(chkDoubleClickProperties->GetValue());
diff --git a/pgadmin/include/ctl/ctlSQLGrid.h b/pgadmin/include/ctl/ctlSQLGrid.h
index b4ebc24..173ec31 100644
--- a/pgadmin/include/ctl/ctlSQLGrid.h
+++ b/pgadmin/include/ctl/ctlSQLGrid.h
@@ -44,6 +44,10 @@ public:
 private:
 	void OnCopy(wxCommandEvent &event);
 	void OnMouseWheel(wxMouseEvent &event);
+	wxString GetColumnName(int colNum);
+	void AppendColumnHeader(wxString &str, int start, int end);
+	void AppendColumnHeader(wxString &str, wxArrayInt columns);
+
 };
 
 #endif
diff --git a/pgadmin/include/utils/sysSettings.h b/pgadmin/include/utils/sysSettings.h
index 945e537..c0eee7c 100644
--- a/pgadmin/include/utils/sysSettings.h
+++ b/pgadmin/include/utils/sysSettings.h
@@ -379,6 +379,16 @@ public:
 	{		
 		Write(wxT("DecimalMark"), newval);
 	}
+	bool GetColumnNames() const
+	{
+		bool b;
+		Read(wxT("ColumnNames"), &b, false);
+		return b;
+	}
+	void SetColumnNames(const bool newval)
+	{
+		WriteBool(wxT("ColumnNames"), newval);
+	}
 	bool GetLineNumber() const
 	{
 		bool b;
diff --git a/pgadmin/ui/frmOptions.xrc b/pgadmin/ui/frmOptions.xrc
index 892a940..a57b0ce 100644
--- a/pgadmin/ui/frmOptions.xrc
+++ b/pgadmin/ui/frmOptions.xrc
@@ -885,6 +885,21 @@
                   </object>
                   <flag>wxEXPAND|wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
                   <border>4</border>
+                </object>
+                <object class="sizeritem">
+                  <object class="wxStaticText" name="stColumnNames">
+                    <label>Copy column names</label>
+                  </object>
+                  <flag>wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
+                  <border>4</border>
+                </object>
+                <object class="sizeritem">
+                  <object class="wxCheckBox" name="chkColumnNames">
+                    <label></label>
+                    <checked>0</checked>
+                  </object>
+                  <flag>wxEXPAND|wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
+                  <border>4</border>
                 </object>                  
                 </object>
               </object>
-- 
1.8.0.msysgit.0

