Index: src/util.cpp
===================================================================
--- src/util.cpp	(revision 755)
+++ src/util.cpp	(working copy)
@@ -52,6 +52,7 @@
 #include "parserintf.h"
 #include "bufstr.h"
 #include "image.h"
+#include "htmlgen.h"
 
 #define ENABLE_TRACINGSUPPORT 0
 
@@ -4598,9 +4599,74 @@
   //printf("substitute(%s,%s,%s)->%s\n",s,src,dst,result.data());
   return result;
 }
+//----------------------------------------------------------------------
+// Clear a text block from begin to end markers
 
+QCString clearBlock(const char *s,const char *begin,const char *end)
+{
+  if (s==0 || begin==0 || end==0) return s;
+  const char *p, *q;
+  int beginLen = strlen(begin);
+  int endLen = strlen(end);
+  int resLen = 0;
+  for(p=s; (q=strstr(p,begin))!=0; p=q+endLen)
+  {
+    resLen+=q-p;
+    p=q+beginLen;
+    if((q=strstr(p,end))==0)
+    {
+      resLen+=beginLen;
+      break;
+    }
+  }
+  resLen+=strlen(p);
+
+  QCString result(resLen+1);
+  char *r;
+  for (r=result.data(), p=s; (q=strstr(p,begin))!=0; p=q+endLen)
+  {
+    int l = (int)(q-p);
+    memcpy(r,p,l);
+    r+=l;
+    p=q+beginLen;
+    if((q=strstr(p,end))==0)
+    {
+      memcpy(r,begin,beginLen);
+      r+=beginLen;
+      break;
+    }
+  }
+  strcpy(r,p);
+  return result;
+}
 //----------------------------------------------------------------------
 
+QCString selectBlock(const QCString& s,const QCString &name,bool which)
+{
+  const QCString begin = "<!--BEGIN " + name + "-->";
+  const QCString end = "<!--END " + name + "-->";
+  const QCString nobegin = "<!--BEGIN NO" + name + "-->";
+  const QCString noend = "<!--END NO" + name + "-->";
+
+  QCString result = s;
+  if (which)
+  {
+    result = substitute(result, begin, "");
+    result = substitute(result, end, "");
+    result = clearBlock(result, nobegin, noend);
+  }
+  else
+  {
+    result = substitute(result, nobegin, "");
+    result = substitute(result, noend, "");
+    result = clearBlock(result, begin, end);
+  }
+
+  return result;
+}
+
+//----------------------------------------------------------------------
+
 struct FindFileCacheElem
 {
   FindFileCacheElem(FileDef *fd,bool ambig) : fileDef(fd), isAmbig(ambig) {}
@@ -4721,6 +4787,69 @@
 
 QCString substituteKeywords(const QCString &s,const char *title,const QCString &relPath)
 {
+  // Build CSS/Javascript tags depending on treeview, search engine settings
+  QCString treeViewCss = "";
+  QCString treeViewJs = "";
+  QCString searchCss = "";
+  QCString searchJs = "";
+  QCString searchBox = "";
+  QCString mathJaxJs = "";
+  static bool treeView = Config_getBool("GENERATE_TREEVIEW");
+  static bool searchEngine = Config_getBool("SEARCHENGINE");
+  static bool serverBasedSearch = Config_getBool("SERVER_BASED_SEARCH");
+  static bool mathJax = Config_getBool("USE_MATHJAX");
+  static bool disableIndex = Config_getBool("DISABLE_INDEX");
+  static bool haveProjectName = !Config_getString("PROJECT_NAME").isEmpty();
+  static bool haveProjectNumber = !Config_getString("PROJECT_NUMBER").isEmpty();
+  static bool haveProjectBrief = !Config_getString("PROJECT_BRIEF").isEmpty();
+  static bool haveProjectLogo = !Config_getString("PROJECT_LOGO").isEmpty();
+  static bool titleArea = (haveProjectName || haveProjectBrief || haveProjectLogo || (disableIndex && searchEngine));
+
+  if (treeView)
+  {
+    treeViewCss = "<link href=\"$relpath$navtree.css\" rel=\"stylesheet\" type=\"text/css\"/>";
+    treeViewJs = "<script type=\"text/javascript\" src=\"$relpath$jquery.js\"></script>\n"
+                 "<script type=\"text/javascript\" src=\"$relpath$resize.js\"></script>\n"
+                 "<script type=\"text/javascript\" src=\"$relpath$navtree.js\"></script>\n"
+                 "<script type=\"text/javascript\">\n"
+                 "  $(document).ready(initResizable);\n"
+                 "</script>";
+  }
+
+  if (searchEngine)
+  {
+    searchCss = "<link href=\"$relpath$search/search.css\" rel=\"stylesheet\" type=\"text/css\"/>";
+    if (!Config_getBool("GENERATE_TREEVIEW"))
+    {
+      searchJs = "<script type=\"text/javascript\" src=\"$relpath$jquery.js\"></script>\n";
+    }
+    searchJs = searchJs + "<script type=\"text/javascript\" src=\"$relpath$search/search.js\"></script>\n";
+
+    if (!serverBasedSearch) {
+      searchJs = searchJs + "<script type=\"text/javascript\">\n"
+                            "  $(document).ready(function() { searchBox.OnSelectItem(0); });\n"
+                            "</script>";
+    }
+    else {
+      searchJs = searchJs + "<script type=\"text/javascript\">\n"
+                            "  $(document).ready(function() {\n"
+                            "    if ($('.searchresults').length > 0) { searchBox.DOMSearchField().focus(); }\n"
+                            "  });\n"
+                            "</script>";
+    }
+    searchBox = HtmlGenerator::getSearchBox(serverBasedSearch, relPath, FALSE);
+  }
+
+  if (mathJax)
+  {
+    mathJaxJs = "<script src=\"$relpath$MathJax.js\">\n"
+                "  MathJax.Hub.Config({\n"
+                "    extensions: [\"tex2jax.js\"],\n"
+                "    jax: [\"input/TeX\",\"output/HTML-CSS\"],\n"
+                "});\n"
+                "</script>\n";
+  }
+
   QCString result = s.copy();
   if (title) result = substitute(result,"$title",title);
   result = substitute(result,"$datetime",dateToString(TRUE));
@@ -4731,7 +4860,22 @@
   result = substitute(result,"$projectnumber",Config_getString("PROJECT_NUMBER"));
   result = substitute(result,"$projectbrief",Config_getString("PROJECT_BRIEF"));
   result = substitute(result,"$projectlogo",Config_getString("PROJECT_LOGO"));
+  result = substitute(result,"$treeviewcss",treeViewCss);
+  result = substitute(result,"$treeviewjs",treeViewJs);
+  result = substitute(result,"$searchcss",searchCss);
+  result = substitute(result,"$searchjs",searchJs);
+  result = substitute(result,"$searchbox",searchBox);
+  result = substitute(result,"$mathjaxjs",mathJaxJs);
   result = substitute(result,"$relpath$",relPath);
+  
+  result = selectBlock(result,"DISABLEINDEX",disableIndex);
+  result = selectBlock(result,"TREEVIEW",treeView);
+  result = selectBlock(result,"SEARCH",searchEngine);
+  result = selectBlock(result,"TITLEAREA",titleArea);
+  result = selectBlock(result,"PROJECTNAME",haveProjectName);
+  result = selectBlock(result,"PROJECTNUMBER",haveProjectNumber);
+  result = selectBlock(result,"PROJECTBRIEF",haveProjectBrief);
+  result = selectBlock(result,"PROJECTLOGO",haveProjectLogo);
   return result;
 }
 
Index: src/htmlgen.h
===================================================================
--- src/htmlgen.h	(revision 755)
+++ src/htmlgen.h	(working copy)
@@ -34,12 +34,13 @@
     virtual ~HtmlGenerator();
     static void init();
     static void writeStyleSheetFile(QFile &f);
-    static void writeHeaderFile(QFile &f);
+    static void writeHeaderFile(QFile &f, const char *cssname);
     static void writeFooterFile(QFile &f);
     static void writeTabData();
     static void writeSearchFooter(FTextStream &t,const QCString &relPath);
     static void writeSearchData(const char *dir);
     static void writeSearchPage();
+    static QCString getSearchBox(bool serverSide, QCString relPath, bool highlightSearch);
     static QCString writeLogoAsString(const char *path);
     static QCString writeSplitBarAsString(const char *name,const char *relpath);
    
Index: src/doxygen.cpp
===================================================================
--- src/doxygen.cpp	(revision 755)
+++ src/doxygen.cpp	(working copy)
@@ -9430,7 +9430,7 @@
           QFile f;
           if (openOutputFile(argv[optind+1],f))
           {
-            HtmlGenerator::writeHeaderFile(f);
+            HtmlGenerator::writeHeaderFile(f, argv[optind+3]);
           }
           f.close();
           if (openOutputFile(argv[optind+2],f))
Index: src/util.h
===================================================================
--- src/util.h	(revision 755)
+++ src/util.h	(working copy)
@@ -178,6 +178,10 @@
 
 QCString substitute(const char *s,const char *src,const char *dst);
 
+QCString clearBlock(const char *s,const char *begin,const char *end);
+
+QCString selectBlock(const QCString& s,const QCString &name,bool which);
+
 QCString resolveDefines(const char *n);
 
 ClassDef *getClass(const char *key);
Index: src/htmlgen.cpp
===================================================================
--- src/htmlgen.cpp	(revision 755)
+++ src/htmlgen.cpp	(working copy)
@@ -58,7 +58,17 @@
 #include "search_css.h"
 ;
 
+static const char search_jquery_script1[]=
+#include "jquery_js.h"
+;
+static const char search_jquery_script2[]=
+#include "sizzle_js.h"
+;
+static const char search_jquery_script3[]=
+#include "jquery_ui_js.h"
+;
 
+
 static QCString g_header;
 static QCString g_footer;
 
@@ -1022,38 +1032,82 @@
 }
 
 
-void HtmlGenerator::writeHeaderFile(QFile &file)
+void HtmlGenerator::writeHeaderFile(QFile &file, const char *cssname)
 {
   FTextStream t(&file);
-  writeDefaultHeaderFile(t,file.name(),"$title",relativePathToRoot(0),TRUE);
-  t << "<div id=\"top\"><!-- do not remove this div! -->" << endl;
-  writeTitleArea(t,"$relpath$","$projectname","$projectbrief","$projectnumber","$projectlogo");
+  
+  // writeDefaultHeaderFile(t,file.name(),"$title",relativePathToRoot(0),TRUE);
+  
+  QString relPathStr = "$relpath$";
+
+  QCString id(file.name());
+  if (id.right(Doxygen::htmlFileExtension.length())==Doxygen::htmlFileExtension) 
+  {
+    id=id.left(id.length()-Doxygen::htmlFileExtension.length());
+  }
+
+  t << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
+  t << "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n"
+       "<meta http-equiv=\"Content-Type\" content=\"text/xhtml;charset=UTF-8\"/>\n"
+       "<title>$title</title>\n";
+  t << "<link href=\"$relpath$tabs.css\" rel=\"stylesheet\" type=\"text/css\"/>\n";
+  t << "<link href=\"$relpath$" << cssname << "\" rel=\"stylesheet\" type=\"text/css\" />\n";
+  t << "$treeviewcss\n"
+       "$searchcss\n";
+  t << "$treeviewjs\n"
+       "$searchjs\n"
+       "$mathjaxjs\n";
+  t << "</head>\n";
+  t << "<body>\n";
+  t << "<div id=\"top\"><!-- do not remove this div! -->\n";
+  
+  t << "<!--BEGIN TITLEAREA-->\n";
+  t << "<div id=\"titlearea\">\n";
+  t << "<table cellspacing=\"0\" cellpadding=\"0\">\n";
+  t << " <tbody>\n";
+  t << " <tr style=\"height: 56px;\">\n";
+  t << " <!--BEGIN PROJECTLOGO--><td id=\"projectlogo\"><img alt=\"Logo\" src=\"$relpath$$projectlogo\"></td><!--END PROJECTLOGO-->\n\n";
+
+  t << " <!--BEGIN PROJECTNAME--><td style=\"padding-left: 0.5em;\">\n";
+  t << "  <div id=\"projectname\">$projectname<!--BEGIN PROJECTNUMBER-->&#160;<span id=\"projectnumber\">$projectnumber</span><!--END PROJECTNUMBER--></div>\n";
+  t << "  <!--BEGIN PROJECTBRIEF--><div id=\"projectbrief\">$projectbrief</div><!--END PROJECTBRIEF-->\n";
+  t << "  </td><!--END PROJECTNAME-->\n";
+  t << " <!--BEGIN NOPROJECTNAME--><!--BEGIN PROJECTBRIEF--><td style=\"padding-left: 0.5em;\">\n";
+  t << "  <div id=\"projectbrief\">$projectbrief</div>\n";
+  t << "  </td><!--END PROJECTBRIEF--><!--END NOPROJECTNAME-->\n\n";  
+  
+  t << "<!--BEGIN DISABLEINDEX--><!--BEGIN SEARCH-->\n";
+  t << "  <td>$searchbox</td>\n";
+  t << "<!--END SEARCH--><!--END DISABLEINDEX-->\n\n";
+  
+  t << " </tr>\n";
+  t << " </tbody>\n";
+  t << "</table>\n";
+  t << "</div>\n";
+  t << "<!--END TITLEAREA-->\n";
 }
 
 void HtmlGenerator::writeFooterFile(QFile &file)
 {
-  static bool generateTreeView = Config_getBool("GENERATE_TREEVIEW");
   FTextStream t(&file);
+  
+  t << "<!--BEGIN TREEVIEW-->\n";
+  t << "    <li class=\"footer\">";
+  t << theTranslator->trGeneratedAt( "$datetime", "$projectname" ) << endl;
+  t << "    <a href=\"http://www.doxygen.org/index.html\">\n";
+  t << "    <img class=\"footer\" src=\"doxygen.png\" alt=\"doxygen\"/></a> $doxygenversion </li>\n";
+  t << "   </ul>\n";
+  t << " </div>\n";
+  t << "<!--END TREEVIEW-->\n";
+  t << "<!--BEGIN NOTREEVIEW-->\n";
+  t << "<hr class=\"footer\"/><address class=\"footer\"><small>\n";
+  t << theTranslator->trGeneratedAt( "$datetime", "$projectname" );
+  t << "&#160;<a href=\"http://www.doxygen.org/index.html\">"
+    << "<img class=\"footer\" src=\"$relpath$doxygen.png\" alt=\"doxygen\"/>"
+    << "</a> $doxygenversion";
+  t << "</small></address>\n";
+  t << "<!--END NOTREEVIEW-->\n";
 
-  if (generateTreeView)
-  {
-    t << "    <li class=\"footer\">";
-    t << theTranslator->trGeneratedAt( "$datetime", "$projectname" ) << endl;
-    t << "    <a href=\"http://www.doxygen.org/index.html\">\n";
-    t << "    <img class=\"footer\" src=\"doxygen.png\" alt=\"doxygen\"/></a> $doxygenversion </li>\n";
-    t << "   </ul>\n";
-    t << " </div>\n";
-  }
-  else
-  {
-    t << "<hr class=\"footer\"/><address class=\"footer\"><small>\n";
-    t << theTranslator->trGeneratedAt( "$datetime", "$projectname" );
-    t << "&#160;<a href=\"http://www.doxygen.org/index.html\">"
-      << "<img class=\"footer\" src=\"$relpath$doxygen.png\" alt=\"doxygen\"/>"
-      << "</a> $doxygenversion";
-    t << "</small></address>\n";
-  }
-
   t << "</body>\n"
     << "</html>\n";
 }
@@ -1217,6 +1271,20 @@
   t << "\n";
 }
 
+QCString HtmlGenerator::getSearchBox(bool serverSide, QCString relPath, bool highlightSearch)
+{
+  QGString result;
+  FTextStream t(&result);
+  if (serverSide) {
+    writeServerSearchBox(t, relPath, highlightSearch);
+  }
+  else {
+     writeClientSearchBox(t, relPath);
+  }
+  return QCString(result);
+}
+
+
 QCString HtmlGenerator::writeLogoAsString(const char *path)
 {
   static bool timeStamp = Config_getBool("HTML_TIMESTAMP");
@@ -2803,6 +2871,17 @@
   {
      err("Failed to open file '%s' for writing...\n",scriptName.data());
   }
+
+    // generate jquery.js, if not done by Treeview
+  if (!generateTreeView)
+  {
+    QFile f(Config_getString("HTML_OUTPUT")+"/jquery.js");
+    if (f.open(IO_WriteOnly))
+    {
+      FTextStream t(&f);
+      t << search_jquery_script1 << search_jquery_script2 << search_jquery_script3;
+    }
+  }
 }
 
 #if 0
