/* notefncs.c */

#include "ml.h"

Menu note_file_menu[] = {
  { NULL, "save", NUL_TERM,
      note_save,  NULL, 0, NULL, NULL, BTN_ON },
  { NULL, "print", NUL_TERM,
      note_print, NULL, 0, NULL, NULL, BTN_ON },
  { NULL, "close_window", NUL_TERM,
      note_close, NULL, 0, NULL, NULL, BTN_ON },
};

Menu note_menu[] = {
  { NULL, "file_menu", NUL_TERM,
      NULL, note_file_menu, XtNumber(note_file_menu), 
      NULL, NULL, BTN_ON },
  { NULL, "HELP", NUL_TERM,
      note_help, NULL, 0, 
      NULL, NULL, BTN_ON },
};


#ifdef __STDC__
Note *new_note(void)
#else
Note *new_note()
#endif
{
  Note *note = (Note *) fs_get(sizeof(Note));
  note->is_realized = FALSE;
  note->autosave = (XtIntervalId) 0;
  return(note);
}

#ifdef __STDC__
void create_note_window(void)
#else
void create_note_window()
#endif
{
  Arg args[ARGLISTSIZE];
  int n = 0;
  Widget form, menubar;
  XtTranslations translations;
  Note *note;


  if(session->note != NULL) {
    if(session->note->is_realized == TRUE)
      de_iconify(session->note->shell);
    else {
      XtPopup(session->note->shell, XtGrabNone);
      session->note->is_realized = TRUE;
    }
    return;
  }

  note = new_note();
  session->note = note;

  note->autosave = (XtIntervalId) 0;

  push_cursor(WATCH_CURSOR);
  XtSetArg (args[n], XmNdeleteResponse, XmDO_NOTHING); n++;        
  note->shell = XtCreatePopupShell("notebook", topLevelShellWidgetClass,
			       session->shell, args, n);
  n = 0;

  AddDestroyCallback (note->shell);
  setup_editres(note->shell);

  if(note_icon != (Pixmap) None)
    XtVaSetValues(note->shell,
		  XmNiconPixmap,note_icon,
		  NULL);

  form = XmCreateForm(note->shell, "form", args, n);            n = 0;
  XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM);           n ++;
  XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM);          n ++;
  XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM);         n ++;
  menubar = XmCreateMenuBar(form,"menubar", args, n);           n = 0;
  XtManageChild(menubar);
  create_buttons(NULL, menubar, note_menu, XtNumber(note_menu), 
		      BTN_ON, (XtPointer) note, ROOTMENULEVEL);

  XtSetArg(args[n], XmNeditable, TRUE);                         n ++;
  XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT);            n ++;
  XtSetArg(args[n], XmNwordWrap, TRUE);                         n ++;
  XtSetArg(args[n], XmNcolumns,  COMPOSEWIDTH - 1);             n ++;
  XtSetArg(args[n], XmNscrollVertical, TRUE);                   n ++; 
  XtSetArg(args[n], XmNscrollHorizontal, FALSE );               n ++;

  note->text =
    XmCreateScrolledText(form, "text", args, n);        n = 0;
  
  translations = XtParseTranslationTable(GLOBAL_text_translations);
  XtOverrideTranslations(note->text, translations);

  XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET);         n ++;
  XtSetArg(args[n], XmNtopWidget, menubar);                     n ++;
  XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM);        n ++;
  XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM);          n ++;
  XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM);         n ++;
  XtSetValues(XtParent(note->text), args, n);        n = 0;
  XtManageChild(note->text);

  translations = XtParseTranslationTable(GLOBAL_note_pop_translations);
  XtOverrideTranslations(note->text, translations);

  XtAddCallback(note->shell, XmNpopdownCallback, 
		(XtCallbackProc) note_popdown, note);

  XtAddCallback(note->shell, XmNdestroyCallback, 
		(XtCallbackProc) note_destroy, note);



  if(preferences.note_save)
    note->autosave =  XtAppAddTimeOut(context,preferences.note_save * 1000L,
				      (XtTimerCallbackProc) note_autosave,
				      note);

  XtManageChild(form);
  XtManageChild(note->shell);
  XtPopup(note->shell, XtGrabNone);
  note->is_realized = TRUE;
  note_load(note);
  pop_cursor();
  return;
}



#ifdef __STDC__
void note_load(Note *note)
#else
void note_load(note)
     Note *note;
#endif
{
  char filename[MAXPATHLEN];
  Binary_Buffer *binary_buffer = NULL;

  sprintf(filename,"%s/%s",session->cachedir,NOTEFILE);
  if((access(filename,X_OK)) != SYSCALL_SUCCESS)
    sprintf(filename,"%s/%s",local_auth.homedir,OLDNOTEFILE);
  binary_buffer = load_binary_file(filename);
  if(binary_buffer) {
    text_blast(note->text,(char *) binary_buffer->data);
    free_binary_buffer(binary_buffer);
  }
  return;
}

#ifdef __STDC__
void note_autosave(Note *note, XtIntervalId *id)
#else
void note_autosave(note,id)
     Note *note;
     XtIntervalId *id;
#endif
{
  if(note->is_realized == TRUE) {
    if(session->busy == FALSE)
      note_save(NULL, note, NULL);
    if(preferences.note_save) 
      note->autosave =  XtAppAddTimeOut(context,preferences.note_save * 1000L,
					(XtTimerCallbackProc) note_autosave,
					note);
    else
      note->autosave = (XtIntervalId) 0;
  }
  return;
}


#ifdef __STDC__
void note_save(Widget w, Note *note, XtPointer xp)
#else
void note_save(w,note,xp)
     Widget w;
     Note *note;
     XtPointer xp;
#endif
{
  char filename[MAXPATHLEN];
  char buffer[FILEBUFFLEN];
  FILE *fp;
  char *str;
  int errors = 0;

  push_cursor(WATCH_CURSOR);

  str = XmTextGetString(note->text);

  if((str == NULL) || (*str == NUL_TERM))
    if(str != NULL)
      fs_give((void **) &str);
    
  if(str) {
    sprintf(filename,"%s/%s",session->cachedir,NOTEFILE);
    if((fp = fopen(filename,"w")) == NULL) {
      sprintf(buffer,MLGetLocalized(XtNmsgCannotWriteFile,MsgCannotWriteFile),
	      filename);
      mm_log(buffer,WARN);
      pop_cursor();
      return;
    }
    (void) chmod(filename,S_IRWXU);

    if((fwrite(str,strlen(str),1,fp)) != 1) 
      errors ++;
    fs_give((void **) &str);
  
    if(fclose(fp))
      errors ++;

    if(errors)
      mm_log(MLGetLocalized(XtNmsgNoteSaveFail,MsgNoteSaveFail),WARN);
    else
      mm_log(MLGetLocalized(XtNmsgNoteSaved,MsgNoteSaved),NIL);
  }

  pop_cursor();
  return;
}

#ifdef __STDC__
void note_print(Widget w, Note *note, XtPointer xp)
#else
void note_print(w,note,xp)
     Widget w;
     Note *note;
     XtPointer xp;
#endif
{
  char *txt;

  if(*preferences.print_command == NUL_TERM) {
    mm_log(MLGetLocalized(XtNmsgNoPrintCommand,MsgNoPrintCommand), WARN);
    return;
  }

  push_cursor(WATCH_CURSOR);

  txt = XmTextGetString(note->text);
  if(txt != NULL) {
    if(write_to_pipe(preferences.print_command, NULL,
		     txt, strlen(txt)))
      mm_log(MLGetLocalized(XtNmsgPrintFail,MsgPrintFail), WARN);
    else
      mm_log(MLGetLocalized(XtNmsgPrintSuccess,MsgPrintSuccess), NIL);
    fs_give((void **) &txt);
  }
  pop_cursor();
  return;
}


#ifdef __STDC__
void note_help(Widget w, Note *note, XtPointer xp)
#else
void note_help(w,note,xp)
     Widget w;
     Note *note;
     XtPointer xp;
#endif
{
  help(note->shell, NOTEHELPFILE);
  return;
}

#ifdef __STDC__
void note_close(Widget w, Note *note, XtPointer xp)
#else
void note_close(w,note,xp)
     Widget w;
     Note *note;
     XtPointer xp;
#endif
{
  note_save(w,note,xp);
  if(note->autosave)
    XtRemoveTimeOut(note->autosave);
  note->autosave = (XtIntervalId) 0;
  XtPopdown(note->shell);
  note->is_realized = FALSE;
  return;
}

#ifdef __STDC__
void note_popdown(Widget w, Note *note, XtPointer xp)
#else
void note_popdown(w,note,xp)
     Widget w;
     Note *note;
     XtPointer xp;
#endif
{
  if(note->autosave)
    XtRemoveTimeOut(note->autosave);
  note->autosave = (XtIntervalId) 0;
  note->is_realized = FALSE;
  return;
}

#ifdef __STDC__
void note_destroy(Widget w, Note *note, XtPointer xp)
#else
void note_destroy(w,note,xp)
     Widget w;
     Note *note;
     XtPointer xp;
#endif
{
  if(note->autosave)
    XtRemoveTimeOut(note->autosave);
  fs_give((void **) &session->note);
  session->note = NULL;
  return;
}


#ifdef __STDC__
void note_pop(Widget w, XtPointer xp)
#else
void note_pop(w,xp)
     Widget w;
     XtPointer xp;
#endif
{
  note_close(w,session->note,NULL);
  return;
}


syntax highlighted by Code2HTML, v. 0.9.1