code.c

来自「超强的嵌入式GUI系统」· C语言 代码 · 共 786 行 · 第 1/2 页

C
786
字号
/***************************************************************************    begin                : Wed Dec 20 2006    copyright            : (C) 2006 - 2007 by Alper Akcan    email                : distchx@yahoo.com ***************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU Lesser General Public License as        * *   published by the Free Software Foundation; either version 2.1 of the  * *   License, or (at your option) any later version.                       * *                                                                         * ***************************************************************************/#include "xynth_.h"#include "widget.h"#include "widgetr.h"#include "code.h"#ifdef WIDGET_SCRIPT_ENGINE_JSextern code_script_t js_script;#endifcode_script_t *code_scripts[] = {#ifdef WIDGET_SCRIPT_ENGINE_JS	&js_script,#endif	NULL,};code_script_t *g_engine = NULL;char * code_trim_quota (char *value){	char *ptr = value;	/* strip trailing white space, returns, etc */	while ((*ptr != '\0') && (ptr[strlen(ptr) - 1] == '"')) {		ptr[strlen(ptr) - 1] = '\0';	}	/* strip leading white space, returns, etc */	while (*ptr && (*ptr == '"')) {		ptr++;	}	return ptr;}char * code_trim_space (char *value){	char *ptr = value;	/* strip trailing white space, returns, etc */	while ((*ptr != '\0') && (ptr[strlen(ptr) - 1] < 33)) {		ptr[strlen(ptr) - 1] = '\0';	}	/* strip leading white space, returns, etc */	while (*ptr && (*ptr < 33)) {		ptr++;	}	return ptr;}void code_tokenize (char *value, char token, int *n, char ***tokens){	char *tmp;	char **tok;	unsigned int nt;	unsigned int count;	for (count = 0, tmp = value; *tmp != 0; tmp++) {		if (*tmp == token) {			count++;		}	}	tok = (char **) s_malloc(sizeof(char **) * (count + 1));	if (tok == NULL) {		*n = 0;		return;	}	nt = 0;	tmp = value;	tok[nt] = tmp;	for (; *tmp != 0; tmp++) {		if (*tmp == token) {			*tmp++ = '\0';			if (*tmp == 0) {				break;			} else {				tok[++nt] = tmp;			}		}	}	*n = nt + 1;	*tokens = tok;		return;}void code_get_enum (s_hashtable_t *htable, char *val, unsigned int *prop){	int i;	int tok_count;	char **tok_vals;	*prop = 0;	if (val == NULL) {		return;	}	code_tokenize(val, '|', &tok_count, &tok_vals);	for (i = 0; i < tok_count; i++) {		*prop |= (unsigned int) s_hashtable_get_data(htable, code_trim_space(tok_vals[i]));	}	s_free(tok_vals);}void code_get_effect (s_hashtable_t *htable, s_xml_node_t *node, EFFECT *effect){	s_xml_node_t *t_effect = s_xml_node_get_path(node, "effect");	*effect = EFFECT_NONE;	if (node == NULL) {		return;	}	if (t_effect) {		code_get_enum(htable, t_effect->value, effect);		t_effect->dontparse = 1;	}}void code_get_style (s_hashtable_t *htable, s_xml_node_t *node, FRAME_SHAPE *fshape, FRAME_SHADOW *fshadow){	s_xml_node_t *shape = s_xml_node_get_path(node, "shape");	s_xml_node_t *shadow = s_xml_node_get_path(node, "shadow");	*fshape = FRAME_NOFRAME;	*fshadow = FRAME_PLAIN;	if (node == NULL) {		return;	}	if (shape) {		code_get_enum(htable, shape->value, fshape);		shape->dontparse = 1;	}	if (shadow) {		code_get_enum(htable, shadow->value, fshadow);		shadow->dontparse = 1;	}}void code_get_image (s_hashtable_t *htable, s_xml_node_t *node, unsigned int *istyle, unsigned int *irotate, unsigned int *icount, char ***ivar){	int i;	int count;	char **var;	char *cntstr;	s_xml_node_t *tmp;	FRAME_SHAPE shape;	FRAME_SHADOW shadow;	FRAME_IMAGE_ROTATION rotate;	*istyle = 0;	*irotate = 0;	*icount = 0;	*ivar = NULL;	count = atoi(s_xml_node_get_path_value(node, "count"));	if (count == 0) {		return;	}	code_get_enum(htable, s_xml_node_get_path_value(node, "rotate"), &rotate);	code_get_style(htable, s_xml_node_get_path(node, "style"), &shape, &shadow);	cntstr = (char *) s_malloc(sizeof(char *) * 255);	var = (char **) s_malloc(sizeof(char **) * count);	for (i = 0; i < count; i++) {		sprintf(cntstr, "image%d", i);		tmp = s_xml_node_get_path(node, cntstr);		var[i] = strdup(tmp->value);		tmp->dontparse = 1;	}	if ((tmp = s_xml_node_get_path(node, "style")) != NULL) { tmp->dontparse = 1; }	if ((tmp = s_xml_node_get_path(node, "style/shape")) != NULL) { tmp->dontparse = 1; }	if ((tmp = s_xml_node_get_path(node, "style/shadow")) != NULL) { tmp->dontparse = 1; }	if ((tmp = s_xml_node_get_path(node, "count")) != NULL) { tmp->dontparse = 1; }	if ((tmp = s_xml_node_get_path(node, "rotate")) != NULL) { tmp->dontparse = 1; }	s_free(cntstr);	*istyle = shape | shadow;	*irotate = rotate;	*icount = count;	*ivar = var;}void code_parse_element (s_xml_node_t *node, s_xml_node_t *style){	int p;	s_xml_node_t *tmp;	s_xml_node_t *dmp;	s_xml_node_t *chl;	s_xml_node_attr_t *sid;	s_xml_node_attr_t *stype;	s_xml_node_attr_t *ntype;	if (style == NULL) {		return;	}	for (p = 0; !s_list_eol(node->nodes, p); p++) {    		tmp = (s_xml_node_t *) s_list_get(node->nodes, p);    		code_parse_element(tmp, style);	}	ntype = s_xml_node_get_attr(node, "type");	if (strcmp(node->name, "element") == 0 && ntype && ntype->value != NULL) {	    	for (p = 0; !s_list_eol(style->nodes, p); p++) {	    		tmp = (s_xml_node_t *) s_list_get(style->nodes, p);	    		sid = s_xml_node_get_attr(tmp, "id");	    		stype = s_xml_node_get_attr(tmp, "type");	    		if (strcmp(tmp->name, "element") == 0 && sid && strcmp(sid->value, ntype->value) == 0) {	    		    	s_xml_node_dublicate(tmp, &dmp);	    		    	s_free(node->name);	    		    	s_free(ntype->value);	    		    	node->name = strdup("object");	    		    	ntype->value = (stype->value) ? strdup(stype->value) : NULL;	    		    	while (!s_list_eol(dmp->nodes, 0)) {	    		    		chl = (s_xml_node_t *) s_list_get(dmp->nodes, dmp->nodes->nb_elt - 1);	    		    		chl->parent = node;	    		    		s_list_add(node->nodes, chl, 0);	    		    		s_list_remove(dmp->nodes, dmp->nodes->nb_elt - 1);	    		    	}	    		    	s_xml_node_uninit(dmp);	    		}	    	}	}}void code_generate_move (s_hashtable_t *htable, s_xml_node_t *node){	int xi = 0;	int yi = 0;	int wi = 0;	int hi = 0;	s_xml_node_t *tmp;	w_object_t *object;	char *x = s_xml_node_get_path_value(node, "x");	char *y = s_xml_node_get_path_value(node, "y");	char *w = s_xml_node_get_path_value(node, "w");	char *h = s_xml_node_get_path_value(node, "h");	if (x) xi = atoi(x);	if (y) yi = atoi(y);	if (w) wi = atoi(w);	if (h) hi = atoi(h);	if (strcmp(node->parent->name, "window") == 0) {		if ((object = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(node->parent, "id"))) != NULL) {			w_window_set_coor(object->window, xi, yi, wi, hi);		}	} else if (strcmp(node->parent->name, "object") == 0) {		if ((object = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(node->parent, "id"))) != NULL) {			w_object_move(object, xi, yi, wi, hi);		}	}	node->dontparse = 1;	if ((tmp = s_xml_node_get_path(node, "x")) != NULL) { tmp->dontparse = 1; }	if ((tmp = s_xml_node_get_path(node, "y")) != NULL) { tmp->dontparse = 1; }	if ((tmp = s_xml_node_get_path(node, "w")) != NULL) { tmp->dontparse = 1; }	if ((tmp = s_xml_node_get_path(node, "h")) != NULL) { tmp->dontparse = 1; }}void code_generate_window (s_hashtable_t *htable, s_xml_node_t *node){	S_WINDOW prop;	s_xml_node_t *tmp;	w_window_t *window;	code_get_enum(htable, s_xml_node_get_attr_value(node, "type"), &prop);	w_window_init(&window, prop, NULL);	s_hashtable_add(htable, s_xml_node_get_attr_value(node, "id"), window->object);	if ((tmp = s_xml_node_get_path(node, "title")) != NULL) {		s_window_set_title(window->window, tmp->value);		tmp->dontparse = 1;	}	if ((tmp = s_xml_node_get_path(node, "move")) != NULL) {		code_generate_move(htable, tmp);		tmp->dontparse = 1;	}}void code_generate_show (s_hashtable_t *htable, s_xml_node_t *node){	w_object_t *object;	if (strcmp(node->parent->name, "window") == 0) {		if ((object = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(node->parent, "id"))) != NULL) {			w_object_show(object);			s_window_show(object->window->window);			s_window_main(object->window->window);		}	} else if (strcmp(node->parent->name, "object") == 0) {		if ((object = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(node->parent, "id"))) != NULL) {			w_object_show(object);		}	}}void code_generate_object_frame (s_hashtable_t *htable, s_xml_node_t *node){	w_frame_t *frame;	s_xml_node_t *tmp;	code_priv_t *priv;	w_object_t *pobject;	w_object_t *wobject;	s_xml_node_t *window = s_xml_node_get_parent(node, "window");	wobject = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(window, "id"));	pobject = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(node->parent, "id"));	w_frame_init(wobject->window, &frame, 0, pobject);	s_hashtable_add(htable, s_xml_node_get_attr_value(node, "id"), frame->object);	while ((tmp = s_xml_node_get_path(node, "style")) != NULL) {		FRAME_SHAPE fshape;		FRAME_SHADOW fshadow;		code_get_style(htable, tmp, &fshape, &fshadow);		w_frame_set_style(frame->object, fshape, fshadow); 		tmp->dontparse = 1;	}	while ((tmp = s_xml_node_get_path(node, "image")) != NULL) {		char **var;		unsigned int count;		unsigned int style;		unsigned int rotate;		code_get_image(htable, tmp, &style, &rotate, &count, &var);		if (var != NULL) {			w_frame_set_image(frame->object, style, rotate, count, var);			while (count--) s_free(var[count]);			s_free(var);		}		tmp->dontparse = 1;	}	priv = (code_priv_t *) s_malloc(sizeof(code_priv_t));	memset(priv, 0, sizeof(code_priv_t));	if ((tmp = s_xml_node_get_path(node, "effect")) != NULL) {		EFFECT effect;		code_get_effect(htable, tmp, &effect);		frame->object->effect->effect = effect;		tmp->dontparse = 1;	}	if ((tmp = s_xml_node_get_path(node, "draw")) != NULL) {		priv->draw = strdup(tmp->value);		if (g_engine) frame->object->draw = g_engine->object_draw;		tmp->dontparse = 1;	}	frame->object->priv = priv;}void code_generate_object_button (s_hashtable_t *htable, s_xml_node_t *node){	s_xml_node_t *tmp;	code_priv_t *priv;	w_button_t *button;	w_object_t *pobject;	w_object_t *wobject;	s_xml_node_t *window = s_xml_node_get_parent(node, "window");	wobject = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(window, "id"));	pobject = (w_object_t *) s_hashtable_get_data(htable, s_xml_node_get_attr_value(node->parent, "id"));	w_button_init(wobject->window, &button, pobject);	s_hashtable_add(htable, s_xml_node_get_attr_value(node, "id"), button->object);	while ((tmp = s_xml_node_get_path(node, "style")) != NULL) {		FRAME_SHAPE fshape;		FRAME_SHADOW fshadow;		code_get_style(htable, tmp, &fshape, &fshadow);		w_button_set_style(button->object, fshape, fshadow); 		tmp->dontparse = 1;	}	while ((tmp = s_xml_node_get_path(node, "image")) != NULL) {		char **var;		unsigned int count;		unsigned int style;		unsigned int rotate;		code_get_image(htable, tmp, &style, &rotate, &count, &var);		if (var != NULL) {			w_button_set_image(button->object, style, rotate, count, var);			while (count--) s_free(var[count]);			s_free(var);		}		tmp->dontparse = 1;	}	priv = (code_priv_t *) s_malloc(sizeof(code_priv_t));	memset(priv, 0, sizeof(code_priv_t));	if ((tmp = s_xml_node_get_path(node, "pressed")) != NULL) {		priv->pressed = strdup(tmp->value);		if (g_engine) w_button_set_pressed(button->object, g_engine->button_pressed);		tmp->dontparse = 1;	}	if ((tmp = s_xml_node_get_path(node, "released")) != NULL) {		priv->released = strdup(tmp->value);		if (g_engine) w_button_set_released(button->object, g_engine->button_released);		tmp->dontparse = 1;	}	if ((tmp = s_xml_node_get_path(node, "clicked")) != NULL) {		priv->clicked = strdup(tmp->value);		if (g_engine) w_button_set_clicked(button->object, g_engine->button_clicked);		tmp->dontparse = 1;	}	if ((tmp = s_xml_node_get_path(node, "draw")) != NULL) {		priv->draw = strdup(tmp->value);		if (g_engine) button->object->draw = g_engine->object_draw;		tmp->dontparse = 1;	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?