1 /*
2 **	GENERIC STYLE IMPLEMENTATION FOR HYPERTEXT
3 **
4 **	(c) COPYRIGHT MIT 1995.
5 **	Please first read the full copyright statement in the file COPYRIGH.
6 **	@(#) $Id$
7 **
8 **	Styles allow the translation between a logical property
9 **	of a piece of text and its physical representation.
10 **
11 **	A stylesheet is a collection of styles that can be applied
12 **	to the layout engine, for example provided by the HText
13 **	implementation.
14 */
15 
16 /* Library include files */
17 #include "wwwsys.h"
18 #include "WWWUtil.h"
19 #include "HTStyle.h"
20 
21 struct _HTStyle {
22     char *	name;		/* Style name */
23     int		element;	/* Element (if any) that this style applies to */
24     void *	context;	/* Implementation specific style context */
25 };
26 
27 struct _HTStyleSheet {
28     char *	name;
29     HTList *	styles;
30 };
31 
32 /* ------------------------------------------------------------------------- */
33 /* 			      INDIVIDUAL STYLES				     */
34 /* ------------------------------------------------------------------------- */
35 
HTStyle_new(const char * name,int element,void * context)36 PUBLIC HTStyle * HTStyle_new (const char * name, int element, void * context)
37 {
38     HTStyle * style = NULL;
39     if ((style = (HTStyle  *) HT_CALLOC(1, sizeof(HTStyle))) == NULL)
40         HT_OUTOFMEM("HTStyleNew");
41     StrAllocCopy(style->name, name ? name : "unknown");
42     style->element = element;
43     style->context = context;
44     return style;
45 }
46 
HTStyle_delete(HTStyle * me)47 PUBLIC BOOL HTStyle_delete  (HTStyle * me)
48 {
49     if (me) {
50 	HT_FREE(me->name);
51 	HT_FREE(me);
52 	return YES;
53     }
54     return NO;
55 }
56 
57 /* ------------------------------------------------------------------------- */
58 /* 			          STYLE SHEETS				     */
59 /* ------------------------------------------------------------------------- */
60 
HTStyleSheet_new(const char * name)61 PUBLIC HTStyleSheet * HTStyleSheet_new (const char * name)
62 {
63     HTStyleSheet * ss;
64     if ((ss = (HTStyleSheet *) HT_CALLOC(1, sizeof(HTStyleSheet))) == NULL)
65         HT_OUTOFMEM("HTStyleSheet_new");
66     StrAllocCopy(ss->name, name ? name : "unknown");
67     ss->styles = HTList_new();
68     return ss;
69 }
70 
HTStyleSheet_delete(HTStyleSheet * me)71 PUBLIC BOOL HTStyleSheet_delete (HTStyleSheet * me)
72 {
73     if (me) {
74 	HTList * cur = me->styles;
75 	HTStyle * pres;
76 	while ((pres = (HTStyle *) HTList_nextObject(cur)))
77 	    HTStyle_delete(pres);
78 	HTList_delete(me->styles);
79 	HT_FREE(me);
80 	return YES;
81     }
82     return NO;
83 }
84 
HTStyleSheet_addStyle(HTStyleSheet * me,HTStyle * style)85 PUBLIC BOOL HTStyleSheet_addStyle (HTStyleSheet * me, HTStyle * style)
86 {
87     return (me && style) ? HTList_addObject(me->styles, style) : NO;
88 }
89 
HTStyleSheet_deleteStyle(HTStyleSheet * me,HTStyle * style)90 PUBLIC BOOL HTStyleSheet_deleteStyle (HTStyleSheet * me, HTStyle * style)
91 {
92     if (me && style) {
93 	HTList_removeObject(me->styles, style);
94 	HTStyle_delete(style);
95 	return YES;
96     }
97     return NO;
98 }
99 
HTStyleSheet_findStyleWithName(HTStyleSheet * me,const char * name)100 PUBLIC HTStyle * HTStyleSheet_findStyleWithName (HTStyleSheet * me, const char * name)
101 {
102     if (me && name) {
103 	HTList * cur = me->styles;
104 	HTStyle * pres;
105 	while ((pres = (HTStyle *) HTList_nextObject(cur))) {
106 	    if (!strcasecomp(pres->name, name)) return pres;
107 	}
108 	HTTRACE(SGML_TRACE, "StyleSheet.. No style named `%s' in stylesheet `%s'\n" _
109 		    name _ me->name);
110     }
111     return NULL;
112 }
113 
HTStyleSheet_findStyleForElement(HTStyleSheet * me,int element)114 PUBLIC HTStyle * HTStyleSheet_findStyleForElement (HTStyleSheet * me, int element)
115 {
116     if (me) {
117 	HTList * cur = me->styles;
118 	HTStyle * pres;
119 	while ((pres = (HTStyle *) HTList_nextObject(cur))) {
120 	    if (pres->element==element) return pres;
121 	}
122 	HTTRACE(SGML_TRACE, "StyleSheet.. No style for element %d in stylesheet `%s'\n" _
123 		    element _ me->name);
124     }
125     return NULL;
126 }
127