documentor; extras/html: (#440, #866) Selection/Cut/Copy/Paste/Delete/Replace
[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[20];
71                      sprintf(hex, "%p", 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://%p\" 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://%p\" 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://%p\" 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://%p\" 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://%p\" 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=%p></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://%p\" 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://%p\" 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://%p\" style=\"text-decoration: none;\">%s</a>", cl.base.templateClass ? cl.base.templateClass : 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                char tempString[1024];
891                String s;
892                while(base.type == enumClass) base = base.base;
893
894                if(base.type == systemClass ||
895                   (base.type == bitClass && base.membersAndProperties.first && !strcmp(cl.fullName, ((DataMember)base.membersAndProperties.first).dataTypeString)))
896                {
897                   if(!base.dataType)
898                      base.dataType = ProcessTypeString(base.dataTypeString, false);
899
900                   if(base.dataType.kind != classType)
901                   {
902                      char string[256];
903                      Symbol classSym;
904                      string[0] = '\0';
905                      PrintType(base.dataType, string, false, true);
906                      classSym = FindClass(string);
907                      dataClass = classSym ? classSym.registered : null;
908                   }
909                   else
910                      dataClass = base.dataType._class ? base.dataType._class.registered : null;
911                }
912                else
913                   dataClass = base;
914                
915                f.Printf("<TR>");
916                f.Printf("<TD valign=top height=22 nowrap=1><a name=%p></a><img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", item, iconNames[typeEnumValue], item.name);
917                if(dataClass.type == systemClass)
918                {
919                   needClass = false;
920                   s = ((char *(*)(void *, void *, char *, void *, bool *))(void *)dataClass._vTbl[__ecereVMethodID_class_OnGetString])(dataClass, &item.data, tempString, null, &needClass);
921                }
922                else
923                   s = ((char *(*)(void *, void *, char *, void *, bool *))(void *)eSystem_FindClass(componentsApp, "class")._vTbl[__ecereVMethodID_class_OnGetString])(dataClass, &item.data, tempString, null, &needClass);
924                if(needClass)
925                   f.Printf("<TD valign=top height=22 nowrap=1>%s { %s }</TD>", dataClass.name, s);
926                else
927                   f.Printf("<TD valign=top height=22 nowrap=1>%s</TD>", s);
928                if(desc)
929                {
930                   if(editing)
931                   {
932                      char fileName[MAX_LOCATION];
933                      FigureFileName(fileName, module, classDoc, cl, enumerationValue, item);
934                      f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
935                      f.Puts(desc);
936                      f.Printf("</a></TD>");
937                   }
938                   else
939                      f.Printf("<TD valign=top height=22>%s</TD>", desc);
940                   delete desc;
941                }
942                f.Printf("</TR>");
943             }
944             f.Printf("</TABLE><BR>\n");
945          }
946       }
947
948       if(cl.conversions.first)
949       {
950          f.Printf($"<a name=Conversions></a><H3>Conversions</H3><br><br>\n");
951          f.Printf("<TABLE >\n");
952          for(prop = cl.conversions.first; prop; prop = prop.next)
953          {
954             if((prop.memberAccess == publicAccess || (prop.memberAccess == privateAccess && showPrivate)) && prop.name)
955             {
956                char * desc = ReadDoc(module, classDoc, cl, conversion, prop);
957                DataRow mRow;
958                char * name;
959                Type type = ProcessTypeString(prop.name, false);
960                name = RSearchString(prop.name, "::", strlen(prop.name), true, false);
961                if(name) name += 2; else name = prop.name;
962
963                f.Printf("<TR>");
964                
965                string[0] = 0;
966                DocPrintType(type, string, true, false);
967                
968                f.Printf("<TD valign=top height=22 nowrap=1><a name=%p></a><img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", prop, iconNames[typeDataType], string);
969                if(desc)
970                {
971                   if(editing)
972                   {
973                      char fileName[MAX_LOCATION];
974                      FigureFileName(fileName, module, classDoc, cl, conversion, prop);
975                      f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
976                      f.Puts(desc);
977                      f.Printf("</a></TD>");
978                   }
979                   else
980                      f.Printf("<TD valign=top height=22>%s</TD>", desc);
981                   delete desc;
982                }
983                
984                f.Printf("</TR>\n");
985                
986                FreeType(type);
987             }
988          }
989          f.Printf("</TABLE><br>\n");
990       }
991
992       if(cl.membersAndProperties.first)
993       {
994          bool first = true;
995          for(prop = (Property)cl.membersAndProperties.first; prop; prop = prop.next)
996          {
997             if(prop.memberAccess == publicAccess || (prop.memberAccess == privateAccess && showPrivate))
998             {
999                if(first)
1000                {
1001                   f.Printf($"<a name=Members></a><H3>Properties and Members</H3><br><br>\n");
1002                   f.Printf("<TABLE >\n");
1003                   first = false;
1004                }
1005
1006                if(prop.isProperty)
1007                {
1008                   char * desc = ReadDoc(module, classDoc, cl, propertyDescription, prop);
1009                   if(!prop.dataType)
1010                      prop.dataType = ProcessTypeString(prop.dataTypeString, false);
1011
1012                   f.Printf("<TR>");
1013                   string[0] = 0;
1014                   DocPrintType(prop.dataType, string, true, false);
1015
1016                   f.Printf("<TD valign=top height=22 nowrap=1><a name=%p></a><img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", prop, iconNames[typeProperty], prop.name);
1017                   f.Printf("<TD valign=top height=22 nowrap=1>%s</TD>", string);
1018                   if(desc)
1019                   {
1020                      if(editing)
1021                      {
1022                         char fileName[MAX_LOCATION];
1023                         FigureFileName(fileName, module, classDoc, cl, propertyDescription, prop);
1024                         f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1025                         f.Puts(desc);
1026                         f.Printf("</a></TD>");
1027                      }
1028                      else
1029                         f.Printf("<TD valign=top height=22>%s</TD>", desc);
1030                      delete desc;
1031                   }
1032                   f.Printf("</TR>\n");
1033                }
1034                else
1035                {
1036                   AddDataMemberToPage(f, (DataMember)prop, 0, showPrivate);
1037                }
1038             }
1039          }
1040          if(!first)
1041             f.Printf("</TABLE><br>\n");
1042       }
1043
1044       if(cl.methods.first)
1045       {
1046          bool first = true;
1047          // Virtual Methods
1048          for(method = (Method)cl.methods.first; method; method = (Method)((BTNode)method).next)
1049          {
1050             if((method.memberAccess == publicAccess || (method.memberAccess == privateAccess && showPrivate)) && method.type == virtualMethod)
1051             {
1052                char * desc = ReadDoc(module, methodDoc, method, description, null);
1053                if(first)
1054                {
1055                   f.Printf($"<a name=VirtualMethods></a><H3>Virtual Methods</H3><br><br>\n");
1056                   f.Printf("<TABLE >\n");
1057                   first = false;
1058                }
1059                if(!method.dataType)
1060                   ProcessMethodType(method);
1061
1062                f.Printf("<TR>");
1063                f.Printf("<TD valign=top height=22 nowrap=1><img valign=center src=\"%s\">&nbsp;&nbsp;<a href=\"api://%p\" style=\"text-decoration: none;\">%s</a></TD>", method.dataType.thisClass ? iconNames[typeEvent] : iconNames[typeMethod], method, method.name);
1064                if(desc)
1065                {
1066                   if(editing)
1067                   {
1068                      char fileName[MAX_LOCATION];
1069                      FigureFileName(fileName, module, methodDoc, method, description, null);
1070                      f.Printf("<TD valign=top height=22> <a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1071                      f.Puts(desc);
1072                      f.Printf("</a></TD>");
1073                   }
1074                   else
1075                      f.Printf("<TD valign=top height=22> %s</TD>", desc);
1076                   delete desc;
1077                }
1078                f.Printf("</TR><br>\n");
1079             }
1080          }
1081          if(!first)
1082             f.Printf("</TABLE><br>\n");
1083
1084          // Non-Virtual Methods
1085          first = true;
1086          for(method = (Method)cl.methods.first; method; method = (Method)((BTNode)method).next)
1087          {
1088             if((method.memberAccess == publicAccess || (method.memberAccess == privateAccess && showPrivate)) && method.type != virtualMethod)
1089             {
1090                char * desc = ReadDoc(module, methodDoc, method, description, null);
1091                if(first)
1092                {
1093                   f.Printf($"<a name=Methods></a><H3>Non-Virtual Methods</H3><br><br>\n");
1094                   f.Printf("<TABLE >\n");
1095                   first = false;
1096                }
1097
1098                if(!method.dataType)
1099                   ProcessMethodType(method);
1100
1101                f.Printf("<TR>");
1102                f.Printf("<TD valign=top height=22 nowrap=1><img valign=center src=\"%s\">&nbsp;&nbsp;<a href=\"api://%p\" style=\"text-decoration: none;\">%s</a></TD>", iconNames[typeMethod], method, method.name);
1103                if(desc)
1104                {
1105                   if(editing)
1106                   {
1107                      char fileName[MAX_LOCATION];
1108                      FigureFileName(fileName, module, methodDoc, method, description, null);
1109                      f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1110                      f.Puts(desc);
1111                      f.Printf("</a></TD>");
1112                   }
1113                   else
1114                      f.Printf("<TD valign=top height=22>%s</TD>", desc);
1115                   delete desc;
1116                }
1117                
1118                f.Printf("</TR><br>\n");
1119             }
1120          }
1121          if(!first)
1122             f.Printf("</TABLE><br>\n");
1123       }
1124       {
1125          char * usageDoc = ReadDoc(module, classDoc, cl, usage, null);
1126          if(usageDoc)
1127          {
1128             f.Printf($"<H3>Usage</H3><br>\n");
1129             if(editing)
1130             {
1131                char fileName[MAX_LOCATION];
1132                FigureFileName(fileName, module, classDoc, cl, usage, null);
1133                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1134                f.Puts(usageDoc);
1135                f.Printf("</a>\n");
1136             }
1137             else
1138                f.Printf("<br>%s\n", usageDoc);
1139             f.Printf("<br><br>\n");
1140             delete usageDoc;
1141          }
1142       }
1143       {
1144          char * exampleDoc = ReadDoc(module, classDoc, cl, example, null);
1145          if(exampleDoc)
1146          {
1147             f.Printf($"<H3>Example</H3><br>\n");
1148             f.Printf($"<FONT face=\"Courier New\">\n");
1149             f.Printf("<br><TABLE >\n");
1150             if(editing)
1151             {
1152                char fileName[MAX_LOCATION];
1153                FigureFileName(fileName, module, classDoc, cl, example, null);
1154                f.Printf("<TR><TD><CODE><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1155                f.Puts(exampleDoc);
1156                f.Printf("</a></CODE></TD></TR>\n"); // bgcolor=#CFC9C0
1157             }
1158             else
1159                f.Printf("<TR><TD><CODE>%s</CODE></TD></TR>\n", exampleDoc);   // bgcolor=#CFC9C0
1160
1161             f.Printf("</TABLE></FONT>\n");
1162             f.Printf("<br>\n");
1163             delete exampleDoc;
1164          }
1165       }
1166       {
1167          char * remarksDoc = ReadDoc(module, classDoc, cl, remarks, null);
1168
1169          if(remarksDoc)
1170          {
1171             f.Printf($"<H3>Remarks</H3><br>\n");
1172             if(editing)
1173             {
1174                char fileName[MAX_LOCATION];
1175                FigureFileName(fileName, module, classDoc, cl, remarks, null);
1176                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1177                f.Puts(remarksDoc);
1178                f.Printf("</a>\n");
1179             }
1180             else
1181                f.Printf("<br>%s\n", remarksDoc);
1182             f.Printf("<br><br>\n");
1183             delete remarksDoc;
1184          }
1185       }
1186       
1187       if(cl.type != systemClass)
1188       {
1189          bool first = true;
1190          OldLink c;
1191          for(c = cl.derivatives.first; c; c = c.next)
1192          {
1193             Class deriv = c.data;
1194             // TO VERIFY: Does this properly check public status?
1195             if(eSystem_FindClass(componentsApp, deriv.fullName))
1196             {
1197                if(first)
1198                {
1199                   f.Printf($"<H3>Derived Classes</H3><br>\n");
1200                   f.Printf("<br>");
1201                   first = false;
1202                }
1203                else
1204                   f.Printf(", ");
1205                f.Printf("<a href=\"api://%p\" style=\"text-decoration: none;\">%s</a>", deriv, deriv.name);
1206              }
1207          }
1208          if(!first)
1209             f.Printf("<br><br>\n");
1210       }
1211       {
1212          char * seeAlsoDoc = ReadDoc(module, classDoc, cl, seeAlso, null);
1213          if(seeAlsoDoc)
1214          {
1215             f.Printf($"<H3>See Also</H3><br>\n");
1216             if(editing)
1217             {
1218                char fileName[MAX_LOCATION];
1219                FigureFileName(fileName, module, classDoc, cl, seeAlso, null);
1220                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1221                f.Puts(seeAlsoDoc);
1222                f.Printf("</a>\n");
1223             }
1224             else
1225                f.Printf("<br>%s\n", seeAlsoDoc);
1226             f.Printf("<br><br>\n");
1227             delete seeAlsoDoc;
1228          }
1229       }
1230       f.Printf("</FONT></BODY></HTML>\n");
1231    }
1232 }
1233
1234 class APIPageMethod : APIPage
1235 {
1236    Method method;
1237    void Generate(File f)
1238    {
1239       Class cl = method._class;
1240       char string[1024];
1241       Module module = cl.module;
1242       Type param;
1243       char nsName[1024], temp[1024];
1244       NameSpace * ns = cl.nameSpace;
1245
1246       nsName[0] = 0;
1247       while(ns && ns->name)
1248       {
1249          strcpy(temp, ns->name);
1250          if(nsName[0]) strcat(temp, "::");
1251          strcat(temp, nsName);
1252          strcpy(nsName, temp);
1253          ns = ns->parent;
1254       }
1255
1256       f.Printf($"<HTML><HEAD><TITLE>API Reference</TITLE></HEAD>\n<BODY><FONT SIZE=\"3\">\n");
1257       f.Printf("<FONT FACE=\"Arial\" SIZE=\"6\">%s</FONT><br><br>\n", name);
1258
1259       f.Printf($"Module: <a href=\"api://%p\" style=\"text-decoration: none;\">%s</a><br>\n", (module && module.name) ? module : null, (!module || !module.name || !strcmp(nsName, "ecere::com")) ? "ecereCOM" : module.name);
1260       if(nsName[0])
1261          f.Printf($"Namespace: <a href=\"api://%p\" style=\"text-decoration: none;\">%s</a><br>\n", cl.nameSpace, nsName);
1262       f.Printf("Class: <a href=\"api://%p\" style=\"text-decoration: none;\">%s</a><br>\n", cl, cl.name);
1263       if(method.dataType.staticMethod)
1264       {
1265          f.Printf($"this pointer class: None<br>\n");
1266       }
1267       else if(method.dataType.thisClass && method.dataType.thisClass.registered && (method.dataType.thisClass.registered != method._class || method.type == virtualMethod))
1268       {
1269          f.Printf($"this pointer class: <a href=\"api://%p\" style=\"text-decoration: none;\">%s</a><br>\n", method.dataType.thisClass.registered, method.dataType.thisClass.registered.name);
1270       }
1271
1272       // Generate Method Page
1273       string[0] = 0;
1274       if(!method.dataType.name)
1275          method.dataType.name = CopyString(method.name);
1276       DocPrintType(method.dataType, string, true, false);
1277       f.Printf("<br>%s", string);
1278
1279       {
1280          char * desc = ReadDoc(module, methodDoc, method, description, null);
1281          if(desc)
1282          {
1283             f.Printf($"<br><br><H3>Description</H3><br><br>\n");
1284             if(editing)
1285             {
1286                char fileName[MAX_LOCATION];
1287                FigureFileName(fileName, module, methodDoc, method, description, null);
1288                f.Printf("<a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1289                f.Puts(desc);
1290                f.Printf("</a>");
1291             }
1292             else
1293                f.Printf("%s", desc);
1294             delete desc;
1295          }
1296       }
1297
1298       f.Printf("<br><br>\n");
1299       if(method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType)
1300       {
1301          f.Printf($"<H3>Parameters</H3><br><br>\n");
1302       }
1303       if((method.dataType.returnType && method.dataType.returnType.kind != voidType) ||
1304          (method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType))
1305       {
1306          f.Printf("<TABLE  valign=center>\n");
1307       }
1308
1309       for(param = method.dataType.params.first; param; param = param.next)
1310       {
1311          // ADD DESCRIPTION HERE
1312          if(param.kind != voidType)
1313          {
1314             char * desc = ReadDoc(module, methodDoc, method, parameter, param);
1315             f.Printf("<TR>");
1316             string[0] = 0;
1317             DocPrintType(param, string, false, false);
1318
1319             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", param.name ? param.name : "");
1320             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", string);
1321             if(desc)
1322             {
1323                if(editing)
1324                {
1325                   char fileName[MAX_LOCATION];
1326                   FigureFileName(fileName, module, methodDoc, method, parameter, param);
1327                   f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1328                   f.Puts(desc);
1329                   f.Printf("s</a>&nbsp;</TD>\n");
1330                }
1331                else
1332                   f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", desc);
1333                delete desc;
1334             }
1335             
1336             f.Printf("</TR>\n");
1337          }
1338       }
1339       if(method.dataType.returnType && method.dataType.returnType.kind != voidType)
1340       {
1341          char * desc = ReadDoc(module, methodDoc, method, returnValue, null);
1342          if(method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType)
1343          {
1344             f.Printf("<TR><TD>&nbsp;</TD></TR>");
1345          }
1346          f.Printf("<TR>");
1347          f.Printf($"<TD valign=top height=22 nowrap=1><B>Return Value</B></TD>\n");
1348          string[0] = 0;
1349          DocPrintType(method.dataType.returnType, string, false, false);
1350          f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", string);
1351          if(desc)
1352          {
1353             if(editing)
1354             {
1355                char fileName[MAX_LOCATION];
1356                FigureFileName(fileName, module, methodDoc, method, returnValue, null);
1357                f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1358                f.Puts(desc);
1359                f.Printf("</a>&nbsp;</TD>\n");
1360             }
1361             else
1362                f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", desc);
1363             delete desc;
1364          }
1365          f.Printf("</TR>\n");
1366          f.Printf("</TABLE>\n");
1367       }
1368       if((method.dataType.returnType && method.dataType.returnType.kind != voidType) ||
1369          (method.dataType.params.first && ((Type)method.dataType.params.first).kind != voidType))
1370       {
1371          f.Printf("</TABLE><br>\n");
1372       }
1373       {
1374          char * usageDoc = ReadDoc(module, methodDoc, method, usage, null);
1375          if(usageDoc)
1376          {
1377             f.Printf($"<H3>Usage</H3><br>\n");
1378             if(editing)
1379             {
1380                char fileName[MAX_LOCATION];
1381                FigureFileName(fileName, module, methodDoc, method, usage, null);
1382                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1383                f.Puts(usageDoc);
1384                f.Printf("</a>\n");
1385             }
1386             else
1387                f.Printf("<br>%s\n", usageDoc);
1388             f.Printf("<br><br>\n");
1389             delete usageDoc;
1390          }
1391       }
1392       {
1393          char * exampleDoc = ReadDoc(module, methodDoc, method, example, null);
1394          if(exampleDoc)
1395          {
1396             f.Printf($"<H3>Example</H3><br>\n");
1397             f.Printf($"<FONT face=\"Courier New\">\n");
1398             f.Printf("<br><TABLE >\n");
1399             if(editing)
1400             {
1401                char fileName[MAX_LOCATION];
1402                FigureFileName(fileName, module, methodDoc, method, example, null);
1403                f.Printf("<TR><TD><CODE><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1404                f.Puts(exampleDoc);
1405                f.Printf("</a></CODE></TD></TR>\n");   // bgcolor=#CFC9C0
1406             }
1407             else
1408                f.Printf("<TR><TD><CODE>%s</CODE></TD></TR>\n", exampleDoc);   // bgcolor=#CFC9C0
1409             f.Printf("</TABLE></FONT>\n");
1410             f.Printf("<br>\n");
1411             delete exampleDoc;
1412          }
1413       }
1414       {
1415          char * remarksDoc = ReadDoc(module, methodDoc, method, remarks, null);
1416          if(remarksDoc)
1417          {
1418             f.Printf($"<H3>Remarks</H3><br>\n");
1419             if(editing)
1420             {
1421                char fileName[MAX_LOCATION];
1422                FigureFileName(fileName, module, methodDoc, method, remarks, null);
1423                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1424                f.Puts(remarksDoc);
1425                f.Printf("</a>\n");
1426             }
1427             else
1428                f.Printf("<br>%s\n", method, remarksDoc);
1429             f.Printf("<br><br>\n");
1430             delete remarksDoc;
1431          }
1432       }
1433       {
1434          char * seeAlsoDoc = ReadDoc(module, methodDoc, method, seeAlso, null);
1435          if(seeAlsoDoc)
1436          {
1437             f.Printf($"<H3>See Also</H3><br>\n");
1438             if(editing)
1439             {
1440                char fileName[MAX_LOCATION];
1441                FigureFileName(fileName, module, methodDoc, method, seeAlso, null);
1442                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1443                f.Puts(seeAlsoDoc);
1444                f.Printf("</a>\n");
1445             }
1446             else
1447                f.Printf("<br>%s\n", method, seeAlsoDoc);
1448             
1449             f.Printf("<br><br>\n");
1450             delete seeAlsoDoc;
1451          }
1452       }
1453       f.Printf("</FONT></BODY></HTML>\n");
1454    }
1455 }
1456
1457 class APIPageFunction : APIPage
1458 {
1459    GlobalFunction function;
1460    void Generate(File f)
1461    {
1462       char string[1024];
1463       Module module = function.module;
1464       Type param;
1465       char nsName[1024], temp[1024];
1466       NameSpace * ns = function.nameSpace;
1467
1468       nsName[0] = 0;
1469       while(ns && ns->name)
1470       {
1471          strcpy(temp, ns->name);
1472          if(nsName[0]) strcat(temp, "::");
1473          strcat(temp, nsName);
1474          strcpy(nsName, temp);
1475          ns = ns->parent;
1476       }
1477
1478       f.Printf($"<HTML><HEAD><TITLE>API Reference</TITLE></HEAD>\n<BODY><FONT SIZE=\"3\">\n");
1479       f.Printf("<FONT FACE=\"Arial\" SIZE=\"6\">%s</FONT><br><br>\n", name);
1480
1481       f.Printf($"Module: <a href=\"api://%p\" style=\"text-decoration: none;\">%s</a><br>\n", (module && module.name) ? module : null, (!module || !module.name || !strcmp(nsName, "ecere::com")) ? "ecereCOM" : module.name);
1482
1483       if(nsName[0])
1484          f.Printf($"Namespace: <a href=\"api://%p\" style=\"text-decoration: none;\">%s</a><br>\n", function.nameSpace, nsName);
1485
1486       if(!function.dataType)
1487          function.dataType = ProcessTypeString(function.dataTypeString, false);
1488
1489       if(function.dataType.thisClass && function.dataType.thisClass.registered)
1490       {
1491          f.Printf($"this pointer class: <a href=\"api://%p\" style=\"text-decoration: none;\">%s</a><br>\n", function.dataType.thisClass.registered, function.dataType.thisClass.registered.name);
1492       }
1493
1494       // Generate Method Page
1495       string[0] = 0;
1496       if(!function.dataType.name)
1497          function.dataType.name = CopyString(function.name);
1498       DocPrintType(function.dataType, string, true, false);
1499       f.Printf("<br>%s", string);
1500
1501       {
1502          char * desc = ReadDoc(module, functionDoc, function, description, null);
1503          if(desc)
1504          {
1505             f.Printf($"<br><br><H3>Description</H3><br><br>\n");
1506             if(editing)
1507             {
1508                char fileName[MAX_LOCATION];
1509                FigureFileName(fileName, module, functionDoc, function, description, null);
1510                f.Printf("<a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1511                f.Puts(desc);
1512                f.Printf("</a>");
1513             }
1514             else
1515                f.Printf("%s", desc);
1516             delete desc;
1517          }
1518       }
1519       f.Printf("<br><br>\n");
1520       if(function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType)
1521       {
1522          f.Printf($"<H3>Parameters</H3><br><br>\n");
1523       }
1524       if((function.dataType.returnType && function.dataType.returnType.kind != voidType) ||
1525          (function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType))
1526       {
1527          f.Printf("<TABLE  valign=center>\n");
1528       }
1529
1530       for(param = function.dataType.params.first; param; param = param.next)
1531       {
1532          // ADD DESCRIPTION HERE
1533          if(param.kind != voidType)
1534          {
1535             char * desc = ReadDoc(module, functionDoc, function, parameter, param);
1536             f.Printf("<TR>");
1537             string[0] = 0;
1538             DocPrintType(param, string, false, false);
1539
1540             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", param.name ? param.name : "");
1541             f.Printf("<TD valign=top height=22 nowrap=1>%s&nbsp;</TD>\n", string);
1542             if(param)
1543             {
1544                if(editing)
1545                {
1546                   char fileName[MAX_LOCATION];
1547                   FigureFileName(fileName, module, functionDoc, function, parameter, param);
1548                   f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1549                   f.Puts(desc);
1550                   f.Printf("</a>&nbsp;</TD>\n");
1551                }
1552                else
1553                   f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", desc);
1554                delete desc;
1555             }
1556             f.Printf("</TR>\n");
1557          }
1558       }
1559       if(function.dataType.returnType && function.dataType.returnType.kind != voidType)
1560       {
1561          char * desc = ReadDoc(module, functionDoc, function, returnValue, null);
1562          if(function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType)
1563          {
1564             f.Printf("<TR><TD>&nbsp;</TD></TR>");
1565          }
1566          f.Printf("<TR>");
1567          f.Printf($"<TD valign=top height=22 nowrap=1><B>Return Value</B></TD>\n");
1568          string[0] = 0;
1569          DocPrintType(function.dataType.returnType, string, false, false);
1570          f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", string);
1571          if(desc)
1572          {
1573             if(editing)
1574             {
1575                char fileName[MAX_LOCATION];
1576                FigureFileName(fileName, module, functionDoc, function, returnValue, null);
1577                f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1578                f.Puts(desc);
1579                f.Printf("</a>&nbsp;</TD>\n");
1580             }
1581             else
1582                f.Printf("<TD valign=top height=22>%s&nbsp;</TD>\n", function, desc);
1583             delete desc;
1584          }
1585          f.Printf("</TR>\n");
1586          f.Printf("</TABLE>\n");
1587       }
1588       if((function.dataType.returnType && function.dataType.returnType.kind != voidType) ||
1589          (function.dataType.params.first && ((Type)function.dataType.params.first).kind != voidType))
1590       {
1591          f.Printf("</TABLE><br>\n");
1592       }
1593       {
1594          char * usageDoc = ReadDoc(module, functionDoc, function, usage, null);
1595          if(usageDoc)
1596          {
1597             f.Printf($"<H3>Usage</H3><br>\n");
1598             if(editing)
1599             {
1600                char fileName[MAX_LOCATION];
1601                FigureFileName(fileName, module, functionDoc, function, usage, null);
1602                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1603                f.Puts(usageDoc);
1604                f.Printf("</a>\n");
1605             }
1606             else
1607                f.Printf("<br>%s\n", usageDoc);
1608             f.Printf("<br><br>\n");
1609             delete usageDoc;
1610          }
1611       }
1612       {
1613          char * exampleDoc = ReadDoc(module, functionDoc, function, example, null);
1614          if(exampleDoc)
1615          {
1616             f.Printf($"<H3>Example</H3><br>\n");
1617             f.Printf($"<FONT face=\"Courier New\">\n");
1618             f.Printf("<br><TABLE >\n");
1619             if(editing)
1620             {
1621                char fileName[MAX_LOCATION];
1622                FigureFileName(fileName, module, functionDoc, function, example, null);
1623                f.Printf("<TR><TD><CODE><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1624                f.Puts(exampleDoc);
1625                f.Printf("</a></CODE></TD></TR>\n");   // bgcolor=#CFC9C0
1626             }
1627             else
1628                f.Printf("<TR><TD><CODE>%s</CODE></TD></TR>\n", exampleDoc);   // bgcolor=#CFC9C0
1629             f.Printf("</TABLE></FONT>\n");
1630             f.Printf("<br>\n");
1631             delete exampleDoc;
1632          }
1633       }
1634       {
1635          char * remarksDoc = ReadDoc(module, functionDoc, function, remarks, null);
1636          if(remarksDoc)
1637          {
1638             f.Printf($"<H3>Remarks</H3><br>\n");
1639             if(editing)
1640             {
1641                char fileName[MAX_LOCATION];
1642                FigureFileName(fileName, module, functionDoc, function, remarks, null);
1643                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1644                f.Puts(remarksDoc);
1645                f.Printf("</a>\n");
1646             }
1647             else
1648                f.Printf("<br>%s\n", remarksDoc);
1649             f.Printf("<br><br>\n");
1650             delete remarksDoc;
1651          }
1652       }
1653       {
1654          char * seeAlsoDoc = ReadDoc(module, functionDoc, function, seeAlso, null);
1655          if(seeAlsoDoc)
1656          {
1657             f.Printf($"<H3>See Also</H3><br>\n");
1658             if(editing)
1659             {
1660                char fileName[MAX_LOCATION];
1661                FigureFileName(fileName, module, functionDoc, function, seeAlso, null);
1662                f.Printf("<br><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1663                f.Puts(seeAlsoDoc);
1664                f.Printf("</a>\n");
1665             }
1666             else
1667                f.Printf("<br>%s\n", seeAlsoDoc);
1668             f.Printf("<br><br>\n");
1669             delete seeAlsoDoc;
1670          }
1671       }
1672       f.Printf("</FONT></BODY></HTML>\n");
1673    }
1674 }
1675
1676 static void AddNameSpace(DataRow parentRow, Module module, NameSpace mainNameSpace, NameSpace comNameSpace, char * parentName, bool showPrivate)
1677 {
1678    char nsName[1024];
1679    NameSpace * ns;
1680    NameSpace * nameSpace = mainNameSpace;
1681    DataRow row;
1682    DataRow classesRow = null;
1683    DataRow functionsRow = null, definesRow = null;
1684    APIPage page;
1685
1686    char fileName[MAX_LOCATION];
1687    
1688    strcpy(nsName, parentName ? parentName : "");
1689    if(nameSpace->name)
1690    {
1691       if(nsName[0])
1692          strcat(nsName, "::");
1693       strcat(nsName, nameSpace->name);
1694    }
1695
1696    if(nsName[0])
1697    {
1698       row = parentRow.AddRow();
1699       row.SetData(null, (page = APIPageNameSpace { nameSpace->name, module = module, nameSpace = nameSpace, showPrivate = showPrivate }));
1700       row.tag = (int)nameSpace;
1701       row.icon = mainForm.icons[typeNameSpace];
1702    }
1703    else
1704    {
1705       // "Global NameSpace"
1706       row = parentRow;
1707       page = parentRow.GetData(null);
1708    }
1709
1710    {
1711       bool first = true;
1712
1713       for(ns = (NameSpace *)mainNameSpace.nameSpaces.first; ns; ns = (NameSpace *)((BTNode)ns).next)
1714       {
1715          NameSpace * comNS = (comNameSpace != null) ? (NameSpace *)comNameSpace.nameSpaces.FindString(ns->name) : null;
1716          AddNameSpace(row, module, ns, comNS, nsName, showPrivate);
1717       }
1718       if(comNameSpace != null)
1719       {
1720          for(ns = (NameSpace *)comNameSpace.nameSpaces.first; ns; ns = (NameSpace *)((BTNode)ns).next)
1721          {
1722             if(!mainNameSpace.nameSpaces.FindString(ns->name))
1723             {
1724                AddNameSpace(row, module, ns, null, nsName, showPrivate);
1725             }
1726          }
1727       }
1728    }
1729    if(mainNameSpace.classes.first || (comNameSpace && comNameSpace.classes.first))
1730    {
1731       for(nameSpace = mainNameSpace ; nameSpace; nameSpace = (nameSpace == mainNameSpace) ? comNameSpace : null)
1732       {
1733          if(nameSpace->classes.first)
1734          {
1735             BTNamedLink link;
1736             Class cl;
1737             for(link = (BTNamedLink)nameSpace->classes.first; link; link = (BTNamedLink)((BTNode)link).next)
1738             {
1739                cl = link.data;
1740                if(!cl.templateClass && (!module || cl.module == module || (!cl.module.name && !strcmp(module.name, "ecere"))))
1741                {
1742                   if(!classesRow) { classesRow = row.AddRow(); classesRow.SetData(null, APIPage { $"Classes", page = page }); classesRow.collapsed = true; classesRow.icon = mainForm.icons[typeClass]; classesRow.tag = 1; }
1743                   AddClass(classesRow, module, cl, nsName, showPrivate);
1744                }
1745             }
1746          }
1747       }
1748    }
1749
1750    if(mainNameSpace.functions.first || (comNameSpace && comNameSpace.functions.first))
1751    {
1752       for(nameSpace = mainNameSpace ; nameSpace; nameSpace = (nameSpace == mainNameSpace) ? comNameSpace : null)
1753       {
1754          if(nameSpace->functions.first)                            
1755          {
1756             BTNamedLink link;
1757             GlobalFunction fn;
1758             for(link = (BTNamedLink)nameSpace->functions.first; link; link = (BTNamedLink)((BTNode)link).next)
1759             {
1760                fn = link.data;
1761                if(!module || fn.module == module || (!fn.module.name && !strcmp(module.name, "ecere")))
1762                {
1763                   char * name = ( name = RSearchString(fn.name, "::", strlen(fn.name), false, false), name ? name + 2 : fn.name);
1764                   DataRow fnRow;
1765                   if(!functionsRow) { functionsRow = row.AddRow(); functionsRow.SetData(null, APIPage { $"Functions", page = page }); functionsRow.collapsed = true; functionsRow.icon = mainForm.icons[typeMethod];  functionsRow.tag = 2; };
1766                   fnRow = functionsRow.AddRow(); fnRow.SetData(null, APIPageFunction { name, function = fn }); fnRow.icon = mainForm.icons[typeMethod]; fnRow.tag = (int)fn;
1767                }
1768             }
1769          }
1770       }
1771    }
1772    
1773    if(mainNameSpace.defines.first || (comNameSpace && comNameSpace.defines.first))
1774    {
1775       for(nameSpace = mainNameSpace ; nameSpace; nameSpace = (nameSpace == mainNameSpace) ? comNameSpace : null)
1776       {
1777          if(nameSpace->defines.first)
1778          {
1779             BTNamedLink link;
1780             Definition def;
1781             for(link = (BTNamedLink)nameSpace->defines.first; link; link = (BTNamedLink)((BTNode)link).next)
1782             {
1783                def = link.data;
1784                //if(def.module == module)
1785                {
1786                   char * name = ( name = RSearchString(def.name, "::", strlen(def.name), false, false), name ? name + 2 : def.name);
1787                   DataRow defRow;
1788                   if(!definesRow) { definesRow = row.AddRow(); definesRow.SetData(null, APIPage { $"Definitions", page = page }); definesRow.collapsed = true; definesRow.icon = mainForm.icons[typeData]; definesRow.tag = 3; };
1789                   defRow = definesRow.AddRow(); defRow.SetData(null, APIPage { name, page = page }); defRow.icon = mainForm.icons[typeData]; defRow.tag = (int)def;
1790                }
1791             }
1792          }
1793       }
1794    }
1795 }
1796
1797 static void AddDataMemberToPage(File f, DataMember member, int indent, bool showPrivate)
1798 {
1799    char string[1024];
1800    int c;
1801    if(!member.dataType)
1802       member.dataType = ProcessTypeString(member.dataTypeString, false);
1803
1804    f.Printf("<TR>");
1805    string[0] = 0;
1806    DocPrintType(member.dataType, string, true, false);
1807
1808    f.Printf("<TD valign=top height=22 nowrap=1><a name=%p></a>", member);
1809    for(c = 0; c<indent; c++)
1810       f.Printf("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
1811    f.Printf("<img valign=center src=\"%s\">&nbsp;&nbsp;%s</TD>", iconNames[typeData], member.name ? member.name : ((member.type == structMember) ? "(struct)" : "(union)"));
1812    f.Printf("<TD valign=top height=22 nowrap=1>%s</TD>", (member.type == normalMember) ? string : "");
1813    if(member.type == normalMember)
1814    {
1815       char * desc = ReadDoc(member._class.module, classDoc, member._class, memberDescription, member);
1816       if(desc)
1817       {
1818          if(editing)
1819          {
1820             char fileName[MAX_LOCATION];
1821             FigureFileName(fileName, member._class.module, classDoc, member._class, memberDescription, member);
1822             f.Printf("<TD valign=top height=22><a style=\"text-decoration:none;\" href=\"edit://%s\">", fileName);
1823             f.Puts(desc);
1824             f.Printf("</a></TD>");
1825          }
1826          else
1827             f.Printf("<TD valign=top height=22>%s</TD>", desc);
1828          delete desc;
1829       }
1830    }
1831    else
1832       f.Printf("<TD valign=top height=22></TD>");
1833    
1834    if(member.type != normalMember)
1835    {
1836       DataMember subMember;
1837       for(subMember = member.members.first; subMember; subMember = subMember.next)
1838       {
1839          if((subMember.memberAccess == publicAccess || (subMember.memberAccess == privateAccess && showPrivate)))
1840          {
1841             AddDataMemberToPage(f, subMember, indent + 1, showPrivate);
1842          }
1843       }
1844    }
1845    f.Printf("</TR><br>\n");
1846 }
1847
1848 static void AddDataMember(DataRow parentRow, APIPage page, DataMember member)
1849 {
1850    DataRow row;
1851    if(member.type == normalMember)
1852    {
1853       row = parentRow.AddRow(); row.SetData(null, APIPage { member.name, page = page }); row.icon = mainForm.icons[typeData];
1854       row.tag = (int)member;
1855    }
1856    else
1857    {
1858       DataMember m;
1859       row = parentRow.AddRow(); row.SetData(null, APIPage { (member.type == unionMember) ? "(union)" : "(struct)", page });
1860       row.icon = mainForm.icons[typeData];
1861       row.tag = (int)member;
1862
1863       for(m = member.members.first; m; m = m.next)
1864       {
1865          if(m.memberAccess == publicAccess || (m.memberAccess == privateAccess && page.showPrivate))
1866             AddDataMember(row, page, m);
1867       }
1868    }
1869 }
1870
1871 static void AddClass(DataRow parentRow, Module module, Class cl, char * nsName, bool showPrivate)
1872 {
1873    char fileName[MAX_LOCATION];
1874    char string[1024];
1875    Method method;
1876    Property prop;
1877    DataMember member;
1878    Type param;
1879    DataRow row;
1880    DataRow methodsRow = null, virtualsRow = null, eventsRow = null;
1881    DataRow propertiesRow = null, membersRow = null, conversionsRow = null, enumRow = null;
1882    APIPage page;
1883
1884    row = parentRow.AddRow();
1885    row.SetData(null, (page = APIPageClass { cl.name, cl = cl, showPrivate = showPrivate }));
1886    row.tag = (int)cl;
1887    row.collapsed = true;
1888    row.icon = (cl.type == enumClass || cl.type == unitClass || cl.type == systemClass) ? mainForm.icons[typeDataType] : mainForm.icons[typeClass];
1889
1890    // METHODS
1891    if(cl.methods.first)
1892    {
1893       for(method = (Method)cl.methods.first; method; method = (Method)((BTNode)method).next)
1894       {
1895          if(method.memberAccess == publicAccess || (method.memberAccess == privateAccess && showPrivate))
1896          {
1897             DataRow mRow;
1898             if(!method.dataType)
1899                ProcessMethodType(method);
1900             if(method.type == virtualMethod)
1901             {
1902                if(method.dataType.thisClass)
1903                {
1904                   if(!eventsRow) { eventsRow = row.AddRow(); eventsRow.SetData(null, APIPage { $"Events", page = page }); eventsRow.collapsed = true; eventsRow.icon = mainForm.icons[typeEvent];  eventsRow.tag = 4; }
1905                   mRow = eventsRow.AddRow(); mRow.SetData(null, APIPageMethod { method.name, method = method }); mRow.icon = mainForm.icons[typeEvent];
1906                   mRow.tag = (int)method;
1907                }
1908                else
1909                {
1910                   if(!virtualsRow) { virtualsRow = row.AddRow(); virtualsRow.SetData(null, APIPage { $"Virtual Methods", page = page }); virtualsRow.collapsed = true; virtualsRow.icon = mainForm.icons[typeMethod]; virtualsRow.tag = 4; }
1911                   mRow = virtualsRow.AddRow(); mRow.SetData(null, APIPageMethod { method.name, method = method }); mRow.icon = mainForm.icons[typeMethod];
1912                   mRow.tag = (int)method;
1913                }
1914             }
1915             else
1916             {
1917                if(!methodsRow) { methodsRow = row.AddRow(); methodsRow.SetData(null, APIPage { $"Methods", page = page }); methodsRow.collapsed = true; methodsRow.icon = mainForm.icons[typeMethod]; methodsRow.tag = 5; }
1918                mRow = methodsRow.AddRow(); mRow.SetData(null, APIPageMethod { method.name, method = method }); mRow.icon = mainForm.icons[typeMethod];
1919                mRow.tag = (int)method;
1920             }
1921          }
1922       }
1923    }
1924
1925    if(cl.membersAndProperties.first)
1926    {
1927       for(prop = (Property)cl.membersAndProperties.first; prop; prop = prop.next)
1928       {
1929          if(prop.memberAccess == publicAccess || (prop.memberAccess == privateAccess && showPrivate))
1930          {
1931             if(!prop.dataType)
1932                prop.dataType = ProcessTypeString(prop.dataTypeString, false);
1933             if(prop.isProperty)
1934             {
1935                DataRow mRow;
1936                if(!propertiesRow) { propertiesRow = row.AddRow(); propertiesRow.SetData(null, APIPage { $"Properties", page = page }); propertiesRow.collapsed = true; propertiesRow.icon = mainForm.icons[typeProperty]; propertiesRow.tag = 6; }
1937                mRow = propertiesRow.AddRow(); mRow.SetData(null, APIPage { prop.name, page }); mRow.icon = mainForm.icons[typeProperty];
1938                mRow.tag = (int)prop;
1939             }
1940             else
1941             {
1942                if(!membersRow) { membersRow = row.AddRow(); membersRow.SetData(null, APIPage { $"Data Members", page = page }); membersRow.collapsed = true; membersRow.icon = mainForm.icons[typeData]; membersRow.tag = 6; }
1943                AddDataMember(membersRow, page, (DataMember)prop);
1944             }
1945          }
1946       }
1947    }
1948
1949    if(cl.conversions.first)
1950    {
1951       for(prop = cl.conversions.first; prop; prop = prop.next)
1952       {
1953          DataRow mRow;
1954          char * name;
1955          if(!conversionsRow) { conversionsRow = row.AddRow(); conversionsRow.SetData(null, APIPage { $"Conversions", page = page }); conversionsRow.collapsed = true; conversionsRow.icon = mainForm.icons[typeDataType]; conversionsRow.tag = 7; }
1956          name = RSearchString(prop.name, "::", strlen(prop.name), true, false);
1957          if(name) name += 2; else name = prop.name;
1958          mRow = conversionsRow.AddRow(); mRow.SetData(null, APIPage { name, page = page }); mRow.icon = mainForm.icons[typeDataType];
1959          mRow.tag = (int)prop;
1960       }
1961    }
1962    if(cl.type == enumClass)
1963    {
1964       EnumClassData enumeration = (EnumClassData)cl.data;
1965       NamedLink item;
1966       for(item = enumeration.values.first; item; item = item.next)
1967       {
1968          DataRow mRow;
1969          if(!enumRow) { enumRow = row.AddRow(); enumRow.SetData(null, APIPage { $"Enumeration Values", page = page }); enumRow.collapsed = true; enumRow.icon = mainForm.icons[typeEnumValue]; enumRow.tag = 8; }
1970          mRow = enumRow.AddRow(); mRow.SetData(null, APIPage { item.name, page = page }); mRow.icon = mainForm.icons[typeEnumValue];
1971          mRow.tag = (int)item;
1972       }
1973    }
1974 }
1975
1976 class MainForm : Window
1977 {
1978    size = { 1000, 600 };
1979    hasClose = true;
1980    borderStyle = sizable;
1981    hasMaximize = true;
1982    hasMinimize = true;
1983    nativeDecorations = true;
1984    icon = { ":documentorIcon.png" };
1985    text = $"API Documentation Browser";
1986
1987    BitmapResource icons[CodeObjectType];
1988
1989    MainForm()
1990    {
1991       CodeObjectType c;
1992       for(c = 0; c < CodeObjectType::enumSize; c++)
1993       {
1994          icons[c] = BitmapResource { iconNames[c], window = this, alphaBlend = true };
1995       }
1996       browser.AddField(DataField { dataType = class(APIPage) });
1997    }
1998
1999    hasMenuBar = true;
2000    menu = Menu { };
2001    Menu fileMenu { menu, $"File", f };
2002    Array<FileFilter> fileFilters
2003    { [
2004       { $"eC Shared Library files (*.dll, *.so, *.dylib)", "dll, so, dylib" },
2005       { $"eC Symbol files (*.sym)", "sym" }
2006    ] };
2007
2008    FileDialog fileDialog
2009    {
2010       filters = fileFilters.array, sizeFilters = fileFilters.count * sizeof(FileFilter)
2011    };
2012    MenuItem fileOpenItem
2013    {
2014       fileMenu, $"Open...", o, ctrlO;
2015
2016       bool NotifySelect(MenuItem selection, Modifiers mods)
2017       {
2018          if(fileDialog.Modal() == ok)
2019          {
2020             OpenModule(fileDialog.filePath);
2021          }
2022          return true;
2023       }
2024    };
2025    MenuItem fileSettingsItem
2026    {
2027       fileMenu, $"Settings...", s, ctrlS; // set the Settings item to the file menu with shortcut keys:s and ctrl+s
2028
2029       bool NotifySelect(MenuItem selection, Modifiers mods)
2030       {
2031          SettingsDialog { master = this }.Modal(); // Open the settings dialog to allow the user to change the directory for the eCdoc files
2032          return true;
2033       }
2034    };
2035    MenuDivider { fileMenu };
2036    MenuItem fileExit { fileMenu, $"Exit", x, altF4, NotifySelect = MenuFileExit };
2037
2038    void OpenModule(char * filePath)
2039    {
2040       char extension[MAX_EXTENSION];
2041       Module module = null;
2042       static char symbolsDir[MAX_LOCATION];
2043
2044       FreeContext(globalContext);
2045       FreeExcludedSymbols(excludedSymbols);
2046       ::defines.Free(FreeModuleDefine);
2047       imports.Free(FreeModuleImport);
2048       
2049       FreeGlobalData(globalData);
2050       FreeTypeData(componentsApp);
2051       FreeIncludeFiles();
2052       delete componentsApp;
2053
2054       SetGlobalContext(globalContext);
2055       componentsApp = __ecere_COM_Initialize(false, 1, null);
2056       SetPrivateModule(componentsApp);
2057
2058       StripLastDirectory(filePath, symbolsDir);
2059       SetSymbolsDir(symbolsDir);
2060
2061       GetExtension(filePath, extension);
2062
2063       mainForm.browser.Clear();
2064
2065       ImportModule(filePath, normalImport, publicAccess, false);
2066
2067       if(extension[0] && strcmpi(extension, "so") && strcmpi(extension, "dll"))
2068          componentsApp.name = CopyString(filePath);
2069       
2070       for(module = componentsApp.allModules.first; module; module = module.next)
2071       {
2072          if(module.name && (!strcmp(module.name, "ecere") || !strcmp(module.name, "ecereCOM")))
2073             break;
2074       }
2075       if(!module)
2076          eModule_LoadStrict(componentsApp, "ecereCOM", publicAccess /*privateAccess*/);
2077       AddComponents(componentsApp, false);
2078
2079       for(module = componentsApp.allModules.first; module; module = module.next)
2080       {
2081          if(module.name && (!strcmp(module.name, filePath)))
2082             break;
2083       }
2084       if(!module) module = componentsApp;
2085       mainForm.browser.SelectRow(mainForm.browser.FindSubRow((int)module));
2086
2087       SetSymbolsDir(null);
2088    }
2089
2090    ListBox browser
2091    {
2092       this, anchor = { left = 0, top = 0, bottom = 0 }, borderStyle = 0, background = aliceBlue;
2093       treeBranches = true; collapseControl = true; fullRowSelect = false; rootCollapseButton = true;
2094       hotKey = alt0;
2095
2096       bool NotifySelect(ListBox listBox, DataRow row, Modifiers mods)
2097       {
2098          APIPage page = row.GetData(null);
2099          if(view.edit) view.OnLeftButtonDown(0,0,0);
2100          if(page && page.page) page = page.page;
2101          view.edit = false;
2102          view.PositionCaret(true);
2103          if(page != view.page)
2104          {
2105             Window activeChild = this.activeChild;
2106
2107             view.Destroy(0);
2108             if(page)
2109                view.Create();
2110             activeChild.Activate();
2111          }
2112          else if(!view.created)
2113             view.Create();
2114          
2115          {
2116             page = row.GetData(null);
2117             if(page && page.page)
2118             {
2119                switch(row.tag)
2120                {
2121                   case 1: view.GoToAnchor("Classes"); break;
2122                   case 2: view.GoToAnchor("Functions"); break;
2123                   case 3: view.GoToAnchor("Definitions"); break;
2124                   case 4: view.GoToAnchor("VirtualMethods"); break;
2125                   case 5: view.GoToAnchor("Methods"); break;
2126                   case 6: view.GoToAnchor("Members"); break;
2127                   case 7: view.GoToAnchor("Conversions"); break;
2128                   case 8: view.GoToAnchor("EnumerationValues"); break;
2129                   default:
2130                   {
2131                      char hex[20];
2132                      sprintf(hex, "%p", row.tag);
2133                      view.GoToAnchor(hex);
2134                   }
2135                }
2136             }
2137             else
2138             {
2139                view.SetScrollPosition(0, 0);
2140             }
2141          }
2142          return true;
2143       }
2144    };
2145    HelpView view
2146    {
2147       this, anchor = { top = 0, bottom = 0, right = 0 };
2148       hotKey = escape;
2149    };
2150    PaneSplitter slider
2151    {
2152       this, leftPane = browser, rightPane = view, split = 300 /*scaleSplit = 0.3 */
2153    };
2154
2155    bool OnClose(bool parentClosing)
2156    {
2157       if(view.edit)
2158          view.OnLeftButtonDown(0,0,0);
2159       return true;
2160    }
2161
2162    bool OnPostCreate()
2163    {
2164       mainForm.OpenModule((((GuiApplication)__thisModule).argc > 1) ? ((GuiApplication)__thisModule).argv[1] : "ecere");
2165       //mainForm.OpenModule("ec");
2166       //mainForm.OpenModule("c:/games/chess/debug/chess.sym");
2167       //mainForm.OpenModule("c:/ide/Objects.IDE.Win32.Debug/ide.sym");
2168       {
2169          int index = mainForm.browser.currentRow.index;
2170          int rowHeight = mainForm.browser.rowHeight;
2171          int height = mainForm.browser.clientSize.h;
2172
2173          mainForm.browser.scroll = { 0, index * rowHeight - height / 2 };
2174       }
2175       return true;
2176    }
2177 };
2178
2179 class EditDialog : Window
2180 {
2181    borderStyle = sizable;
2182    size = { 600, 400 };
2183    autoCreate = false;
2184
2185    EditBox editBox
2186    {
2187       this, anchor = { left = 16, top = 16, right = 18, bottom = 61 }
2188    };
2189    Button saveChanges
2190    {
2191       this, text = $"Save Changes", anchor = { horz = 184, vert = 160 }
2192    };
2193    Button cancel
2194    {
2195       this, text = $"Cancel", anchor = { horz = 254, vert = 160 }
2196    };
2197 }
2198
2199 #define UTF8_IS_FIRST(x)   (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || (b) & 0x40); }))
2200 #define UTF8_NUM_BYTES(x)  (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
2201
2202 class HelpView : HTMLView
2203 {
2204    APIPage page;
2205
2206    hasVertScroll = true;
2207    hasHorzScroll = true;
2208    bool edit;
2209    char editString[MAX_LOCATION];
2210
2211    bool OnCreate()
2212    {
2213       TempFile f { };
2214       page = mainForm.browser.currentRow.GetData(null);
2215       if(page)
2216       {
2217          page.Generate(f);
2218          f.Seek(0, start);
2219          OpenFile(f, null);
2220          GoToAnchor(page.label);
2221          // Go to label...
2222          if(page.page) page = page.page;
2223       }
2224       delete f;
2225       return HTMLView::OnCreate();
2226    }
2227    EditDialog dialog
2228    {
2229
2230    };
2231
2232    void SaveEdit()
2233    {
2234       char archiveFile[MAX_LOCATION];
2235       char fileName[MAX_FILENAME];
2236       char directory[MAX_LOCATION];
2237       char * location;
2238       Archive archive;
2239       SplitArchivePath(editString, archiveFile, &location);
2240       GetLastDirectory(location, fileName);
2241       StripLastDirectory(location, directory);
2242       archive = ArchiveOpen(archiveFile, { true } );
2243       if(archive)
2244       {
2245          TempFile f { };
2246          ArchiveDir dir = archive.OpenDirectory(directory, null, replace);
2247          Block block;
2248          bool empty = true;
2249          for(block = textBlock.parent.subBlocks.first; block; block = block.next)
2250          {
2251             if(block.type == TEXT && block.textLen)
2252             {
2253                empty = false;
2254                break;
2255             }
2256          }
2257          if(!empty)
2258          {
2259             for(block = textBlock.parent.subBlocks.first; block; block = block.next)
2260             {
2261                if(block.type == BR)
2262                   f.Puts("<br>");
2263                else if(block.type == TEXT)
2264                   f.Write(block.text, 1, block.textLen);
2265             }
2266          }
2267          f.Seek(0, start);
2268          dir.AddFromFile(fileName, f, null, replace, 0, null, null);
2269          delete dir;
2270          delete archive;
2271          delete f;
2272          if(empty)
2273          {
2274             Block parent = textBlock.parent;
2275             while((block = parent.subBlocks.first))
2276             {
2277                parent.subBlocks.Remove(block);
2278                delete block;
2279             }
2280             textBlock = Block { type = TEXT, parent = parent, font = parent.font };
2281             textBlock.text = CopyString($"[Add Text]");
2282             textBlock.textLen = strlen(textBlock.text);
2283             parent.subBlocks.Add(textBlock);
2284          }
2285
2286          edit = false;
2287          if(created)
2288          {
2289             ComputeMinSizes();
2290             ComputeSizes();
2291             PositionCaret(true);
2292             Update(null);
2293          }
2294       }
2295    }
2296
2297    bool OnLeftButtonDown(int x, int y, Modifiers mods)
2298    {
2299       bool result = true;
2300
2301       if(edit && (!textBlock || overLink != textBlock.parent))
2302       {
2303          SaveEdit();
2304          HTMLView::OnLeftButtonDown(x, y, mods);
2305          selPosition = curPosition = 0;
2306          selBlock = textBlock;
2307          Update(null);
2308       }
2309       else
2310          result = HTMLView::OnLeftButtonDown(x, y, mods);
2311
2312       if(!edit && clickedLink)
2313       {
2314          ReleaseCapture();
2315          if(clickedLink == overLink && clickedLink.href)
2316          {
2317             if(OnOpen(clickedLink.href))
2318                Update(null);
2319          }
2320       }
2321
2322       if(edit)
2323       {
2324          // Update overLink
2325          if(textBlock && overLink == textBlock.parent)
2326          {
2327             selPosition = curPosition = TextPosFromPoint(x, y, &textBlock);
2328             selBlock = textBlock;
2329             PositionCaret(true);
2330             selecting = true;
2331             Update(null);
2332          }
2333       }
2334       return result;
2335    }
2336
2337    bool OnLeftButtonUp(int x, int y, Modifiers mods)
2338    {
2339       if(!edit || !textBlock || clickedLink != textBlock.parent)
2340       {
2341          HTMLView::OnLeftButtonUp(x, y, mods);
2342          if(edit)
2343          {
2344             selPosition = curPosition = TextPosFromPoint(x, y, &textBlock);
2345             selBlock = textBlock;
2346             PositionCaret(true);
2347             Update(null);
2348          }
2349       }
2350       selecting = false;
2351       return true;
2352    }
2353    bool selecting;
2354
2355    bool OnMouseMove(int x, int y, Modifiers mods)
2356    {
2357       if(edit && selecting)
2358       {
2359          curPosition = TextPosFromPoint(x, y, &textBlock);
2360          PositionCaret(true);
2361          Update(null);
2362       }
2363       return HTMLView::OnMouseMove(x, y, mods);
2364    }
2365
2366    bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
2367    {
2368       int c;
2369       int start = -1;
2370       int numBytes;
2371       for(c = curPosition; c >= 0; c--)
2372       {
2373          unichar ch;
2374          while(c > 0 && !UTF8_IS_FIRST(textBlock.text[c])) c--;
2375          ch = UTF8GetChar(textBlock.text + c, &numBytes);
2376          if(!CharMatchCategories(ch, letters|numbers|marks|connector))
2377             break;
2378          start = c;
2379       }
2380       if(start != -1)
2381       {
2382          for(c = start; c < textBlock.textLen; c += numBytes)
2383          {
2384             unichar ch = UTF8GetChar(textBlock.text + c, &numBytes);
2385             if(!CharMatchCategories(ch, letters|numbers|marks|connector))
2386                break;
2387          }
2388          selPosition = start;
2389          curPosition = c;
2390
2391          PositionCaret(true);
2392          Update(null);
2393          return false;
2394       }
2395       return true;
2396    }
2397
2398    bool OnOpen(char * href)
2399    {
2400       if(!strncmp(href, "api://", 6))
2401       {
2402          int tag = (uint)strtoul(href + 6, null, 16);
2403          DataRow row = mainForm.browser.FindSubRow(tag);
2404          if(row)
2405          {
2406             edit = false;
2407             mainForm.browser.SelectRow(row);
2408             while((row = row.parent))
2409                row.collapsed = false;
2410             row = mainForm.browser.currentRow;
2411             mainForm.browser.scroll = { 0, row.index * mainForm.browser.rowHeight - mainForm.browser.clientSize.h / 2 };
2412          }
2413       }
2414       else if(!strncmp(href, "edit://", 7))
2415       {
2416          Block block;
2417          int startX = clickedLink.startX, startY = clickedLink.startY;
2418          for(block = (Block)clickedLink.subBlocks.first; block; block = block.next)
2419          {
2420             if(block.type == TEXT) startX = block.startX, startY = block.startY;
2421             if(block.type == BR && (!block.prev || !block.next || block.next.type != TEXT))
2422             {
2423                Block newBlock { type = TEXT, parent = block.parent, font = block.parent.font };
2424                int tw = 0, th = 0;
2425                display.FontExtent(block.font.font, " ", 1, null, &th);
2426                if(!block.prev)
2427                {
2428                   block.parent.subBlocks.Insert(null, newBlock);
2429                   block = newBlock;
2430                }
2431                else
2432                {
2433                   block.parent.subBlocks.Insert(block, newBlock);
2434                   startY += block.prev.height;
2435                }
2436                newBlock.startX = startX;
2437                newBlock.startY = startY;
2438                newBlock.text = new0 char[1];
2439             }
2440          }
2441
2442          textBlock = (Block)clickedLink.subBlocks.first;
2443          if(!strcmp(textBlock.text, $"[Add Text]"))
2444          {
2445             textBlock.text[0] = 0;
2446             textBlock.textLen = 0;
2447          }
2448
2449          strcpy(editString, href + 7);
2450          selPosition = curPosition = 0;
2451          selBlock = textBlock;
2452          // dialog.Create();
2453          edit = true;
2454          // PositionCaret(true);
2455       }
2456       return true;
2457    }
2458
2459    char * text;
2460
2461    void DeleteSelection()
2462    {
2463       if(textBlock != selBlock || curPosition != selPosition)
2464       {
2465          if(textBlock == selBlock)
2466          {
2467             // Within same block
2468             int start = Min(curPosition, selPosition);
2469             int end = Max(curPosition, selPosition);
2470             memmove(textBlock.text + start, textBlock.text + end, textBlock.textLen - end);
2471             textBlock.textLen -= end-start;
2472             textBlock.text = renew textBlock.text char[textBlock.textLen + 1];
2473             curPosition = start;
2474             selPosition = start;
2475          }
2476          else
2477          {
2478             int startSel, endSel;
2479             Block startSelBlock = null, endSelBlock = null, b, next;
2480
2481             NormalizeSelection(&startSelBlock, &startSel, &endSelBlock, &endSel);
2482
2483             startSelBlock.text = renew startSelBlock.text char[startSel + endSelBlock.textLen - endSel + 1];
2484             memcpy(startSelBlock.text + startSel, endSelBlock.text + endSel, endSelBlock.textLen - endSel + 1);
2485
2486             startSelBlock.textLen = startSel + endSelBlock.textLen - endSel;
2487             for(b = startSelBlock.next; b; b = next)
2488             {
2489                bool isEnd = b == endSelBlock;
2490                next = GetNextBlock(b);
2491                b.parent.subBlocks.Remove(b);
2492                delete b;
2493                if(isEnd)
2494                   break;
2495             }
2496             textBlock = startSelBlock;
2497             selBlock = startSelBlock;
2498             curPosition = startSel;
2499             selPosition = startSel;
2500          }
2501          ComputeMinSizes();
2502          ComputeSizes();
2503          PositionCaret(true);
2504          Update(null);
2505       }
2506    }
2507
2508    String GetSelectionString()
2509    {
2510       String selection = null;
2511       if(textBlock == selBlock)
2512       {
2513          // Within same block
2514          int start = Min(curPosition, selPosition);
2515          int end = Max(curPosition, selPosition);
2516          int len = end - start;
2517          selection = new char[len + 1];
2518          memcpy(selection, textBlock.text + start, len);
2519          selection[len] = 0;
2520       }
2521       else
2522       {
2523          int startSel, endSel;
2524          Block startSelBlock = null, endSelBlock = null, b;
2525          int totalLen = 0;
2526
2527          NormalizeSelection(&startSelBlock, &startSel, &endSelBlock, &endSel);
2528
2529          // Compute length
2530          for(b = startSelBlock; b; b = GetNextBlock(b))
2531          {
2532             int start = (b == startSelBlock) ? startSel : 0;
2533             int end = (b == endSelBlock) ? endSel : b.textLen;
2534             int len = end - start;
2535             totalLen += len;
2536             if(b == endSelBlock)
2537                break;
2538             else if(b.type == TEXT)
2539                totalLen++;
2540          }
2541
2542          selection = new char[totalLen + 1];
2543          totalLen = 0;
2544          for(b = startSelBlock; b; b = GetNextBlock(b))
2545          {
2546             int start = (b == startSelBlock) ? startSel : 0;
2547             int end = (b == endSelBlock) ? endSel : b.textLen;
2548             int len = end - start;
2549             memcpy(selection + totalLen, b.text + start, len);
2550             totalLen += len;
2551             if(b == endSelBlock)
2552                break;
2553             else if(b.type == TEXT)
2554                selection[totalLen++] = '\n';
2555          }
2556          selection[totalLen] = 0;
2557       }
2558       return selection;
2559    }
2560
2561    void CopySelection()
2562    {
2563       String s = GetSelectionString();
2564       if(s)
2565       {
2566          int len = strlen(s);
2567          ClipBoard cb { };
2568          if(cb.Allocate(len + 1))
2569          {
2570             memcpy(cb.text, s, len + 1);
2571             cb.Save();
2572          }
2573          delete cb;
2574          delete s;
2575       }
2576    }
2577
2578    bool OnKeyDown(Key key, unichar ch)
2579    {
2580       if(edit)
2581       {
2582          switch(key)
2583          {
2584             case escape:
2585                OnLeftButtonDown(0,0,0);
2586                return false;
2587             case Key { end, shift = true }:
2588             case end:
2589                curPosition = textBlock.textLen;
2590                if(!key.shift)
2591                {
2592                   selPosition = curPosition;
2593                   selBlock = textBlock;
2594                }
2595                PositionCaret(true);
2596                Update(null);
2597                break;
2598             case Key { home, shift = true }:
2599             case home:
2600                curPosition = 0;
2601                if(!key.shift)
2602                {
2603                   selPosition = curPosition;
2604                   selBlock = textBlock;
2605                }
2606                PositionCaret(true);
2607                Update(null);
2608                break;
2609             case Key { home, ctrl = true, shift = true }:
2610             case ctrlHome:
2611                curPosition = 0;
2612                while(textBlock.prev)
2613                   textBlock = textBlock.prev.prev;
2614                if(!key.shift)
2615                {
2616                   selPosition = curPosition;
2617                   selBlock = textBlock;
2618                }
2619                PositionCaret(true);
2620                Update(null);
2621                return false;
2622             case Key { end, ctrl = true, shift = true }:
2623             case ctrlEnd:
2624                while(textBlock.next && textBlock.next.next)
2625                   textBlock = textBlock.next.next;
2626                curPosition = textBlock.textLen;
2627                if(!key.shift)
2628                {
2629                   selPosition = curPosition;
2630                   selBlock = textBlock;
2631                }
2632                PositionCaret(true);
2633                Update(null);
2634                return false;
2635          }
2636       }
2637       else
2638          return HTMLView::OnKeyDown(key, ch);
2639       return true;
2640    }
2641
2642    bool OnKeyHit(Key key, unichar ch)
2643    {
2644       if(edit)
2645       {
2646          switch(key)
2647          {
2648             case Key { up, shift = true }:
2649             case up:
2650             {
2651                if(caretY == textBlock.startY)
2652                {
2653                   if(textBlock.prev)
2654                   {
2655                      textBlock = textBlock.prev.prev;
2656                      curPosition = Min(curPosition, textBlock.textLen);
2657                      if(!key.shift)
2658                      {
2659                         selPosition = curPosition;
2660                         selBlock = textBlock;
2661                      }
2662                      Update(null);
2663                      PositionCaret(false);
2664                      caretY = MAXINT;
2665                   }
2666                   else
2667                      return false;
2668                }
2669
2670                {
2671                   int tw = 0, th = 0;
2672                   int textPos = 0;
2673                   int sx = textBlock.startX, sy = textBlock.startY;
2674                   char * text = textBlock.text;
2675                   int maxW;
2676                   Block block = textBlock;
2677                   while(block && block.type != TD) block = block.parent;
2678                   if(block)
2679                   {
2680                      Block table = block;
2681                      while(table && table.type != TABLE) table = table.parent;
2682                      if(table)
2683                         maxW = block.w - 2* table.cellPadding;
2684                      else
2685                         maxW = clientSize.w - 10 - sx;
2686                   }
2687                   else
2688                      maxW = clientSize.w - 10 - sx;
2689                   display.FontExtent(textBlock.font.font, " ", 1, null, &th);
2690
2691                   do
2692                   {
2693                      int startPos = textPos;
2694                      int width = 0;
2695                      int x = 0;
2696                      bool lineComplete = false;
2697                      for(; textPos<textBlock.textLen && !lineComplete;)
2698                      {
2699                         int w;
2700                         int len;
2701                         char * nextSpace = strchr(text + textPos, ' ');
2702
2703                         if(nextSpace)
2704                            len = (nextSpace - (text + textPos)) + 1;
2705                         else
2706                            len = textBlock.textLen - textPos;
2707                         
2708                         display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
2709
2710                         if(x + width + w > maxW && x > 0)
2711                         {
2712                            lineComplete = true;
2713                            break;
2714                         }
2715                         textPos += len;
2716                         width += w;
2717                         if(nextSpace)
2718                         {
2719                            x += width;
2720                            width = 0;
2721                         }
2722                         if(textPos == textBlock.textLen || (sy == caretY - th && caretX <= x + width + sx))
2723                         {
2724                            x += width;
2725                            curPosition = textPos;
2726                            while(curPosition > 0 && x + sx > caretX && textPos > startPos)
2727                            {
2728                               int len;
2729                               while(curPosition > 0 && !UTF8_IS_FIRST(text[--curPosition]));
2730                               len = curPosition - startPos;
2731                               display.FontExtent(textBlock.font.font, text + startPos, len, &x, null);
2732                            }
2733                            if(!key.shift)
2734                            {
2735                               selPosition = curPosition;
2736                               selBlock = textBlock;
2737                            }
2738                            Update(null);
2739
2740                            PositionCaret(false);
2741                            return false;
2742                         }
2743                      }
2744                      if(sy == caretY - th || textPos == textBlock.textLen)
2745                      {
2746                         if(textPos != textBlock.textLen)
2747                         {
2748                            int c = textPos - 1;
2749                            while(c > 0 && text[c] == ' ') c--;
2750                            curPosition = c + 1;
2751                            if(!key.shift)
2752                            {
2753                               selPosition = curPosition;
2754                               selBlock = textBlock;
2755                            }
2756                            Update(null);
2757                         }
2758                         else
2759                         {
2760                            curPosition = textBlock.textLen;
2761                            if(!key.shift)
2762                            {
2763                               selPosition = curPosition;
2764                               selBlock = textBlock;
2765                            }
2766                            Update(null);
2767                         }
2768                         PositionCaret(false);
2769                         return false;
2770                      }
2771                      sy += th;
2772                      sx = textBlock.startX;
2773                   } while(textPos < textBlock.textLen);
2774                   return false;
2775                }
2776                return false;
2777             }
2778             case Key { down, shift = true }:
2779             case down:
2780             {
2781                int tw = 0, th = 0;
2782                int textPos = 0;
2783                int sx = textBlock.startX, sy = textBlock.startY;
2784                char * text = textBlock.text;
2785                int maxW;
2786                Block block = textBlock;
2787                while(block && block.type != TD) block = block.parent;
2788                if(block)
2789                {
2790                   Block table = block;
2791                   while(table && table.type != TABLE) table = table.parent;
2792                   if(table)
2793                      maxW = block.w - 2* table.cellPadding;
2794                   else
2795                      maxW = clientSize.w - 10 - sx;
2796                }
2797                else
2798                   maxW = clientSize.w - 10 - sx;
2799                display.FontExtent(textBlock.font.font, " ", 1, null, &th);
2800
2801                while(!textPos || textPos < textBlock.textLen)
2802                {
2803                   int startPos = textPos;
2804                   int width = 0;
2805                   int x = 0;
2806                   bool lineComplete = false;
2807                   for(; (textPos < textBlock.textLen) && !lineComplete;)
2808                   {
2809                      int w;
2810                      int len;
2811                      char * nextSpace = strchr(text + textPos, ' ');
2812
2813                      if(nextSpace)
2814                         len = (nextSpace - (text + textPos)) + 1;
2815                      else
2816                         len = textBlock.textLen - textPos;
2817                      
2818                      display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
2819
2820                      if(x + width + w > maxW && x > 0)
2821                      {
2822                         lineComplete = true;
2823                         break;
2824                      }
2825                      textPos += len;
2826                      width += w;
2827                      if(nextSpace)
2828                      {
2829                         x += width;
2830                         width = 0;
2831                      }
2832                      if(sy > caretY && (textPos == textBlock.textLen || caretX <= x + width + sx))
2833                      {
2834                         curPosition = textPos;
2835                         x += width;
2836                         while(curPosition > 0 && x + sx > caretX && textPos > startPos)
2837                         {
2838                            int len;
2839                            while(curPosition > 0 && !UTF8_IS_FIRST(text[--curPosition]));
2840                            len = curPosition - startPos;
2841                            display.FontExtent(textBlock.font.font, text + startPos, len, &x, null);
2842                         }
2843                         if(!key.shift)
2844                         {
2845                            selPosition = curPosition;
2846                            selBlock = textBlock;
2847                         }
2848                         Update(null);
2849                         PositionCaret(false);
2850                         return false;
2851                      }
2852                   }
2853                   if(sy > caretY)
2854                   {
2855                      curPosition = textBlock.textLen;
2856                      if(!key.shift)
2857                      {
2858                         selPosition = curPosition;
2859                         selBlock = textBlock;
2860                      }
2861                      Update(null);
2862                      PositionCaret(false);
2863                      return false;
2864                   } 
2865                   else if(textPos == textBlock.textLen && textBlock.next && textBlock.next.next)
2866                   {
2867                      startPos = 0;
2868                      textPos = 0;
2869                      textBlock = textBlock.next.next;
2870                      sy = textBlock.startY;
2871                      sx = textBlock.startX;
2872                      text = textBlock.text;
2873                   }
2874                   else
2875                   {
2876                      sy += th;
2877                      sx = textBlock.startX;
2878                   }
2879                }
2880                
2881                /*if(textBlock.next && textBlock.next.next)
2882                {
2883                   textBlock = textBlock.next.next;
2884                   selPosition = curPosition = Min(curPosition, textBlock.textLen);
2885                   selBlock = textBlock;
2886                   PositionCaret(false);
2887                }*/
2888                break;
2889             }
2890             case Key { right, shift = true, ctrl = true }:
2891             case ctrlRight:
2892             {
2893                bool foundAlpha = false;
2894                bool found = false;
2895                Block line, lastLine;
2896                int lastC;
2897
2898                for(line = textBlock; (line && !found); line = line.next ? line.next.next : null)
2899                {
2900                   int start = (line == textBlock) ? curPosition : 0;
2901                   int c;
2902                   for(c = start; c < line.textLen; c++)
2903                   {
2904                      char ch = line.text[c];
2905                      bool isAlUnder = CharMatchCategories(ch, letters|numbers|marks|connector);
2906                      if(key.shift ? isAlUnder : !isAlUnder)
2907                      {
2908                         foundAlpha = true;
2909                         lastC = c;
2910                         lastLine = line;
2911                      }
2912                      else if(foundAlpha)
2913                      {
2914                         found = true;
2915                         if(!key.shift)
2916                         {
2917                            curPosition = c;
2918                            if(!key.shift)
2919                            {
2920                               selPosition = curPosition;
2921                               selBlock = textBlock;
2922                            }
2923                            Update(null);
2924                            textBlock = line;
2925                            PositionCaret(true);
2926                         }
2927                         break;
2928                      }
2929                   }
2930                   // No next word found, 
2931                   if(!found && (c != curPosition || line != textBlock))
2932                   {
2933                      found = true;
2934                      lastLine = line;
2935                      lastC = line.textLen-1;
2936                      if(key.shift)
2937                         break;
2938                      else
2939                      {
2940                         curPosition = line.textLen;
2941                         if(!key.shift)
2942                         {
2943                            selPosition = curPosition;
2944                            selBlock = textBlock;
2945                         }
2946                         Update(null);
2947
2948                         textBlock = line;
2949                         PositionCaret(true);
2950                      }
2951                   }
2952                   if(!key.shift)
2953                      foundAlpha = true;
2954                }
2955                if(key.shift && found)
2956                {
2957                   curPosition = lastC+1;
2958                   textBlock = lastLine;
2959                   PositionCaret(true);
2960                   Update(null);
2961                }
2962                break;
2963             }
2964             case Key { left, ctrl = true, shift = true }:
2965             case ctrlLeft:
2966             {
2967                bool foundAlpha = false;
2968                bool found = false;
2969                Block line, lastLine;
2970                int lastC;
2971
2972                for(line = textBlock; (line && !found); line = line.prev ? line.prev.prev : null)
2973                {
2974                   int start, c;
2975                   if(curPosition == 0 && line != textBlock)
2976                   {
2977                      foundAlpha = true;
2978                      lastC = line.textLen;
2979                      lastLine = line;
2980                      break;
2981                   }
2982                   if(line == textBlock) start = curPosition-1; else start = line.textLen-1;
2983                   for(c = start; c>=0; c--)
2984                   {
2985                      if(CharMatchCategories(line.text[c], letters|numbers|marks|connector))
2986                      {
2987                         foundAlpha = true;
2988                         lastC = c;
2989                         lastLine = line;
2990                      }
2991                      else
2992                      {
2993                         if(foundAlpha)
2994                         {
2995                            found = true;
2996                            break;
2997                         }
2998                      }
2999                   }
3000                   // No next word found, 
3001                   if(!found && curPosition > 0)
3002                   {
3003                      foundAlpha = true;
3004                      lastC = 0;
3005                      lastLine = line;
3006                      break;
3007                   }
3008                }
3009                if(foundAlpha)
3010                {
3011                   textBlock = lastLine;
3012                   curPosition = lastC;
3013                   if(!key.shift)
3014                   {
3015                      selPosition = curPosition;
3016                      selBlock = textBlock;
3017                   }
3018                   PositionCaret(true);
3019                   Update(null);
3020                }
3021                break;
3022             }
3023             case Key { right, shift = true }:
3024             case right:
3025                if(curPosition < textBlock.textLen)
3026                {
3027                   curPosition += UTF8_NUM_BYTES(textBlock.text[curPosition]);
3028                   if(!key.shift)
3029                   {
3030                      selPosition = curPosition;
3031                      selBlock = textBlock;
3032                   }
3033                   PositionCaret(true);
3034                   Update(null);
3035                }
3036                else if(textBlock.next && textBlock.next.next)
3037                {
3038                   textBlock = textBlock.next.next;
3039                   curPosition = 0;
3040                   if(!key.shift)
3041                   {
3042                      selPosition = curPosition;
3043                      selBlock = textBlock;
3044                   }
3045                   PositionCaret(true);
3046                   Update(null);
3047                }
3048                break;
3049             case Key { left, shift = true }:
3050             case left:
3051                if(curPosition > 0)
3052                {
3053                   while(curPosition > 0 && !UTF8_IS_FIRST(textBlock.text[--curPosition]));
3054                   if(!key.shift)
3055                   {
3056                      selPosition = curPosition;
3057                      selBlock = textBlock;
3058                   }
3059                   PositionCaret(true);
3060                   Update(null);
3061                }
3062                else if(textBlock.prev)
3063                {
3064                   textBlock = textBlock.prev.prev;
3065                   curPosition = textBlock.textLen;
3066                   if(!key.shift)
3067                   {
3068                      selPosition = curPosition;
3069                      selBlock = textBlock;
3070                   }
3071                   PositionCaret(true);
3072                   Update(null);
3073                }
3074                break;
3075             case backSpace:
3076                if(textBlock == selBlock && curPosition == selPosition)
3077                {
3078                   if(curPosition)
3079                   {
3080                      int c = curPosition;
3081                      int nb = 1;
3082                      while(c > 0 && !UTF8_IS_FIRST(textBlock.text[--c])) nb++;
3083                      memmove(textBlock.text + curPosition - nb, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3084                      textBlock.textLen -= nb;
3085                      textBlock.text = renew textBlock.text char[textBlock.textLen + 1];
3086                      curPosition -= nb;
3087                      selPosition = curPosition;
3088                      selBlock = textBlock;
3089
3090                      ComputeMinSizes();
3091                      ComputeSizes();
3092                      PositionCaret(true);
3093                      Update(null);
3094                   }
3095                   else if(textBlock.prev)
3096                   {
3097                      Block prev = textBlock.prev, prevBlock = textBlock.prev.prev;
3098                      prevBlock.text = renew prevBlock.text char[prevBlock.textLen + textBlock.textLen + 1];
3099                      memcpy(prevBlock.text + prevBlock.textLen, textBlock.text, textBlock.textLen + 1);
3100
3101                      selPosition = curPosition = prevBlock.textLen;
3102                      selBlock = textBlock;
3103                      prevBlock.textLen += textBlock.textLen;
3104                      textBlock.parent.subBlocks.Remove(prev);
3105                      if(prev == selBlock)
3106                      {
3107                         selBlock = textBlock;
3108                         selPosition = curPosition;
3109                      }
3110                      delete prev;
3111                      textBlock.parent.subBlocks.Remove(textBlock);
3112                      if(textBlock == selBlock)
3113                      {
3114                         selBlock = prevBlock;
3115                         selPosition = curPosition;
3116                      }
3117                      delete textBlock;
3118                      textBlock = prevBlock;
3119
3120                      ComputeMinSizes();
3121                      ComputeSizes();
3122                      PositionCaret(true);
3123                      Update(null);
3124                   }
3125                }
3126                else
3127                   DeleteSelection();
3128                break;
3129             case del:
3130                if(textBlock != selBlock || curPosition != selPosition)
3131                   DeleteSelection();
3132                else if(textBlock.textLen > curPosition)
3133                {
3134                   int nb = UTF8_NUM_BYTES(textBlock.text[curPosition]);
3135                   memmove(textBlock.text + curPosition, textBlock.text + curPosition + nb, textBlock.textLen - curPosition + 1 - nb + 1);
3136                   textBlock.textLen -= nb;
3137                   textBlock.text = renew textBlock.text char[textBlock.textLen + 1];
3138
3139                   ComputeMinSizes();
3140                   ComputeSizes();
3141
3142                   PositionCaret(true);
3143                   Update(null);
3144                }
3145                else if(textBlock.next && textBlock.next.next)
3146                {
3147                   Block next = textBlock.next, nextBlock = textBlock.next.next;
3148                   textBlock.text = renew textBlock.text char[textBlock.textLen + nextBlock.textLen + 1];
3149                   memcpy(textBlock.text + textBlock.textLen, nextBlock.text, nextBlock.textLen + 1);
3150
3151                   textBlock.textLen += nextBlock.textLen;
3152                   textBlock.parent.subBlocks.Remove(next);
3153                   if(next == selBlock)
3154                   {
3155                      selBlock = textBlock;
3156                      selPosition = curPosition;
3157                   }
3158                   delete next;
3159                   textBlock.parent.subBlocks.Remove(nextBlock);
3160                   if(nextBlock == selBlock)
3161                   {
3162                      selBlock = textBlock;
3163                      selPosition = curPosition;
3164                   }
3165                   delete nextBlock;
3166
3167                   ComputeMinSizes();
3168                   ComputeSizes();
3169                   PositionCaret(true);
3170                   Update(null);
3171                }
3172                break;
3173             case enter:
3174             {
3175                int tw = 0, th = 0;
3176                Block block;
3177                Block newBlock;
3178                int startY, startX;
3179
3180                DeleteSelection();
3181
3182                block = { type = BR, parent = textBlock.parent, font = textBlock.font };
3183                newBlock = { type = TEXT, parent = textBlock.parent, font = textBlock.font };
3184                startY = textBlock.startY;
3185                startX = textBlock.startX;
3186
3187                display.FontExtent(textBlock.font.font, " ", 1, null, &th);
3188                textBlock.parent.subBlocks.Insert(textBlock, block);
3189                textBlock.parent.subBlocks.Insert(block, newBlock);
3190
3191                startY += th;
3192
3193                newBlock.textLen = textBlock.textLen - curPosition;
3194                newBlock.text = new char[newBlock.textLen+1];
3195                memcpy(newBlock.text, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3196                textBlock.textLen = curPosition;
3197                textBlock.text[curPosition] = 0;
3198
3199                newBlock.startY = startY;
3200                newBlock.startX = startX;
3201                selPosition = curPosition = 0;
3202
3203                ComputeMinSizes();
3204                ComputeSizes();
3205
3206                textBlock = newBlock;
3207                selBlock = textBlock;
3208                PositionCaret(true);
3209                Update(null);
3210                break;
3211             }
3212             case ctrlX:
3213             case Key { del, shift = true }:
3214                // Cut
3215                CopySelection();
3216                DeleteSelection();
3217                break;
3218             case ctrlC:
3219             case ctrlInsert:
3220                // Copy
3221                CopySelection();
3222                break;
3223             case shiftInsert:
3224             case ctrlV:
3225             {
3226                ClipBoard clipBoard { };
3227                if(clipBoard.Load())
3228                {  
3229                   int c;
3230                   char * text = clipBoard.memory;
3231                   char ch;
3232                   int start = 0;
3233                   Block parent;
3234                   FontEntry font;
3235
3236                   DeleteSelection();
3237
3238                   parent = textBlock.parent;
3239                   font = textBlock.font;
3240
3241                   for(c = 0; ; c++)
3242                   {
3243                      ch = text[c];
3244                      if(ch == '\n' || ch == '\r' || !ch)
3245                      {
3246                         int len = c - start;
3247                         textBlock.text = renew textBlock.text char[textBlock.textLen + 1 + len];
3248                         memmove(textBlock.text + curPosition + len, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3249                         memcpy(textBlock.text + curPosition, text + start, len);
3250                         textBlock.textLen += len;
3251                         curPosition += len;
3252                         selPosition = curPosition;
3253                         selBlock = textBlock;
3254                         if(!ch) break;
3255                         {
3256                            Block block { type = BR, parent = parent, font = font };
3257                            Block newBlock { type = TEXT, parent = parent, font = font };
3258                            int startY = textBlock.startY, startX = textBlock.startX;
3259                            int tw = 0, th = 0;
3260
3261                            display.FontExtent(textBlock.font.font, " ", 1, null, &th);
3262                            textBlock.parent.subBlocks.Insert(textBlock, block);
3263                            textBlock.parent.subBlocks.Insert(block, newBlock);
3264
3265                            startY += th;
3266
3267                            newBlock.textLen = textBlock.textLen - curPosition;
3268                            newBlock.text = new char[newBlock.textLen+1];
3269                            memcpy(newBlock.text, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3270                            textBlock.textLen = curPosition;
3271                            textBlock.text[curPosition] = 0;
3272
3273                            newBlock.startY = startY;
3274                            newBlock.startX = startX;
3275                            selPosition = curPosition = 0;
3276                            selBlock = textBlock;
3277                            textBlock = newBlock;
3278                         }
3279                         if(ch == '\r' && text[c+1] == '\n') c++;
3280                         start = c + 1;
3281                      }
3282                   }
3283                   ComputeMinSizes();
3284                   ComputeSizes();
3285                   PositionCaret(true);
3286                   Update(null);
3287                }
3288                delete clipBoard;
3289                break;
3290             }
3291             default:
3292             {
3293                // eC BUG HERE: (Should be fixed)
3294                if(!key.ctrl && !key.alt && ch >= 32 && ch != 128 /*&& ch < 128*/)
3295                {
3296                   char string[5];
3297                   int len = UTF32toUTF8Len(&ch, 1, string, 5);
3298                   int c;
3299
3300                   DeleteSelection();
3301
3302                   textBlock.text = renew textBlock.text char[textBlock.textLen + len + 1];
3303                   memmove(textBlock.text + curPosition + len, textBlock.text + curPosition, textBlock.textLen - curPosition + 1);
3304                   
3305                   for(c = 0; c<len; c++)
3306                   {
3307                      textBlock.text[curPosition] = string[c];
3308                      textBlock.textLen++;
3309                      curPosition++;
3310                   }
3311                   selPosition = curPosition;
3312                   selBlock = textBlock;
3313                   
3314                   {
3315                      //Clear(html.block);
3316                      //CreateForms(html.block);
3317                      ComputeMinSizes();
3318                      ComputeSizes();
3319                      //PositionForms();
3320                   }
3321                   PositionCaret(true);
3322                   Update(null);
3323                }
3324             }
3325          }
3326       }
3327       return true;
3328    }
3329
3330    void OnResize(int width, int height)
3331    {
3332       HTMLView::OnResize(width, height);
3333       PositionCaret(true);
3334    }
3335
3336    int caretX, caretY;
3337    void PositionCaret(bool setCaretX)
3338    {
3339       if(edit)
3340       {
3341          int tw = 0, th = 0;
3342          int textPos = 0;
3343          int sx = textBlock.startX, sy = textBlock.startY;
3344          char * text = textBlock.text;
3345          int maxW;
3346          Block block = textBlock;
3347          while(block && block.type != TD) block = block.parent;
3348          if(block)
3349          {
3350             Block table = block;
3351             while(table && table.type != TABLE) table = table.parent;
3352             if(table)
3353                maxW = block.w - 2* table.cellPadding;
3354             else
3355                maxW = clientSize.w - 10 - sx;
3356          }
3357          else
3358             maxW = clientSize.w - 10 - sx;
3359          
3360          display.FontExtent(textBlock.font.font, " ", 1, null, &th);
3361
3362          while(textPos < textBlock.textLen)
3363          {
3364             int startPos = textPos;
3365             int width = 0;
3366             int x = 0;
3367             bool lineComplete = false;
3368
3369             for(; textPos<textBlock.textLen && !lineComplete;)
3370             {
3371                int w;
3372                int len;
3373                char * nextSpace = strchr(text + textPos, ' ');
3374
3375                if(nextSpace)
3376                   len = (nextSpace - (text + textPos)) + 1;
3377                else
3378                   len = textBlock.textLen - textPos;
3379                
3380                display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
3381
3382                if(x + width + w > maxW && x > 0)
3383                {
3384                   lineComplete = true;
3385                   break;
3386                }
3387                textPos += len;
3388
3389                width += w;
3390
3391                if(nextSpace)
3392                {
3393                   x += width;
3394                   width = 0;
3395                }
3396             }
3397             if(curPosition < textPos || textPos == textBlock.textLen)
3398             {
3399                int len = curPosition - startPos;
3400                display.FontExtent(textBlock.font.font, text + startPos, len, &tw, null);
3401                sx += tw;
3402                break;
3403             }
3404             sy += th;
3405             sx = textBlock.startX;
3406          }
3407          if(setCaretX)
3408             caretX = sx;
3409          caretY = sy;
3410          SetCaret(sx, sy, th);
3411          {
3412             Point scrollPos = scroll;
3413             bool doScroll = false;
3414             if(sy - scroll.y + th > clientSize.h)
3415             {
3416                scrollPos.y = sy + th - clientSize.h;
3417                doScroll = true;
3418             }
3419             else if(sy - scroll.y < 0)
3420             {
3421                scrollPos.y = sy;
3422                doScroll = true;
3423             }
3424             if(sx - scroll.x + 10 > clientSize.w)
3425             {
3426                scrollPos.x = sx + 10 - clientSize.w;
3427                doScroll = true;
3428             }
3429             else if(sx - scroll.x < 10)
3430             {
3431                scrollPos.x = sx - 10;
3432                doScroll = true;
3433             }
3434             if(doScroll)
3435                scroll = scrollPos;
3436          }
3437       }
3438       else
3439          SetCaret(0,0,0);
3440    }
3441
3442    // Returns a character offset into the TextBlock from a window coordinate
3443    int TextPosFromPoint(int px, int py, Block * block)
3444    {
3445       Block parentBlock = this.textBlock.parent;
3446       Block textBlock;
3447       int result = 0;
3448       *block = this.textBlock;
3449
3450       px += scroll.x;
3451       py += scroll.y;
3452
3453       for(textBlock = parentBlock.subBlocks.first; textBlock; textBlock = textBlock.next)
3454       {
3455          int sx = textBlock.startX, sy = textBlock.startY;
3456          int th = 0;
3457          int textPos = 0;
3458          char * text = textBlock.text;
3459          int maxW;
3460          Block b = textBlock;
3461          int space;
3462
3463          if(textBlock.type != TEXT) continue;
3464
3465          while(b && b.type != TD) b = b.parent;
3466          if(b)
3467          {
3468             Block table = b;
3469             while(table && table.type != TABLE) table = table.parent;
3470             if(table)
3471                maxW = b.w - 2* table.cellPadding;
3472             else
3473                maxW = clientSize.w - 10 - sx;
3474          }
3475          else
3476             maxW = clientSize.w - 10 - sx;
3477          
3478          display.FontExtent(textBlock.font.font, " ", 1, &space, &th);
3479          //space = space/2+2;
3480          space = 2;
3481
3482          while(textPos < textBlock.textLen)
3483          {
3484             int startPos = textPos;
3485             int width = 0;
3486             int x = 0;
3487             bool lineComplete = false;
3488
3489             for(; textPos<textBlock.textLen && !lineComplete;)
3490             {
3491                int w;
3492                int len;
3493                char * nextSpace = strchr(text + textPos, ' ');
3494
3495                if(nextSpace)
3496                   len = (nextSpace - (text + textPos)) + 1;
3497                else
3498                   len = textBlock.textLen - textPos;
3499                
3500                display.FontExtent(textBlock.font.font, text + textPos, len, &w, &th);
3501
3502                sx = x + textBlock.startX;
3503                if(/*py >= sy && */py < sy + th && /*px >= sx-space && */px < sx + w-space)
3504                {
3505                   int c, numBytes;
3506                   char ch;
3507                   *block = textBlock;
3508                   for(c = textPos; (ch = text[c]); c += numBytes)
3509                   {
3510                      numBytes = UTF8_NUM_BYTES(ch);
3511                      display.FontExtent(textBlock.font.font, text + c, numBytes, &w, &th);
3512                      if(/*py >= sy && */py < sy + th && /*px >= sx-w/2-space && */px < sx + w -w/2-space)
3513                         break;
3514                      sx += w;
3515                   }
3516                   return c;
3517                }
3518
3519                if(x + width + w > maxW && x > 0)
3520                {
3521                   lineComplete = true;
3522                   break;
3523                }
3524                textPos += len;
3525
3526                width += w;
3527
3528                if(nextSpace)
3529                {
3530                   x += width;
3531                   width = 0;
3532                }
3533             }
3534             if(/*py >= sy && */py < sy + th)
3535             {
3536                *block = textBlock;
3537                return textBlock.textLen;
3538             }
3539             sy += th;
3540          }
3541          *block = textBlock;
3542          result = textBlock.textLen;
3543       }
3544       return result;
3545    }
3546 }
3547
3548 Application componentsApp;
3549
3550 class Documentor : GuiApplication
3551 {
3552    bool Init()
3553    {
3554       Platform os = GetRuntimePlatform();
3555       componentsApp = __ecere_COM_Initialize(false, 1, null);
3556       SetPrivateModule(componentsApp);
3557       SetGlobalContext(globalContext);
3558       SetExcludedSymbols(&excludedSymbols);
3559       SetDefines(&::defines);
3560       SetImports(&imports);
3561       
3562       SetGlobalData(globalData);
3563
3564       settingsContainer.dataOwner = &settings;
3565       settingsContainer.Load();
3566       if(!settings.docDir || !settings.docDir[0] )
3567       {
3568          if(os == win32) // if Windows OS then
3569          {
3570             char programFilesDir[MAX_LOCATION];
3571             char appData[MAX_LOCATION]; 
3572             char homeDrive[MAX_LOCATION];
3573             char winDir[MAX_LOCATION];
3574             GetEnvironment("APPDATA", appData, sizeof(appData));
3575             GetEnvironment("HOMEDRIVE", homeDrive, sizeof(homeDrive));
3576             GetEnvironment("windir", winDir, sizeof(winDir));
3577             if(GetEnvironment("ProgramFiles", programFilesDir, MAX_LOCATION))
3578             {
3579                PathCat(programFilesDir, "ECERE SDK\\doc");
3580                settings.docDir = programFilesDir;
3581             }
3582             else if(homeDrive && homeDrive[0])
3583             {
3584                PathCat(homeDrive, "ECERE SDK\\doc");
3585                settings.docDir = homeDrive;
3586             }
3587             else if(winDir && winDir[0])
3588             {
3589                PathCat(winDir, "..\\ECERE SDK\\doc");
3590                settings.docDir = winDir;
3591             }
3592             else
3593                settings.docDir = "C:\\ECERE SDK\\doc";
3594          }
3595          else // if Os is Linux, or Mac OSX or something else
3596             settings.docDir = "/usr/share/ecere/doc/";
3597          settingsContainer.Save();
3598       }
3599
3600       //if(argc > 1)
3601       {
3602       #if 0
3603          Module module = eModule_Load(componentsApp, "ecere" /*argv[1]*/, privateAccess);
3604          DataRow row;
3605          AddComponents(module, true);
3606          mainForm.browser.currentRow = row = mainForm.browser.FindSubRow((int)module);
3607          // mainForm.browser.currentRow = row = mainForm.browser.FindSubRow((int)eSystem_FindClass(componentsApp, "Window"));
3608          while((row = row.parent))
3609             row.collapsed = false;
3610       #endif
3611       }
3612       return true;
3613    }
3614
3615    void Terminate()
3616    {
3617       FreeContext(globalContext);
3618       FreeExcludedSymbols(excludedSymbols);
3619       ::defines.Free(FreeModuleDefine);
3620       imports.Free(FreeModuleImport);
3621
3622       FreeGlobalData(globalData);
3623       FreeTypeData(componentsApp);
3624       FreeIncludeFiles();
3625       delete componentsApp;
3626    }
3627 }
3628
3629 MainForm mainForm { };