
int document_handle;
int document_number = 10;
string document_path;
string document_names;
int document_maxlen=255;

// read documents from file

void document_read(void)
{
 int h;

 h = fileopen(document_path, "rb");
 if(h)
 {
  filereads(document_names, h);
  fileclose(h);
 }
 else
  document_names = "";
}


// write documents to file

void document_write(void)
{
 int h;

 h = fileopen(document_path, "wb");
 if(h)
 {
  filewrites(document_names, h);
  fileclose(h);
 }
}


// add document name to end of list

void document_add(string s)
{
 int i, c;

 document_names += s;

 c = 0;
 for(i = 0; i < slen(document_names); ++i)
  if(mids(document_names, i, 1) == "\t")
   ++c;

 if(c > document_number)
  document_names = document_names << document_names / "\t" + 1;

 document_write();
}


// extract leafname from pathname

string document_leafname(string s)
{
 int i;

 for(i = slen(s); i > 0; --i)
 {
  if(mids(s, i - 1, 1) == ".")
  {
   s = s << i;
   break;
  }
 }
 return(s);
}


// record name of saved document

void document_save(int user, int file)
{
 string s;
 int i;

 fileinfo(file, s);
 i = slen(document_leafname(s));
 if(i > document_maxlen)
  s = mids(s, 0, slen(s) - i + document_maxlen);

 s += "\t";
 i = document_names / s;
 if(i >= 0)
 {
  if(slen(document_names) != i + slen(s))
  {
   document_names = mids(document_names, 0, i) + (document_names << i + slen(s));
   document_add(s);
  }
 }
 else
  document_add(s);

 if(document_handle)
 {
  delete_menu(document_handle);
  document_handle=0;
 }
}


// get filename for specified entry

string document_filename(int entry)
{
 int c, i;
 string s, t;

 s = document_names;

 for(c = 0; c <= entry; ++c)
 {
  i = s / "\t";
  t = mids(s, 0, i);
  s = s << i + 1;
 }
 return(t);
}


// shade 'Document' menu entry

int document_flags(int entry, string &text)
{
 if(document_names == "")
  return(SHADED);
}


// open directory or run the file

int document_menuentry(int entry, int subcode)
{
 string s;

 if(objectexists(document_filename(entry)))
  loadfile(document_filename(entry));
 else
 {
  s = "{DOCUMENT_01}";
  translate(s);
  messagebox(s);
 }
 return(0);
}


// shade open documents

int document_menuflags(int entry, string &text)
{
 int f;
 string s;

 f = 0;
 while((f=nextuserfile(f)))
 {
  fileinfo(f, s);
  if(s == document_filename(entry))
   return(SHADED);
 }
}


// rebuild menu each time it is opened

int document_menu(int open)
{
 string s, t;
 int i = 0;

 if(!document_handle)
 {
  document_handle=create_menu("{DOCUMENT_00}");

  s = document_names;
  while((i = s / "\t") >= 0)
  {
   t = mids(s, 0, i);
   s = s << i + 1;
   addentry_menu(document_handle,"document_menuentry","document_menuflags","","", document_leafname(t));
  }
 }

 return(document_handle);
}


// find the position of an entry on a menu

int document_findmenuentry(int handle, string &t)
{
 int entry;
 string s;

 entry = 0;

 while(menudefstring(handle, entry, 4, s))
 {
  if(s == t)
   return(entry);
  entry++;
 }
 return(0);
}


// create 'Documents' submenu and add it to the icon bar menu

void main(void)
{
 int index;

 document_path = "OvationPro$AppletsDir";
 getenvs(document_path);
 document_path += ".!Document.Documents";

 index = document_findmenuentry(info_handle, "Quit");
 insertentry_menu(info_handle,index,"","document_flags","document_menu","","{DOCUMENT_00}");

 addeventhandler(0x211, 0, "document_save");
 document_read();
}
