// Version 1.00 07/10/96  First full release.
//         1.01 04/11/96  Shade New and Marker options if there is no active text frame.
//         1.02 22/11/96  'New' menu changed to 'Bookmark'.


int bookmark_handle;
int bookmark_listhandle;
int bookmark_newhandle;


// return the handle of the bookmark at the index specified

int bookmark_fromindex(int index)
{
 string s;

 return(bmname(index, s));
}


// if marker exists, mark from bm to caret else set up bm

void bookmark_marker(void)
{
 int bm;
 string s = "{BOOKMARK_05}";


 translate(s);
 bm=bmfind(s);
 if(bm)
 {
  setzonetobm(bm);
  bmdelete(bm);
 }
 else
 {
  bm=bmcreate(s);
  if(bm)
  {
   setbmtocaret(bm);
   bbc_vdu(7);
  }
 }
}


// deal with 'Bookmarks' menu entries

int bookmark_entry(int entry,int subcode)
{
 switch(entry)
 {
  case 0:               // Goto
         setcarettobm(bookmark_fromindex(subcode));
         break;

  case 1:               // View
         bmview(bookmark_fromindex(subcode));
         break;

  case 2:               // Delete
         bmdelete(bookmark_fromindex(subcode));
         break;
  case 4:
         bookmark_marker();
         break;
 }
 return(0);
}


// shade 'Bookmarks' entries if there are no bookmarks

int bookmark_flags(int entry,string & text)
{
 string s;

 switch(entry)
 {
  case 0:
  case 1:
  case 2:
         return(bmname(0, s) ? 0 : SHADED);
         break;
  case 3:
  case 4:
         return((activetype(TEXTFRAME) > 1) ? 0 : SHADED);
         break;
 }
}


// deal with 'New' menu entry

int bookmark_newentry(int entry,int subcode)
{
 string s;
 int    bm;

 s = string_menu(bookmark_newhandle,0);
 if(slen(s))
 {
  bm=bmcreate(s);
  if(bm)
   setbmtocaret(bm);
 }
 return(0);
}


// make 'New' sub-menu writable

int bookmark_newflags(int entry,string & text)
{
 return((8<<24)|(8<<16)|WRITEABLE);
}


int bookmark_menu(int open)
{
 return(bookmark_handle);
}


// deal with entries on the bookmark list menu

int bookmark_listentry(int entry,int subcode)
{
 return(entry);
}              


// create a menu listing all the bookmarks that have been created

int bookmark_list(int open)
{
 int    i;
 string s;

 if(bookmark_listhandle)
  delete_menu(bookmark_listhandle);
 bookmark_listhandle = create_menu("{BOOKMARK_06}");

 i = 0;
 while(bmname(i++, s))
  addentry_menu(bookmark_listhandle,"bookmark_listentry","","","",s);

 return(bookmark_listhandle);
}


int bookmark_new(int open)
{
 return(bookmark_newhandle);
}


// create 'Bookmarks' menu and add it to 'Applets' menu

void main(void)
{
 script_menu_initialise();

 bookmark_handle=create_menu("{BOOKMARK_00}"); 
 addentry_menu(bookmark_handle,"bookmark_entry","bookmark_flags","bookmark_list","","{BOOKMARK_01}");
 addentry_menu(bookmark_handle,"bookmark_entry","bookmark_flags","bookmark_list","","{BOOKMARK_02}");
 addentry_menu(bookmark_handle,"bookmark_entry","bookmark_flags","bookmark_list","","{BOOKMARK_03}");
 addentry_menu(bookmark_handle,"","bookmark_flags","bookmark_new","","{BOOKMARK_04}");
 addentry_menu(bookmark_handle,"bookmark_entry","bookmark_flags","","C_Y","{BOOKMARK_05}");

 addentry_menu(script_handle,"","","bookmark_menu","","{BOOKMARK_00}");

 bookmark_newhandle=create_menu("{BOOKMARK_06}");
 addentry_menu(bookmark_newhandle,"bookmark_newentry","bookmark_newflags","","","");
}
