From 559a90536a6fecdda6198e78989f26f708bfa060 Mon Sep 17 00:00:00 2001
From: Vinicius Santos <vinicius.santos.lista@gmail.com>
Date: Thu, 27 Oct 2011 00:20:34 -0200
Subject: [PATCH 3/3] Implementation Nulls First option on "dlgEditGridOptions" screen.

---
 pgadmin/dlg/dlgEditGridOptions.cpp       |   92 ++++++++++++++++++++++--------
 pgadmin/include/dlg/dlgEditGridOptions.h |    1 +
 pgadmin/include/frm/frmMain.h            |   13 ++++
 pgadmin/ui/dlgEditGridOptions.xrc        |   18 ++++++-
 4 files changed, 99 insertions(+), 25 deletions(-)

diff --git a/pgadmin/dlg/dlgEditGridOptions.cpp b/pgadmin/dlg/dlgEditGridOptions.cpp
index c4682b3..0572e96 100644
--- a/pgadmin/dlg/dlgEditGridOptions.cpp
+++ b/pgadmin/dlg/dlgEditGridOptions.cpp
@@ -39,6 +39,7 @@
 #define pnlSort                     CTRL_PANEL("pnlSort")
 #define pnlFilter                   CTRL_PANEL("pnlFilter")
 #define filter                      CTRL_SQLBOX("sqlFilter")
+#define radNulls				    CTRL_RADIOBOX("radNulls")
 
 BEGIN_EVENT_TABLE(dlgEditGridOptions, pgDialog)
 	EVT_CLOSE(                                      dlgEditGridOptions::OnClose)
@@ -66,7 +67,7 @@ dlgEditGridOptions::dlgEditGridOptions(frmEditGrid *win, pgConn *conn, const wxS
 	wxWindowBase::SetFont(settings->GetSystemFont());
 	LoadResource(win, wxT("dlgEditGridOptions"));
 	conv = conn->GetConv();
-
+	
 	// Icon
 	SetIcon(*sortfilter_png_ico);
 	RestorePosition();
@@ -82,18 +83,23 @@ dlgEditGridOptions::dlgEditGridOptions(frmEditGrid *win, pgConn *conn, const wxS
 	OnCboColumnsChange(nullEvent);
 	wxListEvent nullLstEvent;
 	OnLstSortColsChange(nullLstEvent);
+	
+	if (!conn->BackendMinimumVersion(8,3))
+		radNulls->Disable();
 
 	// Setup the list box
 	int leftSize = 140, rightSize;
 	leftSize = ConvertDialogToPixels(wxPoint(leftSize, 0)).x;
 	rightSize = lstSortCols->GetClientSize().GetWidth() - leftSize;
+	
 	// This check is to work around a bug in wxGTK that doesn't set
 	// appropriately the GetClientSize().
 	// Without this workaround, we have an invisible second column.
 	if (rightSize < leftSize)
 		rightSize = leftSize + 1;
-	lstSortCols->InsertColumn(0, _("Column name"), wxLIST_FORMAT_LEFT, leftSize);
-	lstSortCols->InsertColumn(1, _("Sort order"), wxLIST_FORMAT_LEFT, rightSize);
+	lstSortCols->InsertColumn(0, _("Column name"), leftSize);
+	lstSortCols->InsertColumn(1, _("Sort order"), rightSize - leftSize);
+	lstSortCols->InsertColumn(2, _("Nulls"), leftSize);
 
 	// Setup the filter SQL box. This is an XRC 'unknown' control so must
 	// be manually created and attache to the XRC global resource.
@@ -127,13 +133,15 @@ dlgEditGridOptions::dlgEditGridOptions(frmEditGrid *win, pgConn *conn, const wxS
 				if (dir.GetChar(0) == 'D')
 				{
 					lstSortCols->SetItem(itm, 1, _("Descending"));
-					lstSortCols->SetItemData(itm, 0);
+					dir.Find(wxT("FIRST")) != wxNOT_FOUND ? lstSortCols->SetItemData(itm, DESC_NULLS_FIRST) : lstSortCols->SetItemData(itm, DESC_NULLS_LAST);
 				}
 				else
 				{
 					lstSortCols->SetItem(itm, 1, _("Ascending"));
-					lstSortCols->SetItemData(itm, 1);
+					dir.Find(wxT("FIRST")) != wxNOT_FOUND ? lstSortCols->SetItemData(itm, ASC_NULLS_FIRST) : lstSortCols->SetItemData(itm, ASC_NULLS_LAST);
 				}
+				
+				dir.Find(wxT("FIRST")) != wxNOT_FOUND ? lstSortCols->SetItem(itm, 2, _("First")) : lstSortCols->SetItem(itm, 2, _("Last"));
 				col = wxT("");
 				dir = wxT("");
 				++itm;
@@ -152,13 +160,17 @@ dlgEditGridOptions::dlgEditGridOptions(frmEditGrid *win, pgConn *conn, const wxS
 		if (dir.GetChar(0) == 'D')
 		{
 			lstSortCols->SetItem(itm, 1, _("Descending"));
-			lstSortCols->SetItemData(itm, 0);
+			dir.Find(wxT("FIRST")) != wxNOT_FOUND ? lstSortCols->SetItemData(itm, DESC_NULLS_FIRST) : lstSortCols->SetItemData(itm, DESC_NULLS_LAST);
 		}
 		else
 		{
 			lstSortCols->SetItem(itm, 1, _("Ascending"));
-			lstSortCols->SetItemData(itm, 1);
+			dir.Find(wxT("FIRST")) != wxNOT_FOUND ? lstSortCols->SetItemData(itm, ASC_NULLS_FIRST) : lstSortCols->SetItemData(itm, ASC_NULLS_LAST);
+
 		}
+		
+		dir.Find(wxT("FIRST")) != wxNOT_FOUND ? lstSortCols->SetItem(itm, 2, _("First")) : lstSortCols->SetItem(itm, 2, _("Last"));;
+
 	}
 
 	// Finally (phew!) remove all columns we're already sorting on from the list.
@@ -208,13 +220,38 @@ void dlgEditGridOptions::OnRemove(wxCommandEvent &ev)
 	OnLstSortColsChange(nullLstEvent);
 }
 
-
-void dlgEditGridOptions::OnAsc(wxCommandEvent &ev)
+void dlgEditGridOptions::OnClickSort(wxCommandEvent &ev, bool asc)
 {
+	
 	long itm = lstSortCols->GetItemCount();
 	lstSortCols->InsertItem(itm, cboColumns->GetValue());
-	lstSortCols->SetItem(itm, 1, _("Ascending"));
-	lstSortCols->SetItemData(itm, 1);
+	
+	if (asc)
+		lstSortCols->SetItem(itm, 1, _("Ascending"));
+	else
+		lstSortCols->SetItem(itm, 1, _("Descending"));
+	
+	
+	if (radNulls->GetSelection() == NULLS_FIRST)
+	{
+		lstSortCols->SetItem(itm, 2, _("First"));
+		
+		if (asc)
+			lstSortCols->SetItemData(itm, ASC_NULLS_FIRST);
+		else
+			lstSortCols->SetItemData(itm, DESC_NULLS_FIRST);
+	}
+	else
+	{
+		lstSortCols->SetItem(itm, 2, _("Last"));
+
+		if (asc)
+			lstSortCols->SetItemData(itm, ASC_NULLS_LAST);
+		else
+			lstSortCols->SetItemData(itm, DESC_NULLS_LAST);
+
+	}
+	
 	cboColumns->Delete(cboColumns->GetCurrentSelection());
 
 	// Setup the buttons
@@ -223,18 +260,14 @@ void dlgEditGridOptions::OnAsc(wxCommandEvent &ev)
 	OnLstSortColsChange(nullLstEvent);
 }
 
-void dlgEditGridOptions::OnDesc(wxCommandEvent &ev)
+void dlgEditGridOptions::OnAsc(wxCommandEvent &ev)
 {
-	long itm = lstSortCols->GetItemCount();
-	lstSortCols->InsertItem(itm, cboColumns->GetValue());
-	lstSortCols->SetItem(itm, 1, _("Descending"));
-	lstSortCols->SetItemData(itm, 0);
-	cboColumns->Delete(cboColumns->GetCurrentSelection());
+	OnClickSort(ev, true);
+}
 
-	// Setup the buttons
-	OnCboColumnsChange(ev);
-	wxListEvent nullLstEvent;
-	OnLstSortColsChange(nullLstEvent);
+void dlgEditGridOptions::OnDesc(wxCommandEvent &ev)
+{
+	OnClickSort(ev, false);
 }
 
 #ifdef __WXMAC__
@@ -263,11 +296,13 @@ void dlgEditGridOptions::OnCboColumnsChange(wxCommandEvent &ev)
 	{
 		btnAsc->Enable(false);
 		btnDesc->Enable(false);
+		radNulls->Enable(false);
 	}
 	else
 	{
 		btnAsc->Enable(true);
 		btnDesc->Enable(true);
+		radNulls->Enable(true);
 	}
 }
 
@@ -303,15 +338,24 @@ void dlgEditGridOptions::OnOK(wxCommandEvent &ev)
 	if (nbOptions->GetPageCount() > 1)
 	{
 		wxString sortCols;
-		long x, count = lstSortCols->GetItemCount();
+		long x, dir, count = lstSortCols->GetItemCount();
 
 		for (x = 0; x < count; x++)
 		{
 			sortCols += qtIdent(lstSortCols->GetItemText(x));
-			if (lstSortCols->GetItemData(x) == 0)
+			dir = lstSortCols->GetItemData(x);
+			
+			if (dir == DESC_NULLS_FIRST || dir == DESC_NULLS_LAST)
+			{
 				sortCols += wxT(" DESC");
+				dir == DESC_NULLS_FIRST ? sortCols += wxT(" NULLS FIRST") : wxT("");
+			}
 			else
-				sortCols += wxT(" ASC");
+			{
+				sortCols += wxT(" ASC"); 
+				dir == ASC_NULLS_FIRST ? sortCols += wxT(" NULLS FIRST") : wxT("");
+			}
+			
 			sortCols += wxT(", ");
 		}
 
diff --git a/pgadmin/include/dlg/dlgEditGridOptions.h b/pgadmin/include/dlg/dlgEditGridOptions.h
index 3e99ace..281ba03 100644
--- a/pgadmin/include/dlg/dlgEditGridOptions.h
+++ b/pgadmin/include/dlg/dlgEditGridOptions.h
@@ -55,6 +55,7 @@ private:
 	void OnAsc(wxCommandEvent &ev);
 	void OnDesc(wxCommandEvent &ev);
 	void OnValidate(wxCommandEvent &ev);
+	void OnClickSort(wxCommandEvent &ev, bool asc);
 	void OnCboColumnsChange(wxCommandEvent &ev);
 	void OnLstSortColsChange(wxListEvent &ev);
 	bool Validate();
diff --git a/pgadmin/include/frm/frmMain.h b/pgadmin/include/frm/frmMain.h
index 7aa15e8..d2fb8ad 100644
--- a/pgadmin/include/frm/frmMain.h
+++ b/pgadmin/include/frm/frmMain.h
@@ -82,6 +82,19 @@ enum
 	REFRESH_OBJECT_AND_CHILDREN
 };
 
+enum
+{
+	NULLS_FIRST
+};
+
+enum
+{
+	DESC_NULLS_LAST = 0,
+	ASC_NULLS_LAST,
+	DESC_NULLS_FIRST,
+	ASC_NULLS_FIRST
+};
+
 // Class declarations
 class frmMain : public pgFrame
 {
diff --git a/pgadmin/ui/dlgEditGridOptions.xrc b/pgadmin/ui/dlgEditGridOptions.xrc
index c2ea2e1..9ff3659 100644
--- a/pgadmin/ui/dlgEditGridOptions.xrc
+++ b/pgadmin/ui/dlgEditGridOptions.xrc
@@ -16,7 +16,7 @@
             <object class="wxPanel" name="pnlSort">
               <object class="wxFlexGridSizer">
                 <cols>1</cols>
-                <rows>5</rows>
+                <rows>6</rows>
                 <vgap>5</vgap>
                 <hgap>5</hgap>
                 <growablerows>1</growablerows>
@@ -52,6 +52,22 @@
                   <flag>wxEXPAND|wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
                   <border>4</border>
                 </object>
+                
+                <object class="sizeritem">
+                  <object class="wxRadioBox" name="radNulls">
+                    <label>Nulls</label>
+                    <content>
+                      <item>&amp;First</item>
+                      <item>&amp;Last</item>
+                    </content>
+                    <selection>1</selection>
+                    <style>wxRA_SPECIFY_ROWS</style>
+                    <tooltip>Choose how will appears the nulls.</tooltip>
+                  </object>
+                  <flag>wxEXPAND|wxALIGN_CENTER_VERTICAL|wxTOP|wxLEFT|wxRIGHT</flag>
+                  <border>4</border>
+                </object>
+                
                 <object class="sizeritem">
                   <object class="wxFlexGridSizer">
                     <cols>3</cols>
-- 
1.7.4.msysgit.0

