2e44b52a0b77a34f09cf4bd2dc924fcd4ea676ef
[sdk] / documentor / src / Documentor.ec
1 import "ecere"
2 import "ec"
3 import "HTMLView"
4 import "IDESettings"
5 import "SettingsDialog"
6
7 static Context globalContext { };
8 static OldList defines { };
9 static OldList imports { };
10 static NameSpace globalData;
11 static OldList excludedSymbols { offset = (uint)&((Symbol)0).left }; 
12
13 #define UTF8_NUM_BYTES(x)  (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
14
15 default:
16 /*extern */int __ecereVMethodID_class_OnGetString;
17 private:
18
19
20 static void Dummy()
21 {
22 int a;
23 a.OnGetString(null, null, null);
24 }
25
26 static bool editing = true;
27
28 enum CodeObjectType { typeClass, typeData, typeMethod, typeEvent, typeProperty, typeNameSpace, typeDataType, typeEnumValue, typeDataPrivate, typeMethodPrivate, typePropertyPrivate };
29
30 static char * iconNames[CodeObjectType] = 
31 {
32    "<:ecere>constructs/class.png",
33    "<:ecere>constructs/data.png",
34    "<:ecere>constructs/method.png",
35    "<:ecere>constructs/event.png",
36    "<:ecere>constructs/property.png",
37    "<:ecere>constructs/namespace.png",
38    "<:ecere>constructs/dataType.png",
39    "<:ecere>constructs/enumValue.png",
40    "<:ecere>constructs/dataPrivate.png",
41    "<:ecere>constructs/methodPrivate.png",
42    "<:ecere>constructs/propertyPrivate.png"
43 };
44
45 IDESettings settings { }; // instantiate the IDESettings class from the IDESettings.ec file. Do this at a global level so that all methods can access settings.
46
47 IDESettingsContainer settingsContainer
48 {
49    driver = "JSON";
50    data = settings;
51    dataOwner = &settings;
52 };
53
54 // WARNING : This function expects a null terminated string since it recursively concatenate...
55 static void _PrintType(Type type, char * string, bool printName, bool printFunction, bool fullName)
56 {
57    if(type)
58    {
59       switch(type.kind)
60       {
61          case classType:
62             if(type._class && type._class.string)
63             {
64                if(fullName)
65                   strcat(string, type._class.string);
66                else
67                {
68                   if(type._class.registered)
69                   {
70                      char hex[10];
71                      sprintf(hex, "%08x", type._class.registered);
72                      strcat(string, "<a href=\"api://");
73                      strcat(string, hex);
74                      strcat(string, "\" style=\"text-decoration: none;\">");
75                      strcat(string, type._class.registered.name);
76                      strcat(string, "</a>");
77                   }
78                   else
79                      strcat(string, type._class.string);                     
80                }
81             }
82             break;
83          case pointerType:
84          {
85             /*Type funcType;
86             for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
87             if(funcType && funcType.kind == functionType)
88             {
89                Type param;
90                DocPrintType(funcType.returnType, string, false, fullName);
91                strcat(string, "(*");
92                if(printName || funcType.thisClass)
93                {
94                   strcat(string, " ");
95                   if(funcType.thisClass)
96                   {
97                      strcat(string, funcType.thisClass.string);
98                      strcat(string, "::");
99                   }
100                   if(type.name)
101                      strcat(string, type.name);
102                }
103                strcat(string, ")(");
104                for(param = funcType.params.first; param; param = param.next)
105                {
106                   DocPrintType(param, string, false, fullName);
107                   if(param.next) strcat(string, ", ");
108                }
109                strcat(string, ")");               
110             }
111             else*/
112             {
113                _PrintType(type.type, string, false /*printName*/, printFunction, fullName);
114                if(string[strlen(string)-1] == '(')
115                   strcat(string, "*");
116                else
117                   strcat(string, " *");
118             }
119             break;
120          }
121          case voidType: strcat(string, "void"); break;
122          case intType:  strcat(string, type.isSigned ? "int" : "uint"); break;
123          case int64Type:  strcat(string, type.isSigned ? "int64" : "uint64"); break;
124          case charType: strcat(string, type.isSigned ? "char" : "byte"); break;
125          case shortType: strcat(string, type.isSigned ? "short" : "uint16"); break;
126          case floatType: strcat(string, "float"); break;
127          case doubleType: strcat(string, "double"); break;
128          case structType:
129             if(type.enumName)
130             {
131                strcat(string, "struct ");
132                strcat(string, type.enumName);
133             }
134             else if(type.typeName)
135             {
136                strcat(string, type.typeName);
137             }
138             else
139             {
140                /*
141                strcat(string, "struct ");
142                strcat(string,"(unnamed)");
143                */
144                Type member;
145                strcat(string, "struct {");
146                for(member = type.members.first; member; member = member.next)
147                {
148                   DocPrintType(member, string, true, fullName);
149                   strcat(string,"; ");
150                }
151                strcat(string,"}");
152             }
153             break;
154          case unionType:
155             if(type.enumName)
156             {
157                strcat(string, "union ");
158                strcat(string, type.enumName);
159             }
160             else if(type.typeName)
161             {
162                strcat(string, type.typeName);
163             }
164             else
165             {
166                strcat(string, "union ");
167                strcat(string,"(unnamed)");
168             }
169             break;
170          case enumType:
171             if(type.enumName)
172             {
173                strcat(string, "enum ");
174                strcat(string, type.enumName);
175             }
176             else if(type.typeName)
177             {
178                strcat(string, type.typeName);
179             }
180             else
181                strcat(string, "enum");
182             break;
183          case functionType:
184          {
185             if(printFunction)
186             {
187                if(type.dllExport)
188                   strcat(string, "dllexport ");
189                DocPrintType(type.returnType, string, false, fullName);
190                strcat(string, " ");
191             }
192             
193             // DANGER: Testing This
194             if(printName)
195             {
196                if(type.name)
197                {
198                   if(fullName)
199                      strcat(string, type.name);
200                   else
201                   {
202                      char * name = RSearchString(type.name, "::", strlen(type.name), true, false);
203                      if(name) name += 2; else name = type.name;
204                      strcat(string, "<b>");
205                      strcat(string, name);
206                      strcat(string, "</b>");
207                   }
208                }
209                else
210                {
211                   printf("");
212                }
213             }
214
215             if(printFunction)
216             {
217                Type param;
218                strcat(string, "(");
219                for(param = type.params.first; param; param = param.next)
220                {
221                   DocPrintType(param, string, true, fullName);
222                   if(param.next) strcat(string, ", ");
223                }
224                strcat(string, ")");
225             }
226             break;
227          }
228          case arrayType:
229          {
230             /*Type funcType;
231             for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
232             if(funcType && funcType.kind == functionType)
233             {
234                Type param;
235                DocPrintType(funcType.returnType, string, false, fullName);
236                strcat(string, "(*");
237                if(printName || funcType.thisClass)
238                {
239                   strcat(string, " ");
240                   if(funcType.thisClass)
241                   {
242                      strcat(string, funcType.thisClass.string);
243                      strcat(string, "::");
244                   }
245                   if(type.name)
246                      strcat(string, type.name);
247                }
248                strcat(string, ")(");
249                for(param = funcType.params.first; param; param = param.next)
250                {
251                   DocPrintType(param, string, false, fullName);
252                   if(param.next) strcat(string, ", ");
253                }
254                strcat(string, ")");               
255             }
256             else*/
257             {
258                char baseType[1024], size[256];
259                Type arrayType = type;
260                baseType[0] = '\0';
261                size[0] = '\0';
262
263                while(arrayType.kind == TypeKind::arrayType)
264                {
265                   strcat(size, "[");
266                   if(arrayType.enumClass)
267                      strcat(size, arrayType.enumClass.string);
268                   else if(arrayType.arraySizeExp)
269                      PrintExpression(arrayType.arraySizeExp, size);
270                   //sprintf(string, "%s[%s]", baseType, size); 
271                   strcat(size, "]");
272
273                   arrayType = arrayType.arrayType;
274                }
275                _PrintType(arrayType, baseType, printName, printFunction, fullName);
276                strcat(string, baseType);
277                strcat(string, size);
278             }
279
280             /*
281                DocPrintType(type.arrayType, baseType, printName, fullName);
282                if(type.enumClass)
283                   strcpy(size, type.enumClass.string);
284                else if(type.arraySizeExp)
285                   PrintExpression(type.arraySizeExp, size);
286                //sprintf(string, "%s[%s]", baseType, size); 
287                strcat(string, baseType);
288                strcat(string, "[");
289                strcat(string, size); 
290                strcat(string, "]");
291                */
292
293             printName = false;
294             break;
295          }
296          case ellipsisType:
297             strcat(string, "...");
298             break;
299          case methodType:
300             _PrintType(type.method.dataType, string, false, printFunction, fullName);
301             break;
302          case subClassType:
303             strcat(string, "subclass(");
304             strcat(string, type._class ? type._class.string : "int");
305             strcat(string, ")");                  
306             break;
307          default:
308             printf("");
309       }
310       if(type.name && printName && type.kind != functionType && (type.kind != pointerType || type.type.kind != functionType))
311       {
312          strcat(string, " ");
313          strcat(string, type.name);
314       }
315    }
316 }
317
318 void DocPrintType(Type type, char * string, bool printName, bool fullName)
319 {
320    Type funcType;
321    for(funcType = type; funcType && (funcType.kind == pointerType || funcType.kind == arrayType); funcType = funcType.type);
322    if(funcType && funcType.kind == functionType && type != funcType)
323    {
324       char typeString[1024];
325       Type param;
326
327       DocPrintType(funcType.returnType, string, false, fullName);
328       strcat(string, "(");
329       _PrintType(type, string, printName, false, fullName);
330       strcat(string, ")");
331       /*
332       if(type.name)
333          strcat(string, type.name);
334       else
335       {
336          printf("");
337       }
338       */
339       strcat(string, "(");
340       for(param = funcType.params.first; param; param = param.next)
341       {
342          DocPrintType(param, string, true, fullName);
343          if(param.next) strcat(string, ", ");
344       }
345       strcat(string, ")");
346    }
347    else
348       _PrintType(type, string, printName, true, fullName);
349 }
350
351 void AddComponents(Module module, bool isDll)
352 {
353    DataRow row = null;
354    SubModule m;
355
356    if(module.name && (!strcmp(module.name, "ecere") || !strcmp(module.name, "ecereCOM")))
357    {
358       row = mainForm.browser.AddRow();
359       row.SetData(null, APIPageNameSpace { name = "ecereCOM", nameSpace = &module.application.systemNameSpace });
360       row.tag = (int)null;
361       AddNameSpace(row, null, module.application.systemNameSpace, null, "", !isDll);
362    }
363
364    for(m = module.modules.first; m; m = m.next)
365    {
366       if(m.importMode == publicAccess || !isDll)
367          AddComponents(m.module, true);
368    }
369
370    // PUT MODULE DESCRIPTION HERE
371    if(module.name && strcmp(module.name, "ecereCOM"))
372    {
373       row = mainForm.browser.AddRow();
374       row.SetData(null, APIPageNameSpace { name = module.name, module = module, nameSpace = &module.publicNameSpace });
375       row.tag = (int)module;
376       AddNameSpace(row, module, module.publicNameSpace, null /*module.application.systemNameSpace*/, "", !isDll);
377       if(!isDll)
378          AddNameSpace(row, module, module.privateNameSpace, null /*module.application.systemNameSpace*/, "", !isDll);
379    }
380 }
381
382 class APIPage
383 {
384 public:
385    char * name;
386    APIPage page;
387    char * label;
388    bool showPrivate;
389
390    char * OnGetString(char * tempString, void * fieldData, bool * needClass)
391    {
392       return name;
393    }
394
395    virtual void Generate(File f)
396    {
397       page.Generate(f);
398    }
399 };
400
401 enum DocumentationType
402 {
403    nameSpaceDoc,
404    classDoc,
405    functionDoc,
406    methodDoc
407 };
408
409 enum DocumentationItem
410 {
411    description,
412    usage,
413    remarks,
414    example,
415    seeAlso,
416    enumerationValue,
417    definition,
418    conversion,
419    memberDescription,
420    propertyDescription,
421    parameter,
422    returnValue
423 };
424
425 static void FigureFileName(char * fileName, Module module, DocumentationType type, void * object, DocumentationItem item, void * data)
426 {
427    NameSpace * nameSpace, * ns;
428    Class cl = null;
429    Method method = null;
430    GlobalFunction function = null;
431    char nsName[1024], temp[1024];
432    char docFile[1024];
433
434
435    switch(type)
436    {
437       case nameSpaceDoc: nameSpace = object; break;
438       case classDoc:     cl = (Class)object; nameSpace = cl.nameSpace; break;
439       case functionDoc:  function = object; nameSpace = function.nameSpace; break;
440       case methodDoc:    method = object; cl = method._class; nameSpace = cl.nameSpace; break;
441    }
442
443    nsName[0] = 0;
444    ns = nameSpace;
445    while(ns && ns->name)
446    {
447       strcpy(temp, "namespaces/");
448       strcat(temp, ns->name);
449       strcat(temp, "/");
450       strcat(temp, nsName);
451       strcpy(nsName, temp);
452       ns = ns->parent;
453    }
454    sprintf(docFile, "%s.eCdoc", (!module || !module.name || !strcmp(nsName, "namespaces/ecere/namespaces/com")) ? "ecereCOM" : module.name);
455
456    sprintf(fileName, "<%s/%s>", settings.docDir, docFile); // Note that in the ecereIDE.ini file, there can be no quotes around the path, and there needs to be the final backslash. Otherwise this does not work.
457    strcat(fileName, nsName);
458
459    if(cl)
460    {
461       strcat(fileName, "classes/");
462       strcat(fileName, cl.name);
463       strcat(fileName, "/");
464    }
465
466    if(method)
467    {
468       strcat(fileName, "methods/");
469       strcat(fileName, method.name);
470       strcat(fileName, "/");
471    }
472    else if(function)
473    {
474       char * name = RSearchString(function.name, "::", strlen(function.name), true, false);
475       if(name) name += 2; else name = function.name;
476       strcat(fileName, "functions/");
477       strcat(fileName, name);
478       strcat(fileName, "/");
479    }
480
481    switch(item)
482    {
483       case description: strcat(fileName, "description"); break;
484       case usage: strcat(fileName, "usage"); break;
485       case remarks: strcat(fileName, "remarks"); break;
486       case example: strcat(fileName, "example"); break;
487       case seeAlso: strcat(fileName, "seeAlso"); break;
488       case returnValue: strcat(fileName, "returnValue"); break;
489       case enumerationValue: 
490          strcat(fileName, "enumeration values/");
491          strcat(fileName, ((NamedLink)data).name);
492          break;
493       case definition:
494          strcat(fileName, "definitions/");
495          strcat(fileName, ((Definition)data).name);
496          break;
497       case conversion:
498       {
499          char * name = RSearchString(((Property)data).name, "::", strlen(((Property)data).name), true, false);
500          if(name) name += 2; else name = ((Property)data).name;
501          strcat(fileName, "conversions/");
502          strcat(fileName, name);
503          break;
504       }
505       case memberDescription:
506          strcat(fileName, "data members/");
507          strcat(fileName, ((DataMember)data).name);
508          break;
509       case propertyDescription:
510          strcat(fileName, "properties/");
511          strcat(fileName, ((Property)data).name);
512          break;
513       case parameter:
514       {
515          int count;
516          char name[1024];
517          Type prev;
518          strcat(fileName, "parameters/");
519          for(prev = data, count = 0; prev; prev = prev.prev, count++);
520          sprintf(name, "%s.%d", ((Type)data).name, count);
521          strcat(fileName, name);
522          break;
523       }
524    }
525 }
526
527 static char * ReadDoc(Module module, DocumentationType type, void * object, DocumentationItem item, void * data)
528 {
529    char fileName[MAX_LOCATION];
530    String contents = null;
531    File file;
532
533    FigureFileName(fileName, module, type, object, item, data);
534    file = FileOpen(fileName, read);
535    if(file)
536    {
537       uint len;
538       if((len = file.GetSize()))
539       {
540          contents = new char[len+1];
541          file.Read(contents, 1, len);
542          contents[len] = '\0';      
543       }
544       delete file;
545    }
546    if(contents)
547    {
548       int c;
549       for(c = 0; contents[c]; c++)
550          if(!isspace(contents[c])) break;
551       if(!contents[c])
552          delete contents;      
553    }
554    if(editing && !contents)
555       contents = CopyString($"[Add Text]");
556    return contents;
557 }
558
559 class APIPageNameSpace : APIPage
560 {
561    NameSpace * nameSpace;
562    Module module;
563    
564    void Generate(File f)
565    {
566       char string[1024];
567       char nsName[1024], temp[1024];
568       NameSpace * ns;
569       BTNamedLink link;
570       uint tag;
571
572       nsName[0] = 0;
573       ns = nameSpace;
574       while(ns && ns->name)
575       {
576          strcpy(temp, ns->name);
577          if(nsName[0]) strcat(temp, "::");
578          strcat(temp, nsName);
579          strcpy(nsName, temp);
580          ns = ns->parent;
581       }
582       // Generate Class Page
583       f.Printf($"<HTML><HEAD><TITLE>API Reference</TITLE></HEAD>\n<BODY><FONT SIZE=\"3\">\n");
584       if(nsName[0])
585       {
586          f.Printf("<FONT FACE=\"Arial\" SIZE=\"6\">%s</FONT><br><br>\n", nsName );
587          tag = (uint)nameSpace;
588          f.Printf($"Module: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", (module && module.name) ? module : null, (!module || !module.name || !strcmp(nsName, "ecere::com")) ? "ecereCOM" : module.name);
589       }
590       else
591       {
592          tag = (uint)((!module || !module.name || !strcmp(nsName, "ecere::com") ? null : module));
593          f.Printf($"<FONT FACE=\"Arial\" SIZE=\"6\">Module %s</FONT><br>\n", (!module || !module.name || !strcmp(nsName, "ecere::com")) ? "ecereCOM" : module.name);
594       }
595
596       nsName[0] = 0;
597       ns = nameSpace->parent;
598       while(ns && ns->name)
599       {
600          strcpy(temp, ns->name);
601          if(nsName[0]) strcat(temp, "::");
602          strcat(temp, nsName);
603          strcpy(nsName, temp);
604          ns = ns->parent;
605       }
606       if(nsName[0]) 
607          f.Printf($"Parent namespace: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", nameSpace->parent, nsName);
608
609       f.Printf("<br>");
610       {
611          char * desc = ReadDoc(module, nameSpaceDoc, nameSpace, description, null);
612          if(desc)
613          {
614             f.Printf($"<H3>Description</H3><br><br>\n");
615             if(editing)
616             {
617                char fileName[MAX_LOCATION];
618                FigureFileName(fileName, module, nameSpaceDoc, nameSpace, description, null);
619                f.Printf("<a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
620                f.Puts(desc);
621                f.Printf("</a><br><br>");
622             }
623             else
624                f.Printf("%s<br><br>", desc);
625             delete desc;
626          }
627       }
628
629       if(nameSpace->nameSpaces.first)
630       {
631          bool first = true;
632          for(ns = (NameSpace *)nameSpace->nameSpaces.first; ns; ns = (NameSpace *)((BTNode)ns).next)
633          {
634             char * desc = ReadDoc(module, nameSpaceDoc, ns, description, null);
635             if(first)
636             {
637                f.Printf($"<H3>Sub Namespaces</H3><br><br>\n");
638                f.Printf("<TABLE >\n");
639                first = false;
640             }
641             f.Printf("<TR>");
642             f.Printf("<TD valign=top height=22 nowrap=1><img valign=center src=\"%s\">&nbsp;&nbsp;<a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a></TD>", iconNames[typeNameSpace], ns, ns->name);
643             if(desc)
644             {
645                if(editing)
646                {
647                   char fileName[MAX_LOCATION];
648                   FigureFileName(fileName, module, nameSpaceDoc, ns, description, null);
649                   f.Printf("<TD valign=top height=22> <a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
650                   f.Puts(desc);
651                   f.Printf("</a></TD>");
652                }
653                else
654                   f.Printf("<TD valign=top height=22> %s</TD>", desc);
655                delete desc;
656             }
657             f.Printf("</TR><br>\n");
658          }
659          if(!first)
660             f.Printf("</TABLE><br>\n");
661       }
662
663       if(nameSpace->classes.first)
664       {
665          bool first = true;
666          for(link = (BTNamedLink)nameSpace->classes.first; link; link = (BTNamedLink)((BTNode)link).next)
667          {
668             Class cl = link.data;
669             if(!cl.templateClass)
670             {
671                char * desc = ReadDoc(module, classDoc, cl, description, null);
672
673                if(first)
674                {
675                   f.Printf($"<a name=Classes></a><H3>Classes</H3><br><br>\n");
676                   f.Printf("<TABLE >\n");
677                   first = false;
678                }
679
680                f.Printf("<TR>");
681
682                f.Printf("<TD valign=top height=22 nowrap=1><img valign=center src=\"%s\">&nbsp;&nbsp;<a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a></TD>", (cl.type == enumClass || cl.type == unitClass || cl.type == systemClass) ? iconNames[typeDataType] : iconNames[typeClass], cl, cl.name);
683                if(desc)
684                {
685                   if(editing)
686                   {
687                      char fileName[MAX_LOCATION];
688                      FigureFileName(fileName, module, classDoc, cl, description, null);
689                      f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
690                      f.Puts(desc);
691                      f.Printf("</a></TD>");
692                   }
693                   else
694                      f.Printf("<TD valign=top height=22>%s</TD>", desc);
695                   delete desc;
696                }            
697                f.Printf("</TR>\n");
698             }
699          }
700          if(!first)
701             f.Printf("</TABLE><br>\n");
702       }
703
704       if(nameSpace->functions.first)
705       {
706          bool first = true;
707          for(link = (BTNamedLink)nameSpace->functions.first; link; link = (BTNamedLink)((BTNode)link).next)
708          {
709             GlobalFunction function = link.data;
710             char * desc = ReadDoc(module, functionDoc, function, description, null);
711             char * name = RSearchString(function.name, "::", strlen(function.name), true, false);
712             if(name) name += 2; else name = function.name;
713             if(first)
714             {
715                f.Printf("<a name=Functions></a><H3>Functions</H3><br><br>\n");
716                f.Printf("<TABLE >\n");
717                first = false;
718             }
719             f.Printf("<TR>");
720             f.Printf("<TD valign=top height=22 nowrap=1><img valign=center src=\"%s\">&nbsp;&nbsp;<a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a></TD>", iconNames[typeMethod], function, name);
721             if(desc)
722             {
723                if(editing)
724                {
725                   char fileName[MAX_LOCATION];
726                   FigureFileName(fileName, module, functionDoc, function, description, null);
727                   f.Printf("<TD valign=top height=22> <a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
728                   f.Puts(desc);
729                   f.Printf("</a></TD>");
730                }
731                else
732                   f.Printf("<TD valign=top height=22> %s</TD>", desc);
733                delete desc;
734             }
735             f.Printf("</TR><br>\n");
736          }
737          if(!first)
738             f.Printf("</TABLE><br>\n");
739       }
740
741       if(nameSpace->defines.first)
742       {
743          bool first = true;
744          for(link = (BTNamedLink)nameSpace->defines.first; link; link = (BTNamedLink)((BTNode)link).next)
745          {
746             DefinedExpression def = link.data;
747             char * desc = ReadDoc(module, nameSpaceDoc, nameSpace, definition, def);
748             if(first)
749             {
750                f.Printf($"<a name=Definitions></a><H3>Definitions</H3><br><br>\n");
751                f.Printf("<TABLE >\n");
752                first = false;
753             }
754             f.Printf("<TR>");
755             f.Printf("<TD valign=top height=22 nowrap=1><a name=%08x></a><img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", def, iconNames[typeData], def.name);
756             f.Printf("<TD valign=top height=22>%s</TD>", def.value);
757             if(desc)
758             {
759                if(editing)
760                {
761                   char fileName[MAX_LOCATION];
762                   FigureFileName(fileName, module, nameSpaceDoc, nameSpace, definition, def);
763                   f.Printf("<TD valign=top height=22> <a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
764                   f.Puts(desc);
765                   f.Printf("</a></TD>");
766                }
767                else
768                   f.Printf("<TD valign=top height=22> %s</TD>", desc);
769                delete desc;
770             }
771             f.Printf("</TR><br>\n");
772          }
773          if(!first)
774             f.Printf("</TABLE><br>\n");
775       }
776
777       f.Printf("</FONT></BODY></HTML>\n");
778    }   
779 }
780
781 class APIPageClass : APIPage
782 {
783    Class cl;
784
785    void Generate(File f)
786    {
787       char string[1024];
788       Method method;
789       Property prop;
790       DataMember member;
791       char nsName[1024], temp[1024];
792       NameSpace * ns = cl.nameSpace;
793       Module module = cl.module;
794
795       nsName[0] = 0;
796       while(ns && ns->name)
797       {
798          strcpy(temp, ns->name);
799          if(nsName[0]) strcat(temp, "::");
800          strcat(temp, nsName);
801          strcpy(nsName, temp);
802          ns = ns->parent;
803       }
804       // Generate Class Page
805       f.Printf($"<HTML><HEAD><TITLE>API Reference</TITLE></HEAD>\n<BODY><FONT SIZE=\"3\">\n");
806       f.Printf("<FONT FACE=\"Arial\" SIZE=\"6\">%s</FONT><br><br>\n", name);
807
808       f.Printf($"Module: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", (module && module.name) ? module : null, (!module || !module.name || !strcmp(nsName, "ecere::com")) ? "ecereCOM" : module.name);
809       if(nsName[0]) 
810          f.Printf($"Namespace: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", cl.nameSpace, nsName);
811
812       {
813          char * classType = null;
814          switch(cl.type)
815          {
816             case bitClass:
817                classType = $"Bit Collection";
818                break;
819             case enumClass:
820                classType = $"Enumeration";
821                break;
822             case structClass:
823                classType = $"Structure";
824                break;
825             case normalClass:
826                classType = $"Class";
827                break;
828             case noHeadClass:
829                classType = $"Class (No header)";
830                break;
831             case unitClass:
832                classType = $"Unit";
833                break;
834             case systemClass:
835                classType = $"Basic Data Type";
836                break;
837          }
838          f.Printf($"Type: %s<br>\n", classType);
839       }
840       
841       if(cl.type != systemClass && cl.base)
842       {
843          f.Printf($"Base Class: ");
844          if(!strcmp(cl.base.name, "struct") || !strcmp(cl.base.name, "class"))
845          {
846             f.Printf(cl.type == bitClass ? cl.dataTypeString : $"None");
847          }
848          else if(cl.type == enumClass && !strcmp(cl.base.name, "enum"))
849             f.Printf("%s", cl.dataTypeString);
850          else
851             f.Printf("<a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a>", cl.base, cl.base.name);
852          f.Printf("<br>\n");
853       }
854
855       {
856          char * desc = ReadDoc(module, classDoc, cl, description, null);
857          if(desc)
858          {
859             f.Printf($"<br><H3>Description</H3><br><br>\n");
860             if(editing)
861             {
862                char fileName[MAX_LOCATION];
863                FigureFileName(fileName, module, classDoc, cl, description, null);
864                f.Printf("<a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
865                f.Puts(desc);
866                f.Printf("</a><br><br>");
867             }
868             else
869                f.Printf("%s<br><br>", desc);
870             delete desc;
871          }
872       }
873
874       if(cl.type == enumClass)
875       {
876          EnumClassData enumeration = (EnumClassData)cl.data;
877          if(enumeration.values.first)
878          {
879             NamedLink item;
880                         
881             f.Printf($"<a name=EnumerationValues></a><H3>Enumeration Values</H3><br><br>\n");
882             f.Printf("<TABLE >\n");
883
884             for(item = enumeration.values.first; item; item = item.next)
885             {
886                char * desc = ReadDoc(module, classDoc, cl, enumerationValue, item);
887                bool needClass = true;
888                Class dataClass;
889                Class base = cl;
890                while(base.type == enumClass) base = base.base;
891
892                if(base.type == systemClass ||
893                   (base.type == bitClass && base.membersAndProperties.first && !strcmp(cl.fullName, ((DataMember)base.membersAndProperties.first).dataTypeString)))
894                {
895                   if(!base.dataType)
896                      base.dataType = ProcessTypeString(base.dataTypeString, false);
897
898                   if(base.dataType.kind != classType)
899                   {
900                      char string[256];
901                      Symbol classSym;
902                      string[0] = '\0';
903                      PrintType(base.dataType, string, false, true);
904                      classSym = FindClass(string);
905                      dataClass = classSym ? classSym.registered : null;
906                   }
907                   else
908                      dataClass = base.dataType._class ? base.dataType._class.registered : null;
909                }
910                else
911                   dataClass = base;                  
912                
913                f.Printf("<TR>");
914                f.Printf("<TD valign=top height=22 nowrap=1><a name=%08x></a><img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", item, iconNames[typeEnumValue], item.name);
915                if(dataClass.type == systemClass)
916                {
917                   needClass = false;
918                   dataClass._vTbl[__ecereVMethodID_class_OnGetString](dataClass, &item.data, string, sizeof(string), &needClass);
919                }
920                else
921                   eSystem_FindClass(componentsApp, "class")._vTbl[__ecereVMethodID_class_OnGetString](dataClass, &item.data, string, sizeof(string), &needClass);
922                if(needClass)
923                   f.Printf("<TD valign=top height=22 nowrap=1>%s { %s }</TD>", dataClass.name, string);
924                else
925                   f.Printf("<TD valign=top height=22 nowrap=1>%s</TD>", string);
926                if(desc)
927                {
928                   if(editing)
929                   {
930                      char fileName[MAX_LOCATION];
931                      FigureFileName(fileName, module, classDoc, cl, enumerationValue, item);
932                      f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
933                      f.Puts(desc);
934                      f.Printf("</a></TD>");
935                   }
936                   else
937                      f.Printf("<TD valign=top height=22>%s</TD>", desc);
938                   delete desc;
939                }
940                f.Printf("</TR>");
941             }
942             f.Printf("</TABLE><BR>\n");
943          }
944       }
945
946       if(cl.conversions.first)
947       {
948          f.Printf($"<a name=Conversions></a><H3>Conversions</H3><br><br>\n");
949          f.Printf("<TABLE >\n");
950          for(prop = cl.conversions.first; prop; prop = prop.next)
951          {
952             if((prop.memberAccess == publicAccess || (prop.memberAccess == privateAccess && showPrivate)) && prop.name)
953             {
954                char * desc = ReadDoc(module, classDoc, cl, conversion, prop);
955                DataRow mRow;
956                char * name;
957                Type type = ProcessTypeString(prop.name, false);
958                name = RSearchString(prop.name, "::", strlen(prop.name), true, false);
959                if(name) name += 2; else name = prop.name;
960
961                f.Printf("<TR>");
962                
963                string[0] = 0;
964                DocPrintType(type, string, true, false);
965                
966                f.Printf("<TD valign=top height=22 nowrap=1><a name=%08x></a><img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", prop, iconNames[typeDataType], string);
967                if(desc)
968                {
969                   if(editing)
970                   {
971                      char fileName[MAX_LOCATION];
972                      FigureFileName(fileName, module, classDoc, cl, conversion, prop);
973                      f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
974                      f.Puts(desc);
975                      f.Printf("</a></TD>");
976                   }
977                   else
978                      f.Printf("<TD valign=top height=22>%s</TD>", desc);
979                   delete desc;
980                }
981                
982                f.Printf("</TR>\n");
983                
984                FreeType(type);
985             }
986          }
987          f.Printf("</TABLE><br>\n");
988       }
989
990       if(cl.membersAndProperties.first)
991       {
992          bool first = true;
993          for(prop = (Property)cl.membersAndProperties.first; prop; prop = prop.next)
994          {
995             if(prop.memberAccess == publicAccess || (prop.memberAccess == privateAccess && showPrivate))
996             {
997                if(first)
998                {
999                   f.Printf($"<a name=Members></a><H3>Properties and Members</H3><br><br>\n");
1000                   f.Printf("<TABLE >\n");
1001                   first = false;
1002                }
1003
1004                if(prop.isProperty)
1005                {
1006                   char * desc = ReadDoc(module, classDoc, cl, propertyDescription, prop);
1007                   if(!prop.dataType)
1008                      prop.dataType = ProcessTypeString(prop.dataTypeString, false);
1009
1010                   f.Printf("<TR>");
1011                   string[0] = 0;
1012                   DocPrintType(prop.dataType, string, true, false);
1013
1014                   f.Printf("<TD valign=top height=22 nowrap=1><a name=%08x></a><img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", prop, iconNames[typeProperty], prop.name);
1015                   f.Printf("<TD valign=top height=22 nowrap=1>%s</TD>", string);
1016                   if(desc)
1017                   {
1018                      if(editing)
1019                      {
1020                         char fileName[MAX_LOCATION];
1021                         FigureFileName(fileName, module, classDoc, cl, propertyDescription, prop);
1022                         f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1023                         f.Puts(desc);
1024                         f.Printf("</a></TD>");
1025                      }
1026                      else
1027                         f.Printf("<TD valign=top height=22>%s</TD>", desc);
1028                      delete desc;
1029                   }
1030                   f.Printf("</TR>\n");
1031                }
1032                else
1033                {
1034                   AddDataMemberToPage(f, (DataMember)prop, 0, showPrivate);
1035                }
1036             }
1037          }
1038          if(!first)
1039             f.Printf("</TABLE><br>\n");
1040       }
1041
1042       if(cl.methods.first)
1043       {
1044          bool first = true;
1045          // Virtual Methods
1046          for(method = (Method)cl.methods.first; method; method = (Method)((BTNode)method).next)
1047          {
1048             if((method.memberAccess == publicAccess || (method.memberAccess == privateAccess && showPrivate)) && method.type == virtualMethod)
1049             {
1050                char * desc = ReadDoc(module, methodDoc, method, description, null);
1051                if(first)
1052                {
1053                   f.Printf($"<a name=VirtualMethods></a><H3>Virtual Methods</H3><br><br>\n");
1054                   f.Printf("<TABLE >\n");
1055                   first = false;
1056                }
1057                if(!method.dataType)
1058                   ProcessMethodType(method);
1059
1060                f.Printf("<TR>");
1061                f.Printf("<TD valign=top height=22 nowrap=1><img valign=center src=\"%s\">&nbsp;&nbsp;<a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a></TD>", method.dataType.thisClass ? iconNames[typeEvent] : iconNames[typeMethod], method, method.name);
1062                if(desc)
1063                {
1064                   if(editing)
1065                   {
1066                      char fileName[MAX_LOCATION];
1067                      FigureFileName(fileName, module, methodDoc, method, description, null);
1068                      f.Printf("<TD valign=top height=22> <a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1069                      f.Puts(desc);
1070                      f.Printf("</a></TD>");
1071                   }
1072                   else
1073                      f.Printf("<TD valign=top height=22> %s</TD>", desc);
1074                   delete desc;
1075                }
1076                f.Printf("</TR><br>\n");
1077             }
1078          }
1079          if(!first)
1080             f.Printf("</TABLE><br>\n");
1081
1082          // Non-Virtual Methods
1083          first = true;
1084          for(method = (Method)cl.methods.first; method; method = (Method)((BTNode)method).next)
1085          {
1086             if((method.memberAccess == publicAccess || (method.memberAccess == privateAccess && showPrivate)) && method.type != virtualMethod)
1087             {
1088                char * desc = ReadDoc(module, methodDoc, method, description, null);
1089                if(first)
1090                {
1091                   f.Printf($"<a name=Methods></a><H3>Non-Virtual Methods</H3><br><br>\n");
1092                   f.Printf("<TABLE >\n");
1093                   first = false;
1094                }
1095
1096                if(!method.dataType)
1097                   ProcessMethodType(method);
1098
1099                f.Printf("<TR>");
1100                f.Printf("<TD valign=top height=22 nowrap=1><img valign=center src=\"%s\">&nbsp;&nbsp;<a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a></TD>", iconNames[typeMethod], method, method.name);
1101                if(desc)
1102                {
1103                   if(editing)
1104                   {
1105                      char fileName[MAX_LOCATION];
1106                      FigureFileName(fileName, module, methodDoc, method, description, null);
1107                      f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1108                      f.Puts(desc);
1109                      f.Printf("</a></TD>");
1110                   }
1111                   else
1112                      f.Printf("<TD valign=top height=22>%s</TD>", desc);
1113                   delete desc;
1114                }
1115                
1116                f.Printf("</TR><br>\n");
1117             }
1118          }
1119          if(!first)
1120             f.Printf("</TABLE><br>\n");
1121       }
1122       {
1123          char * usageDoc = ReadDoc(module, classDoc, cl, usage, null);
1124          if(usageDoc)
1125          {
1126             f.Printf($"<H3>Usage</H3><br>\n");
1127             if(editing)
1128             {
1129                char fileName[MAX_LOCATION];
1130                FigureFileName(fileName, module, classDoc, cl, usage, null);
1131                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1132                f.Puts(usageDoc);
1133                f.Printf("</a>\n");
1134             }
1135             else
1136                f.Printf("<br>%s\n", usageDoc);
1137             f.Printf("<br><br>\n");
1138             delete usageDoc;
1139          }
1140       }
1141       {
1142          char * exampleDoc = ReadDoc(module, classDoc, cl, example, null);
1143          if(exampleDoc)
1144          {
1145             f.Printf($"<H3>Example</H3><br>\n");
1146             f.Printf("<FONT face=\"Courier New\">\n");
1147             f.Printf("<br><TABLE >\n");
1148             if(editing)
1149             {
1150                char fileName[MAX_LOCATION];
1151                FigureFileName(fileName, module, classDoc, cl, example, null);
1152                f.Printf("<TR><TD><CODE><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1153                f.Puts(exampleDoc);
1154                f.Printf("</a></CODE></TD></TR>\n"); // bgcolor=#CFC9C0
1155             }
1156             else
1157                f.Printf("<TR><TD><CODE>%s</CODE></TD></TR>\n", exampleDoc);   // bgcolor=#CFC9C0
1158
1159             f.Printf("</TABLE></FONT>\n");
1160             f.Printf("<br>\n");
1161             delete exampleDoc;
1162          }
1163       }
1164       {
1165          char * remarksDoc = ReadDoc(module, classDoc, cl, remarks, null);
1166
1167          if(remarksDoc)
1168          {
1169             f.Printf($"<H3>Remarks</H3><br>\n");
1170             if(editing)
1171             {
1172                char fileName[MAX_LOCATION];
1173                FigureFileName(fileName, module, classDoc, cl, remarks, null);
1174                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1175                f.Puts(remarksDoc);
1176                f.Printf("</a>\n");
1177             }
1178             else
1179                f.Printf("<br>%s\n", remarksDoc);
1180             f.Printf("<br><br>\n");
1181             delete remarksDoc;
1182          }
1183       }
1184       
1185       if(cl.type != systemClass)
1186       {
1187          bool first = true;
1188          OldLink c;
1189          for(c = cl.derivatives.first; c; c = c.next)
1190          {
1191             Class deriv = c.data;
1192             // TO VERIFY: Does this properly check public status?
1193             if(eSystem_FindClass(componentsApp, deriv.fullName))
1194             {
1195                if(first)
1196                {
1197                   f.Printf($"<H3>Derived Classes</H3><br>\n");
1198                   f.Printf("<br>");
1199                   first = false;
1200                }
1201                else
1202                   f.Printf(", ");
1203                f.Printf("<a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a>", deriv, deriv.name);
1204              }            
1205          }
1206          if(!first)
1207             f.Printf("<br><br>\n");
1208       }
1209       {
1210          char * seeAlsoDoc = ReadDoc(module, classDoc, cl, seeAlso, null);
1211          if(seeAlsoDoc)
1212          {
1213             f.Printf($"<H3>See Also</H3><br>\n");
1214             if(editing)
1215             {
1216                char fileName[MAX_LOCATION];
1217                FigureFileName(fileName, module, classDoc, cl, seeAlso, null);
1218                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1219                f.Puts(seeAlsoDoc);
1220                f.Printf("</a>\n");
1221             }
1222             else
1223                f.Printf("<br>%s\n", seeAlsoDoc);
1224             f.Printf("<br><br>\n");
1225             delete seeAlsoDoc;
1226          }
1227       }
1228       f.Printf("</FONT></BODY></HTML>\n");
1229    }   
1230 }
1231
1232 class APIPageMethod : APIPage
1233 {
1234    Method method;
1235    void Generate(File f)
1236    {
1237       Class cl = method._class;
1238       char string[1024];
1239       Module module = cl.module;
1240       Type param;
1241       char nsName[1024], temp[1024];
1242       NameSpace * ns = cl.nameSpace;
1243
1244       nsName[0] = 0;
1245       while(ns && ns->name)
1246       {
1247          strcpy(temp, ns->name);
1248          if(nsName[0]) strcat(temp, "::");
1249          strcat(temp, nsName);
1250          strcpy(nsName, temp);
1251          ns = ns->parent;
1252       }
1253
1254       f.Printf($"<HTML><HEAD><TITLE>API Reference</TITLE></HEAD>\n<BODY><FONT SIZE=\"3\">\n");
1255       f.Printf("<FONT FACE=\"Arial\" SIZE=\"6\">%s</FONT><br><br>\n", name);
1256
1257       f.Printf($"Module: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", (module && module.name) ? module : null, (!module || !module.name || !strcmp(nsName, "ecere::com")) ? "ecereCOM" : module.name);
1258       if(nsName[0])
1259          f.Printf($"Namespace: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", cl.nameSpace, nsName);
1260       f.Printf("Class: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", cl, cl.name);
1261       if(method.dataType.staticMethod)
1262       {
1263          f.Printf($"this pointer class: None<br>\n");
1264       }
1265       else if(method.dataType.thisClass && method.dataType.thisClass.registered && (method.dataType.thisClass.registered != method._class || method.type == virtualMethod))
1266       {
1267          f.Printf($"this pointer class: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", method.dataType.thisClass.registered, method.dataType.thisClass.registered.name);
1268       }
1269
1270       // Generate Method Page
1271       string[0] = 0;
1272       if(!method.dataType.name)
1273          method.dataType.name = CopyString(method.name);
1274       DocPrintType(method.dataType, string, true, false);
1275       f.Printf("<br>%s", string);
1276
1277       {
1278          char * desc = ReadDoc(module, methodDoc, method, description, null);
1279          if(desc)
1280          {
1281             f.Printf($"<br><br><H3>Description</H3><br><br>\n");
1282             if(editing)
1283             {
1284                char fileName[MAX_LOCATION];
1285                FigureFileName(fileName, module, methodDoc, method, description, null);
1286                f.Printf("<a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1287                f.Puts(desc);
1288                f.Printf("</a>");
1289             }
1290             else
1291                f.Printf("%s", desc);
1292             delete desc;
1293          }
1294       }
1295
1296       f.Printf("<br><br>\n");
1297       if(method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType)
1298       {
1299          f.Printf($"<H3>Parameters</H3><br><br>\n");
1300       }
1301       if((method.dataType.returnType && method.dataType.returnType.kind != voidType) ||
1302          (method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType))
1303       {
1304          f.Printf("<TABLE  valign=center>\n");
1305       }
1306
1307       for(param = method.dataType.params.first; param; param = param.next)
1308       {
1309          // ADD DESCRIPTION HERE
1310          if(param.kind != voidType)
1311          {
1312             char * desc = ReadDoc(module, methodDoc, method, parameter, param);
1313             f.Printf("<TR>");
1314             string[0] = 0;
1315             DocPrintType(param, string, false, false);
1316
1317             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", param.name ? param.name : "");
1318             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", string);
1319             if(desc)
1320             {
1321                if(editing)
1322                {
1323                   char fileName[MAX_LOCATION];
1324                   FigureFileName(fileName, module, methodDoc, method, parameter, param);
1325                   f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1326                   f.Puts(desc);
1327                   f.Printf("s</a>&nbsp;</TD>\n");
1328                }
1329                else
1330                   f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", desc);
1331                delete desc;
1332             }
1333             
1334             f.Printf("</TR>\n");
1335          }
1336       }
1337       if(method.dataType.returnType && method.dataType.returnType.kind != voidType)
1338       {
1339          char * desc = ReadDoc(module, methodDoc, method, returnValue, null);
1340          if(method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType)
1341          {
1342             f.Printf("<TR><TD>&nbsp;</TD></TR>");
1343          }
1344          f.Printf("<TR>");
1345          f.Printf($"<TD valign=top height=22 nowrap=1><B>Return Value</B></TD>\n");
1346          string[0] = 0;
1347          DocPrintType(method.dataType.returnType, string, false, false);
1348          f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", string);
1349          if(desc)
1350          {
1351             if(editing)
1352             {
1353                char fileName[MAX_LOCATION];
1354                FigureFileName(fileName, module, methodDoc, method, returnValue, null);
1355                f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1356                f.Puts(desc);
1357                f.Printf("</a>&nbsp;</TD>\n");
1358             }
1359             else
1360                f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", desc);
1361             delete desc;
1362          }
1363          f.Printf("</TR>\n");
1364          f.Printf("</TABLE>\n");
1365       }
1366       if((method.dataType.returnType && method.dataType.returnType.kind != voidType) ||
1367          (method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType))
1368       {
1369          f.Printf("</TABLE><br>\n");
1370       }
1371       {
1372          char * usageDoc = ReadDoc(module, methodDoc, method, usage, null);
1373          if(usageDoc)
1374          {
1375             f.Printf($"<H3>Usage</H3><br>\n");
1376             if(editing)
1377             {
1378                char fileName[MAX_LOCATION];
1379                FigureFileName(fileName, module, methodDoc, method, usage, null);
1380                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1381                f.Puts(usageDoc);
1382                f.Printf("</a>\n");
1383             }
1384             else
1385                f.Printf("<br>%s\n", usageDoc);
1386             f.Printf("<br><br>\n");
1387             delete usageDoc;
1388          }
1389       }
1390       {
1391          char * exampleDoc = ReadDoc(module, methodDoc, method, example, null);
1392          if(exampleDoc)
1393          {
1394             f.Printf($"<H3>Example</H3><br>\n");
1395             f.Printf("<FONT face=\"Courier New\">\n");
1396             f.Printf("<br><TABLE >\n");
1397             if(editing)
1398             {
1399                char fileName[MAX_LOCATION];
1400                FigureFileName(fileName, module, methodDoc, method, example, null);
1401                f.Printf("<TR><TD><CODE><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1402                f.Puts(exampleDoc);
1403                f.Printf("</a></CODE></TD></TR>\n");   // bgcolor=#CFC9C0
1404             }
1405             else
1406                f.Printf("<TR><TD><CODE>%s</CODE></TD></TR>\n", exampleDoc);   // bgcolor=#CFC9C0
1407             f.Printf("</TABLE></FONT>\n");
1408             f.Printf("<br>\n");
1409             delete exampleDoc;
1410          }
1411       }
1412       {
1413          char * remarksDoc = ReadDoc(module, methodDoc, method, remarks, null);
1414          if(remarksDoc)
1415          {
1416             f.Printf($"<H3>Remarks</H3><br>\n");
1417             if(editing)
1418             {
1419                char fileName[MAX_LOCATION];
1420                FigureFileName(fileName, module, methodDoc, method, remarks, null);
1421                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1422                f.Puts(remarksDoc);
1423                f.Printf("</a>\n");
1424             }
1425             else
1426                f.Printf("<br>%s\n", method, remarksDoc);
1427             f.Printf("<br><br>\n");
1428             delete remarksDoc;
1429          }
1430       }
1431       {
1432          char * seeAlsoDoc = ReadDoc(module, methodDoc, method, seeAlso, null);
1433          if(seeAlsoDoc)
1434          {
1435             f.Printf($"<H3>See Also</H3><br>\n");
1436             if(editing)
1437             {
1438                char fileName[MAX_LOCATION];
1439                FigureFileName(fileName, module, methodDoc, method, seeAlso, null);
1440                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1441                f.Puts(seeAlsoDoc);
1442                f.Printf("</a>\n");
1443             }
1444             else
1445                f.Printf("<br>%s\n", method, seeAlsoDoc);
1446             
1447             f.Printf("<br><br>\n");
1448             delete seeAlsoDoc;
1449          }
1450       }
1451       f.Printf("</FONT></BODY></HTML>\n");
1452    }   
1453 }
1454
1455 class APIPageFunction : APIPage
1456 {
1457    GlobalFunction function;
1458    void Generate(File f)
1459    {
1460       char string[1024];
1461       Module module = function.module;
1462       Type param;
1463       char nsName[1024], temp[1024];
1464       NameSpace * ns = function.nameSpace;
1465
1466       nsName[0] = 0;
1467       while(ns && ns->name)
1468       {
1469          strcpy(temp, ns->name);
1470          if(nsName[0]) strcat(temp, "::");
1471          strcat(temp, nsName);
1472          strcpy(nsName, temp);
1473          ns = ns->parent;
1474       }
1475
1476       f.Printf($"<HTML><HEAD><TITLE>API Reference</TITLE></HEAD>\n<BODY><FONT SIZE=\"3\">\n");
1477       f.Printf("<FONT FACE=\"Arial\" SIZE=\"6\">%s</FONT><br><br>\n", name);
1478
1479       f.Printf($"Module: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n"), (module && module.name) ? module : null, (!module || !module.name || !strcmp(nsName, "ecere::com") ? "ecereCOM" : module.name);
1480       if(nsName[0])
1481          f.Printf($"Namespace: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", function.nameSpace, nsName);
1482
1483       if(!function.dataType)
1484          function.dataType = ProcessTypeString(function.dataTypeString, false);
1485
1486       if(function.dataType.thisClass && function.dataType.thisClass.registered)
1487       {
1488          f.Printf($"this pointer class: <a href=\"api://%08x\" style=\"text-decoration: none;\">%s</a><br>\n", function.dataType.thisClass.registered, function.dataType.thisClass.registered.name);
1489       }
1490
1491       // Generate Method Page
1492       string[0] = 0;
1493       if(!function.dataType.name)
1494          function.dataType.name = CopyString(function.name);
1495       DocPrintType(function.dataType, string, true, false);
1496       f.Printf("<br>%s", string);
1497
1498       {
1499          char * desc = ReadDoc(module, functionDoc, function, description, null);
1500          if(desc)
1501          {
1502             f.Printf($"<br><br><H3>Description</H3><br><br>\n");
1503             if(editing)
1504             {
1505                char fileName[MAX_LOCATION];
1506                FigureFileName(fileName, module, functionDoc, function, description, null);
1507                f.Printf("<a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1508                f.Puts(desc);
1509                f.Printf("</a>");
1510             }
1511             else
1512                f.Printf("%s", desc);
1513             delete desc;
1514          }
1515       }
1516       f.Printf("<br><br>\n");
1517       if(function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType)
1518       {
1519          f.Printf($"<H3>Parameters</H3><br><br>\n");
1520       }
1521       if((function.dataType.returnType && function.dataType.returnType.kind != voidType) ||
1522          (function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType))
1523       {
1524          f.Printf("<TABLE  valign=center>\n");
1525       }
1526
1527       for(param = function.dataType.params.first; param; param = param.next)
1528       {
1529          // ADD DESCRIPTION HERE
1530          if(param.kind != voidType)
1531          {
1532             char * desc = ReadDoc(module, functionDoc, function, parameter, param);
1533             f.Printf("<TR>");
1534             string[0] = 0;
1535             DocPrintType(param, string, false, false);
1536
1537             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", param.name ? param.name : "");
1538             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", string);
1539             if(param)
1540             {
1541                if(editing)
1542                {
1543                   char fileName[MAX_LOCATION];
1544                   FigureFileName(fileName, module, functionDoc, function, parameter, param);
1545                   f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1546                   f.Puts(desc);
1547                   f.Printf("</a>&nbsp;</TD>\n");
1548                }
1549                else
1550                   f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", desc);
1551                delete desc;
1552             }
1553             f.Printf("</TR>\n");
1554          }
1555       }
1556       if(function.dataType.returnType && function.dataType.returnType.kind != voidType)
1557       {
1558          char * desc = ReadDoc(module, functionDoc, function, returnValue, null);
1559          if(function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType)
1560          {
1561             f.Printf("<TR><TD>&nbsp;</TD></TR>");
1562          }
1563          f.Printf("<TR>");
1564          f.Printf($"<TD valign=top height=22 nowrap=1><B>Return Value</B></TD>\n");
1565          string[0] = 0;
1566          DocPrintType(function.dataType.returnType, string, false, false);
1567          f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", string);
1568          if(desc)
1569          {
1570             if(editing)
1571             {
1572                char fileName[MAX_LOCATION];
1573                FigureFileName(fileName, module, functionDoc, function, returnValue, null);
1574                f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1575                f.Puts(desc);
1576                f.Printf("</a>&nbsp;</TD>\n");
1577             }
1578             else
1579                f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", function, desc);
1580             delete desc;
1581          }
1582          f.Printf("</TR>\n");
1583          f.Printf("</TABLE>\n");
1584       }
1585       if((function.dataType.returnType && function.dataType.returnType.kind != voidType) ||
1586          (function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType))
1587       {
1588          f.Printf("</TABLE><br>\n");
1589       }
1590       {
1591          char * usageDoc = ReadDoc(module, functionDoc, function, usage, null);
1592          if(usageDoc)
1593          {
1594             f.Printf($"<H3>Usage</H3><br>\n");
1595             if(editing)
1596             {
1597                char fileName[MAX_LOCATION];
1598                FigureFileName(fileName, module, functionDoc, function, usage, null);
1599                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1600                f.Puts(usageDoc);
1601                f.Printf("</a>\n");
1602             }
1603             else
1604                f.Printf("<br>%s\n", usageDoc);
1605             f.Printf("<br><br>\n");
1606             delete usageDoc;
1607          }
1608       }
1609       {
1610          char * exampleDoc = ReadDoc(module, functionDoc, function, example, null);
1611          if(exampleDoc)
1612          {
1613             f.Printf($"<H3>Example</H3><br>\n");
1614             f.Printf("<FONT face=\"Courier New\">\n");
1615             f.Printf("<br><TABLE >\n");
1616             if(editing)
1617             {
1618                char fileName[MAX_LOCATION];
1619                FigureFileName(fileName, module, functionDoc, function, example, null);
1620                f.Printf("<TR><TD><CODE><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1621                f.Puts(exampleDoc);
1622                f.Printf("</a></CODE></TD></TR>\n");   // bgcolor=#CFC9C0
1623             }
1624             else
1625                f.Printf("<TR><TD><CODE>%s</CODE></TD></TR>\n", exampleDoc);   // bgcolor=#CFC9C0
1626             f.Printf("</TABLE></FONT>\n");
1627             f.Printf("<br>\n");
1628             delete exampleDoc;
1629          }
1630       }
1631       {
1632          char * remarksDoc = ReadDoc(module, functionDoc, function, remarks, null);
1633          if(remarksDoc)
1634          {
1635             f.Printf($"<H3>Remarks</H3><br>\n");
1636             if(editing)
1637             {
1638                char fileName[MAX_LOCATION];
1639                FigureFileName(fileName, module, functionDoc, function, remarks, null);
1640                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1641                f.Puts(remarksDoc);
1642                f.Printf("</a>\n");
1643             }
1644             else
1645                f.Printf("<br>%s\n", remarksDoc);
1646             f.Printf("<br><br>\n");
1647             delete remarksDoc;
1648          }
1649       }
1650       {
1651          char * seeAlsoDoc = ReadDoc(module, functionDoc, function, seeAlso, null);
1652          if(seeAlsoDoc)
1653          {
1654             f.Printf($"<H3>See Also</H3><br>\n");
1655             if(editing)
1656             {
1657                char fileName[MAX_LOCATION];
1658                FigureFileName(fileName, module, functionDoc, function, seeAlso, null);
1659                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1660                f.Puts(seeAlsoDoc);
1661                f.Printf("</a>\n");
1662             }
1663             else
1664                f.Printf("<br>%s\n", seeAlsoDoc);
1665             f.Printf("<br><br>\n");
1666             delete seeAlsoDoc;
1667          }
1668       }
1669       f.Printf("</FONT></BODY></HTML>\n");
1670    }   
1671 }
1672
1673 static void AddNameSpace(DataRow parentRow, Module module, NameSpace mainNameSpace, NameSpace comNameSpace, char * parentName, bool showPrivate)
1674 {
1675    char nsName[1024];
1676    NameSpace * ns;
1677    NameSpace * nameSpace = mainNameSpace;
1678    DataRow row;
1679    DataRow classesRow = null;
1680    DataRow functionsRow = null, definesRow = null;
1681    APIPage page;
1682
1683    char fileName[MAX_LOCATION];
1684    
1685    strcpy(nsName, parentName ? parentName : "");
1686    if(nameSpace->name)
1687    {
1688       if(nsName[0])
1689          strcat(nsName, "::");
1690       strcat(nsName, nameSpace->name);
1691    }
1692
1693    if(nsName[0])
1694    {
1695       row = parentRow.AddRow();
1696       row.SetData(null, (page = APIPageNameSpace { nameSpace->name, module = module, nameSpace = nameSpace, showPrivate = showPrivate }));
1697       row.tag = (int)nameSpace;
1698       row.icon = mainForm.icons[typeNameSpace];
1699    }
1700    else
1701    {
1702       // "Global NameSpace"
1703       row = parentRow;
1704       page = parentRow.GetData(null);
1705    }
1706
1707    {
1708       bool first = true;
1709
1710       for(ns = (NameSpace *)mainNameSpace.nameSpaces.first; ns; ns = (NameSpace *)((BTNode)ns).next)
1711       {
1712          NameSpace * comNS = (comNameSpace != null) ? (NameSpace *)comNameSpace.nameSpaces.FindString(ns->name) : null;
1713          AddNameSpace(row, module, ns, comNS, nsName, showPrivate);
1714       }
1715       if(comNameSpace != null)
1716       {
1717          for(ns = (NameSpace *)comNameSpace.nameSpaces.first; ns; ns = (NameSpace *)((BTNode)ns).next)
1718          {
1719             if(!mainNameSpace.nameSpaces.FindString(ns->name))
1720             {
1721                AddNameSpace(row, module, ns, null, nsName, showPrivate);
1722             }
1723          }
1724       }
1725    }
1726    if(mainNameSpace.classes.first || (comNameSpace && comNameSpace.classes.first))
1727    {
1728       for(nameSpace = mainNameSpace ; nameSpace; nameSpace = (nameSpace == mainNameSpace) ? comNameSpace : null)
1729       {
1730          if(nameSpace->classes.first)
1731          {
1732             BTNamedLink link;
1733             Class cl;
1734             for(link = (BTNamedLink)nameSpace->classes.first; link; link = (BTNamedLink)((BTNode)link).next)
1735             {
1736                cl = link.data;
1737                if(!cl.templateClass && (!module || cl.module == module || (!cl.module.name && !strcmp(module.name, "ecere"))))
1738                {
1739                   if(!classesRow) { classesRow = row.AddRow(); classesRow.SetData(null, APIPage { $"Classes", page = page }); classesRow.collapsed = true; classesRow.icon = mainForm.icons[typeClass]; classesRow.tag = 1; }
1740                   AddClass(classesRow, module, cl, nsName, showPrivate);
1741                }
1742             }
1743          }
1744       }
1745    }
1746
1747    if(mainNameSpace.functions.first || (comNameSpace && comNameSpace.functions.first))
1748    {
1749       for(nameSpace = mainNameSpace ; nameSpace; nameSpace = (nameSpace == mainNameSpace) ? comNameSpace : null)
1750       {
1751          if(nameSpace->functions.first)                            
1752          {
1753             BTNamedLink link;
1754             GlobalFunction fn;
1755             for(link = (BTNamedLink)nameSpace->functions.first; link; link = (BTNamedLink)((BTNode)link).next)
1756             {
1757                fn = link.data;
1758                if(!module || fn.module == module || (!fn.module.name && !strcmp(module.name, "ecere")))
1759                {
1760                   char * name = ( name = RSearchString(fn.name, "::", strlen(fn.name), false, false), name ? name + 2 : fn.name);
1761                   DataRow fnRow;
1762                   if(!functionsRow) { functionsRow = row.AddRow(); functionsRow.SetData(null, APIPage { $"Functions", page = page }); functionsRow.collapsed = true; functionsRow.icon = mainForm.icons[typeMethod];  functionsRow.tag = 2; };
1763                   fnRow = functionsRow.AddRow(); fnRow.SetData(null, APIPageFunction { name, function = fn }); fnRow.icon = mainForm.icons[typeMethod]; fnRow.tag = (int)fn;
1764                }
1765             }            
1766          }
1767       }
1768    }
1769    
1770    if(mainNameSpace.defines.first || (comNameSpace && comNameSpace.defines.first))
1771    {
1772       for(nameSpace = mainNameSpace ; nameSpace; nameSpace = (nameSpace == mainNameSpace) ? comNameSpace : null)
1773       {
1774          if(nameSpace->defines.first)
1775          {
1776             BTNamedLink link;
1777             Definition def;
1778             for(link = (BTNamedLink)nameSpace->defines.first; link; link = (BTNamedLink)((BTNode)link).next)
1779             {
1780                def = link.data;
1781                //if(def.module == module)
1782                {
1783                   char * name = ( name = RSearchString(def.name, "::", strlen(def.name), false, false), name ? name + 2 : def.name);
1784                   DataRow defRow;
1785                   if(!definesRow) { definesRow = row.AddRow(); definesRow.SetData(null, APIPage { $"Definitions", page = page }); definesRow.collapsed = true; definesRow.icon = mainForm.icons[typeData]; definesRow.tag = 3; };
1786                   defRow = definesRow.AddRow(); defRow.SetData(null, APIPage { name, page = page }); defRow.icon = mainForm.icons[typeData]; defRow.tag = (int)def;
1787                }
1788             }            
1789          }
1790       }
1791    }
1792 }
1793
1794 static void AddDataMemberToPage(File f, DataMember member, int indent, bool showPrivate)
1795 {
1796    char string[1024];
1797    int c;
1798    if(!member.dataType)
1799       member.dataType = ProcessTypeString(member.dataTypeString, false);
1800
1801    f.Printf("<TR>");
1802    string[0] = 0;
1803    DocPrintType(member.dataType, string, true, false);
1804
1805    f.Printf("<TD valign=top height=22 nowrap=1><a name=%08x></a>", member);
1806    for(c = 0; c<indent; c++)
1807       f.Printf("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
1808    f.Printf("<img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", iconNames[typeData], member.name ? member.name : ((member.type == structMember) ? "(struct)" : "(union)"));
1809    f.Printf("<TD valign=top height=22 nowrap=1>%s</TD>", (member.type == normalMember) ? string : "");
1810    if(member.type == normalMember)
1811    {
1812       char * desc = ReadDoc(member._class.module, classDoc, member._class, memberDescription, member);
1813       if(desc)
1814       {
1815          if(editing)
1816          {
1817             char fileName[MAX_LOCATION];
1818             FigureFileName(fileName, member._class.module, classDoc, member._class, memberDescription, member);
1819             f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1820             f.Puts(desc);
1821             f.Printf("</a></TD>");
1822          }
1823          else
1824             f.Printf("<TD valign=top height=22>%s</TD>", desc);
1825          delete desc;
1826       }
1827    }
1828    else
1829       f.Printf("<TD valign=top height=22></TD>");
1830    
1831    if(member.type != normalMember)
1832    {
1833       DataMember subMember;
1834       for(subMember = member.members.first; subMember; subMember = subMember.next)
1835       {
1836          if((subMember.memberAccess == publicAccess || (subMember.memberAccess == privateAccess && showPrivate)))
1837          {
1838             AddDataMemberToPage(f, subMember, indent + 1, showPrivate);
1839          }
1840       }
1841    }
1842    f.Printf("</TR><br>\n");
1843 }
1844
1845 static void AddDataMember(DataRow parentRow, APIPage page, DataMember member)
1846 {
1847    DataRow row;
1848    if(member.type == normalMember)
1849    {
1850       row = parentRow.AddRow(); row.SetData(null, APIPage { member.name, page = page }); row.icon = mainForm.icons[typeData];
1851       row.tag = (int)member;
1852    }
1853    else
1854    {
1855       DataMember m;
1856       row = parentRow.AddRow(); row.SetData(null, APIPage { (member.type == unionMember) ? "(union)" : "(struct)", page });
1857       row.icon = mainForm.icons[typeData];
1858       row.tag = (int)member;
1859
1860       for(m = member.members.first; m; m = m.next)
1861       {
1862          if(m.memberAccess == publicAccess || (m.memberAccess == privateAccess && page.showPrivate))
1863             AddDataMember(row, page, m);
1864       }
1865    }
1866 }
1867
1868 static void AddClass(DataRow parentRow, Module module, Class cl, char * nsName, bool showPrivate)
1869 {
1870    char fileName[MAX_LOCATION];
1871    char string[1024];
1872    Method method;
1873    Property prop;
1874    DataMember member;
1875    Type param;
1876    DataRow row;
1877    DataRow methodsRow = null, virtualsRow = null, eventsRow = null;
1878    DataRow propertiesRow = null, membersRow = null, conversionsRow = null, enumRow = null;
1879    APIPage page;
1880
1881    row = parentRow.AddRow();
1882    row.SetData(null, (page = APIPageClass { cl.name, cl = cl, showPrivate = showPrivate }));
1883    row.tag = (int)cl;
1884    row.collapsed = true;
1885    row.icon = (cl.type == enumClass || cl.type == unitClass || cl.type == systemClass) ? mainForm.icons[typeDataType] : mainForm.icons[typeClass];
1886
1887    // METHODS
1888    if(cl.methods.first)
1889    {
1890       for(method = (Method)cl.methods.first; method; method = (Method)((BTNode)method).next)
1891       {
1892          if(method.memberAccess == publicAccess || (method.memberAccess == privateAccess && showPrivate))
1893          {
1894             DataRow mRow;
1895             if(!method.dataType)
1896                ProcessMethodType(method);
1897             if(method.type == virtualMethod)
1898             {
1899                if(method.dataType.thisClass)
1900                {
1901                   if(!eventsRow) { eventsRow = row.AddRow(); eventsRow.SetData(null, APIPage { $"Events", page = page }); eventsRow.collapsed = true; eventsRow.icon = mainForm.icons[typeEvent];  eventsRow.tag = 4; }
1902                   mRow = eventsRow.AddRow(); mRow.SetData(null, APIPageMethod { method.name, method = method }); mRow.icon = mainForm.icons[typeEvent];
1903                   mRow.tag = (int)method;
1904                }
1905                else
1906                {
1907                   if(!virtualsRow) { virtualsRow = row.AddRow(); virtualsRow.SetData(null, APIPage { $"Virtual Methods", page = page }); virtualsRow.collapsed = true; virtualsRow.icon = mainForm.icons[typeMethod]; virtualsRow.tag = 4; }
1908                   mRow = virtualsRow.AddRow(); mRow.SetData(null, APIPageMethod { method.name, method = method }); mRow.icon = mainForm.icons[typeMethod];
1909                   mRow.tag = (int)method;
1910                }
1911             }
1912             else
1913             {
1914                if(!methodsRow) { methodsRow = row.AddRow(); methodsRow.SetData(null, APIPage { $"Methods", page = page }); methodsRow.collapsed = true; methodsRow.icon = mainForm.icons[typeMethod]; methodsRow.tag = 5; }
1915                mRow = methodsRow.AddRow(); mRow.SetData(null, APIPageMethod { method.name, method = method }); mRow.icon = mainForm.icons[typeMethod];
1916                mRow.tag = (int)method;
1917             }
1918          }
1919       }
1920    }
1921
1922    if(cl.membersAndProperties.first)
1923    {
1924       for(prop = (Property)cl.membersAndProperties.first; prop; prop = prop.next)
1925       {
1926          if(prop.memberAccess == publicAccess || (prop.memberAccess == privateAccess && showPrivate))
1927          {
1928             if(!prop.dataType)
1929                prop.dataType = ProcessTypeString(prop.dataTypeString, false);
1930             if(prop.isProperty)
1931             {
1932                DataRow mRow;
1933                if(!propertiesRow) { propertiesRow = row.AddRow(); propertiesRow.SetData(null, APIPage { $"Properties", page = page }); propertiesRow.collapsed = true; propertiesRow.icon = mainForm.icons[typeProperty]; propertiesRow.tag = 6; }
1934                mRow = propertiesRow.AddRow(); mRow.SetData(null, APIPage { prop.name, page }); mRow.icon = mainForm.icons[typeProperty];
1935                mRow.tag = (int)prop;
1936             }
1937             else
1938             {
1939                if(!membersRow) { membersRow = row.AddRow(); membersRow.SetData(null, APIPage { $"Data Members", page = page }); membersRow.collapsed = true; membersRow.icon = mainForm.icons[typeData]; membersRow.tag = 6; }
1940                AddDataMember(membersRow, page, (DataMember)prop);
1941             }
1942          }
1943       }
1944    }
1945
1946    if(cl.conversions.first)
1947    {
1948       for(prop = cl.conversions.first; prop; prop = prop.next)
1949       {
1950          DataRow mRow;
1951          char * name;
1952          if(!conversionsRow) { conversionsRow = row.AddRow(); conversionsRow.SetData(null, APIPage { $"Conversions", page = page }); conversionsRow.collapsed = true; conversionsRow.icon = mainForm.icons[typeDataType]; conversionsRow.tag = 7; }
1953          name = RSearchString(prop.name, "::", strlen(prop.name), true, false);
1954          if(name) name += 2; else name = prop.name;
1955          mRow = conversionsRow.AddRow(); mRow.SetData(null, APIPage { name, page = page }); mRow.icon = mainForm.icons[typeDataType];
1956          mRow.tag = (int)prop;
1957       }
1958    }
1959    if(cl.type == enumClass)
1960    {
1961       EnumClassData enumeration = (EnumClassData)cl.data;
1962       NamedLink item;
1963       for(item = enumeration.values.first; item; item = item.next)
1964       {
1965          DataRow mRow;                                                                                                                                                                                      
1966          if(!enumRow) { enumRow = row.AddRow(); enumRow.SetData(null, APIPage { $"Enumeration Values", page = page }); enumRow.collapsed = true; enumRow.icon = mainForm.icons[typeEnumValue]; enumRow.tag = 8; }
1967          mRow = enumRow.AddRow(); mRow.SetData(null, APIPage { item.name, page = page }); mRow.icon = mainForm.icons[typeEnumValue];         
1968          mRow.tag = (int)item;
1969       }
1970    }
1971 }
1972
1973 class MainForm : Window
1974 {
1975    size = { 1000, 600 };
1976    hasClose = true;
1977    borderStyle = sizable;
1978    hasMaximize = true;
1979    hasMinimize = true;
1980    text = $"API Documentation Browser";
1981
1982    BitmapResource icons[CodeObjectType];
1983
1984    MainForm()
1985    {
1986       CodeObjectType c;
1987       for(c = 0; c < CodeObjectType::enumSize; c++)
1988       {
1989          icons[c] = BitmapResource { iconNames[c], window = this, alphaBlend = true };
1990       }
1991       browser.AddField(DataField { dataType = class(APIPage) });
1992    }
1993
1994    hasMenuBar = true;
1995    menu = Menu { };
1996    Menu fileMenu { menu, $"File", f };
1997    Array<FileFilter> fileFilters
1998    { [
1999       { $"eC Shared Library files (*.dll, *.so, *.dylib)", "dll, so, dylib" },
2000       { $"eC Symbol files (*.sym)", "sym" }
2001    ] };
2002
2003    FileDialog fileDialog
2004    {
2005       filters = fileFilters.array, sizeFilters = fileFilters.count * sizeof(FileFilter)
2006    };
2007    MenuItem fileOpenItem
2008    {
2009       fileMenu, $"Open...", o, ctrlO;
2010
2011       bool NotifySelect(MenuItem selection, Modifiers mods)
2012       {
2013          if(fileDialog.Modal() == ok)
2014          {
2015             OpenModule(fileDialog.filePath);
2016          }
2017          return true;
2018       }
2019    };
2020    MenuItem fileSettingsItem
2021    {
2022       fileMenu, $"Settings...", s, ctrlS; // set the Settings item to the file menu with shortcut keys:s and ctrl+s
2023
2024       bool NotifySelect(MenuItem selection, Modifiers mods)
2025       {
2026          SettingsDialog { master = this }.Modal(); // Open the settings dialog to allow the user to change the directory for the eCdoc files
2027       }
2028    };
2029    MenuDivider { fileMenu };
2030    MenuItem fileExit { fileMenu, $"Exit", x, altF4, NotifySelect = MenuFileExit };
2031
2032    void OpenModule(char * filePath)
2033    {
2034       char extension[MAX_EXTENSION];
2035       Module module = null;
2036       static char symbolsDir[MAX_LOCATION];
2037
2038       FreeContext(globalContext);
2039       FreeExcludedSymbols(excludedSymbols);
2040       ::defines.Free(FreeModuleDefine);
2041       imports.Free(FreeModuleImport);
2042       
2043       FreeGlobalData(globalData);
2044       FreeTypeData(componentsApp);
2045       FreeIncludeFiles();
2046       delete componentsApp;
2047
2048       SetGlobalContext(globalContext);
2049       componentsApp = __ecere_COM_Initialize(false, 1, null);
2050       SetPrivateModule(componentsApp);
2051
2052       StripLastDirectory(filePath, symbolsDir);
2053       SetSymbolsDir(symbolsDir);
2054
2055       GetExtension(filePath, extension);
2056
2057       mainForm.browser.Clear();
2058
2059       ImportModule(filePath, normalImport, publicAccess, false);
2060
2061       if(extension[0] && strcmpi(extension, "so") && strcmpi(extension, "dll"))
2062          componentsApp.name = CopyString(filePath);
2063       
2064       for(module = componentsApp.allModules.first; module; module = module.next)
2065       {
2066          if(module.name && (!strcmp(module.name, "ecere") || !strcmp(module.name, "ecereCOM")))
2067             break;
2068       }
2069       if(!module)
2070          eModule_LoadStrict(componentsApp, "ecereCOM", publicAccess /*privateAccess*/);
2071       AddComponents(componentsApp, false);
2072
2073       for(module = componentsApp.allModules.first; module; module = module.next)
2074       {
2075          if(module.name && (!strcmp(module.name, filePath)))
2076             break;
2077       }
2078       if(!module) module = componentsApp;
2079       mainForm.browser.SelectRow(mainForm.browser.FindSubRow((int)module));
2080
2081       SetSymbolsDir(null);
2082    }
2083
2084    ListBox browser
2085    {
2086       this, anchor = { left = 0, top = 0, bottom = 0 }, borderStyle = 0, background = aliceBlue;
2087       treeBranches = true; collapseControl = true; fullRowSelect = false; rootCollapseButton = true;
2088       hotKey = alt0;
2089
2090       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
2091       {
2092          APIPage page = row.GetData(null);
2093          if(view.edit) view.OnLeftButtonDown(0,0,0);
2094          if(page && page.page) page = page.page;
2095          view.edit = false;
2096          view.PositionCaret(true);
2097          if(page != view.page)
2098          {
2099             Window activeChild = this.activeChild;
2100
2101             view.Destroy(0);
2102             if(page)
2103                view.Create();
2104             activeChild.Activate();
2105          }
2106          else if(!view.created)
2107             view.Create();
2108          
2109          {
2110             page = row.GetData(null);
2111             if(page && page.page)
2112             {
2113                switch(row.tag)
2114                {
2115                   case 1: view.GoToAnchor("Classes"); break;
2116                   case 2: view.GoToAnchor("Functions"); break;
2117                   case 3: view.GoToAnchor("Definitions"); break;
2118                   case 4: view.GoToAnchor("VirtualMethods"); break;
2119                   case 5: view.GoToAnchor("Methods"); break;
2120                   case 6: view.GoToAnchor("Members"); break;
2121                   case 7: view.GoToAnchor("Conversions"); break;
2122                   case 8: view.GoToAnchor("EnumerationValues"); break;
2123                   default:
2124                   {
2125                      char hex[10];
2126                      sprintf(hex, "%08x", row.tag);
2127                      view.GoToAnchor(hex);
2128                   }
2129                }
2130             }
2131             else
2132             {
2133                view.SetScrollPosition(0, 0);
2134             }
2135          }
2136          return true;
2137       }
2138    };
2139    HelpView view
2140    {
2141       this, anchor = { top = 0, bottom = 0, right = 0 };
2142       hotKey = escape;
2143    };
2144    PaneSplitter slider
2145    {
2146       this, leftPane = browser, rightPane = view, split = 300 /*scaleSplit = 0.3 */
2147    };
2148
2149    bool OnClose(bool parentClosing)
2150    {
2151       if(view.edit)
2152          view.OnLeftButtonDown(0,0,0);
2153       return true;
2154    }
2155
2156    bool OnPostCreate()
2157    {
2158       mainForm.OpenModule((((GuiApplication)__thisModule).argc > 1) ? ((GuiApplication)__thisModule).argv[1] : "ecere");
2159       //mainForm.OpenModule("ec");
2160       //mainForm.OpenModule("c:/games/chess/debug/chess.sym");
2161       //mainForm.OpenModule("c:/ide/Objects.IDE.Win32.Debug/ide.sym");
2162       {
2163          int index = mainForm.browser.currentRow.index;
2164          int rowHeight = mainForm.browser.rowHeight;
2165          int height = mainForm.browser.clientSize.h;
2166
2167          mainForm.browser.scroll = { 0, index * rowHeight - height / 2 };
2168       }
2169       return true;
2170    }
2171 };
2172
2173 class EditDialog : Window
2174 {
2175    borderStyle = sizable;
2176    size = { 600, 400 };
2177    autoCreate = false;
2178
2179    EditBox editBox
2180    {
2181       this, anchor = { left = 16, top = 16, right = 18, bottom = 61 }
2182    };
2183    Button saveChanges
2184    {
2185       this, text = $"Save Changes", anchor = { horz = 184, vert = 160 }
2186    };
2187    Button cancel
2188    {
2189       this, text = $"Cancel", anchor = { horz = 254, vert = 160 }
2190    };
2191 }
2192
2193 #define UTF8_IS_FIRST(x)   (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || (b) & 0x40); }))
2194 #define UTF8_NUM_BYTES(x)  (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
2195
2196 class HelpView : HTMLView
2197 {
2198    APIPage page;
2199
2200    hasVertScroll = true;
2201    hasHorzScroll = true;
2202    bool edit;
2203    char editString[MAX_LOCATION];
2204
2205    bool OnCreate()
2206    {
2207       TempFile f { };
2208       page = mainForm.browser.currentRow.GetData(null);
2209       if(page)
2210       {
2211          page.Generate(f);
2212          f.Seek(0, start);
2213          OpenFile(f, null);
2214          GoToAnchor(page.label);
2215          // Go to label...
2216          if(page.page) page = page.page;
2217       }
2218       delete f;
2219       return HTMLView::OnCreate();
2220    }
2221    EditDialog dialog
2222    {
2223
2224    };
2225
2226    void SaveEdit()
2227    {
2228       char archiveFile[MAX_LOCATION];
2229       char fileName[MAX_FILENAME];
2230       char directory[MAX_LOCATION];
2231       char * location;
2232       Archive archive;
2233       SplitArchivePath(editString, archiveFile, &location);
2234       GetLastDirectory(location, fileName);
2235       StripLastDirectory(location, directory);
2236       archive = ArchiveOpen(archiveFile, { true } );
2237       if(archive)
2238       {
2239          TempFile f { };
2240          ArchiveDir dir = archive.OpenDirectory(directory, null, replace);
2241          Block block;
2242          bool empty = true;
2243          for(block = textBlock.parent.subBlocks.first; block; block = block.next)
2244          {
2245             if(block.type == TEXT && block.textLen)
2246             {
2247                empty = false;
2248                break;
2249             }
2250          }
2251          if(!empty)
2252          {
2253             for(block = textBlock.parent.subBlocks.first; block; block = block.next)
2254             {
2255                if(block.type == BR)
2256                   f.Puts("<br>");
2257                else if(block.type == TEXT)
2258                   f.Write(block.text, 1, block.textLen);
2259             }
2260          }
2261          f.Seek(0, start);
2262          dir.AddFromFile(fileName, f, null, replace, 0, null, null);
2263          delete dir;
2264          delete archive;
2265          delete f;
2266          if(empty)
2267          {
2268             Block parent = textBlock.parent;
2269             while((block = parent.subBlocks.first))
2270             {
2271                parent.subBlocks.Remove(block);
2272                delete block;
2273             }
2274             textBlock = Block { type = TEXT, parent = parent, font = parent.font };
2275             textBlock.text = CopyString($"[Add Text]");
2276             textBlock.textLen = strlen(textBlock.text);
2277             parent.subBlocks.Add(textBlock);
2278          }
2279
2280          edit = false;            
2281          if(created)
2282          {
2283             ComputeMinSizes();
2284             ComputeSizes();
2285             PositionCaret(true);
2286             Update(null);
2287          }
2288       }
2289    }
2290
2291    bool OnLeftButtonDown(int x, int y, Modifiers mods)
2292    {
2293       if(edit)
2294       {
2295          // Update overLink
2296          HTMLView::OnMouseMove(x, y, mods);
2297          if(textBlock && overLink == textBlock.parent)
2298          {
2299             selPosition = curPosition = TextPosFromPoint(x, y, &textBlock);
2300             PositionCaret(true);            
2301          }
2302          else
2303          {
2304             SaveEdit();
2305             HTMLView::OnLeftButtonDown(x, y, mods);
2306          }
2307          return true;
2308       }
2309       return HTMLView::OnLeftButtonDown(x, y, mods);
2310    }
2311
2312    bool OnLeftButtonUp(int x, int y, Modifiers mods)
2313    {
2314       if(!edit || !textBlock || clickedLink != textBlock.parent)
2315       {
2316          HTMLView::OnLeftButtonUp(x, y, mods);
2317          if(edit)
2318          {
2319             selPosition = curPosition = TextPosFromPoint(x, y, &textBlock);
2320             PositionCaret(true);
2321          }
2322       }
2323       return true;
2324    }
2325
2326    // Returns true if it needs scrolling
2327    /*
2328    bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2329    {
2330       int w;
2331       int c;
2332       int x, y;
2333       EditLine line;
2334       bool needHScroll = false;
2335
2336       if(py < 0)
2337       {
2338          if(this.viewY > 0)
2339          {
2340             y = this.viewY-1;
2341             line = this.viewLine ? (void *)this.viewLine.prev : null;
2342          }
2343          else
2344          {
2345             y = 0;
2346             line = (void *)this.lines.first;
2347          }
2348       }
2349       else
2350       {
2351          py = Min(py, clientSize.h);
2352          py /= this.space.h;
2353          py = Min(py, this.lineCount);
2354          y = this.viewY;
2355          for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2356          {
2357             y++;
2358          }
2359       }
2360
2361       if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2362          needHScroll = true;
2363       px = Max(px,0);
2364       px = Min(px,clientSize.w+this.space.w);
2365
2366       if(tx && line)
2367       {
2368          *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2369       }
2370
2371       if(tline) *tline = line;
2372       if(ty) *ty = y;
2373
2374       // Prevent divide by 0 from non valid this.font
2375       if(!this.space.h)
2376          return (y < this.viewY) || needHScroll;
2377       else
2378          return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2379       return false;
2380    }
2381 */
2382 /*
2383    bool OnLeftButtonDown(int mx, int my, Modifiers mods)
2384    {
2385       int x,y;
2386       EditLine line;
2387
2388       if(style.noSelect) return true;
2389
2390       if(!mods.isActivate)
2391       {
2392          Capture();
2393          mouseSelect = true;
2394       }
2395
2396       mouseX = mx - XOFFSET;
2397       mouseY = my;
2398
2399       FindMouse(mouseX, mouseY, &x, &y, &line, true);
2400
2401       if(!style.readOnly)
2402       {
2403          if(wordSelect)
2404             mouseMove = false;
2405          else if(IsMouseOnSelection() && !mods.isActivate)
2406          {
2407             DirtyLine(this.y);
2408             mouseMove = true;
2409             dropX = x;
2410             dropY = y;
2411             dropLine = line;
2412          }
2413       }
2414
2415       if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
2416       {
2417          if(mods.shift && !mods.isActivate)
2418          {
2419             this.x = x;
2420             this.y = y;
2421             this.line = line;
2422             DirtyAll();
2423          }
2424          else
2425          {
2426             SelDirty();
2427             DirtyLine(this.y);
2428             this.x = x;
2429             this.y = y;
2430             this.line = line;
2431             DirtyLine(this.y);
2432             this.selLine = this.line;
2433             this.selX = this.x;
2434             this.selY = this.y;
2435             //Deselect();
2436          }
2437          ComputeColumn();
2438       }
2439       
2440       UpdateDirty();
2441       UpdateCaretPosition(true);
2442       return true;
2443    }
2444 */
2445 /*
2446    bool OnLeftButtonUp(int x, int y, Modifiers mods)
2447    {
2448       timer.Stop();
2449
2450       mouseSelect = false;
2451       wordSelect = false;
2452       
2453       x -= XOFFSET;
2454       
2455       ReleaseCapture();
2456       if(!style.readOnly)
2457       {
2458          if(mouseMove)
2459          {
2460             EditLine line;
2461             FindMouse(mouseX, mouseY, &x, &y, &line, true);
2462     
2463             dropX = x;
2464             dropY = y;
2465             dropLine = line;
2466
2467             mouseMove = IsMouseOnSelection();
2468
2469             if(!mouseMove)
2470             {
2471                int size = SelSize();
2472                if(size)
2473                {
2474                   char * text = new char[size+1];
2475                   if(text)
2476                   {
2477                      int moveX = 0;
2478                      GetSel(text, false);
2479
2480                      if(Max(selY, this.y) == dropY)
2481                      {
2482                         if(this.x > selX)
2483                         {
2484                            if(this.dropX > this.selX)
2485                               moveX = this.x - this.selX;
2486                         }
2487                         else
2488                         {
2489                            if(this.dropX > this.x)
2490                               moveX = this.selX - this.x;
2491                         }
2492                      }
2493                      DelSel(null);
2494                      this.dropX -= moveX;
2495                      this.selX = this.x = this.dropX;
2496                      this.selY = this.y = this.dropY;
2497                      this.selLine = this.line = this.dropLine;
2498                      AddS(text);
2499                      SetViewToCursor(true);
2500                      delete text;
2501                      Modified();
2502                   }
2503                }
2504             }
2505             else
2506             {
2507                SelDirty();
2508                DirtyLine(this.y);
2509                this.x = x;
2510                this.y = y;
2511                this.line = line;
2512                ComputeColumn();
2513                DirtyLine(this.y);
2514                Deselect();
2515                UpdateDirty();
2516             }
2517          }
2518          else
2519          {
2520             EditLine line;
2521             mouseX = x;
2522             mouseY = y;
2523
2524             FindMouse(mouseX, mouseY, &x, &y, &line, true);
2525
2526             NotifyDropped(master, this, x, y);
2527          }
2528       }
2529       mouseMove = false;
2530       return true;
2531    }
2532
2533    bool OnMouseMove(int mx, int my, Modifiers mods)
2534    {
2535       int x,y;
2536       EditLine line;
2537       bool needScroll;
2538       
2539       if(mods != -1 && mods.isSideEffect) 
2540       { 
2541          SetSelectCursor(); 
2542          return true; 
2543       }
2544       if(style.noSelect) return true;
2545       if(wordSelect) return true;
2546       mouseX = mx - XOFFSET;
2547       mouseY = my;
2548
2549       needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
2550
2551       if(this.mouseMove || this.mouseSelect)
2552       {
2553          if(!needScroll)
2554             timer.Stop();
2555          else 
2556          {
2557             if(needScroll)
2558                timer.Start();
2559             if(mods != -1 && 
2560                ((style.hScroll) || (style.vScroll)))
2561                return true;
2562          }
2563       }
2564
2565       if(this.mouseMove)
2566       {
2567          DirtyLine(this.dropY);
2568          this.dropX = x;
2569          this.dropY = y;
2570          DirtyLine(this.dropY);
2571          this.dropLine = line;
2572          SetViewToCursor(true);
2573       }
2574       else if(this.mouseSelect)
2575       {
2576          DirtyLine(this.selY);
2577          DirtyLine(this.y);
2578          this.x = x;
2579          this.y = y;
2580          ComputeColumn();
2581          DirtyLine(this.y);
2582          this.line = line;
2583          SetViewToCursor(true);
2584          UpdateDirty();
2585       }
2586       SetSelectCursor();
2587       return true;
2588    }
2589
2590    bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
2591    {
2592       int x,y;
2593       EditLine line;
2594       
2595       mx -= XOFFSET;
2596
2597       if(style.noSelect) return true;
2598       FindMouse(mx, my, &x, &y, &line, false);
2599       if(!NotifyDoubleClick(master, this, line, mods))
2600          return false;
2601       if(x < line.count)
2602       {
2603          int c;
2604          int start = -1;
2605          int numBytes;
2606          for(c = x; c >= 0; c--)
2607          {
2608             unichar ch;
2609             while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
2610             ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
2611             if(!IS_ALUNDER(ch))
2612                break;
2613             start = c;
2614          }
2615          if(start != -1)
2616          {
2617             for(c = start; c<line.count; c += numBytes)
2618             {
2619                unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
2620                if(!IS_ALUNDER(ch))
2621                   break;
2622             }
2623             SelDirty();
2624             DirtyLine(this.y);
2625             this.y = y;
2626             DirtyLine(this.y);
2627             this.selX = start;
2628             this.x = c;
2629             ComputeColumn();
2630             this.line = this.selLine = line;
2631             this.wordSelect = (c != start);
2632             UpdateDirty();
2633          }
2634       }
2635       return true;
2636    }
2637 */
2638    bool OnOpen(char * href)
2639    {
2640       if(!strncmp(href, "api://", 6))
2641       {
2642          int tag = strtoul(href + 6, null, 16);
2643          DataRow row = mainForm.browser.FindSubRow(tag);
2644          if(row)
2645          {
2646             edit = false;
2647             mainForm.browser.SelectRow(row);
2648             while((row = row.parent))
2649                row.collapsed = false;
2650             row = mainForm.browser.currentRow;
2651             mainForm.browser.scroll = { 0, row.index * mainForm.browser.rowHeight - mainForm.browser.clientSize.h / 2 };
2652          }
2653       }
2654       else if(!strncmp(href, "edit://", 7))
2655       {
2656          Block block;
2657          int startX = clickedLink.startX, startY = clickedLink.startY;
2658          for(block = (Block)clickedLink.subBlocks.first; block; block = block.next)
2659          {
2660             if(block.type == TEXT) startX = block.startX, startY = block.startY;
2661             if(block.type == BR && (!block.prev || !block.next || block.next.type != TEXT))
2662             {
2663                Block newBlock { type = TEXT, parent = block.parent, font = block.parent.font };
2664                int tw = 0, th = 0;
2665                display.FontExtent(block.font.font, " ", 1, null, &th);
2666                if(!block.prev)
2667                {
2668                   block.parent.subBlocks.Insert(null, newBlock);
2669                   block = newBlock;
2670                }
2671                else
2672                {
2673                   block.parent.subBlocks.Insert(block, newBlock);
2674                   startY += th;
2675                }               
2676                newBlock.startX = startX;
2677                newBlock.startY = startY;
2678                newBlock.text = new0 char[1];
2679             }
2680          }
2681
2682          textBlock = (Block)clickedLink.subBlocks.first;
2683          if(!strcmp(textBlock.text, $"[Add Text]"))
2684          {
2685             textBlock.text[0] = 0;
2686             textBlock.textLen = 0;               
2687          }
2688
2689          strcpy(editString, href + 7);
2690          selPosition = curPosition = 0;
2691          // dialog.Create();
2692          edit = true;
2693          PositionCaret(true);
2694       }
2695       return true;
2696    }
2697
2698    Block textBlock;
2699    char * text;
2700    int curPosition, selPosition;
2701
2702    bool OnKeyDown(Key key, unichar ch)
2703    {
2704       if(edit)
2705       {
2706          switch(key)
2707          {
2708             case escape:
2709                OnLeftButtonDown(0,0,0);
2710                return false;
2711             case end:
2712                selPosition = curPosition = textBlock.textLen;
2713                PositionCaret(true);
2714                Update(null);
2715                break;
2716             case home:
2717                selPosition = curPosition = 0;
2718                PositionCaret(true);
2719                Update(null);
2720                break;
2721             case ctrlHome:
2722                selPosition = curPosition = 0;
2723                while(textBlock.prev)
2724                   textBlock = textBlock.prev.prev;
2725                PositionCaret(true);
2726                Update(null);
2727                return false;
2728             case ctrlEnd:
2729                while(textBlock.next && textBlock.next.next)
2730                   textBlock = textBlock.next.next;
2731                selPosition = curPosition = textBlock.textLen;
2732                PositionCaret(true);
2733                Update(null);
2734                return false;
2735          }
2736       }
2737       else
2738          return HTMLView::OnKeyDown(key, ch);
2739       return true;
2740    }
2741    bool OnKeyHit(Key key, unichar ch)
2742    {
2743       if(edit)
2744       {
2745          switch(key)
2746          {
2747             case up:
2748             {
2749                if(caretY == textBlock.startY)
2750                {
2751                   if(textBlock.prev)
2752                   {
2753                      textBlock = textBlock.prev.prev;
2754                      selPosition = curPosition = Min(curPosition, textBlock.textLen);
2755                      PositionCaret(false);
2756                      caretY = MAXINT;
2757                   }
2758                   else
2759                      return false;
2760                }
2761
2762                {
2763                   int tw = 0, th = 0;
2764                   int textPos = 0;
2765                   int sx = textBlock.startX, sy = textBlock.startY;
2766                   char * text = textBlock.text;
2767                   int maxW;
2768                   Block block = textBlock;
2769                   while(block && block.type != TD) block = block.parent;
2770                   if(block)
2771                   {
2772                      Block table = block;
2773                      while(table && table.type != TABLE) table = table.parent;
2774                      if(table)
2775                         maxW = block.w - 2* table.cellPadding;
2776                      else
2777                         maxW = clientSize.w - 10 - sx;
2778                   }
2779                   else
2780                      maxW = clientSize.w - 10 - sx;
2781                   display.FontExtent(textBlock.font.font, " ", 1, null, &th);
2782
2783                   do
2784                   {
2785                      int startPos = textPos;
2786                      int width = 0;
2787                      int x = 0;
2788                      bool lineComplete = false;
2789                      for(; textPos<textBlock.textLen && !lineComplete;)
2790                      {
2791                         int w;
2792                         int len;
2793                         char * nextSpace = strchr(text + textPos, ' ');
2794
2795                         if(nextSpace)
2796                            len = (nextSpace - (text + textPos)) + 1;
2797                         else
2798                            len = textBlock.textLen - textPos;
2799                         
2800                         display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
2801
2802                         if(x + width + w > maxW && x > 0)
2803                         {
2804                            lineComplete = true;
2805                            break;
2806                         }
2807                         textPos += len;
2808                         width += w;
2809                         if(nextSpace)
2810                         {
2811                            x += width;
2812                            width = 0;
2813                         }
2814                         if(textPos == textBlock.textLen || (sy == caretY - th && caretX <= x + width + sx))
2815                         {
2816                            x += width;
2817                            curPosition = textPos;
2818                            while(curPosition > 0 && x + sx > caretX && textPos > startPos)
2819                            {
2820                               int len;
2821                               while(curPosition > 0 && !UTF8_IS_FIRST(text[--curPosition]));
2822                               len = curPosition - startPos;
2823                               display.FontExtent(textBlock.font.font, text + startPos, len, &x, null);
2824                            }
2825                            selPosition = curPosition;
2826                            PositionCaret(false);
2827                            return false;
2828                         }
2829                      }
2830                      if(sy == caretY - th || textPos == textBlock.textLen)
2831                      {
2832                         if(textPos != textBlock.textLen)
2833                         {
2834                            int c = textPos - 1;
2835                            while(c > 0 && text[c] == ' ') c--;
2836                            selPosition = curPosition = c + 1;
2837                         }
2838                         else
2839                            selPosition = curPosition = textBlock.textLen;
2840                         PositionCaret(false);
2841                         return false;
2842                      }
2843                      sy += th;
2844                      sx = textBlock.startX;
2845                   } while(textPos < textBlock.textLen);
2846                   return false;
2847                }
2848                return false;
2849             }
2850             case down:
2851             {
2852                int tw = 0, th = 0;
2853                int textPos = 0;
2854                int sx = textBlock.startX, sy = textBlock.startY;
2855                char * text = textBlock.text;
2856                int maxW;
2857                Block block = textBlock;
2858                while(block && block.type != TD) block = block.parent;
2859                if(block)
2860                {
2861                   Block table = block;
2862                   while(table && table.type != TABLE) table = table.parent;
2863                   if(table)
2864                      maxW = block.w - 2* table.cellPadding;
2865                   else
2866                      maxW = clientSize.w - 10 - sx;
2867                }
2868                else
2869                   maxW = clientSize.w - 10 - sx;
2870                display.FontExtent(textBlock.font.font, " ", 1, null, &th);
2871
2872                while(!textPos || textPos < textBlock.textLen)
2873                {
2874                   int startPos = textPos;
2875                   int width = 0;
2876                   int x = 0;
2877                   bool lineComplete = false;
2878                   for(; (textPos < textBlock.textLen) && !lineComplete;)
2879                   {
2880                      int w;
2881                      int len;
2882                      char * nextSpace = strchr(text + textPos, ' ');
2883
2884                      if(nextSpace)
2885                         len = (nextSpace - (text + textPos)) + 1;
2886                      else
2887                         len = textBlock.textLen - textPos;
2888                      
2889                      display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
2890
2891                      if(x + width + w > maxW && x > 0)
2892                      {
2893                         lineComplete = true;
2894                         break;
2895                      }
2896                      textPos += len;
2897                      width += w;
2898                      if(nextSpace)
2899                      {
2900                         x += width;
2901                         width = 0;
2902                      }
2903                      if(sy > caretY && (textPos == textBlock.textLen || caretX <= x + width + sx))
2904                      {
2905                         curPosition = textPos;
2906                         x += width;
2907                         while(curPosition > 0 && x + sx > caretX && textPos > startPos)
2908                         {
2909                            int len;
2910                            while(curPosition > 0 && !UTF8_IS_FIRST(text[--curPosition]));
2911                            len = curPosition - startPos;
2912                            display.FontExtent(textBlock.font.font, text + startPos, len, &x, null);
2913                         }
2914                         selPosition = curPosition;
2915                         PositionCaret(false);
2916                         return false;
2917                      }
2918                   }
2919                   if(sy > caretY)
2920                   {
2921                      selPosition = curPosition = textBlock.textLen;
2922                      PositionCaret(false);
2923                      return false;
2924                   } 
2925                   else if(textPos == textBlock.textLen && textBlock.next && textBlock.next.next)
2926                   {
2927                      startPos = 0;
2928                      textPos = 0;
2929                      textBlock = textBlock.next.next;
2930                      sy = textBlock.startY;
2931                      sx = textBlock.startX;
2932                      text = textBlock.text;
2933                   }
2934                   else
2935                   {
2936                      sy += th;
2937                      sx = textBlock.startX;
2938                   }
2939                }
2940                
2941                /*if(textBlock.next && textBlock.next.next)
2942                {
2943                   textBlock = textBlock.next.next;
2944                   selPosition = curPosition = Min(curPosition, textBlock.textLen);
2945                   PositionCaret(false);
2946                }*/
2947                break;
2948             }
2949             #define IS_ALUNDER(ch) ((ch) == '_' || isalnum((ch)))
2950             case ctrlRight:
2951             {
2952                // SELECTION CTRL-RIGHT
2953                /*
2954                bool foundAlpha = false;
2955                bool found = false;
2956                Block line, lastLine;
2957                int lastC;
2958
2959                for(line = textBlock; (line && !found); line = line.next ? line.next.next : null)
2960                {
2961                   int start = (line == textBlock) ? curPosition : 0;
2962                   int c;
2963                   for(c = start; c < line.textLen; c++)
2964                   {
2965                      if(IS_ALUNDER(line.text[c]))
2966                      {
2967                         foundAlpha = true;
2968                         lastC = c;
2969                         lastLine = line;
2970                      }
2971                      else if(foundAlpha)
2972                      {
2973                         found = true;
2974                         break;
2975                      }
2976                   }
2977                   if(!found && (c != curPosition || line != textBlock))
2978                   {
2979                      found = true;
2980                      lastLine = line;
2981                      lastC = line.textLen-1;
2982                      break;
2983                   }
2984                }  
2985                if(found)
2986                {
2987                   selPosition = curPosition = lastC+1;
2988                   textBlock = lastLine;
2989                   PositionCaret(true);
2990                }
2991                */
2992                
2993                bool foundAlpha = false;
2994                bool found = false;
2995                Block line;
2996
2997                for(line = textBlock; (line && !found); line = line.next ? line.next.next : null)
2998                {
2999                   int start = (line == textBlock) ? curPosition : 0;
3000                   int c;
3001                   for(c = start; c < line.textLen; c++)
3002                   {
3003                      if(!IS_ALUNDER(line.text[c]))
3004                         foundAlpha = true;
3005                      else if(foundAlpha)
3006                      {
3007                         found = true;
3008                         selPosition = curPosition = c;
3009                         textBlock = line;
3010                         PositionCaret(true);
3011                         break;
3012                      }
3013                   }
3014                   // No next word found, 
3015                   if(!found && (c != curPosition || line != textBlock))
3016                   {
3017                      found = true;
3018                      selPosition = curPosition = line.textLen;
3019                      textBlock = line;
3020                      PositionCaret(true);
3021                   }
3022                   foundAlpha = true;
3023                }
3024                break;
3025             }
3026             case ctrlLeft:
3027             {
3028                bool foundAlpha = false;
3029                bool found = false;
3030                Block line, lastLine;
3031                int lastC;
3032
3033                for(line = textBlock; (line && !found); line = line.prev ? line.prev.prev : null)
3034                {
3035                   int start, c;
3036                   if(curPosition == 0 && line != textBlock)
3037                   {
3038                      foundAlpha = true;
3039                      lastC = line.textLen;
3040                      lastLine = line;
3041                      break;
3042                   }
3043                   if(line == textBlock) start = curPosition-1; else start = line.textLen-1;
3044                   for(c = start; c>=0; c--)
3045                   {
3046                      if(IS_ALUNDER(line.text[c]))
3047                      {
3048                         foundAlpha = true;
3049                         lastC = c;
3050                         lastLine = line;
3051                      }
3052                      else
3053                      {
3054                         if(foundAlpha)
3055                         {
3056                            found = true;
3057                            break;
3058                         }
3059                      }
3060                   }
3061                   // No next word found, 
3062                   if(!found && curPosition > 0)
3063                   {
3064                      foundAlpha = true;
3065                      lastC = 0;
3066                      lastLine = line;
3067                      break;
3068                   }
3069                }
3070                if(foundAlpha)
3071                {
3072                   textBlock = lastLine;
3073                   selPosition = curPosition = lastC;
3074                   PositionCaret(true);
3075                }
3076                break;
3077             }
3078             case right:
3079                if(curPosition < textBlock.textLen)
3080                {
3081                   curPosition += UTF8_NUM_BYTES(textBlock.text[curPosition]);
3082                   PositionCaret(true);
3083                   selPosition = curPosition;
3084                }
3085                else if(textBlock.next && textBlock.next.next)
3086                {
3087                   textBlock = textBlock.next.next;
3088                   selPosition = curPosition = 0;
3089                   PositionCaret(true);
3090                }
3091                break;
3092             case left:
3093                if(curPosition > 0)
3094                {
3095                   while(curPosition > 0 && !UTF8_IS_FIRST(textBlock.text[--curPosition]));
3096                   PositionCaret(true);
3097                   selPosition = curPosition;
3098                }
3099                else if(textBlock.prev)
3100                {
3101                   textBlock = textBlock.prev.prev;
3102                   selPosition = curPosition = textBlock.textLen;
3103                   PositionCaret(true);
3104                }
3105                break;
3106             case backSpace:
3107                if(curPosition)
3108                {
3109                   int c = curPosition;
3110                   int nb = 1;
3111                   while(c > 0 && !UTF8_IS_FIRST(textBlock.text[--c])) nb++;
3112                   memmove(textBlock.text + curPosition - nb, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3113                   textBlock.textLen -= nb;
3114                   textBlock.text = renew textBlock.text char[textBlock.textLen + 1];
3115                   curPosition -= nb;
3116                   selPosition = curPosition;
3117                   {
3118                      //Clear(html.block);
3119                      //CreateForms(html.block);
3120                      ComputeMinSizes();
3121                      ComputeSizes();
3122                      //PositionForms();
3123                   }
3124                   PositionCaret(true);
3125                   Update(null);
3126                }
3127                else if(textBlock.prev)
3128                {
3129                   Block prev = textBlock.prev, prevBlock = textBlock.prev.prev;
3130                   prevBlock.text = renew prevBlock.text char[prevBlock.textLen + textBlock.textLen + 1];
3131                   memcpy(prevBlock.text + prevBlock.textLen, textBlock.text, textBlock.textLen + 1);
3132
3133                   selPosition = curPosition = prevBlock.textLen;
3134                   prevBlock.textLen += textBlock.textLen;
3135                   textBlock.parent.subBlocks.Remove(prev);
3136                   delete prev;
3137                   textBlock.parent.subBlocks.Remove(textBlock);                  
3138                   delete textBlock;
3139                   textBlock = prevBlock;
3140                   
3141                   {
3142                      //Clear(html.block);
3143                      //CreateForms(html.block);
3144                      ComputeMinSizes();
3145                      ComputeSizes();
3146                      //PositionForms();
3147                   }
3148                   PositionCaret(true);
3149                   Update(null);
3150                }
3151                break;
3152             case del:
3153                if(textBlock.textLen > curPosition)
3154                {
3155                   int nb = UTF8_NUM_BYTES(textBlock.text[curPosition]);
3156                   memmove(textBlock.text + curPosition, textBlock.text + curPosition + nb, textBlock.textLen - curPosition + 1 - nb + 1);
3157                   textBlock.textLen -= nb;
3158                   textBlock.text = renew textBlock.text char[textBlock.textLen + 1];
3159                   {
3160                      //Clear(html.block);
3161                      //CreateForms(html.block);
3162                      ComputeMinSizes();
3163                      ComputeSizes();
3164                      //PositionForms();
3165                   }
3166                   PositionCaret(true);
3167                   Update(null);
3168                }
3169                else if(textBlock.next && textBlock.next.next)
3170                {
3171                   Block next = textBlock.next, nextBlock = textBlock.next.next;
3172                   textBlock.text = renew textBlock.text char[textBlock.textLen + nextBlock.textLen + 1];
3173                   memcpy(textBlock.text + textBlock.textLen, nextBlock.text, nextBlock.textLen + 1);
3174
3175                   textBlock.textLen += nextBlock.textLen;
3176                   textBlock.parent.subBlocks.Remove(next);
3177                   delete next;
3178                   textBlock.parent.subBlocks.Remove(nextBlock);                  
3179                   delete nextBlock;
3180                   
3181                   {
3182                      //Clear(html.block);
3183                      //CreateForms(html.block);
3184                      ComputeMinSizes();
3185                      ComputeSizes();
3186                      //PositionForms();
3187                   }
3188                   PositionCaret(true);
3189                   Update(null);
3190                }
3191                break;
3192             case enter:
3193             {
3194                Block block { type = BR, parent = textBlock.parent, font = textBlock.font };
3195                Block newBlock { type = TEXT, parent = textBlock.parent, font = textBlock.font };
3196                int startY = textBlock.startY, startX = textBlock.startX;
3197                int tw = 0, th = 0;
3198
3199                display.FontExtent(textBlock.font.font, " ", 1, null, &th);
3200                textBlock.parent.subBlocks.Insert(textBlock, block);
3201                textBlock.parent.subBlocks.Insert(block, newBlock);
3202
3203                startY += th;
3204
3205                newBlock.textLen = textBlock.textLen - curPosition;
3206                newBlock.text = new char[newBlock.textLen+1];
3207                memcpy(newBlock.text, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3208                textBlock.textLen = curPosition;
3209                textBlock.text[curPosition] = 0;
3210
3211                newBlock.startY = startY;
3212                newBlock.startX = startX;
3213                selPosition = curPosition = 0;
3214                {
3215                   //Clear(html.block);
3216                   //CreateForms(html.block);
3217                   ComputeMinSizes();
3218                   ComputeSizes();
3219                   //PositionForms();
3220                }
3221                textBlock = newBlock;
3222                PositionCaret(true);
3223                Update(null);
3224                break;
3225             }
3226             case shiftInsert:
3227             case ctrlV:
3228             {
3229                ClipBoard clipBoard { };
3230                if(clipBoard.Load())
3231                {  
3232                   int c;
3233                   char * text = clipBoard.memory;
3234                   char ch;
3235                   int start = 0;
3236                   Block parent = textBlock.parent;
3237                   FontEntry font = textBlock.font;
3238                   for(c = 0; ; c++)
3239                   {
3240                      ch = text[c];
3241                      if(ch == '\n' || ch == '\r' || !ch)
3242                      {
3243                         int len = c - start;
3244                         textBlock.text = renew textBlock.text char[textBlock.textLen + 1 + len];
3245                         memmove(textBlock.text + curPosition + len, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3246                         memcpy(textBlock.text + curPosition, text + start, len);
3247                         textBlock.textLen += len;
3248                         curPosition += len;
3249                         selPosition = curPosition;
3250                         if(!ch) break;
3251                         {
3252                            Block block { type = BR, parent = parent, font = font };
3253                            Block newBlock { type = TEXT, parent = parent, font = font };
3254                            int startY = textBlock.startY, startX = textBlock.startX;
3255                            int tw = 0, th = 0;
3256
3257                            display.FontExtent(textBlock.font.font, " ", 1, null, &th);
3258                            textBlock.parent.subBlocks.Insert(textBlock, block);
3259                            textBlock.parent.subBlocks.Insert(block, newBlock);
3260
3261                            startY += th;
3262
3263                            newBlock.textLen = textBlock.textLen - curPosition;
3264                            newBlock.text = new char[newBlock.textLen+1];
3265                            memcpy(newBlock.text, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3266                            textBlock.textLen = curPosition;
3267                            textBlock.text[curPosition] = 0;
3268
3269                            newBlock.startY = startY;
3270                            newBlock.startX = startX;
3271                            selPosition = curPosition = 0;
3272                            textBlock = newBlock;
3273                         }
3274                         if(ch == '\r' && text[c+1] == '\n') c++;
3275                         start = c + 1;
3276                      }
3277                   }
3278                   ComputeMinSizes();
3279                   ComputeSizes();
3280                   PositionCaret(true);
3281                   Update(null);
3282                }
3283                delete clipBoard;
3284                break;
3285             }
3286             default:
3287             {
3288                // eC BUG HERE: (Should be fixed)
3289                if(key.ctrl && !key.alt && ch >= 32 && ch != 128 /*&& ch < 128*/)
3290                {
3291                   char string[5];
3292                   int len = UTF32toUTF8Len(&ch, 1, string, 5);
3293                   int c;
3294
3295                   textBlock.text = renew textBlock.text char[textBlock.textLen + len + 1];
3296                   memmove(textBlock.text + curPosition + len, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3297                   
3298                   for(c = 0; c<len; c++)
3299                   {
3300                      textBlock.text[curPosition] = string[c];
3301                      textBlock.textLen++;
3302                      curPosition++;
3303                   }
3304                   selPosition = curPosition;
3305                   
3306                   {
3307                      //Clear(html.block);
3308                      //CreateForms(html.block);
3309                      ComputeMinSizes();
3310                      ComputeSizes();
3311                      //PositionForms();
3312                   }
3313                   PositionCaret(true);
3314                   Update(null);
3315                }
3316             }
3317          }
3318       }
3319       return true;
3320    }
3321
3322    void OnResize(int width, int height)
3323    {
3324       HTMLView::OnResize(width, height);
3325       PositionCaret(true);
3326    }
3327
3328    int caretX, caretY;
3329    void PositionCaret(bool setCaretX)
3330    {
3331       if(edit)
3332       {
3333          int tw = 0, th = 0;
3334          int textPos = 0;
3335          int sx = textBlock.startX, sy = textBlock.startY;
3336          char * text = textBlock.text;
3337          int maxW;
3338          Block block = textBlock;
3339          while(block && block.type != TD) block = block.parent;
3340          if(block)
3341          {
3342             Block table = block;
3343             while(table && table.type != TABLE) table = table.parent;
3344             if(table)
3345                maxW = block.w - 2* table.cellPadding;
3346             else
3347                maxW = clientSize.w - 10 - sx;
3348          }
3349          else
3350             maxW = clientSize.w - 10 - sx;
3351          
3352          display.FontExtent(textBlock.font.font, " ", 1, null, &th);
3353
3354          while(textPos < textBlock.textLen)
3355          {
3356             int startPos = textPos;
3357             int width = 0;
3358             int x = 0;
3359             bool lineComplete = false;
3360
3361             for(; textPos<textBlock.textLen && !lineComplete;)
3362             {
3363                int w;
3364                int len;
3365                char * nextSpace = strchr(text + textPos, ' ');
3366
3367                if(nextSpace)
3368                   len = (nextSpace - (text + textPos)) + 1;
3369                else
3370                   len = textBlock.textLen - textPos;
3371                
3372                display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
3373
3374                if(x + width + w > maxW && x > 0)
3375                {
3376                   lineComplete = true;
3377                   break;
3378                }
3379                textPos += len;
3380
3381                width += w;
3382
3383                if(nextSpace)
3384                {
3385                   x += width;
3386                   width = 0;
3387                }
3388             }
3389             if(curPosition < textPos || textPos == textBlock.textLen)
3390             {
3391                int len = curPosition - startPos;
3392                display.FontExtent(textBlock.font.font, text + startPos, len, &tw, null);
3393                sx += tw;
3394                break;            
3395             }
3396             sy += th;
3397             sx = textBlock.startX;
3398          }
3399          if(setCaretX)
3400             caretX = sx;
3401          caretY = sy;
3402          SetCaret(sx, sy, th);
3403          {
3404             Point scrollPos = scroll;
3405             bool doScroll = false;
3406             if(sy - scroll.y + th > clientSize.h)
3407             {
3408                scrollPos.y = sy + th - clientSize.h;
3409                doScroll = true;
3410             }
3411             else if(sy - scroll.y < 0)
3412             {
3413                scrollPos.y = sy;
3414                doScroll = true;            
3415             }
3416             if(sx - scroll.x + 10 > clientSize.w)
3417             {
3418                scrollPos.x = sx + 10 - clientSize.w;
3419                doScroll = true;
3420             }
3421             else if(sx - scroll.x < 10)
3422             {
3423                scrollPos.x = sx - 10;
3424                doScroll = true;            
3425             }
3426             if(doScroll)
3427                scroll = scrollPos;
3428          }
3429       }
3430       else
3431          SetCaret(0,0,0);
3432    }
3433
3434    // Returns a character offset into the TextBlock from a window coordinate
3435    int TextPosFromPoint(int px, int py, Block * block)
3436    {
3437       Block parentBlock = this.textBlock.parent;
3438       Block textBlock;
3439       int result = 0;
3440       *block = this.textBlock;
3441
3442       px += scroll.x;
3443       py += scroll.y;
3444
3445       for(textBlock = parentBlock.subBlocks.first; textBlock; textBlock = textBlock.next)
3446       {
3447          int sx = textBlock.startX, sy = textBlock.startY;
3448          int th = 0;
3449          int textPos = 0;
3450          char * text = textBlock.text;
3451          int maxW;
3452          Block b = textBlock;
3453          int space;
3454
3455          if(textBlock.type != TEXT) continue;
3456
3457          while(b && b.type != TD) b = b.parent;
3458          if(b)
3459          {
3460             Block table = b;
3461             while(table && table.type != TABLE) table = table.parent;
3462             if(table)
3463                maxW = b.w - 2* table.cellPadding;
3464             else
3465                maxW = clientSize.w - 10 - sx;
3466          }
3467          else
3468             maxW = clientSize.w - 10 - sx;
3469          
3470          display.FontExtent(textBlock.font.font, " ", 1, &space, &th);
3471          //space = space/2+2;
3472          space = 2;
3473
3474          while(textPos < textBlock.textLen)
3475          {
3476             int startPos = textPos;
3477             int width = 0;
3478             int x = 0;
3479             bool lineComplete = false;
3480
3481             for(; textPos<textBlock.textLen && !lineComplete;)
3482             {
3483                int w;
3484                int len;
3485                char * nextSpace = strchr(text + textPos, ' ');
3486
3487                if(nextSpace)
3488                   len = (nextSpace - (text + textPos)) + 1;
3489                else
3490                   len = textBlock.textLen - textPos;
3491                
3492                display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
3493
3494                sx = x + textBlock.startX;
3495                if(/*py >= sy && */py < sy + th && /*px >= sx-space && */px < sx + w-space)
3496                {
3497                   int c, numBytes;
3498                   char ch;
3499                   *block = textBlock;
3500                   for(c = textPos; (ch = text[c]); c += numBytes)
3501                   {
3502                      numBytes = UTF8_NUM_BYTES(ch);
3503                      display.FontExtent(textBlock.font.font, text + c, numBytes, &w, &th);
3504                      if(/*py >= sy && */py < sy + th && /*px >= sx-w/2-space && */px < sx + w -w/2-space)
3505                         break;
3506                      sx += w;
3507                   }
3508                   return c;
3509                }
3510
3511                if(x + width + w > maxW && x > 0)
3512                {
3513                   lineComplete = true;
3514                   break;
3515                }
3516                textPos += len;
3517
3518                width += w;
3519
3520                if(nextSpace)
3521                {
3522                   x += width;
3523                   width = 0;
3524                }
3525             }
3526             if(/*py >= sy && */py < sy + th)
3527             {
3528                *block = textBlock;
3529                return textBlock.textLen;
3530             }
3531             sy += th;
3532          }
3533          *block = textBlock;
3534          result = textBlock.textLen;
3535       }
3536       return result;
3537    }
3538 }
3539
3540 Application componentsApp;
3541
3542 class Documentor : GuiApplication
3543 {
3544    bool Init()
3545    {
3546       Platform os = GetRuntimePlatform();
3547       componentsApp = __ecere_COM_Initialize(false, 1, null);
3548       SetPrivateModule(componentsApp);
3549       SetGlobalContext(globalContext);
3550       SetExcludedSymbols(&excludedSymbols);
3551       SetDefines(&::defines);
3552       SetImports(&imports);
3553       
3554       SetGlobalData(globalData);
3555
3556       settingsContainer.dataOwner = &settings;
3557       settingsContainer.Load();
3558       if(!settings.docDir || !settings.docDir[0] )
3559       {
3560          if(os == win32) // if Windows OS then
3561          {
3562             char programFilesDir[MAX_LOCATION];
3563             char appData[MAX_LOCATION]; 
3564             char homeDrive[MAX_LOCATION];
3565             char winDir[MAX_LOCATION];
3566             GetEnvironment("APPDATA", appData, sizeof(appData));
3567             GetEnvironment("HOMEDRIVE", homeDrive, sizeof(homeDrive));
3568             GetEnvironment("windir", winDir, sizeof(winDir));
3569             if(GetEnvironment("ProgramFiles", programFilesDir, MAX_LOCATION))
3570             {
3571                PathCat(programFilesDir, "ECERE SDK\\doc");
3572                settings.docDir = programFilesDir;
3573             }
3574             else if(homeDrive && homeDrive[0])
3575             {
3576                PathCat(homeDrive, "ECERE SDK\\doc");
3577                settings.docDir = homeDrive;
3578             }
3579             else if(winDir && winDir[0])
3580             {
3581                PathCat(winDir, "..\\ECERE SDK\\doc");
3582                settings.docDir = winDir;
3583             }
3584             else
3585                settings.docDir = "C:\\ECERE SDK\\doc";
3586          }
3587          else // if Os is Linux, or Mac OSX or something else
3588             settings.docDir = "/usr/share/ecere/doc/";
3589          settingsContainer.Save();
3590       }
3591
3592       //if(argc > 1)
3593       {
3594       #if 0
3595          Module module = eModule_Load(componentsApp, "ecere" /*argv[1]*/, privateAccess);
3596          DataRow row;
3597          AddComponents(module, true);
3598          mainForm.browser.currentRow = row = mainForm.browser.FindSubRow((int)module);
3599          // mainForm.browser.currentRow = row = mainForm.browser.FindSubRow((int)eSystem_FindClass(componentsApp, "Window"));
3600          while((row = row.parent))
3601             row.collapsed = false;
3602       #endif
3603       }
3604       return true;
3605    }
3606
3607    void Terminate()
3608    {
3609       FreeContext(globalContext);
3610       FreeExcludedSymbols(excludedSymbols);
3611       ::defines.Free(FreeModuleDefine);
3612       imports.Free(FreeModuleImport);
3613
3614       FreeGlobalData(globalData);
3615       FreeTypeData(componentsApp);
3616       FreeIncludeFiles();
3617       delete componentsApp;
3618    }
3619 }
3620
3621 MainForm mainForm { };