예제 #1
0
/**
 * validation_new :
 * @title : will be copied.
 * @msg   : will be copied.
 * @texpr0 : absorb the reference to the expression (optionally %NULL).
 * @texpr1 : absorb the reference to the expression (optionally %NULL).
 *
 * Does _NOT_ require all necessary information to be set here.
 * validation_set_expr can be used to change the expressions after creation,
 * and validation_is_ok can be used to ensure that things are properly setup.
 *
 * Returns a new @GnmValidation object that needs to be unrefed.
 **/
GnmValidation *
validation_new (ValidationStyle style,
		ValidationType  type,
		ValidationOp    op,
		char const *title, char const *msg,
		GnmExprTop const *texpr0, GnmExprTop const *texpr1,
		gboolean allow_blank, gboolean use_dropdown)
{
	GnmValidation *v;
	int nops, i;

	g_return_val_if_fail (type >= 0, NULL);
	g_return_val_if_fail (type < G_N_ELEMENTS (typeinfo), NULL);
	g_return_val_if_fail (op >= VALIDATION_OP_NONE, NULL);
	g_return_val_if_fail (op < (int)G_N_ELEMENTS (opinfo), NULL);

	switch (type) {
	case VALIDATION_TYPE_CUSTOM:
	case VALIDATION_TYPE_IN_LIST:
		nops = 1;
		if (op != VALIDATION_OP_NONE) {
			/*
			 * This can happen if an .xls file was saved
			 * as a .gnumeric.
			 */
			op = VALIDATION_OP_NONE;
		}
		break;
	case VALIDATION_TYPE_ANY:
		nops = 0;
		break;
	default:
		nops = (op == VALIDATION_OP_NONE) ? 0 : opinfo[op].nops;
	}

	v = g_new0 (GnmValidation, 1);
	v->ref_count = 1;
	v->title = title && title[0] ? go_string_new (title) : NULL;
	v->msg = msg && msg[0] ? go_string_new (msg) : NULL;
	v->texpr[0] = texpr0;
	v->texpr[1] = texpr1;
	v->style = style;
	v->type = type;
	v->op = op;
	v->allow_blank = (allow_blank != FALSE);
	v->use_dropdown = (use_dropdown != FALSE);

	/* Clear excess expressions.  */
	for (i = nops; i < 2; i++)
		if (v->texpr[i]) {
			gnm_expr_top_unref (v->texpr[i]);
			v->texpr[i] = NULL;
		}

	return v;
}
void
gnm_data_cache_source_set_name (GnmDataCacheSource *src, char const *name)
{
	GOString *new_val;

	g_return_if_fail (IS_GNM_DATA_CACHE_SOURCE (src));

	new_val = go_string_new (name);
	go_string_unref (src->src_name);
	src->src_name =  new_val;
}
예제 #3
0
/**
 * expr_name_set_name :
 * @nexpr : the named expression
 * @new_name : the new name of the expression
 *
 * returns: TRUE on error.
 */
gboolean
expr_name_set_name (GnmNamedExpr *nexpr,
		    const char *new_name)
{
	const char *old_name;
	GHashTable *h;

	g_return_val_if_fail (nexpr != NULL, TRUE);
	g_return_val_if_fail (nexpr->scope == NULL || new_name, TRUE);

	old_name = nexpr->name->str;
	if (go_str_compare (new_name, old_name) == 0)
		return FALSE;

#if 0
	g_printerr ("Renaming %s to %s\n", old_name, new_name);
#endif
	h = nexpr->scope
		? (nexpr->is_placeholder
		   ? nexpr->scope->placeholders
		   : nexpr->scope->names)
		: NULL;
	if (h) {
		if (new_name &&
		    (g_hash_table_lookup (nexpr->scope->placeholders, new_name) ||
		     g_hash_table_lookup (nexpr->scope->names, new_name))) {
			/* The only error not to be blamed on the programmer is
			   already-in-use.  */
			return TRUE;
		}

		g_hash_table_steal (h, old_name);
	}

	go_string_unref (nexpr->name);
	nexpr->name = go_string_new (new_name);

	if (h)
		g_hash_table_insert (h, (gpointer)nexpr->name->str, nexpr);

	return FALSE;
}
예제 #4
0
/**
 * expr_name_new :
 *
 * Creates a new name without linking it into any container.
 **/
GnmNamedExpr *
expr_name_new (char const *name)
{
	GnmNamedExpr *nexpr;

	g_return_val_if_fail (name != NULL, NULL);

	nexpr = g_new0 (GnmNamedExpr,1);

	nexpr->ref_count	= 1;
	nexpr->name		= go_string_new (name);
	nexpr->texpr		= NULL;
	nexpr->dependents	= NULL;
	nexpr->is_placeholder	= TRUE;
	nexpr->is_hidden	= FALSE;
	nexpr->is_permanent	= FALSE;
	nexpr->is_editable	= TRUE;
	nexpr->scope = NULL;

	if (gnm_debug_flag ("names"))
		g_printerr ("Created new name %s\n", name);

	return nexpr;
}
예제 #5
0
/**
 * expr_name_add:
 * @pp:
 * @name:
 * @texpr: if texpr == NULL then create a placeholder with value #NAME?
 * @error_msg:
 * @link_to_container:
 *
 * Absorbs the reference to @texpr.
 * If @error_msg is non NULL it may hold a pointer to a translated descriptive
 * string.  NOTE : caller is responsible for freeing the error message.
 *
 * The reference semantics of the new expression are
 * 1) new names with @link_to_container TRUE are referenced by the container.
 *    The caller DOES NOT OWN a reference to the result, and needs to add their
 *    own.
 * 2) if @link_to_container is FALSE the caller DOES OWN a reference, and
 *    can free the result by unrefing the name.
 **/
GnmNamedExpr *
expr_name_add (GnmParsePos const *pp, char const *name,
	       GnmExprTop const *texpr, char **error_msg,
	       gboolean link_to_container,
	       GnmNamedExpr *stub)
{
	GnmNamedExpr *nexpr = NULL;
	GnmNamedExprCollection *scope = NULL;

	g_return_val_if_fail (pp != NULL, NULL);
	g_return_val_if_fail (pp->sheet != NULL || pp->wb != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (stub == NULL || stub->is_placeholder, NULL);

	if (texpr != NULL && expr_name_check_for_loop (name, texpr)) {
		gnm_expr_top_unref (texpr);
		if (error_msg)
			*error_msg = g_strdup_printf (_("'%s' has a circular reference"), name);
		return NULL;
	}

	scope = (pp->sheet != NULL) ? pp->sheet->names : pp->wb->names;
	/* see if there was a place holder */
	nexpr = g_hash_table_lookup (scope->placeholders, name);
	if (nexpr != NULL) {
		if (texpr == NULL) {
			/* there was already a placeholder for this */
			expr_name_ref (nexpr);
			return nexpr;
		}

		/* convert the placeholder into a real name */
		g_hash_table_steal (scope->placeholders, name);
		nexpr->is_placeholder = FALSE;
	} else {
		nexpr = g_hash_table_lookup (scope->names, name);
		/* If this is a permanent name, we may be adding it */
		/* on opening of a file, although */
		/* the name is already in place. */
		if (nexpr != NULL) {
			if (nexpr->is_permanent)
				link_to_container = FALSE;
			else {
				if (error_msg != NULL)
					*error_msg = (pp->sheet != NULL)
						? g_strdup_printf (_("'%s' is already defined in sheet"), name)
						: g_strdup_printf (_("'%s' is already defined in workbook"), name);

				gnm_expr_top_unref (texpr);
				return NULL;
			}
		}
	}

	if (error_msg)
		*error_msg = NULL;

	if (nexpr == NULL) {
		if (stub != NULL) {
			nexpr = stub;
			stub->is_placeholder = FALSE;
			go_string_unref (stub->name);
			stub->name = go_string_new (name);
		} else {
			nexpr = expr_name_new (name);
			nexpr->is_placeholder = (texpr == NULL);
		}
	}
	parse_pos_init (&nexpr->pos,
		pp->wb, pp->sheet, pp->eval.col, pp->eval.row);
	if (texpr == NULL)
		texpr = gnm_expr_top_new_constant
			(value_new_error_NAME (NULL));
	expr_name_set_expr (nexpr, texpr);
	if (link_to_container)
		gnm_named_expr_collection_insert (scope, nexpr);

	return nexpr;
}
예제 #6
0
/**
 * gnm_validation_new :
 * @title: will be copied.
 * @msg: will be copied.
 * @texpr0: absorb the reference to the expression (optionally %NULL).
 * @texpr1: absorb the reference to the expression (optionally %NULL).
 *
 * Does _NOT_ require all necessary information to be set here.
 * gnm_validation_set_expr can be used to change the expressions after creation,
 * and gnm_validation_is_ok can be used to ensure that things are properly setup.
 *
 * Returns a new @GnmValidation object that needs to be unrefed.
 **/
GnmValidation *
gnm_validation_new (ValidationStyle style,
		ValidationType type,
		ValidationOp op,
		Sheet *sheet,
		char const *title, char const *msg,
		GnmExprTop const *texpr0, GnmExprTop const *texpr1,
		gboolean allow_blank, gboolean use_dropdown)
{
	GnmValidation *v;
	int nops;

	g_return_val_if_fail (type >= 0, NULL);
	g_return_val_if_fail (type < G_N_ELEMENTS (typeinfo), NULL);
	g_return_val_if_fail (op >= GNM_VALIDATION_OP_NONE, NULL);
	g_return_val_if_fail (op < (int)G_N_ELEMENTS (opinfo), NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);

	switch (type) {
	case GNM_VALIDATION_TYPE_CUSTOM:
	case GNM_VALIDATION_TYPE_IN_LIST:
		nops = 1;
		if (op != GNM_VALIDATION_OP_NONE) {
			/*
			 * This can happen if an .xls file was saved
			 * as a .gnumeric.
			 */
			op = GNM_VALIDATION_OP_NONE;
		}
		break;
	case GNM_VALIDATION_TYPE_ANY:
		nops = 0;
		break;
	default:
		nops = (op == GNM_VALIDATION_OP_NONE) ? 0 : opinfo[op].nops;
	}

	v = g_new0 (GnmValidation, 1);
	v->ref_count = 1;
	v->title = title && title[0] ? go_string_new (title) : NULL;
	v->msg = msg && msg[0] ? go_string_new (msg) : NULL;

	dependent_managed_init (&v->deps[0], sheet);
	if (texpr0) {
		if (nops > 0)
			dependent_managed_set_expr (&v->deps[0], texpr0);
		gnm_expr_top_unref (texpr0);
	}

	dependent_managed_init (&v->deps[1], sheet);
	if (texpr1) {
		if (nops > 1)
			dependent_managed_set_expr (&v->deps[1], texpr1);
		gnm_expr_top_unref (texpr1);
	}

	v->style = style;
	v->type = type;
	v->op = op;
	v->allow_blank = (allow_blank != FALSE);
	v->use_dropdown = (use_dropdown != FALSE);

	return v;
}