[gnumeric] ODF: import/export buttons



commit d3490375a3b17819c845fbdb615a65dcb9d14169
Author: Andreas J Guelzow <aguelzow pyrshep ca>
Date:   Sat Sep 11 11:09:16 2010 -0600

    ODF: import/export buttons
    
    2010-09-10  Andreas J. Guelzow <aguelzow pyrshep ca>
    
    	* openoffice-read.c (od_draw_control_start): handle button
    	(odf_form_control): save OOControl for subelements
    	(odf_form_button): new
    	(odf_form_button_end): new
    	(odf_button_event_listener): new
    	(opendoc_content_dtd): connect the above
    	* openoffice-write.c (odf_write_sheet_control_content): assume the
    	  GnmExprTop reference; change all callers
    	(odf_write_sheet_control_linked_cell): ditto
    	(odf_write_sheet_control_button): new
    	(odf_write_sheet_controls): write toggle button

 plugins/openoffice/ChangeLog          |   14 +++++++
 plugins/openoffice/openoffice-read.c  |   65 +++++++++++++++++++++++++++++++--
 plugins/openoffice/openoffice-write.c |   61 +++++++++++++++++++++++++++----
 3 files changed, 129 insertions(+), 11 deletions(-)
---
diff --git a/plugins/openoffice/ChangeLog b/plugins/openoffice/ChangeLog
index c4e8453..28110c7 100644
--- a/plugins/openoffice/ChangeLog
+++ b/plugins/openoffice/ChangeLog
@@ -1,5 +1,19 @@
 2010-09-10  Andreas J. Guelzow <aguelzow pyrshep ca>
 
+	* openoffice-read.c (od_draw_control_start): handle button
+	(odf_form_control): save OOControl for subelements
+	(odf_form_button): new
+	(odf_form_button_end): new
+	(odf_button_event_listener): new
+	(opendoc_content_dtd): connect the above
+	* openoffice-write.c (odf_write_sheet_control_content): assume the
+	  GnmExprTop reference; change all callers
+	(odf_write_sheet_control_linked_cell): ditto
+	(odf_write_sheet_control_button): new
+	(odf_write_sheet_controls): write toggle button
+
+2010-09-10  Andreas J. Guelzow <aguelzow pyrshep ca>
+
 	* openoffice-read.c (od_draw_control_start): handle combobox
 	(odf_form_combobox): new
 	(opendoc_content_dtd): connect odf_form_combobox
diff --git a/plugins/openoffice/openoffice-read.c b/plugins/openoffice/openoffice-read.c
index 02d7d9f..8d2e176 100644
--- a/plugins/openoffice/openoffice-read.c
+++ b/plugins/openoffice/openoffice-read.c
@@ -345,6 +345,7 @@ typedef struct {
 	} page_breaks;
 
 	char const *object_name;
+	OOControl *cur_control;
 
 	OOSettings settings;
 
@@ -4747,6 +4748,9 @@ od_draw_control_start (GsfXMLIn *xin, xmlChar const **attrs)
 				   oc->t == sheet_widget_combo_get_type ()) {
 				so = state->chart.so = g_object_new 
 					(oc->t, NULL);
+			} else if (oc->t == sheet_widget_button_get_type ()) {
+				so = state->chart.so = g_object_new 
+					(oc->t, "text", oc->label, NULL);
 			}
 
 			od_draw_frame_end (xin, NULL);
@@ -4775,6 +4779,9 @@ od_draw_control_start (GsfXMLIn *xin, xmlChar const **attrs)
 						else if (oc->t == sheet_widget_radio_button_get_type ())
 							sheet_widget_radio_button_set_link
 								(so, texpr);
+						else if (oc->t == sheet_widget_button_get_type ())
+							sheet_widget_button_set_link
+								(so, texpr);
 						else if (oc->t == sheet_widget_list_get_type () ||
 							 oc->t == sheet_widget_combo_get_type ()) {
 							gnm_expr_top_ref ((result_texpr = texpr));
@@ -6235,8 +6242,16 @@ odf_form_control (GsfXMLIn *xin, xmlChar const **attrs, GType t)
 		} else
 			oc->t = t;
 		g_hash_table_replace (state->controls, name, oc);
-	} else
+	} else {
 		oo_control_free (oc);
+		oc = NULL;
+	}
+
+	if (t == sheet_widget_button_get_type ())
+		state->cur_control = oc;
+	else
+		state->cur_control = NULL;
+
 }
 
 
@@ -6271,6 +6286,48 @@ odf_form_combobox (GsfXMLIn *xin, xmlChar const **attrs)
 	odf_form_control (xin, attrs, sheet_widget_combo_get_type ());
 }
 
+static void
+odf_form_button (GsfXMLIn *xin, xmlChar const **attrs)
+{
+	odf_form_control (xin, attrs, sheet_widget_button_get_type ());
+}
+
+static void
+odf_form_button_end (GsfXMLIn *xin, G_GNUC_UNUSED GsfXMLBlob *blob)
+{
+	OOParseState *state = (OOParseState *)xin->user_state;
+	state->cur_control = NULL;
+}
+
+static void
+odf_button_event_listener (GsfXMLIn *xin, xmlChar const **attrs)
+{
+	OOParseState *state = (OOParseState *)xin->user_state;
+	char const *event_name = NULL;
+	char const *language = NULL;
+	char const *macro_name = NULL;
+
+	if (state->cur_control == NULL)
+		return;
+
+	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
+		if (gsf_xml_in_namecmp (xin, CXML2C (attrs[0]), 
+					OO_NS_SCRIPT, "event-name"))
+			event_name = CXML2C (attrs[1]);
+		else if (gsf_xml_in_namecmp (xin, CXML2C (attrs[0]), 
+					OO_NS_SCRIPT, "language"))
+			language = CXML2C (attrs[1]);
+		else if (gsf_xml_in_namecmp (xin, CXML2C (attrs[0]), 
+					OO_NS_SCRIPT, "macro-name"))
+			macro_name = CXML2C (attrs[1]);
+
+	if (0 == strcmp (event_name, "dom:mousedown") &&
+	    0 == strcmp (language, "gnm:short-macro") &&
+	    g_str_has_prefix (macro_name, "set-to-TRUE:"))
+		state->cur_control->linked_cell = g_strdup (macro_name + 12);
+}
+
+
 /****************************************************************************/
 /******************************** settings.xml ******************************/
 
@@ -6867,10 +6924,10 @@ static GsfXMLInNode const opendoc_content_dtd [] =
 	          GSF_XML_IN_NODE (FORM, FORM_PROPERTIES, OO_NS_FORM, "properties", GSF_XML_NO_CONTENT, NULL, NULL),
 	            GSF_XML_IN_NODE (FORM_PROPERTIES, FORM_PROPERTY, OO_NS_FORM, "property", GSF_XML_NO_CONTENT, NULL, NULL),
 	              GSF_XML_IN_NODE (FORM_PROPERTIES, FORM_LIST_PROPERTY, OO_NS_FORM, "list-property", GSF_XML_NO_CONTENT, NULL, NULL),
-	          GSF_XML_IN_NODE (FORM, FORM_BUTTON, OO_NS_FORM, "button", GSF_XML_NO_CONTENT, NULL, NULL),
+	          GSF_XML_IN_NODE (FORM, FORM_BUTTON, OO_NS_FORM, "button", GSF_XML_NO_CONTENT, &odf_form_button, &odf_form_button_end),
 	            GSF_XML_IN_NODE (FORM_BUTTON, FORM_PROPERTIES, OO_NS_FORM, "properties", GSF_XML_NO_CONTENT, NULL, NULL),			/* 2nd Def */
-	            GSF_XML_IN_NODE (FORM_BUTTON, FORM_EVENT_LISTENERS, OO_NS_OFFICE, "event-listeners", GSF_XML_NO_CONTENT, NULL, NULL),
-	              GSF_XML_IN_NODE (FORM_EVENT_LISTENERS, SCRIPT_LISTENER, OO_NS_SCRIPT, "event-listener", GSF_XML_NO_CONTENT, NULL, NULL),
+	            GSF_XML_IN_NODE (FORM_BUTTON, BUTTON_OFFICE_EVENT_LISTENERS, OO_NS_OFFICE, "event-listeners", GSF_XML_NO_CONTENT, NULL, NULL),
+	              GSF_XML_IN_NODE (BUTTON_OFFICE_EVENT_LISTENERS, BUTTON_EVENT_LISTENER, OO_NS_SCRIPT, "event-listener", GSF_XML_NO_CONTENT, &odf_button_event_listener, NULL),
 	          GSF_XML_IN_NODE (FORM, FORM_VALUE_RANGE, OO_NS_FORM, "value-range", GSF_XML_NO_CONTENT, &odf_form_value_range, NULL),
 	            GSF_XML_IN_NODE (FORM_VALUE_RANGE, FORM_PROPERTIES, OO_NS_FORM, "properties", GSF_XML_NO_CONTENT, NULL, NULL),			/* 2nd Def */
 	          GSF_XML_IN_NODE (FORM, FORM_CHECKBOX, OO_NS_FORM, "checkbox", GSF_XML_NO_CONTENT, &odf_form_checkbox, NULL),
diff --git a/plugins/openoffice/openoffice-write.c b/plugins/openoffice/openoffice-write.c
index da5bc6c..94b0fb8 100644
--- a/plugins/openoffice/openoffice-write.c
+++ b/plugins/openoffice/openoffice-write.c
@@ -93,6 +93,7 @@
 #define XLINK	 "xlink:"
 #define CONFIG   "config:"
 #define FORM     "form:"
+#define SCRIPT   "script:"
 #define OOO      "ooo:"
 #define XML      "xml:"
 #define GNMSTYLE "gnm:"  /* We use this for attributes and elements not supported by ODF */
@@ -3102,6 +3103,7 @@ odf_write_sheet_control_content (GnmOOExport *state, GnmExprTop const *texpr)
 					      GNMSTYLE "source-cell-range", 
 					      odf_strip_brackets (link));
 		g_free (link);
+		gnm_expr_top_unref (texpr);
 	}
 }
 
@@ -3122,6 +3124,7 @@ odf_write_sheet_control_linked_cell (GnmOOExport *state, GnmExprTop const *texpr
 			gsf_xml_out_add_cstr (state->xml, GNMSTYLE "linked-cell", 
 					      odf_strip_brackets (link));
 		g_free (link);
+		gnm_expr_top_unref (texpr);
 	}
 }
 
@@ -3168,8 +3171,6 @@ odf_write_sheet_control_scrollbar (GnmOOExport *state, SheetObject *so,
 /* 			      OOO "com.sun.star.form.component.ScrollBar"); */
 
 	odf_write_sheet_control_linked_cell (state, texpr);
-	gnm_expr_top_unref (texpr);
-	
 	gsf_xml_out_end_element (state->xml); /* form:value-range */
 }
 
@@ -3186,7 +3187,6 @@ odf_write_sheet_control_checkbox (GnmOOExport *state, SheetObject *so)
 	gsf_xml_out_add_cstr (state->xml, FORM "label", label);
 
 	odf_write_sheet_control_linked_cell (state, texpr);
-	gnm_expr_top_unref (texpr);
 	
 	gsf_xml_out_end_element (state->xml); /* form:checkbox */
 
@@ -3202,11 +3202,9 @@ odf_write_sheet_control_list (GnmOOExport *state, SheetObject *so,
 	odf_sheet_control_start_element (state, so, element);
 
 	odf_write_sheet_control_linked_cell (state, texpr);
-	gnm_expr_top_unref (texpr);
 
 	texpr = sheet_widget_list_base_get_content_link (so);
 	odf_write_sheet_control_content (state, texpr);
-	gnm_expr_top_unref (texpr);
 
 	if (get_gsf_odf_version () > 101)
 		gsf_xml_out_add_cstr_unchecked 
@@ -3274,14 +3272,61 @@ odf_write_sheet_control_radio_button (GnmOOExport *state, SheetObject *so)
 	}
 
 	odf_write_sheet_control_linked_cell (state, texpr);
-	gnm_expr_top_unref (texpr);
-	
+
 	gsf_xml_out_end_element (state->xml); /* form:checkbox */
 
 	g_free (label);
 }
 
+static void
+odf_write_sheet_control_button (GnmOOExport *state, SheetObject *so)
+{
+	GnmExprTop const *texpr = sheet_widget_button_get_link (so);
+	char *label = NULL;
+	
+	g_object_get (G_OBJECT (so), "text", &label, NULL);
+
+	odf_sheet_control_start_element (state, so, FORM "button");
+	gsf_xml_out_add_cstr (state->xml, FORM "label", label);
+	gsf_xml_out_add_cstr_unchecked (state->xml, FORM "button-type", "push");
+
+	if (texpr != NULL ) {
+		char *link = NULL, *name = NULL;
+		GnmParsePos pp;
+
+		parse_pos_init_sheet (&pp, state->sheet);
+		link = gnm_expr_top_as_string (texpr, &pp, state->conv);
+
+		gsf_xml_out_start_element (state->xml, OFFICE "event-listeners");
 
+		gsf_xml_out_start_element (state->xml, SCRIPT "event-listener");
+		gsf_xml_out_add_cstr_unchecked (state->xml, SCRIPT "event-name",
+						"dom:mousedown");
+		gsf_xml_out_add_cstr_unchecked (state->xml, SCRIPT "language",
+						GNMSTYLE "short-macro");
+		name = g_strdup_printf ("set-to-TRUE:%s", odf_strip_brackets (link));
+		gsf_xml_out_add_cstr (state->xml, SCRIPT "macro-name", name);
+		g_free (name);
+		gsf_xml_out_end_element (state->xml); /* script:event-listener */
+
+		gsf_xml_out_start_element (state->xml, SCRIPT "event-listener");
+		gsf_xml_out_add_cstr_unchecked (state->xml, SCRIPT "event-name",
+						"dom:mouseup");
+		gsf_xml_out_add_cstr_unchecked (state->xml, SCRIPT "language",
+						GNMSTYLE "short-macro");
+		name = g_strdup_printf ("set-to-FALSE:%s", odf_strip_brackets (link));
+		gsf_xml_out_add_cstr (state->xml, SCRIPT "macro-name", name);
+		g_free (name);
+		gsf_xml_out_end_element (state->xml); /* script:event-listener */
+
+		gsf_xml_out_end_element (state->xml); /* office:event-listeners */
+
+		g_free (link);
+		gnm_expr_top_unref (texpr);
+
+	}
+	gsf_xml_out_end_element (state->xml); /* form:checkbox */
+}
 
 static void
 odf_write_sheet_controls (GnmOOExport *state)
@@ -3316,6 +3361,8 @@ odf_write_sheet_controls (GnmOOExport *state)
 		else if (GNM_IS_SOW_COMBO (so))
 			odf_write_sheet_control_list (state, so,
 						      FORM "combobox");
+		else if (GNM_IS_SOW_BUTTON (so))
+			odf_write_sheet_control_button (state, so);
 	}
 
 	gsf_xml_out_end_element (state->xml); /* form:form */



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