[gnumeric] Docs: handle evaluated examples.



commit f93d4aaf7cd33bc7dca90a716448b75233267b62
Author: Morten Welinder <terra gnome org>
Date:   Mon Jun 29 11:05:18 2009 -0400

    Docs: handle evaluated examples.

 plugins/numtheory/numtheory.c        |    4 +-
 src/dialogs/ChangeLog                |    6 +++
 src/dialogs/dialog-function-select.c |   73 ++++++++++++++++++++++++++++++----
 src/func.h                           |   43 ++++++++++++++++----
 4 files changed, 107 insertions(+), 19 deletions(-)
---
diff --git a/plugins/numtheory/numtheory.c b/plugins/numtheory/numtheory.c
index c554393..1208e24 100644
--- a/plugins/numtheory/numtheory.c
+++ b/plugins/numtheory/numtheory.c
@@ -442,7 +442,7 @@ static GnmFuncHelp const help_pfactor[] = {
 	{ GNM_FUNC_HELP_ARG, F_("n:positive integer")},
 	{ GNM_FUNC_HELP_DESCRIPTION, F_("PFACTOR finds the smallest prime factor of its argument.")},
 	{ GNM_FUNC_HELP_NOTE, F_("The argument @{n} must be at least 2. Otherwise a #VALUE! error is returned.") },
-	{ GNM_FUNC_HELP_EXAMPLES, F_("pfactor(57)") },
+	{ GNM_FUNC_HELP_EXAMPLES, "=PFACTOR(57)" },
 	{ GNM_FUNC_HELP_SEEALSO, "ITHPRIME"},
 	{ GNM_FUNC_HELP_END }
 };
@@ -472,7 +472,7 @@ static GnmFuncHelp const help_nt_pi[] = {
 	{ GNM_FUNC_HELP_NAME, F_("NT_PI:number of primes upto @{n}")},
 	{ GNM_FUNC_HELP_ARG, F_("n:positive integer")},
 	{ GNM_FUNC_HELP_DESCRIPTION, F_("NT_PI returns the number of primes less than or equal to @{n}.")},
-	{ GNM_FUNC_HELP_EXAMPLES, "=NT_PI{11}" },
+	{ GNM_FUNC_HELP_EXAMPLES, "=NT_PI(11)" },
 	{ GNM_FUNC_HELP_SEEALSO, "ITHPRIME,NT_PHI,NT_D,NT_SIGMA"},
 	{ GNM_FUNC_HELP_EXTREF, F_("wolfram:PrimeCountingFunction.html") },
 	{ GNM_FUNC_HELP_END }
diff --git a/src/dialogs/ChangeLog b/src/dialogs/ChangeLog
index ceec5a4..5bcdd7c 100644
--- a/src/dialogs/ChangeLog
+++ b/src/dialogs/ChangeLog
@@ -1,3 +1,9 @@
+2009-06-29  Morten Welinder  <terra gnome org>
+
+	* dialog-function-select.c (make_expr_example): New function.
+	(describe_new_style): Call make_expr_example when we see an
+	expression.
+
 2009-06-28 Andreas J. Guelzow <aguelzow pyrshep ca>
 
 	* function-select.glade: add left and right margins to description
diff --git a/src/dialogs/dialog-function-select.c b/src/dialogs/dialog-function-select.c
index f80b1a2..ce19759 100644
--- a/src/dialogs/dialog-function-select.c
+++ b/src/dialogs/dialog-function-select.c
@@ -34,6 +34,10 @@
 #include <workbook.h>
 #include <wbc-gtk.h>
 #include <application.h>
+#include <position.h>
+#include <expr.h>
+#include <value.h>
+#include <sheet.h>
 #include <gnumeric-gconf.h>
 
 #include <gsf/gsf-impl-utils.h>
@@ -49,7 +53,8 @@
 
 typedef struct {
 	WBCGtk  *wbcg;
-	Workbook  *wb;
+	Workbook *wb;
+	Sheet *sheet;
 
 	GladeXML  *gui;
 	GtkWidget *dialog;
@@ -398,6 +403,50 @@ cb_link_event (GtkTextTag *link, GObject *trigger,
 	return FALSE;
 }
 
+static char *
+make_expr_example (Sheet *sheet, const char *text, gboolean localized)
+{
+	GnmLocale *oldlocale = NULL;
+	GnmExprTop const *texpr;
+	char *res;
+	GnmParsePos pp;
+	GnmEvalPos ep;
+	GnmConventions const *convs = gnm_conventions_default;
+
+	eval_pos_init_sheet (&ep, sheet);
+	parse_pos_init_evalpos (&pp, &ep);
+
+	if (!localized)
+		oldlocale = gnm_push_C_locale ();
+	texpr = gnm_expr_parse_str (text, &pp,
+				    GNM_EXPR_PARSE_DEFAULT,
+				    convs,
+				    NULL);
+	if (!localized)
+		gnm_pop_C_locale (oldlocale);
+
+	if (texpr) {
+		char *etxt = gnm_expr_top_as_string (texpr, &pp, convs);
+		GnmValue *val = gnm_expr_top_eval (texpr, &ep, 0);
+		GnmExprTop const *texpr_res = gnm_expr_top_new_constant (val);
+		char *vtxt = gnm_expr_top_as_string (texpr_res, &pp, convs);
+
+		gnm_expr_top_unref (texpr);
+		gnm_expr_top_unref (texpr_res);
+		
+		res = g_strdup_printf (_("%s evaluates to %s."), etxt, vtxt);
+
+		g_free (etxt);
+		g_free (vtxt);
+	} else {
+		g_warning ("Failed to parse [%s]", text);
+		res = g_strdup ("");
+	}
+
+	return res;
+}
+
+
 
 #define ADD_LTEXT(text,len) gtk_text_buffer_insert (description, &ti, (text), (len))
 #define ADD_TEXT(text) ADD_LTEXT((text),-1)
@@ -408,7 +457,7 @@ cb_link_event (GtkTextTag *link, GObject *trigger,
 			if (at != NULL) { ADD_BOLD_TEXT(t, at - t); t = at + 1; } else {ADD_TEXT (t); break;}}} 
 
 static void
-describe_new_style (GtkTextBuffer *description, GnmFunc const *func)
+describe_new_style (GtkTextBuffer *description, GnmFunc const *func, Sheet *sheet)
 {
 	GnmFuncHelp const *help;
 	GtkTextIter ti;
@@ -470,7 +519,8 @@ describe_new_style (GtkTextBuffer *description, GnmFunc const *func)
 			break;
 		}
 		case GNM_FUNC_HELP_EXAMPLES: {
-			const char *text = help->text;
+			const char *text = F_(help->text);
+			gboolean was_translated = (text != help->text);
 
 			if (!seen_examples) {
 				seen_examples = TRUE;
@@ -479,7 +529,13 @@ describe_new_style (GtkTextBuffer *description, GnmFunc const *func)
 				ADD_TEXT ("\n");
 			}
 
-			ADD_TEXT_WITH_ARGS (text);
+			if (text[0] == '=') {
+				char *example = make_expr_example (sheet, text + 1, was_translated);
+				ADD_TEXT (example);
+				g_free (example);
+			} else {
+				ADD_TEXT_WITH_ARGS (text);
+			}
 			ADD_TEXT ("\n");
 			break;
 		}
@@ -735,7 +791,7 @@ cb_description_clicked (GtkTextBuffer *textbuffer,
 }
 
 static void
-cb_dialog_function_select_fun_selection_changed (GtkTreeSelection *the_selection,
+cb_dialog_function_select_fun_selection_changed (GtkTreeSelection *selection,
 						 FunctionSelectState *state)
 {
 	GtkTreeIter  iter;
@@ -751,7 +807,7 @@ cb_dialog_function_select_fun_selection_changed (GtkTreeSelection *the_selection
 				      0.1, TRUE, 0.0, 0.0);
 	gtk_text_buffer_set_text (description, "", 0);
 
-	if (gtk_tree_selection_get_selected (the_selection, &model, &iter)) {
+	if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
 		gtk_tree_model_get (model, &iter,
 				    FUNCTION, &func,
 				    -1);
@@ -763,7 +819,7 @@ cb_dialog_function_select_fun_selection_changed (GtkTreeSelection *the_selection
 		else if (func->help[0].type == GNM_FUNC_HELP_OLD)
 			describe_old_style (description, func);
 		else
-			describe_new_style (description, func);
+			describe_new_style (description, func, state->sheet);
 
 		gtk_widget_set_sensitive (state->ok_button, TRUE);
 	} else {
@@ -940,7 +996,8 @@ dialog_function_select (WBCGtk *wbcg, char const *key)
 
 	state = g_new (FunctionSelectState, 1);
 	state->wbcg  = wbcg;
-	state->wb    = wb_control_get_workbook (WORKBOOK_CONTROL (wbcg));
+	state->sheet = wb_control_cur_sheet (WORKBOOK_CONTROL (wbcg));
+	state->wb    = state->sheet->workbook;
         state->gui   = gui;
         state->dialog = glade_xml_get_widget (state->gui, "selection_dialog");
 	state->formula_guru_key = key;
diff --git a/src/func.h b/src/func.h
index 1714f1b..6170b79 100644
--- a/src/func.h
+++ b/src/func.h
@@ -136,15 +136,40 @@ typedef enum {
 	GNM_FUNC_HELP_END,		/* Format */
 					/* ------ */
 	GNM_FUNC_HELP_OLD,		/* old token based format */
-	GNM_FUNC_HELP_NAME,		/* <NAME>:<1 SENTENCE DESCRIPTION>	(translated) */
-	GNM_FUNC_HELP_ARG,		/* <NAME>:<1 SENTENCE DESCRIPTION>	(translated) */
-	GNM_FUNC_HELP_DESCRIPTION,	/* <LONG DESCRIPTION (reference args using @{arg})>		(translated) */
-	GNM_FUNC_HELP_NOTE,		/* <SPECIAL CASES (reference args using @{arg})>		(translated) */
-	GNM_FUNC_HELP_EXAMPLES,		/* <TEXT and EXAMPLES ?? get a hook to enter the sample ?? >	(translated) */
-	GNM_FUNC_HELP_SEEALSO,		/* name,name,name ...			(not translated) */
-	GNM_FUNC_HELP_EXTREF,           /* wolfram:Sine.html wiki:en:Trigonometric_functions */
-	GNM_FUNC_HELP_EXCEL,             /* <SPECIAL NOTE RE EXCEL (reference args using @{arg})>       	(translated) */
-	GNM_FUNC_HELP_ODF               /* <SPECIAL NOTE RE ODF (reference args using @{arg})>       	(translated) */
+
+	GNM_FUNC_HELP_NAME,
+	/* <NAME>:<1 SENTENCE DESCRIPTION> (translated) */
+
+	GNM_FUNC_HELP_ARG,
+	/* <NAME>:<1 SENTENCE DESCRIPTION> (translated) */
+
+	GNM_FUNC_HELP_DESCRIPTION,
+	/* <LONG DESCRIPTION (reference args using @{arg})> (translated) */
+
+	GNM_FUNC_HELP_NOTE,
+	/* <SPECIAL CASES (reference args using @{arg})> (translated) */
+
+	GNM_FUNC_HELP_EXAMPLES,
+	/*
+	 * Either translated text, or a formula that is only marked for
+	 * translation if it contains strings that need to be translated.
+	 */
+
+	GNM_FUNC_HELP_SEEALSO,
+	/* name,name,name ...	(not translated) */
+
+	GNM_FUNC_HELP_EXTREF,
+	/*
+	 * Link to external descriptions.  The following styles defined:
+	 * wolfram:Sine.html
+	 * wiki:en:Trigonometric_functions
+	 */
+
+	GNM_FUNC_HELP_EXCEL,
+	/* <SPECIAL NOTE RE EXCEL (reference args using @{arg})> (translated) */
+
+	GNM_FUNC_HELP_ODF
+	/* <SPECIAL NOTE RE ODF (reference args using @{arg})> (translated) */
 } GnmFuncHelpType;
 typedef struct {
     GnmFuncHelpType	 type;



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