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