Return to templateUtils.c CVS log | Up to [Public] / Amaya / amaya |
1.9 vatton 1: /* 2: * 3: * COPYRIGHT INRIA and W3C, 2006-2007 4: * Please first read the full copyright statement in file COPYRIGHT. 5: * 6: */ 7: 1.3 quint 8: #include "templates.h" 1.14 ! kia 9: #include "Templatename.h" ! 10: #include "templates_f.h" 1.1 francesc 11: 12: /*---------------------------------------------------------------------- 13: GetSchemaFromDocType: Returns the name of the schema corresponding to 14: a doc type. 15: ----------------------------------------------------------------------*/ 16: char *GetSchemaFromDocType (DocumentType docType) 17: { 18: #ifdef TEMPLATES 19: switch (docType) 20: { 21: case docAnnot : 22: return "Annot"; 23: case docBookmark : 24: return "Topics"; 25: case docSVG : 26: return "SVG"; 27: case docMath : 28: return "MathML"; 29: case docXml : 30: return "XML"; 31: default : 32: return "HTML"; 33: } 34: #endif // TEMPLATES 35: return "HTML"; 36: } 37: 1.5 kia 38: /*---------------------------------------------------------------------- 39: Set the value of a string attribute 40: ----------------------------------------------------------------------*/ 41: void SetAttributeStringValue (Element el, int att, char* value) 42: { 43: #ifdef TEMPLATES 1.7 vatton 44: Document doc = TtaGetDocument(el); 45: AttributeType attType; 46: Attribute attribute; 1.5 kia 47: 1.10 kia 48: if (doc == 0 || !TtaGetDocumentAccessMode(doc)) 1.9 vatton 49: return; 1.5 kia 50: attType.AttrSSchema = TtaGetElementType(el).ElSSchema; 51: attType.AttrTypeNum = att; 1.7 vatton 52: attribute = TtaGetAttribute(el, attType); 53: if (attribute == NULL) 54: { 55: attribute = TtaNewAttribute (attType); 56: TtaAttachAttribute(el, attribute, doc); 57: } 1.5 kia 58: TtaSetAttributeText(attribute, value, el, doc); 59: #endif /* TEMPLATES */ 60: } 61: 1.11 kia 62: /*---------------------------------------------------------------------- 63: Set the value of a string attribute and registering it in undo sequence. 64: ----------------------------------------------------------------------*/ 65: void SetAttributeStringValueWithUndo (Element el, int att, char* value) 66: { 67: #ifdef TEMPLATES 68: Document doc = TtaGetDocument(el); 69: AttributeType attType; 70: Attribute attribute; 71: 72: if (doc == 0 || !TtaGetDocumentAccessMode(doc)) 73: return; 74: attType.AttrSSchema = TtaGetElementType(el).ElSSchema; 75: attType.AttrTypeNum = att; 76: attribute = TtaGetAttribute(el, attType); 77: if (attribute == NULL) 78: { 79: attribute = TtaNewAttribute (attType); 80: TtaAttachAttribute(el, attribute, doc); 81: TtaRegisterAttributeCreate(attribute, el, doc); 82: } 83: TtaSetAttributeText(attribute, value, el, doc); 84: TtaRegisterAttributeReplace(attribute, el, doc); 85: #endif /* TEMPLATES */ 86: } 87: 1.13 kia 88: /*---------------------------------------------------------------------- 89: Returns the value of a string attribute without copy it 90: ----------------------------------------------------------------------*/ 91: void GiveAttributeStringValueFromNum (Element el, int att, char* buff, int* sz) 92: { 93: #ifdef TEMPLATES 94: AttributeType attType; 95: Attribute attribute; 96: int size; 97: 98: attType.AttrSSchema = TtaGetElementType(el).ElSSchema; 99: attType.AttrTypeNum = att; 100: attribute = TtaGetAttribute(el, attType); 101: 102: size = TtaGetTextAttributeLength(attribute); 103: TtaGiveTextAttributeValue (attribute, buff, &size); 104: buff[size] = EOS; 105: if(sz) 106: *sz = size; 107: #endif /* TEMPLATES */ 108: } 109: 110: 1.1 francesc 111: 112: /*---------------------------------------------------------------------- 113: Returns the value of a string attribute 114: ----------------------------------------------------------------------*/ 1.8 kia 115: char *GetAttributeStringValueFromNum (Element el, int att, int* sz) 1.1 francesc 116: { 117: #ifdef TEMPLATES 118: AttributeType attType; 1.12 vatton 119: Attribute attribute; 120: char *aux; 121: int size; 122: 1.1 francesc 123: attType.AttrSSchema = TtaGetElementType(el).ElSSchema; 124: attType.AttrTypeNum = att; 1.12 vatton 125: attribute = TtaGetAttribute(el, attType); 1.1 francesc 126: 1.12 vatton 127: size = TtaGetTextAttributeLength(attribute); 128: aux = (char*) TtaGetMemory(size+1); 1.1 francesc 129: TtaGiveTextAttributeValue (attribute, aux, &size); 1.12 vatton 130: aux[size] = EOS; 1.4 kia 131: if(sz) 132: *sz = size; 1.1 francesc 133: return aux; 134: #else 1.8 kia 135: return NULL; 1.1 francesc 136: #endif /* TEMPLATES */ 137: } 1.2 francesc 138: 139: /*---------------------------------------------------------------------- 140: Returns the value of a string attribute 141: ----------------------------------------------------------------------*/ 1.4 kia 142: char *GetAttributeStringValue (Element el, Attribute attribute, int* sz) 1.2 francesc 143: { 144: #ifdef TEMPLATES 145: int size = TtaGetTextAttributeLength(attribute); 146: char *aux = (char*) TtaGetMemory(size+1); 1.12 vatton 147: 1.2 francesc 148: TtaGiveTextAttributeValue (attribute, aux, &size); 1.12 vatton 149: aux[size] = EOS; 1.4 kia 150: if(sz) 151: *sz = size; 1.2 francesc 152: return aux; 153: #else 1.8 kia 154: return NULL; 1.2 francesc 155: #endif /* TEMPLATES */ 156: } 1.6 kia 157: 158: 159: /*---------------------------------------------------------------------- 160: GetFirstEditableElement 161: Returns the first descendant element which is modifiable. 162: ----------------------------------------------------------------------*/ 163: Element GetFirstEditableElement (Element el) 164: { 165: Element res = NULL; 166: Element current = TtaGetFirstChild(el); 167: 168: while(!res && current) 169: { 170: res = GetFirstEditableElement(current); 171: TtaNextSibling(¤t); 172: } 173: 174: if(!res && !TtaIsReadOnly(el)) 175: res = el; 176: 177: return res; 178: } 1.14 ! kia 179: ! 180: /*---------------------------------------------------------------------- ! 181: ValidateTemplateAttrInMenu ! 182: Validate the status of an attribute according to xt::atribute rules. ! 183: ----------------------------------------------------------------------*/ ! 184: ThotBool ValidateTemplateAttrInMenu (NotifyAttribute * event) ! 185: { ! 186: #ifdef TEMPLATES ! 187: Element elem; ! 188: Element parent; ! 189: ElementType elType; ! 190: AttributeType attrType; ! 191: Attribute attr; ! 192: char* attrName; ! 193: char buffer[MAX_LENGTH]; ! 194: int sz; ! 195: int useAt, type; ! 196: ! 197: /* Prevent from showing attributes for template instance but not templates. */ ! 198: if(IsTemplateInstanceDocument(event->document)) ! 199: { ! 200: /* Prevent if attribute's element is not a descendant of xt:use */ ! 201: parent = event->element; ! 202: elem = GetFirstTemplateParentElement(parent); ! 203: if(!elem) ! 204: return TRUE; ! 205: elType = TtaGetElementType(elem); ! 206: if(elType.ElTypeNum!=Template_EL_useSimple) ! 207: return TRUE; ! 208: ! 209: /* Search for the corresponding xt:attribute element*/ ! 210: attrName = TtaGetAttributeName(event->attributeType); ! 211: attrType.AttrSSchema = TtaGetSSchema ("Template", event->document); ! 212: for(elem = TtaGetFirstChild(parent); elem; TtaNextSibling(&elem)) ! 213: { ! 214: attrType.AttrTypeNum = Template_ATTR_ref_name; ! 215: elType = TtaGetElementType(elem); ! 216: if(elType.ElTypeNum==Template_EL_attribute && ! 217: elType.ElSSchema==TtaGetSSchema ("Template", event->document)) ! 218: { ! 219: attr = TtaGetAttribute(elem, attrType); ! 220: if(attr) ! 221: { ! 222: sz = MAX_LENGTH; ! 223: TtaGiveTextAttributeValue(attr, buffer, &sz); ! 224: if(!strcmp(buffer, attrName)) ! 225: { ! 226: /* Process the attribute filtering */ ! 227: /* Get 'useAt' attr value. */ ! 228: attrType.AttrTypeNum = Template_ATTR_useAt; ! 229: attr = TtaGetAttribute(elem, attrType); ! 230: if(attr) ! 231: useAt = TtaGetAttributeValue(attr); ! 232: else ! 233: useAt = Template_ATTR_useAt_VAL_required; ! 234: /* Get 'type' attr value. */ ! 235: attrType.AttrTypeNum = Template_ATTR_type; ! 236: attr = TtaGetAttribute(elem, attrType); ! 237: if(attr) ! 238: type = TtaGetAttributeValue(attr); ! 239: else ! 240: type = Template_ATTR_type_VAL_string; ! 241: ! 242: /******************************************************************************/ ! 243: printf("Attribute : %s \n", attrName); ! 244: switch(useAt) ! 245: { ! 246: case Template_ATTR_useAt_VAL_required: ! 247: printf(" required"); ! 248: break; ! 249: case Template_ATTR_useAt_VAL_optional: ! 250: printf(" optional"); ! 251: break; ! 252: case Template_ATTR_useAt_VAL_prohibited: ! 253: printf(" prohibited"); ! 254: break; ! 255: default: ! 256: printf(" error"); ! 257: break; ! 258: } ! 259: switch(type) ! 260: { ! 261: case Template_ATTR_type_VAL_string: ! 262: printf(" string\n"); ! 263: break; ! 264: case Template_ATTR_type_VAL_number: ! 265: printf(" number\n"); ! 266: break; ! 267: case Template_ATTR_type_VAL_listVal: ! 268: printf(" list\n"); ! 269: break; ! 270: default: ! 271: printf(" error\n"); ! 272: break; ! 273: } ! 274: /******************************************************************************/ ! 275: ! 276: event->restr.RestrType = (RestrictionContentType)type; ! 277: /* If attr is prohibited, dont show it.*/ ! 278: if(useAt==Template_ATTR_useAt_VAL_prohibited) ! 279: return TRUE; ! 280: if(useAt==Template_ATTR_useAt_VAL_required) ! 281: { ! 282: /* Force the usage of this attribute.*/ ! 283: event->restr.RestrFlags |= attr_mandatory; ! 284: } ! 285: ! 286: /* Get 'fixed' attr value. */ ! 287: attrType.AttrTypeNum = Template_ATTR_fixed; ! 288: attr = TtaGetAttribute(elem, attrType); ! 289: if(attr) ! 290: { ! 291: sz = MAX_LENGTH; ! 292: TtaGiveTextAttributeValue(attr, buffer, &sz); ! 293: event->restr.RestrFlags |= attr_readonly; ! 294: event->restr.RestrDefVal = TtaStrdup(buffer); ! 295: return FALSE; ! 296: } ! 297: ! 298: /* Get 'default' attr value.*/ ! 299: attrType.AttrTypeNum = Template_ATTR_defaultAt; ! 300: attr = TtaGetAttribute(elem, attrType); ! 301: if(attr) ! 302: { ! 303: sz = MAX_LENGTH; ! 304: TtaGiveTextAttributeValue(attr, buffer, &sz); ! 305: event->restr.RestrDefVal = TtaStrdup(buffer); ! 306: } ! 307: ! 308: /* Get 'values' attr value.*/ ! 309: attrType.AttrTypeNum = Template_ATTR_values; ! 310: attr = TtaGetAttribute(elem, attrType); ! 311: if(attr) ! 312: { ! 313: sz = MAX_LENGTH; ! 314: TtaGiveTextAttributeValue(attr, buffer, &sz); ! 315: event->restr.RestrEnumVal = TtaStrdup(buffer); ! 316: event->restr.RestrFlags |= attr_enum; ! 317: } ! 318: return FALSE; ! 319: } ! 320: } ! 321: } ! 322: } ! 323: ! 324: return FALSE; ! 325: } ! 326: else ! 327: #endif /* TEMPLATES */ ! 328: return FALSE; ! 329: }