home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / workbench / stickit2 / source.lha / source / notemenus.c < prev    next >
C/C++ Source or Header  |  1995-01-09  |  5KB  |  242 lines

  1. /**************************************************
  2.  **************    notemenus.c   ******************
  3.  **************************************************/
  4.  
  5. #define INTUI_V36_NAMES_ONLY
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <libraries/iffparse.h>
  10. #include <libraries/gadtools.h>
  11.  
  12. #include <clib/iffparse_protos.h>
  13.  
  14. #include <stdio.h>        /* for printf */
  15. #include <string.h>
  16.  
  17. #include "stickit2.h"
  18.  
  19. #include "consts.h"
  20. #include "structs.h"
  21. #include "proto.h"
  22.  
  23. #define ID_FTXT        MAKE_ID('F','T','X','T')
  24. #define ID_CHRS        MAKE_ID('C','H','R','S')
  25.  
  26. extern prj_p prj;
  27.  
  28. /*
  29. Function : BOOL notemenu_editcopy(note_p curr_note)
  30. Puporse : Copy the current note's text into the clipboard. Returns TRUE if
  31.     sucessful, FALSE otherwise.
  32. */
  33.  
  34. BOOL notemenu_editcopy(note_p curr_note)
  35. {
  36.     struct IFFHandle *iffhandle = NULL;
  37.  
  38.     char errmsg[STRLEN_ERRMSG];
  39.  
  40.     long errno = 0;
  41.     int textlen;
  42.  
  43.     /* Is it a valid note ? */
  44.  
  45.     if (!curr_note)
  46.         return (FALSE);
  47.  
  48.     /* Allocate IFF structure */
  49.  
  50.     iffhandle = AllocIFF();
  51.  
  52.     if (!iffhandle) {
  53.         error("Can't allocate clipboard structure",ERR_WARNING,__LINE__,
  54. __FILE__);
  55.         return (FALSE);
  56.     }
  57.  
  58.     /* Set up IFF_File for clipboard IO */
  59.  
  60.     iffhandle->iff_Stream = (ULONG)OpenClipboard(prj->prefs.clipunit);
  61.  
  62.     if (!iffhandle) {
  63.         sprintf(errmsg,"Can't open clipboard unit %ul",
  64. prj->prefs.clipunit);
  65.         error(errmsg,ERR_WARNING,__LINE__,__FILE__);
  66.  
  67.         FreeIFF(iffhandle);
  68.  
  69.         return(FALSE);
  70.     }
  71.  
  72.     InitIFFasClip(iffhandle);
  73.  
  74.     /* Start the write to the clipboard */
  75.  
  76.     if (OpenIFF(iffhandle,IFFF_WRITE)) {
  77.         error("Can't write to clipboard unit",ERR_WARNING,__LINE__,
  78. __FILE__);
  79.  
  80.         CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
  81.         FreeIFF(iffhandle);
  82.  
  83.         return(FALSE);
  84.     }
  85.  
  86.     /* Write the data out */
  87.  
  88.     if (!(errno = PushChunk(iffhandle,ID_FTXT,ID_FORM,IFFSIZE_UNKNOWN))) {
  89.         if (!(errno = PushChunk(iffhandle,0,ID_CHRS,IFFSIZE_UNKNOWN))) {
  90.             textlen = strlen(curr_note->text);
  91.  
  92.             if (WriteChunkBytes(iffhandle,curr_note->text,textlen)!=
  93. textlen) {
  94.                 error("Can't write text to clipboard",
  95. ERR_WARNING,__LINE__,__FILE__);
  96.  
  97.                 errno = IFFERR_WRITE;
  98.             }
  99.         }
  100.  
  101.         if (!errno)
  102.             errno = PopChunk(iffhandle);
  103.     }
  104.  
  105.     if (!errno)
  106.         errno = PopChunk(iffhandle);
  107.  
  108.     /* Free resources */
  109.  
  110.     CloseIFF(iffhandle);
  111.  
  112.     CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
  113.  
  114.     /* Finally, free iffhandle */
  115.  
  116.     FreeIFF(iffhandle);
  117.  
  118.     if (errno)
  119.         return (FALSE);
  120.     else
  121.         return (TRUE);
  122. }
  123.  
  124. /*
  125. Function : void notemenu_editpaste(note_p curr_note)
  126. Puporse : Copy the current note's from the clipboard into the current cursor
  127.     position.
  128. */
  129.  
  130. void notemenu_editpaste(note_p curr_note)
  131. {
  132.     struct IFFHandle *iffhandle = NULL;
  133.     struct ContextNode *contextnode = NULL;
  134.  
  135.     char errmsg[STRLEN_ERRMSG];
  136.     char pastebuffer[STRLEN_NOTETEXT];
  137.  
  138.     long errno = 0;
  139.     int textlen;
  140.     int l;
  141.  
  142.     /* Is it a valid note with carat ? */
  143.  
  144.     if (!curr_note)
  145.         return;
  146.  
  147.     if (!curr_note->carat.text)
  148.         return;
  149.  
  150.     /* Allocate IFF structure */
  151.  
  152.     iffhandle = AllocIFF();
  153.  
  154.     if (!iffhandle) {
  155.         error("Can't allocate clipboard structure",ERR_WARNING,__LINE__,
  156. __FILE__);
  157.         return;
  158.     }
  159.  
  160.     /* Set up IFF_File for clipboard IO */
  161.  
  162.     iffhandle->iff_Stream = (ULONG)OpenClipboard(prj->prefs.clipunit);
  163.  
  164.     if (!iffhandle) {
  165.         sprintf(errmsg,"Can't open clipboard unit %ul",
  166. prj->prefs.clipunit);
  167.         error(errmsg,ERR_WARNING,__LINE__,__FILE__);
  168.  
  169.         FreeIFF(iffhandle);
  170.  
  171.         return;
  172.     }
  173.  
  174.     InitIFFasClip(iffhandle);
  175.  
  176.     /* Start the write to the clipboard */
  177.  
  178.     if (OpenIFF(iffhandle,IFFF_READ)) {
  179.         error("Can't read from clipboard unit",ERR_WARNING,__LINE__,
  180. __FILE__);
  181.  
  182.         CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
  183.         FreeIFF(iffhandle);
  184.  
  185.         return;
  186.     }
  187.  
  188.     /* If can't find right chunk */
  189.  
  190.     if (StopChunk(iffhandle,ID_FTXT,ID_CHRS)) {
  191.         CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
  192.         FreeIFF(iffhandle);
  193.  
  194.         return;
  195.     }
  196.  
  197.     while (TRUE) {
  198.         errno = ParseIFF(iffhandle,IFFPARSE_SCAN);
  199.  
  200.         if (errno == IFFERR_EOC)
  201.             continue;        /* enter next context */
  202.         else if (errno)
  203.             break;
  204.  
  205.         contextnode = CurrentChunk(iffhandle);
  206.  
  207.         if ((contextnode) && (contextnode->cn_Type == ID_FTXT) &&
  208. (contextnode->cn_ID)) {
  209.             while ((textlen = ReadChunkBytes(iffhandle,pastebuffer,
  210. STRLEN_NOTETEXT)) > 0) {
  211.                 pastebuffer[textlen] = '\0';
  212.  
  213.                 for (l = 0; l < textlen; l++)
  214.                     carat_addachar(curr_note,(char)
  215. ((pastebuffer[l] == '\012')?' ':pastebuffer[l]),FALSE);
  216.             }
  217.  
  218.             if (textlen < 0)
  219.                 errno = textlen;
  220.         }
  221.     }
  222.  
  223.     /* Refresh the note */
  224.  
  225.     carat_from_ptr(curr_note,PTRCHANGE_CLICK);
  226.     note_refresh(curr_note);
  227.  
  228.     /* Free resources */
  229.  
  230.     CloseIFF(iffhandle);
  231.  
  232.     CloseClipboard((struct ClipboardHandle *)iffhandle->iff_Stream);
  233.  
  234.     /* Finally, free iffhandle */
  235.  
  236.     FreeIFF(iffhandle);
  237.  
  238.     /* The note text has probably been changed */
  239.  
  240.     prj->projectchanged = TRUE;
  241. }
  242.