Exemple #1
0
/**
 * stf_dialog_result_attach_formats_to_cr
 * @dialogresult: a dialogresult struct
 * @cr: a cell region
 *
 * Attach the formats of the dialogresult to the given cell region.
 *
 * returns: nothing
 **/
void
stf_dialog_result_attach_formats_to_cr (DialogStfResult_t *dialogresult,
					GnmCellRegion *cr)
{
	unsigned int col, targetcol;

	g_return_if_fail (dialogresult != NULL);
	g_return_if_fail (cr != NULL);

	targetcol = 0;
	for (col = 0; col < dialogresult->parseoptions->formats->len; col++) {
		if (dialogresult->parseoptions->col_import_array[col]) {
			GOFormat *sf = g_ptr_array_index
				(dialogresult->parseoptions->formats, col);
			GnmStyleRegion *sr = g_new (GnmStyleRegion, 1);

			sr->range.start.col = targetcol;
			sr->range.start.row = 0;
			sr->range.end.col   = targetcol;
			sr->range.end.row   = dialogresult->rowcount - 1;
			sr->style = gnm_style_new_default ();
			gnm_style_set_format (sr->style, sf);
			targetcol++;

			cr->styles = g_slist_prepend (cr->styles, sr);
		}
	}
}
Exemple #2
0
static gboolean
sylk_rtd_f_parse (SylkReader *state, char *str)
{
	GnmStyle *style = NULL;
	char *next;
	int   tmp, size = -1;
	gboolean is_default_style = FALSE;
	int full_col = -1, full_row = -1;

	for (; *str != '\0' ; str = next) {
		next = sylk_next_token (str);
		switch (*str) {
		case 'D': /* Default sheet style
			     ;D<fmt-id><digits><alignment><num_cols??> default Format. */
			is_default_style = TRUE;

		case 'F': { /* Cell Format
			     ;F<fmt-id><digits><alignment>: Single cell format. */

			 /* Format:
				D - default
				C - currency (extended)
				E - exponent
				F - fixed
				G - general,
				$ - dollar
				* - graph
				% - percent

			   Alignment: DCGLR-X
				*/
			char ch1, alignment;
			if (3 == sscanf (str+1, "%c%d%c", &ch1, &tmp, &alignment)) {
				int a = -1;
				switch (alignment) {
				case 'S' : /* standard ? how does this differ from default */
				case 'D' : a = GNM_HALIGN_GENERAL; break;
				case 'L' : a = GNM_HALIGN_LEFT; break;
				case 'R' : a = GNM_HALIGN_RIGHT; break;
				case 'C' : a = GNM_HALIGN_CENTER; break;
				case 'X' : a = GNM_HALIGN_FILL; break;
				default :
					   break;
				}
				if (a >= 0) {
					if (style == NULL) style = gnm_style_new ();
					gnm_style_set_align_h (style, a);
				}
			}
			break;
		}

		/**************************************************/
		/* Globals */
		case 'E':
			state->pp.sheet->display_formulas = TRUE;
			break;
		case 'G':
			state->pp.sheet->hide_grid = TRUE;
			break;
		case 'H':
			state->pp.sheet->hide_col_header = TRUE;
			state->pp.sheet->hide_row_header = TRUE;
			break;
		case 'K': /* show commas ?? */
			break;
		case 'Z': /* hide zeros */
			state->pp.sheet->hide_zero = TRUE;
			break;
		/**************************************************/

		case 'M' : /* row or col size * 20 or num cols for global */
			sylk_parse_int (str+1, &size);
			break;

		case 'N' : { /* global font */
			int size;
			if (2 == sscanf (str+1, "%d %d", &tmp, &size) &&
			    1 <= tmp && tmp <= (int)state->fonts->len) {
				GnmStyle const *font =
					g_ptr_array_index (state->fonts, tmp-1);
				if (style == NULL) style = gnm_style_new ();
				is_default_style = TRUE;
				gnm_style_merge_element (style, font,
					MSTYLE_FONT_NAME);
				gnm_style_merge_element (style, font,
					MSTYLE_FONT_SIZE);
				/* It looks like the size from the id dominates
				 * this size */
			}
			break;
		}

		case 'P':
			if (sylk_parse_int (str+1, &tmp) &&
			    0 <= tmp && tmp < (int)state->formats->len) {
				if (style == NULL) style = gnm_style_new ();
				gnm_style_set_format (style,
					g_ptr_array_index (state->formats, tmp));
			}
			break;

		case 'S':
			for (str++ ; *str && *str != ';' ; str++) {
				switch (*str) {
				case 'I':
					if (style == NULL) style = gnm_style_new ();
					gnm_style_set_font_italic (style, TRUE);
					break;

				case 'D':
					if (style == NULL) style = gnm_style_new ();
					gnm_style_set_font_bold (style, TRUE);
					break;

				case 'M':
					if (sylk_parse_int (str+1, &tmp) &&
					    1 <= tmp && tmp <= (int)state->fonts->len) {
						GnmStyle const *font =
							g_ptr_array_index (state->fonts, tmp-1);
						if (style == NULL) style = gnm_style_new ();
						gnm_style_merge_element (style, font,
									 MSTYLE_FONT_NAME);
						gnm_style_merge_element (style, font,
									 MSTYLE_FONT_SIZE);
					}
					str = (char *)" ";
					break;

				case 'S': /* seems to stipple things */
					if (style == NULL) style = gnm_style_new ();
					gnm_style_set_pattern (style, 5);
					break;

				case 'T': style = sylk_set_border (style, MSTYLE_BORDER_TOP); break;
				case 'B': style = sylk_set_border (style, MSTYLE_BORDER_BOTTOM); break;
				case 'L': style = sylk_set_border (style, MSTYLE_BORDER_LEFT); break;
				case 'R': style = sylk_set_border (style, MSTYLE_BORDER_RIGHT); break;

				default:
					sylk_read_warning (state, "unhandled style S%c.", *str);
				}
			}
			break;

		case 'W': {
			int first, last, width;
			if (3 == sscanf (str+1, "%d %d %d", &first, &last, &width)) {
				/* width seems to be in characters */
				if (first <= last &&
				    first < gnm_sheet_get_max_cols (state->pp.sheet) &&
				    last < gnm_sheet_get_max_cols (state->pp.sheet))
					while (first <= last)
						sheet_col_set_size_pts (state->pp.sheet,
							first++ - 1, width*7.45, TRUE);
			}
			break;
		}

		case 'C': if (sylk_parse_int (str+1, &tmp)) full_col = tmp - 1; break;
		case 'R': if (sylk_parse_int (str+1, &tmp)) full_row = tmp - 1; break;
		case 'X': if (sylk_parse_int (str+1, &tmp)) state->pp.eval.col = tmp - 1; break;
		case 'Y': if (sylk_parse_int (str+1, &tmp)) state->pp.eval.row = tmp - 1; break;
		default:
			sylk_read_warning (state, "unhandled F option %c.", *str);
		}
	}

	if (full_col >= 0) {
		if (NULL != style)
			sheet_style_apply_col (state->pp.sheet, full_col, style);
		if (size > 0)
			sheet_col_set_size_pts (state->pp.sheet,
						full_col, size / 20.0, FALSE);
	} else if (full_row >= 0) {
		if (NULL != style)
			sheet_style_apply_row (state->pp.sheet, full_row, style);
		if (size > 0)
			sheet_row_set_size_pts (state->pp.sheet,
						full_row, size / 20.0, FALSE);
	} else if (NULL != style) {
		if (is_default_style) {
			GnmRange r;
			range_init_full_sheet (&r, state->pp.sheet);
			sheet_style_apply_range (state->pp.sheet, &r, style);
		} else
			sheet_style_apply_pos (state->pp.sheet,
				state->pp.eval.col, state->pp.eval.row, style);
	}

	return TRUE;
}
Exemple #3
0
/**
 * wbcg_edit_start:
 * @wbcg:       The workbook to be edited.
 * @blankp:   If true, erase current cell contents first.  If false, leave the
 *            contents alone.
 * @cursorp:  If true, create an editing cursor in the current sheet.  (If
 *            false, the text will be editing in the edit box above the sheet,
 *            but this is not handled by this function.)
 *
 * Initiate editing of a cell in the sheet.  Note that we have two modes of
 * editing:
 *  1) in-cell editing when you just start typing, and
 *  2) above sheet editing when you hit F2.
 *
 * Returns TRUE if we did indeed start editing.  Returns FALSE if the
 * cell-to-be-edited was locked.
 */
gboolean
wbcg_edit_start (WBCGtk *wbcg,
		 gboolean blankp, gboolean cursorp)
{
	/* We could save this, but the situation is rare, if confusing.  */
	static gboolean warn_on_text_format = TRUE;
	SheetView *sv;
	SheetControlGUI *scg;
	GnmCell *cell;
	char *text = NULL;
	int col, row;
	WorkbookView *wbv;
	int cursor_pos = -1;

	g_return_val_if_fail (GNM_IS_WBC_GTK (wbcg), FALSE);

	if (wbcg_is_editing (wbcg))
		return TRUE;

	/* Avoid recursion, and do not begin editing if a guru is up */
	if (wbcg->inside_editing || wbc_gtk_get_guru (wbcg) != NULL)
		return TRUE;
	wbcg->inside_editing = TRUE;

	wbv = wb_control_view (GNM_WBC (wbcg));
	sv = wb_control_cur_sheet_view (GNM_WBC (wbcg));
	scg = wbcg_cur_scg (wbcg);

	col = sv->edit_pos.col;
	row = sv->edit_pos.row;

	/* don't edit a locked cell */
	/* TODO : extend this to disable edits that cannot succeed
	 * like editing a single cell of an array.  I think we have enough
	 * information if we look at the selection.
	 */
	if (wb_view_is_protected (wbv, TRUE) &&
	    gnm_style_get_contents_locked (sheet_style_get (sv->sheet, col, row))) {
		char *pos =  g_strdup_printf ( _("%s!%s is locked"),
			sv->sheet->name_quoted, cell_coord_name (col, row));
		go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbcg), pos,
			wb_view_is_protected (wbv, FALSE)
			 ? _("Unprotect the workbook to enable editing.")
			 : _("Unprotect the sheet to enable editing."));
		wbcg->inside_editing = FALSE;
		g_free (pos);
		return FALSE;
	}

	cell = sheet_cell_get (sv->sheet, col, row);
	if (cell &&
	    warn_on_text_format &&
	    go_format_is_text (gnm_cell_get_format (cell)) &&
	    (gnm_cell_has_expr (cell) || !VALUE_IS_STRING (cell->value))) {
		gint res; /* Using GtkResponseType would yield a warning on the switch */
		GtkWidget *check;
		GtkWidget *align;

		GtkWidget *d = gnm_message_dialog_create
			(wbcg_toplevel (wbcg),
			 GTK_DIALOG_DESTROY_WITH_PARENT,
			 GTK_MESSAGE_WARNING,
			 _("You are about to edit a cell with \"text\" format."),
			 _("The cell does not currently contain text, though, so if "
			   "you go on editing then the contents will be turned into "
			   "text."));
		gtk_dialog_add_button (GTK_DIALOG (d), GTK_STOCK_EDIT, GTK_RESPONSE_OK);
		go_gtk_dialog_add_button
			(GTK_DIALOG (d), _("Remove format"), GTK_STOCK_REMOVE,
			 GNM_RESPONSE_REMOVE);
		gtk_dialog_add_button (GTK_DIALOG (d), GNM_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
		gtk_dialog_set_default_response (GTK_DIALOG (d), GTK_RESPONSE_CANCEL);

		check = gtk_check_button_new_with_label (_("Show this dialog next time."));
		g_signal_connect (check, "toggled", G_CALLBACK (cb_warn_toggled), &warn_on_text_format);
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE);
		align = gtk_alignment_new (0.5, 0.5, 0, 0);
		gtk_container_add (GTK_CONTAINER (align), check);
		gtk_widget_show_all (align);
		gtk_box_pack_end (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (d))), align, TRUE, TRUE, 0);
		res = go_gtk_dialog_run (GTK_DIALOG (d), wbcg_toplevel (wbcg));

		switch (res) {
		case GNM_RESPONSE_REMOVE: {
			GnmStyle *style = gnm_style_new ();
			gnm_style_set_format (style, go_format_general ());
			if (!cmd_selection_format (GNM_WBC (wbcg),
						   style, NULL, NULL))
				break;
			/* Fall through.  */
		}
		default:
		case GTK_RESPONSE_CANCEL:
			wbcg->inside_editing = FALSE;
			return FALSE;
		case GTK_RESPONSE_OK:
			break;
		}
	}

	gnm_app_clipboard_unant ();

	if (blankp)
		gtk_entry_set_text (wbcg_get_entry (wbcg), "");
	else if (cell != NULL) {
		gboolean quoted = FALSE;

		text = gnm_cell_get_text_for_editing (cell, sv->sheet, &quoted, &cursor_pos);

		if (text)
			gtk_entry_set_text (wbcg_get_entry (wbcg), text);

		if (cell->value != NULL) {
			GOFormat const *fmt = VALUE_FMT (cell->value);
			if (fmt != NULL && go_format_is_markup (fmt)) {
				PangoAttrList *markup =
					pango_attr_list_copy ((PangoAttrList *)go_format_get_markup (fmt));
				if (quoted)
					go_pango_attr_list_open_hole (markup, 0, 1);
				wbcg_edit_init_markup (wbcg, markup);
			}
		}
	}

	gnm_expr_entry_set_scg (wbcg->edit_line.entry, scg);
	gnm_expr_entry_set_flags (wbcg->edit_line.entry,
		GNM_EE_SHEET_OPTIONAL | GNM_EE_FORMULA_ONLY,
		GNM_EE_SINGLE_RANGE | GNM_EE_SHEET_OPTIONAL | GNM_EE_FORMULA_ONLY | GNM_EE_FORCE_REL_REF | GNM_EE_FORCE_ABS_REF);
	scg_edit_start (scg);

	/* Redraw the cell contents in case there was a span */
	sheet_redraw_region (sv->sheet, col, row, col, row);

	if (cursorp && /* autocompletion code will not work in the edit line */
	    wbv->do_auto_completion &&
	    (text == NULL || g_unichar_isalpha (g_utf8_get_char (text)))) {
		wbcg->auto_complete = gnm_complete_sheet_new (
			sv->sheet, col, row,
			workbook_edit_complete_notify, wbcg);
		wbcg->auto_completing = TRUE;
		wbcg->auto_max_size = 0;
	} else
		wbcg->auto_complete = NULL;

	/* Give the focus to the edit line */
	if (!cursorp)
		gtk_window_set_focus (wbcg_toplevel (wbcg),
			(GtkWidget *) wbcg_get_entry (wbcg));

	wbcg->editing = TRUE;
	wbcg->editing_sheet = sv->sheet;
	wbcg->editing_cell = cell;

	/* If this assert fails, it means editing was not shut down
	 * properly before
	 */
	g_return_val_if_fail (wbcg->edit_line.signal_changed == 0, TRUE);
	wbcg->edit_line.signal_changed = g_signal_connect (
		G_OBJECT (wbcg_get_entry (wbcg)),
		"changed",
		G_CALLBACK (cb_entry_changed), wbcg);
	wbcg->edit_line.signal_insert = g_signal_connect (
		G_OBJECT (wbcg_get_entry (wbcg)),
		"insert-text",
		G_CALLBACK (cb_entry_insert_text), wbcg);
	wbcg->edit_line.signal_delete = g_signal_connect (
		G_OBJECT (wbcg_get_entry (wbcg)),
		"delete-text",
		G_CALLBACK (cb_entry_delete_text), wbcg);
	wbcg->edit_line.signal_cursor_pos = g_signal_connect_swapped (
		G_OBJECT (wbcg_get_entry (wbcg)),
		"notify::cursor-position",
		G_CALLBACK (cb_entry_cursor_pos), wbcg);
	wbcg->edit_line.signal_selection_bound = g_signal_connect_swapped (
		G_OBJECT (wbcg_get_entry (wbcg)),
		"notify::selection-bound",
		G_CALLBACK (cb_entry_cursor_pos), wbcg);

	g_free (text);
	wb_control_update_action_sensitivity (GNM_WBC (wbcg));

	wbcg->inside_editing = FALSE;

	gtk_editable_set_position (GTK_EDITABLE (wbcg_get_entry (wbcg)), cursor_pos);

	return TRUE;
}