Return to templates.c CVS log | Up to [Public] / Amaya / amaya |
1.1 cvs 1: /* 2: * 1.27 vatton 3: * COPYRIGHT INRIA and W3C, 1996-2005 1.1 cvs 4: * Please first read the full copyright statement in file COPYRIGHT. 5: * 6: */ 1.14 cvs 7: 1.1 cvs 8: /* 1.51 francesc 9: * Authors: Francesc Campoy Flores 1.1 cvs 10: * 11: */ 12: 13: #define THOT_EXPORT extern 14: #include "amaya.h" 15: #include "document.h" 1.52 vatton 16: 1.46 vatton 17: #ifdef TEMPLATES 18: #include "Template.h" 1.67 quint 19: #include "templates.h" 1.52 vatton 20: #include "templateDeclarations.h" 21: 1.69 quint 22: #include "mydictionary_f.h" 1.67 quint 23: #include "templateLoad_f.h" 24: #include "templateDeclarations_f.h" 1.76 vatton 25: #include "templateInstantiate_f.h" 1.28 tollenae 26: #include "appdialogue_wx.h" 1.29 tollenae 27: #include "init_f.h" 1.46 vatton 28: #include "wxdialogapi_f.h" 1.60 francesc 29: #include "AHTURLTools_f.h" 30: 1.52 vatton 31: #endif /* TEMPLATES */ 1.1 cvs 32: 33: /*---------------------------------------------------------------------- 1.51 francesc 34: NewTemplate: Create the "new document from template" dialog 1.1 cvs 35: ----------------------------------------------------------------------*/ 1.18 cvs 36: void NewTemplate (Document doc, View view) 1.1 cvs 37: { 1.51 francesc 38: #ifdef TEMPLATES 1.76 vatton 39: char *templateDir = TtaGetEnvString ("TEMPLATES_DIRECTORY"); 40: ThotBool created; 1.28 tollenae 41: 1.76 vatton 42: if (Templates_Dic == NULL) 43: InitializeTemplateEnvironment (); 44: created = CreateNewTemplateDocDlgWX (BaseDialog + OpenTemplate, 1.61 francesc 45: /*TtaGetViewFrame (doc, view)*/NULL, doc, 1.52 vatton 46: TtaGetMessage (AMAYA, AM_NEW_TEMPLATE),templateDir); 1.51 francesc 47: 1.28 tollenae 48: if (created) 1.25 vatton 49: { 1.28 tollenae 50: TtaSetDialoguePosition (); 51: TtaShowDialogue (BaseDialog + OpenTemplate, TRUE); 1.25 vatton 52: } 1.51 francesc 53: 1.52 vatton 54: #endif /* TEMPLATES */ 1.1 cvs 55: } 1.25 vatton 56: 1.53 vatton 57: /*---------------------------------------------------------------------- 58: Load a template and create the instance file - update images and 59: stylesheets related to the template. 60: ----------------------------------------------------------------------*/ 1.61 francesc 61: void CreateInstanceOfTemplate (Document doc, char *templatename, char *docname) 1.53 vatton 62: { 63: #ifdef TEMPLATES 64: 1.60 francesc 65: char *s; 1.65 francesc 66: ThotBool dontReplace = DontReplaceOldDoc; 1.60 francesc 67: 68: if (!IsW3Path (docname) && TtaFileExist (docname)) 69: { 70: s = (char *)TtaGetMemory (strlen (docname) + 71: strlen (TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK)) + 2); 72: sprintf (s, TtaGetMessage (AMAYA, AM_OVERWRITE_CHECK), docname); 73: InitConfirm (0, 0, s); 1.61 francesc 74: TtaFreeMemory (s); 1.62 francesc 75: if (!UserAnswer) 76: return; 77: } 78: 1.76 vatton 79: LoadTemplate (0, templatename); 1.65 francesc 80: DontReplaceOldDoc = dontReplace; 1.76 vatton 81: CreateInstance (templatename, docname); 1.62 francesc 82: 1.53 vatton 83: #endif /* TEMPLATES */ 84: } 85: 1.52 vatton 86: /*---------------------------------------------------------------------- 87: ----------------------------------------------------------------------*/ 1.76 vatton 88: void giveItems (char *text, int size, struct menuType **items, int *nbitems) 1.1 cvs 89: { 1.70 quint 90: #ifdef TEMPLATES 1.52 vatton 91: ThotBool inElement = TRUE; 92: struct menuType *menu; 93: char *iter; 94: char temp[128]; 95: int i; 96: int labelSize; 1.28 tollenae 97: 1.52 vatton 98: *nbitems = 1; 99: for (i = 0; i < size; i++) 100: { 101: if (isEOSorWhiteSpace (text[i])) 102: { 103: if (inElement) 104: inElement = FALSE; 105: } 106: else if (!inElement) 107: { 108: inElement = TRUE; 109: (*nbitems)++; 110: } 111: } 1.51 francesc 112: 1.76 vatton 113: menu = (struct menuType*) TtaGetMemory (sizeof (struct menuType)* *nbitems); 1.52 vatton 114: iter = text; 115: for (i = 0; i < *nbitems; i++) 116: { 117: labelSize = 0; 118: while (isEOSorWhiteSpace (*iter)) 119: iter++; 120: 121: while (!isEOSorWhiteSpace (*iter)) 122: { 123: temp[labelSize++] = *iter; 124: iter++; 125: } 126: 127: temp[labelSize] = EOS; 1.76 vatton 128: menu[i].label = (char *) TtaStrdup (temp); 1.68 quint 129: menu[i].type = SimpleTypeNat; /* @@@@@ ???? @@@@@ */ 1.52 vatton 130: *items = menu; 131: } 1.70 quint 132: #endif /* TEMPLATES */ 1.28 tollenae 133: } 1.37 tollenae 134: 1.70 quint 135: #ifdef TEMPLATES 1.52 vatton 136: /*---------------------------------------------------------------------- 137: ----------------------------------------------------------------------*/ 138: static char *createMenuString (const struct menuType* items, const int nbItems) 139: { 140: char *result, *iter; 141: int size = 0; 142: int i; 143: 144: for (i=0; i < nbItems; i++) 145: size += 2 + strlen (items[i].label); 146: 1.76 vatton 147: result = (char *) TtaGetMemory (size); 1.52 vatton 148: iter = result; 149: for (i=0; i < nbItems; i++) 150: { 151: *iter = 'B'; 152: ++iter; 1.51 francesc 153: 1.52 vatton 154: strcpy (iter, items[i].label); 155: iter += strlen (items[i].label)+1; 156: } 1.51 francesc 157: return result; 1.36 tollenae 158: } 1.71 quint 159: #endif /* TEMPLATES */ 1.29 tollenae 160: 1.71 quint 161: /*---------------------------------------------------------------------- 162: UseToBeCreated 163: An new use element will be created by the user through some generic editing 164: command 165: -----------------------------------------------------------------------*/ 166: ThotBool UseToBeCreated (NotifyElement *event) 167: { 168: #ifdef TEMPLATES 1.75 quint 169: Element el; 1.72 quint 170: Document doc; 171: 172: el = event->element; 173: doc = event->document; 174: /* is there a limit to the number of elements in the xt:repeat ? */ 1.71 quint 175: /* @@@@@ */ 1.52 vatton 176: #endif /* TEMPLATES */ 1.71 quint 177: return FALSE; /* let Thot perform normal operation */ 178: } 179: 180: /*---------------------------------------------------------------------- 181: UseCreated 182: A new "use" element has just been created by the user with a generic editing 183: command. 184: -----------------------------------------------------------------------*/ 185: void UseCreated (NotifyElement *event) 186: { 187: #ifdef TEMPLATES 188: Document doc; 189: Element el; 190: XTigerTemplate t; 191: 192: doc = event->document; 1.72 quint 193: el = event->element; 194: if (TtaGetFirstChild (el)) 195: /* this Use element has already some content. It has already been 196: instanciated */ 197: return; 1.76 vatton 198: t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url); 1.71 quint 199: if (!t) 200: return; // no template ?!?! 1.76 vatton 201: InstantiateUse (t, el, doc, TRUE); 1.71 quint 202: #endif /* TEMPLATES */ 203: } 1.29 tollenae 204: 1.46 vatton 205: /*---------------------------------------------------------------------- 1.63 vatton 206: UseMenuClicked 1.51 francesc 207: Shows a menu with all the types that can be used in a use element. 1.46 vatton 208: ----------------------------------------------------------------------*/ 1.56 francesc 209: ThotBool UseMenuClicked (NotifyElement *event) 1.39 tollenae 210: { 1.43 tollenae 211: #ifdef TEMPLATES 1.70 quint 212: Document doc; 213: Element el, comp; 1.76 vatton 214: ElementType elType; 215: Attribute att; 216: AttributeType attributeType; 1.67 quint 217: XTigerTemplate t; 218: Declaration dec; 1.70 quint 219: Record rec, first; 1.52 vatton 220: int nbitems, size; 221: struct menuType *items; 222: char *types, *menuString; 1.77 ! vatton 223: View view; ! 224: ! 225: TtaGetActiveView (&doc, &view); ! 226: if (view != 1) ! 227: return FALSE; /* let Thot perform normal operation */ 1.70 quint 228: 229: doc = event->document; 230: el = event->element; 1.76 vatton 231: elType = TtaGetElementType (el); 232: t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url); 1.67 quint 233: if (!t) 1.77 ! vatton 234: return FALSE; /* let Thot perform normal operation */ 1.67 quint 235: 1.76 vatton 236: // give the list of possible items 237: attributeType.AttrSSchema = elType.ElSSchema; 238: attributeType.AttrTypeNum = Template_ATTR_types; 239: att = TtaGetAttribute (el, attributeType); 240: size = TtaGetTextAttributeLength (att); 1.52 vatton 241: types = (char *) TtaGetMemory (size+1); 1.76 vatton 242: TtaGiveTextAttributeValue (att, types, &size); 1.52 vatton 243: giveItems (types, size, &items, &nbitems); 1.70 quint 244: TtaFreeMemory (types); 245: 1.67 quint 246: if (nbitems == 1) 247: { 1.76 vatton 248: dec = GetDeclaration (t, items[0].label); 1.69 quint 249: /* if it's a union, display the menu of this union */ 1.70 quint 250: if (dec) 1.76 vatton 251: switch (dec->nature) 1.70 quint 252: { 253: case SimpleTypeNat : 254: nbitems = 0; 255: break; 256: case XmlElementNat : 257: nbitems = 0; 258: break; 259: case ComponentNat : 260: nbitems = 0; 261: break; 262: case UnionNat : 263: first = dec->unionType.include->first; 264: rec = first; 265: /* count the number of elements in the union */ 266: nbitems = 0; 267: while (rec) 268: { 269: nbitems++; 270: rec = rec->next; 271: } 272: if (nbitems > 0) 273: { 1.76 vatton 274: items = (menuType*) TtaGetMemory (sizeof (struct menuType)* nbitems); 1.70 quint 275: rec = first; 276: nbitems = 0; 277: while (rec) 278: { 1.76 vatton 279: items[nbitems].label = (char *) TtaStrdup (rec->key); 1.70 quint 280: items[nbitems].type = SimpleTypeNat; /* @@@@@ ???? @@@@@ */ 281: nbitems++; 282: rec = rec->next; 283: } 284: } 285: break; 286: default : 287: //Impossible 288: break; 289: } 290: } 291: if (nbitems > 0) 292: { 293: menuString = createMenuString (items, nbitems); 294: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), 295: NULL, nbitems, menuString , NULL, false, 'L'); 296: TtaFreeMemory (menuString); 1.74 vatton 297: ReturnOption = -1; // no selection yet 1.70 quint 298: TtaShowDialogue (BaseDialog + OptionMenu, FALSE); 1.76 vatton 299: TtaWaitShowProcDialogue (); 1.70 quint 300: TtaDestroyDialogue (BaseDialog + OptionMenu); 301: /* result: items[ReturnOption].label @@@@@ */ 1.75 quint 302: if (ReturnOption != -1) 1.76 vatton 303: dec = GetDeclaration (t, items[ReturnOption].label); 1.70 quint 304: TtaFreeMemory (items); 1.75 quint 305: if (ReturnOption == -1) 306: return FALSE; 1.70 quint 307: if (dec) 1.67 quint 308: { 1.76 vatton 309: switch (dec->nature) 1.70 quint 310: { 311: case SimpleTypeNat : 312: /* @@@@@ */ 313: break; 314: case XmlElementNat : 315: /* @@@@@ */ 316: break; 317: case ComponentNat : 318: /* copy element dec->componentType.content */ 319: comp = TtaCopyTree (dec->componentType.content, doc, doc, el); 320: TtaInsertFirstChild (&comp, el, doc); 321: /* @@@@@ */ 322: break; 323: case UnionNat : 324: /* @@@@@ */ 325: break; 326: default : 327: //Impossible 328: break; 329: } 1.67 quint 330: } 331: } 1.77 ! vatton 332: return TRUE; 1.63 vatton 333: #endif /* TEMPLATES */ 334: return TRUE; 335: } 1.42 tollenae 336: 1.63 vatton 337: /*---------------------------------------------------------------------- 338: OptionMenuClicked 339: ----------------------------------------------------------------------*/ 340: ThotBool OptionMenuClicked (NotifyElement *event) 341: { 342: #ifdef TEMPLATES 1.75 quint 343: Element child, grandChild, next; 344: ElementType elType, elType1; 345: Document doc; 346: XTigerTemplate t; 1.77 ! vatton 347: View view; 1.75 quint 348: 1.77 ! vatton 349: TtaGetActiveView (&doc, &view); ! 350: if (view != 1) ! 351: return FALSE; /* let Thot perform normal operation */ 1.75 quint 352: doc = event->document; 353: child = TtaGetFirstChild (event->element); 354: if (!child) 1.77 ! vatton 355: return FALSE; /* let Thot perform normal operation */ 1.75 quint 356: elType = TtaGetElementType (child); 357: elType1 = TtaGetElementType (event->element); 1.76 vatton 358: if ((elType.ElTypeNum != Template_EL_useEl && 359: elType.ElTypeNum != Template_EL_useSimple) || 1.75 quint 360: elType.ElSSchema != elType1.ElSSchema) 361: return FALSE; 362: grandChild = TtaGetFirstChild (child); 363: if (!grandChild) 1.76 vatton 364: /* the "use" element is empty. Instantiate it */ 1.75 quint 365: { 1.76 vatton 366: t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url); 1.75 quint 367: if (!t) 368: return FALSE; // no template ?!?! 1.76 vatton 369: InstantiateUse (t, child, doc, TRUE); 1.75 quint 370: } 371: else 372: /* remove the content of the "use" element */ 373: { 374: do 375: { 376: next = grandChild; 377: TtaNextSibling (&next); 378: TtaDeleteTree (grandChild, doc); 379: grandChild = next; 380: } 381: while (next); 382: } 1.77 ! vatton 383: return TRUE; /* don't let Thot perform normal operation */ 1.57 francesc 384: #endif /* TEMPLATES */ 1.52 vatton 385: return TRUE; 1.42 tollenae 386: } 1.51 francesc 387: 1.56 francesc 388: /*---------------------------------------------------------------------- 1.63 vatton 389: RepeatMenuClicked 1.56 francesc 390: Shows a menu with all the types that can be used in a use element. 391: ----------------------------------------------------------------------*/ 1.63 vatton 392: ThotBool RepeatMenuClicked (NotifyElement *event) 1.56 francesc 393: { 394: #ifdef TEMPLATES 1.70 quint 395: XTigerTemplate t; 396: Document doc; 397: Element el, child, newEl; 398: ElementType elt, elt1; 1.63 vatton 399: int nbitems, size; 400: struct menuType *items; 401: char *types, *menuString; 1.77 ! vatton 402: View view; ! 403: ! 404: TtaGetActiveView (&doc, &view); ! 405: if (view != 1) ! 406: return FALSE; /* let Thot perform normal operation */ 1.63 vatton 407: 1.70 quint 408: doc = event->document; 1.76 vatton 409: t = (XTigerTemplate) Get (Templates_Dic, DocumentMeta[doc]->template_url); 1.70 quint 410: if (!t) 1.77 ! vatton 411: return FALSE; /* let Thot perform normal operation */ 1.63 vatton 412: types = "top end"; 413: size = strlen (types); 414: giveItems (types, size, &items, &nbitems); 415: menuString = createMenuString (items, nbitems); 416: TtaNewScrollPopup (BaseDialog + OptionMenu, TtaGetViewFrame (doc, 1), NULL, 417: nbitems, menuString , NULL, false, 'L'); 418: TtaFreeMemory (menuString); 1.74 vatton 419: ReturnOption = -1; // no selection yet 1.63 vatton 420: TtaShowDialogue (BaseDialog + OptionMenu, FALSE); 1.76 vatton 421: TtaWaitShowProcDialogue (); 1.63 vatton 422: TtaDestroyDialogue (BaseDialog + OptionMenu); 1.70 quint 423: if (ReturnOption == 0 || ReturnOption == 1) 424: { 425: el = event->element; 426: child = TtaGetFirstChild (el); 427: if (child) 428: { 429: elt = TtaGetElementType (el); 430: elt1 = TtaGetElementType (child); 431: if (elt.ElSSchema == elt1.ElSSchema && 1.76 vatton 432: (elt1.ElTypeNum == Template_EL_useEl || 433: elt1.ElTypeNum == Template_EL_useSimple)) 1.70 quint 434: { 1.76 vatton 435: newEl = InstantiateUse (t, child, doc, FALSE); 1.70 quint 436: if (newEl) 437: { 438: if (ReturnOption == 0) 439: TtaInsertFirstChild (&newEl, el, doc); 440: else 441: { 442: child = TtaGetLastChild (el); 443: TtaInsertSibling (newEl, child, FALSE, doc); 444: } 445: } 446: } 447: } 448: } 1.77 ! vatton 449: return TRUE; /* don't let Thot perform normal operation */ 1.57 francesc 450: #endif /* TEMPLATES */ 1.56 francesc 451: return TRUE; 452: } 1.64 francesc 453: 1.66 vatton 454: /*---------------------------------------------------------------------- 455: ----------------------------------------------------------------------*/ 1.76 vatton 456: void OpeningInstance (char *fileName, Document doc) 1.65 francesc 457: { 458: #ifdef TEMPLATES 1.76 vatton 459: XTigerTemplate t; 460: char *content, *ptr; 461: gzFile stream; 462: char buffer[2000]; 1.77 ! vatton 463: int res; 1.65 francesc 464: 1.76 vatton 465: stream = TtaGZOpen (fileName); 466: if (stream != 0) 1.65 francesc 467: { 1.76 vatton 468: res = gzread (stream, buffer, 1999); 469: if (res >= 0) 1.65 francesc 470: { 1.76 vatton 471: ptr = strstr (buffer, "<?xtiger"); 472: if (ptr) 473: ptr = strstr (ptr, "template"); 474: if (ptr) 475: ptr = strstr (ptr, "="); 476: if (ptr) 477: ptr = strstr (ptr, "\""); 478: if (ptr) 479: { 480: // template URI 481: content = &ptr[1]; 482: ptr = strstr (content, "\""); 483: } 484: if (ptr) 485: { 486: *ptr = EOS; 487: //Get now the template URI 488: DocumentMeta[doc]->template_url = TtaStrdup (content); 489: if (Templates_Dic == NULL) 490: InitializeTemplateEnvironment (); 491: t = (XTigerTemplate) Get (Templates_Dic, content); 492: if (!t) 493: { 494: LoadTemplate (0, content); 495: t = (XTigerTemplate) Get (Templates_Dic, content); 496: } 497: AddUser (t); 498: } 1.65 francesc 499: } 500: } 1.76 vatton 501: TtaGZClose (stream); 1.65 francesc 502: #endif /* TEMPLATES */ 503: } 504: 1.64 francesc 505: /*---------------------------------------------------------------------- 1.65 francesc 506: ClosingInstance 1.64 francesc 507: Callback called before closing a document. Checks for unused templates. 508: ----------------------------------------------------------------------*/ 1.65 francesc 509: ThotBool ClosingInstance(NotifyDialog* dialog) 1.64 francesc 510: { 1.65 francesc 511: #ifdef TEMPLATES 512: //If it is a template all has been already freed 1.76 vatton 513: if (DocumentMeta[dialog->document] == NULL) 514: return FALSE; 1.65 francesc 515: 516: char *turl = DocumentMeta[dialog->document]->template_url; 1.73 vatton 517: if (turl) 1.65 francesc 518: { 1.76 vatton 519: XTigerTemplate t = (XTigerTemplate) Get (Templates_Dic, turl); 1.73 vatton 520: if (t) 1.76 vatton 521: RemoveUser (t); 522: TtaFreeMemory (turl); 1.65 francesc 523: } 524: #endif /* TEMPLATES */ 525: return FALSE; 1.64 francesc 526: }