style-condition.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * style-condition.c: Implementation of the condition framework.
 *
 * Copyright (C) Almer S. Tigelaar <almer@gnome.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

21 22
#include <gnumeric-config.h>
#include "gnumeric.h"
23 24 25 26 27 28 29 30 31
#include "style-condition.h"

#include "expr.h"
#include "sheet.h"
#include "value.h"
#include "workbook.h"

/*********************************************************************************/

32
#define DEP_TO_STYLE_CONDITION_EXPR(d_ptr) (StyleConditionExpr *)(((char *)d_ptr) - GTK_STRUCT_OFFSET(StyleConditionExpr, dep))
33 34

static void
35
style_condition_expr_dep_eval (Dependent *dep)
36
{
37
	StyleConditionExpr *sce;
38 39
	
	g_return_if_fail (dep != NULL);
40
	sce = DEP_TO_STYLE_CONDITION_EXPR (dep);
41
	
42 43 44
	if (sce->val) {
		value_release (sce->val);
		sce->val = NULL;
45 46 47 48 49 50 51 52
	}
		
	if (dep->expression) {
		EvalPos ep;

		ep.eval.row = ep.eval.col = 0;
		ep.sheet = dep->sheet;
			
53
		sce->val = expr_eval (dep->expression, &ep, 0);
54 55 56 57
	}
}

static void
58
style_condition_expr_dep_set_expr (Dependent *dep, ExprTree *new_expr)
59
{
60
	StyleConditionExpr *sce = DEP_TO_STYLE_CONDITION_EXPR (dep);
61
	
62
	/* Make sure no invalid 'cached' value
63 64
	 * of the previous expression remains
	 */
65 66 67
	if (sce->val) {
		value_release (sce->val);
		sce->val = NULL;
68 69 70 71
	}
}

static void
72
style_condition_expr_dep_debug_name (Dependent const *dep, FILE *out)
73 74 75 76 77 78
{
	g_return_if_fail (dep != NULL);
	
	fprintf (out, "StyleCondition Dep %p", dep);
}

79
static DEPENDENT_MAKE_TYPE (style_condition_expr_dep, &style_condition_expr_dep_set_expr)
80 81 82

/*********************************************************************************/

83 84 85 86 87 88 89
/**
 * style_condition_new_expr :
 * @op :
 * @expr :
 *
 * Absorbs the reference to the expression.
 */
90
StyleCondition *
91
style_condition_new_expr (StyleConditionOperator op, ExprTree *expr)
92 93 94
{
	StyleCondition *sc;

95
	g_return_val_if_fail (expr != NULL, NULL);
96 97 98
	
	sc = g_new0 (StyleCondition, 1);

99 100 101 102
	sc->type      = SCT_EXPR;
	sc->ref_count = 1;
	
	sc->u.expr.op        = op;
103
	sc->u.expr.dep.sheet = NULL;
104
	sc->u.expr.dep.flags = style_condition_expr_dep_get_dep_type ();
105
	sc->u.expr.dep.expression = expr;
106 107 108 109
	sc->u.expr.val = NULL;
	
	sc->next    = NULL;
	sc->next_op = SCB_NONE;
110
		
111 112 113 114 115 116 117 118 119 120 121 122 123
	return sc;
}

StyleCondition *
style_condition_new_constraint (StyleConditionConstraint constraint)
{
	StyleCondition *sc;

	sc = g_new0 (StyleCondition, 1);

	sc->type      = SCT_CONSTRAINT;
	sc->ref_count = 1;

124
	sc->u.constraint = constraint;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	
	sc->next    = NULL;
	sc->next_op = SCB_NONE;

	return sc;
}

StyleCondition *
style_condition_new_flags (StyleConditionFlags flags)
{
	StyleCondition *sc;

	sc = g_new0 (StyleCondition, 1);

	sc->type      = SCT_FLAGS;
	sc->ref_count = 1;

	sc->u.flags = flags;
	
	sc->next    = NULL;
	sc->next_op = SCB_NONE;

	return sc;
148 149 150 151 152 153 154 155 156 157 158
}

void
style_condition_ref (StyleCondition *sc)
{
	g_return_if_fail (sc != NULL);
	g_return_if_fail (sc->ref_count > 0);

	sc->ref_count++;
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
void
style_condition_link (StyleCondition *sc, Sheet *sheet)
{
	static CellPos const pos = { 0, 0 };
	g_return_if_fail (sc != NULL);

	do {
		g_return_if_fail (sc->u.expr.dep.sheet == NULL);
		sc->u.expr.dep.sheet = sheet;
		if (sc->u.expr.dep.expression != NULL)
			dependent_link (&sc->u.expr.dep, &pos);
		sc = sc->next;
	} while (sc != NULL);
}

void
style_condition_unlink (StyleCondition *sc)
{
	static CellPos const pos = { 0, 0 };
	g_return_if_fail (sc != NULL);
	do {
		if (sc->u.expr.dep.expression != NULL)
			dependent_unlink (&sc->u.expr.dep, &pos);
		sc = sc->next;
	} while (sc != NULL);
}

186 187 188 189 190 191 192 193 194 195 196 197
void
style_condition_unref (StyleCondition *sc)
{
	g_return_if_fail (sc != NULL);
	g_return_if_fail (sc->ref_count > 0);

	sc->ref_count--;
	
	if (sc->ref_count <= 0) {
		if (sc->next)
			style_condition_unref (sc->next);

198 199 200 201 202 203 204 205 206
		if (sc->type == SCT_EXPR) {
			dependent_set_expr (&sc->u.expr.dep, NULL);
			if (sc->u.expr.val) {
				value_release (sc->u.expr.val);
				sc->u.expr.val = NULL;
			}
		} else if (sc->type != SCT_CONSTRAINT && sc->type != SCT_FLAGS)
			g_warning ("Unhandled StyleCondition type");
		
207 208 209 210 211 212
		g_free (sc);
	}
}

/**
 * style_condition_chain:
213
 * Chain @src to @dst with operator @op.
214 215 216 217
 *
 * Return value: @dst (the beginning of the chain)
 **/
StyleCondition *
218 219
style_condition_chain (StyleCondition *dst, StyleConditionBool op,
		       StyleCondition *src)
220 221 222
{
	g_return_val_if_fail (dst != NULL, NULL);
	g_return_val_if_fail (src != NULL, NULL);
223
	g_return_val_if_fail (dst->next == NULL, NULL);
224
	
225 226
	dst->next    = src;
	dst->next_op = op;
227 228 229
	return dst;
}

230 231
static gboolean
style_condition_expr_eval (StyleConditionExpr *sce, Value *val, StyleFormat *format)
232
{
233
	ValueCompare vc;
234
			
235
	if (sce->val == NULL) {
236
		sce->dep.flags |= DEPENDENT_NEEDS_RECALC;
237 238 239
		dependent_eval (&sce->dep);
		g_return_val_if_fail (sce->val != NULL, FALSE);
	}
240

241 242 243 244 245
	if (sce->op == SCO_BOOLEAN_EXPR) {
		gboolean dummy;
		return value_get_as_bool (sce->val, &dummy);
	}

246
	vc = value_compare (val, sce->val, TRUE);
247
			
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	switch (sce->op) {
	case SCO_EQUAL :
		if (vc != IS_EQUAL) return FALSE; break;
	case SCO_NOT_EQUAL :
		if (vc == IS_EQUAL) return FALSE; break;
	case SCO_LESS :
		if (vc != IS_LESS) return FALSE; break;
	case SCO_GREATER :
		if (vc != IS_GREATER) return FALSE; break;
	case SCO_LESS_EQUAL :
		if (vc == IS_GREATER) return FALSE; break;
	case SCO_GREATER_EQUAL :
		if (vc == IS_LESS) return FALSE; break;
	default :
		g_warning ("Style Condition: Unhandled operator"); return FALSE;
	}

	return TRUE;
}

static gboolean
style_condition_constraint_eval (StyleConditionConstraint scc, Value *val, StyleFormat *format)
{
	switch (scc) {
	case SCC_IS_INT :
		if (val->type != VALUE_BOOLEAN
		    && val->type != VALUE_INTEGER)
			return FALSE; break;
	case SCC_IS_FLOAT :
		if (val->type != VALUE_FLOAT) return FALSE; break;
	case SCC_IS_IN_LIST :
		g_warning ("Style Condition: 'Is In List' not implemented");
		break;
	case SCC_IS_DATE :
		g_warning ("Style Condition: 'In Date' not implemented");
		break;
	case SCC_IS_TIME :
		g_warning ("Style Condition: 'Is Time' not implemented");
		break;
	case SCC_IS_TEXTLEN :
		g_warning ("Style Condition: 'Is Text Length' not implemented");
		break;
290 291
	case SCC_IS_CUSTOM :
		return TRUE;
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	default :
		g_warning ("Style Condition: Unhandled operator");
		return FALSE;
	}
	
	return TRUE;
}

static gboolean
style_condition_flags_eval (StyleConditionFlags scf, Value *val, StyleFormat *format)
{
	if (val->type == VALUE_EMPTY && (scf & SCF_ALLOW_BLANK))
		return TRUE;
	else
		return FALSE;
}

void
style_condition_dump (StyleCondition *sc)
{
	StyleCondition *sci;
	int             i;

	g_return_if_fail (sc != NULL);
	
	fprintf (stdout, "---------------------------------------\n");
	for (i = 0, sci = sc; sci != NULL; sci = sci->next, i++) {
		char     *t = NULL;
		GString  *s = NULL;
		
		fprintf (stdout, "=> Element %d\n", i);

		switch (sci->type) {
		case SCT_EXPR       : t = "Expression"; break;
		case SCT_CONSTRAINT : t = "Constraint"; break;
		case SCT_FLAGS      : t = "Flags";      break;
		default :
			t = "Unknown";
		}
		fprintf (stdout, "\tType          : %s\n", t);
		fprintf (stdout, "\tRef. Count    : %d\n", sci->ref_count);

		switch (sci->type) {
		case SCT_EXPR :
			switch (sci->u.expr.op) {
			case SCO_EQUAL         : t = "== Equal";         break;
			case SCO_NOT_EQUAL     : t = "!= Not Equal";     break;
			case SCO_GREATER       : t = ">  Greater";       break;
			case SCO_LESS          : t = "<  Less";          break;
			case SCO_GREATER_EQUAL : t = ">= Greater Equal"; break;
			case SCO_LESS_EQUAL    : t = "<= Less Equal";    break;
343
			case SCO_BOOLEAN_EXPR  : t = "Boolean expression"; break;
344
			default :
345
				t = "?  Unknown";
346
			}
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
			fprintf (stdout, "\t\tOperator   : %s\n", t);

			if (sci->u.expr.dep.expression) {
				ParsePos  pp;
				Sheet    *sheet = sci->u.expr.dep.sheet;
				
				parse_pos_init (&pp, sheet->workbook, sheet, 0, 0);
				t = expr_tree_as_string (sci->u.expr.dep.expression, &pp);
				fprintf (stdout, "\t\tExpression : %s\n", t);
				g_free (t);
				t = NULL;
			} else
				fprintf (stdout, "\t\tExpression : (NULL)\n");

			if (sci->u.expr.val)
				fprintf (stdout, "\t\tResult     : %s (Cached)\n",
					 value_peek_string (sci->u.expr.val));
			else
				fprintf (stdout, "\t\tResult     : (NULL)\n");
366
			
367 368 369 370 371 372 373 374 375 376
			break;
		case SCT_CONSTRAINT :
			switch (sci->u.constraint) {
			case SCC_IS_INT     : t = "Whole Number (Integer)"; break;
			case SCC_IS_FLOAT   : t = "Decimals     (Float)";   break;
			case SCC_IS_IN_LIST : t = "List         (Region)";  break;
			case SCC_IS_DATE    : t = "Date         (Integer)"; break;
			case SCC_IS_TIME    : t = "Time         (Integer)"; break;
			case SCC_IS_TEXTLEN : t = "TextLength   (Integer)"; break;
			case SCC_IS_CUSTOM  : t = "Custom       (Any)";     break;
377
			default :
378
				t = "Unknown      (???)";
379
			}
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
			fprintf (stdout, "\t\tConstraint : %s\n", t);
			
			break;
		case SCT_FLAGS :
			s = g_string_new ("");

			if (sci->u.flags & SCF_ALLOW_BLANK)
				g_string_append (s, "(Allow Blank)");
			if (sci->u.flags & SCF_IN_CELL_DROPDOWN)
				g_string_append (s, "(In Cell Dropdown)");
			
			fprintf (stdout, "\t\tFlags      : %s\n", s->str);
			g_string_free (s, TRUE);
			
			break;
		}

		switch (sci->next_op) {
		case SCB_NONE     : t = "  (None)";     break;
		case SCB_AND      : t = "& (And)";      break;
		case SCB_OR       : t = "| (Or)";       break;
		case SCB_AND_PAIR : t = "& (And PAIR)"; break;
		case SCB_OR_PAIR  : t = "| (Or PAIR)";  break;
		default :
			t = "? (Unknown)";
		}
		fprintf (stdout, "\tNext Operator : %s\n\n", t);
	}
	fprintf (stdout, "---------------------------------------\n\n");
}

gboolean
style_condition_eval (StyleCondition *sc, Value *val, StyleFormat *format)
{
	StyleCondition     *sci;
	StyleConditionBool  scb = SCB_NONE;
	gboolean            prev_result = FALSE;
	gboolean            result = TRUE;
	
	g_return_val_if_fail (val != NULL, FALSE);

	for (sci = sc; sci != NULL; sci = sci->next) {
		result = TRUE;
		
		switch (sci->type) {
		case SCT_EXPR :
			result = style_condition_expr_eval (&sci->u.expr, val, format);
			break;
		case SCT_CONSTRAINT :
			result = style_condition_constraint_eval (sci->u.constraint, val, format);
			break;
		case SCT_FLAGS :
			result = style_condition_flags_eval (sci->u.flags, val, format);
			break;
		default :
			g_warning ("Unknown StyleCondition type");
		}

		/*
		 * NOTE: Pay careful attention to the four chain operators.
		 *
		 * The normal two, SCB_AND and SCB_OR, just evaluate things as list.
		 * Example : X SCB_AND Y SCB_OR Z
		 *           (first does "X & Y" then does "Y | Z")
		 * Observe that none of the operators take precedence over the other.
		 *
		 * The other two, SCB_AND_PAIR and SCB_OR_PAIR explicitely indicate
		 * precendence by using 'parens'.
		 * Example : X SCB_OR_PAIR Y SCB_AND_PAIR Z
		 *           (can be written as : "((X | Y) & Z)")
		 *
		 * And a combination  : X SCB_OR_PAIR Y SCB_AND_PAIR Z SCB_OR J
		 * Which evaluates to : ((X | Y) & Z) | J
		 */
		switch (scb) {
		case SCB_AND :
			if (!(result && prev_result))
				return FALSE;
			else
				result = TRUE;
			break;
		case SCB_OR :
			if (!(result || prev_result))
				return FALSE;
			else
				result = TRUE;
			break;
		case SCB_AND_PAIR :
			result = (result && prev_result);
			break;
		case SCB_OR_PAIR :
			result = (result || prev_result);
			break;
		case SCB_NONE :
			if (sci != sc && sci->next != NULL)
				g_warning ("Corrupted StyleCondition Chain");
			break;
		default :
			g_warning ("Unknown StyleCondition Chain Operator");
479
		}
480 481 482

		scb = sci->next_op;
		prev_result = result;
483 484
	}
	
485
	return result;
486
}