[gnumeric] Add menu item to wrap SORT around an existing range.



commit 1d9a24c7468e2b57bdeb4f5aafc833380a4b5c0b
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Mon Jul 12 09:03:05 2010 -0600

    Add menu item to wrap SORT around an existing range.
    
    2010-07-12  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* component/Gnumeric-embed.xml.in: add submenu to Insert menu
    	* src/GNOME_Gnumeric-gtk.xml.in: add submenu to Insert menu
    	* src/HILDON_Gnumeric-gtk.xml.in: add submenu to Insert menu
    	* src/wbc-gtk-actions.c (cb_insert_sort_*cending): new actions
    	(permanent_actions): add MenuInsertFormulaWrap, InsertSortDecreasing
    	  and InsertSortIncreasing
    	* src/wbc-gtk.c (wbcg_menu_state_update): handle the above new actions
    	* src/workbook-cmd-format.c (workbook_cmd_wrap_sort): new
    	(cb_get_cell_content): new
    	* src/workbook-cmd-format.h (workbook_cmd_wrap_sort): new

 ChangeLog                       |   13 +++++
 NEWS                            |    1 +
 component/Gnumeric-embed.xml.in |    4 ++
 src/GNOME_Gnumeric-gtk.xml.in   |    4 ++
 src/HILDON_Gnumeric-gtk.xml.in  |    4 ++
 src/wbc-gtk-actions.c           |   13 +++++-
 src/wbc-gtk.c                   |   17 ++++++-
 src/workbook-cmd-format.c       |  104 +++++++++++++++++++++++++++++++++++++++
 src/workbook-cmd-format.h       |    2 +
 9 files changed, 159 insertions(+), 3 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 2eaabe9..e993e37 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2010-07-12  Andreas J. Guelzow <aguelzow pyrshep ca>
+
+	* component/Gnumeric-embed.xml.in: add submenu to Insert menu
+	* src/GNOME_Gnumeric-gtk.xml.in: add submenu to Insert menu
+	* src/HILDON_Gnumeric-gtk.xml.in: add submenu to Insert menu
+	* src/wbc-gtk-actions.c (cb_insert_sort_*cending): new actions
+	(permanent_actions): add MenuInsertFormulaWrap, InsertSortDecreasing
+	  and InsertSortIncreasing
+	* src/wbc-gtk.c (wbcg_menu_state_update): handle the above new actions
+	* src/workbook-cmd-format.c (workbook_cmd_wrap_sort): new
+	(cb_get_cell_content): new
+	* src/workbook-cmd-format.h (workbook_cmd_wrap_sort): new
+
 2010-07-11  Jean Brefort  <jean brefort normalesup org>
 
 	* src/item-edit.c (item_edit_draw), (item_edit_button_pressed),
diff --git a/NEWS b/NEWS
index 3206b2d..829e614 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,7 @@ Andreas:
 	* Add autofit actions to cell context menu.
 	* Add new function ARRAY. [#570689]
 	* Add new function SORT. [#59144]
+	* Add menu item to wrap SORT around an existing range.
 
 Jean:
 	* Fix strong/weak cursor display. [#623241]
diff --git a/component/Gnumeric-embed.xml.in b/component/Gnumeric-embed.xml.in
index e26288a..1435308 100644
--- a/component/Gnumeric-embed.xml.in
+++ b/component/Gnumeric-embed.xml.in
@@ -117,6 +117,10 @@
             -->
       <menuitem action="InsertImage"/>
       <menuitem action="InsertFormula"/>
+      <menu name="InsertFormulaWrap" action="MenuInsertFormulaWrap">
+        <menuitem action="InsertSortDecreasing"/>
+        <menuitem action="InsertSortIncreasing"/>
+      </menu>
       <menuitem action="InsertNames"/>
       <menuitem action="InsertComment"/>
       <menuitem action="InsertHyperlink"/>
diff --git a/src/GNOME_Gnumeric-gtk.xml.in b/src/GNOME_Gnumeric-gtk.xml.in
index 028d849..f84b41d 100644
--- a/src/GNOME_Gnumeric-gtk.xml.in
+++ b/src/GNOME_Gnumeric-gtk.xml.in
@@ -118,6 +118,10 @@
             -->
       <menuitem action="InsertImage"/>
       <menuitem action="InsertFormula"/>
+      <menu name="InsertFormulaWrap" action="MenuInsertFormulaWrap">
+        <menuitem action="InsertSortDecreasing"/>
+        <menuitem action="InsertSortIncreasing"/>
+      </menu>
       <menuitem action="InsertNames"/>
       <menuitem action="InsertComment"/>
       <menuitem action="InsertHyperlink"/>
diff --git a/src/HILDON_Gnumeric-gtk.xml.in b/src/HILDON_Gnumeric-gtk.xml.in
index ca87fa7..b897a1c 100644
--- a/src/HILDON_Gnumeric-gtk.xml.in
+++ b/src/HILDON_Gnumeric-gtk.xml.in
@@ -127,6 +127,10 @@
             -->
       <menuitem action="InsertImage"/>
       <menuitem action="InsertFormula"/>
+      <menu name="InsertFormulaWrap" action="MenuInsertFormulaWrap">
+        <menuitem action="InsertSortDecreasing"/>
+        <menuitem action="InsertSortIncreasing"/>
+      </menu>
       <menuitem action="InsertNames"/>
       <menuitem action="InsertComment"/>
       <menuitem action="InsertHyperlink"/>
diff --git a/src/wbc-gtk-actions.c b/src/wbc-gtk-actions.c
index 7084fd7..e828c97 100644
--- a/src/wbc-gtk-actions.c
+++ b/src/wbc-gtk-actions.c
@@ -1156,6 +1156,8 @@ static GNM_ACTION_DEF (cb_insert_image)
 
 static GNM_ACTION_DEF (cb_insert_hyperlink)	{ dialog_hyperlink (wbcg, SHEET_CONTROL (wbcg_cur_scg (wbcg))); }
 static GNM_ACTION_DEF (cb_formula_guru)		{ dialog_formula_guru (wbcg, NULL); }
+static GNM_ACTION_DEF (cb_insert_sort_ascending) { workbook_cmd_wrap_sort (WORKBOOK_CONTROL (wbcg), 1);}
+static GNM_ACTION_DEF (cb_insert_sort_descending){ workbook_cmd_wrap_sort (WORKBOOK_CONTROL (wbcg), 0);}
 
 static void
 sort_by_rows (WBCGtk *wbcg, gboolean descending)
@@ -1789,6 +1791,8 @@ static GtkActionEntry const permanent_actions[] = {
 		{ "MenuViewToolbars",		NULL, N_("_Toolbars") },
 	{ "MenuInsert",		NULL, N_("_Insert") },
 		{ "MenuInsertSpecial",		NULL, N_("S_pecial") },
+		{ "MenuInsertFormulaWrap", "Gnumeric_FormulaGuru", 
+		  N_("Func_tion Wrapper") },
 	{ "MenuFormat",		NULL, N_("F_ormat") },
 		{ "MenuFormatCells",		NULL, N_("_Cells") },
 		{ "MenuFormatColumn",		NULL, N_("C_olumn") },
@@ -2117,6 +2121,13 @@ static GtkActionEntry const actions[] = {
 	{ "InsertHyperlink", "Gnumeric_Link_Add", N_("Hyper_link..."),
 		"<control>K", N_("Insert a Hyperlink"),
 		G_CALLBACK (cb_insert_hyperlink) },
+	{ "InsertSortDecreasing", GTK_STOCK_SORT_DESCENDING, N_("Sort (_Descending)"),
+		NULL, N_("Wrap with SORT (descending)"),
+		G_CALLBACK (cb_insert_sort_descending) },
+	{ "InsertSortIncreasing", GTK_STOCK_SORT_ASCENDING, N_("Sort (_Ascending)"),
+		NULL, N_("Wrap with SORT (ascending)"),
+		G_CALLBACK (cb_insert_sort_ascending) },
+	
 /* Insert -> Special */
 	{ "InsertCurrentDate", NULL, N_("Current _date"),
 		"<control>semicolon", N_("Insert the current date into the selected cell(s)"),
@@ -2436,7 +2447,7 @@ static GtkActionEntry const actions[] = {
 	{ "AutoSum", "Gnumeric_AutoSum", N_("Sum"),
 		"<alt>equal", N_("Sum into the current cell"),
 		G_CALLBACK (cb_autosum) },
-	{ "InsertFormula", "Gnumeric_FormulaGuru", N_("Function"), NULL,
+	{ "InsertFormula", "Gnumeric_FormulaGuru", N_("_Function"), NULL,
 		N_("Edit a function in the current cell"),
 		G_CALLBACK (cb_formula_guru) },
 
diff --git a/src/wbc-gtk.c b/src/wbc-gtk.c
index a064855..b119e51 100644
--- a/src/wbc-gtk.c
+++ b/src/wbc-gtk.c
@@ -1632,8 +1632,11 @@ wbcg_menu_state_update (WorkbookControl *wbc, int flags)
 
 	if (MS_COMMENT_LINKS_RANGE & flags) {
 		GSList *l;
+		int count = 0;
 		gboolean has_links = FALSE, has_comments = FALSE;
-		for (l = scg_view (scg)->selections; 
+		gboolean sel_is_vector = FALSE;
+		SheetView *sv = scg_view (scg);
+		for (l = sv->selections; 
 		     l != NULL; l = l->next) {
 			GnmRange const *r = l->data;
 			GSList *objs;
@@ -1650,13 +1653,23 @@ wbcg_menu_state_update (WorkbookControl *wbc, int flags)
 				has_comments = (objs != NULL);
 				g_slist_free (objs);
 			}
-			if(has_comments && has_links)
+			if((count++ > 1) && has_comments && has_links)
 				break;
 		}
 		wbc_gtk_set_action_sensitivity 
 			(wbcg, "EditClearHyperlinks", has_links);
 		wbc_gtk_set_action_sensitivity 
 			(wbcg, "EditClearComments", has_comments);
+		if (count == 1) {
+			GnmRange const *r = sv->selections->data;
+			sel_is_vector = (range_width (r) == 1 ||
+					 range_height (r) == 1) &&
+				!range_is_singleton (r);
+ 		}
+		wbc_gtk_set_action_sensitivity 
+			(wbcg, "InsertSortDecreasing", sel_is_vector);
+		wbc_gtk_set_action_sensitivity 
+			(wbcg, "InsertSortIncreasing", sel_is_vector);
 	}
 	{
 		gboolean const has_slicer = (NULL != sv_editpos_in_slicer (sv));
diff --git a/src/workbook-cmd-format.c b/src/workbook-cmd-format.c
index 898c0cd..da39327 100644
--- a/src/workbook-cmd-format.c
+++ b/src/workbook-cmd-format.c
@@ -11,10 +11,16 @@
 #include "gnumeric.h"
 #include "workbook-cmd-format.h"
 
+#include "cell.h"
 #include "dependent.h"
+#include "expr.h"
+#include "func.h"
 #include "ranges.h"
 #include "gui-util.h"
 #include "selection.h"
+#include "sheet-merge.h"
+#include "sheet-view.h"
+#include "value.h"
 #include "workbook-control.h"
 #include "workbook-view.h"
 #include "workbook.h"
@@ -108,3 +114,101 @@ workbook_cmd_dec_indent (WorkbookControl *wbc)
 		cmd_selection_format (wbc, style, NULL, _("Decrease Indent"));
 	}
 }
+
+struct workbook_cmd_wrap_sort_t {
+	GnmExprList    *args;
+	GnmRange const *r;
+	Workbook *wb;
+};
+
+static GnmValue *
+cb_get_cell_content (GnmCellIter const *iter, struct workbook_cmd_wrap_sort_t *cl)
+{
+	GnmExpr const *expr;
+
+	if (iter->cell == NULL)
+		expr = gnm_expr_new_constant (value_new_empty ());
+	else if (gnm_cell_has_expr (iter->cell)) {
+		char	 *text;
+		GnmParsePos pp;
+		GnmExprTop const *texpr;
+
+		parse_pos_init (&pp, cl->wb, iter->pp.sheet, 
+				cl->r->start.col, cl->r->start.row);
+		text = gnm_expr_as_string   ((iter->cell)->base.texpr->expr, 
+					     &iter->pp, NULL);
+		texpr = gnm_expr_parse_str (text, &pp, GNM_EXPR_PARSE_DEFAULT,
+					    NULL, NULL);
+		g_free (text);
+		expr = gnm_expr_copy (texpr->expr);
+		gnm_expr_top_unref (texpr);
+		
+	} else if (iter->cell->value != NULL)
+		expr = gnm_expr_new_constant (value_dup (iter->cell->value));
+	else
+		expr = gnm_expr_new_constant (value_new_empty ());
+
+	cl->args = gnm_expr_list_prepend (cl->args, expr);
+	return NULL;
+}
+
+void 
+workbook_cmd_wrap_sort (WorkbookControl *wbc, int type)
+{
+	WorkbookView const *wbv = wb_control_view (wbc);
+	SheetView *sv = wb_view_cur_sheet_view (wbv);
+	GSList *l = sv->selections, *merges;
+	GnmExpr const *expr;
+	GnmFunc	   *fd_sort;
+	GnmFunc	   *fd_array;
+	GnmExprTop const *texpr;
+	struct workbook_cmd_wrap_sort_t cl = {NULL, NULL};
+
+	cl.r = selection_first_range 
+		(sv, GO_CMD_CONTEXT (wbc), _("Wrap SORT"));;
+	cl.wb = wb_control_get_workbook (wbc);
+
+	if (g_slist_length (l) > 1) {
+		go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbc), _("Wrap SORT"),
+			_("A single selection is required."));
+		
+		return;
+	}
+	if (range_height (cl.r) > 1 && range_width (cl.r) > 1) {
+		go_cmd_context_error_invalid 
+			(GO_CMD_CONTEXT (wbc), _("Wrap SORT"),
+			 _("An n\xe2\xa8\xaf""1 or 1\xe2\xa8\xaf"
+			   "n selection is required."));
+		return;
+	}
+	if (range_height (cl.r) == 1 && range_width (cl.r) == 1) {
+		go_cmd_context_error_invalid 
+			(GO_CMD_CONTEXT (wbc), _("Wrap SORT"),
+			 _("There is no point in sorting a single cell."));
+		return;
+	}
+	merges = gnm_sheet_merge_get_overlap (sv->sheet, cl.r);
+	if (merges != NULL) {
+		g_slist_free (merges);
+		go_cmd_context_error_invalid 
+			(GO_CMD_CONTEXT (wbc), _("Wrap SORT"),
+			 _("The range to be sorted may not contain any merged cells."));
+		return;		
+	}
+	fd_sort = gnm_func_lookup_or_add_placeholder ("sort", cl.wb, TRUE);
+	fd_array = gnm_func_lookup_or_add_placeholder ("array", cl.wb, TRUE);
+	
+	sheet_foreach_cell_in_range 
+		(sv->sheet, CELL_ITER_ALL,
+		 cl.r->start.col, cl.r->start.row, 
+		 cl.r->end.col, cl.r->end.row,
+		 (CellIterFunc)&cb_get_cell_content, &cl);
+
+	cl.args = g_slist_reverse (cl.args);
+	expr = gnm_expr_new_funcall (fd_array, cl.args);
+	expr = gnm_expr_new_funcall2 
+		(fd_sort, expr, gnm_expr_new_constant (value_new_int (type))); 
+	texpr = gnm_expr_top_new (expr);
+	cmd_area_set_array_expr (wbc, sv, texpr);
+	gnm_expr_top_unref (texpr);
+}
diff --git a/src/workbook-cmd-format.h b/src/workbook-cmd-format.h
index afd95fe..73ab07b 100644
--- a/src/workbook-cmd-format.h
+++ b/src/workbook-cmd-format.h
@@ -14,6 +14,8 @@ void workbook_cmd_autofit_selection        (WorkbookControl *wbc, Sheet *sheet,
 void workbook_cmd_inc_indent		   (WorkbookControl *wbc);
 void workbook_cmd_dec_indent		   (WorkbookControl *wbc);
 
+void workbook_cmd_wrap_sort		   (WorkbookControl *wbc, int type);
+
 G_END_DECLS
 
 #endif /* _GNM_WORKBOOK_CMD_FORMAT_H_ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]