home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d9xx / d913 / stickit.lha / StickIt / Source / Source.lha / notes.c < prev    next >
C/C++ Source or Header  |  1993-08-03  |  10KB  |  418 lines

  1. /***************************************
  2.  *************   notes.c   *************
  3.  ***************************************/
  4.  
  5. #include <dos/dos.h>
  6. #include <exec/types.h>
  7. #include <intuition/intuition.h>
  8. #include <intuition/classes.h>
  9. #include <intuition/classusr.h>
  10. #include <intuition/imageclass.h>
  11. #include <intuition/gadgetclass.h>
  12. #include <libraries/gadtools.h>
  13. #include <graphics/displayinfo.h>
  14. #include <graphics/gfxbase.h>
  15. #include <graphics/text.h>
  16.  
  17. #include <clib/exec_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/gadtools_protos.h>
  20. #include <clib/graphics_protos.h>
  21. #include <clib/utility_protos.h>
  22. #include <clib/diskfont_protos.h>
  23. #include <clib/dos_protos.h>
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. #include "stickit.h"
  30.  
  31. #include "consts.h"
  32. #include "structs.h"
  33. #include "prototype.h"
  34.  
  35. extern prjptr prj;
  36.  
  37. struct EasyStruct reqnonotes =
  38.    {
  39.    sizeof(struct EasyStruct),
  40.    0,
  41.    "StickIt",
  42.    "No notes to display",
  43.    "Continue"
  44.    };
  45.  
  46. void shownotes()
  47.    {
  48.    struct IntuiMessage *nextmessage;
  49.  
  50.    nteptr currnote = NULL;
  51.    struct ndnode *ndptr;
  52.  
  53.    int done = FALSE;
  54.  
  55.    /***   Create message port   ***/
  56.  
  57.    prj->msgport = CreateMsgPort();
  58.  
  59.    if (!prj->msgport)
  60.       error("Can't create message port",ERR_MSGPORT);
  61.  
  62.    prj->no_notes_visable = 0;
  63.  
  64.    opennotes();
  65.  
  66.    if (prj->no_notes_visable == 0)
  67.       {
  68.       EasyRequest(NULL,&reqnonotes,NULL);
  69.  
  70.       /***   Remove message port   ***/
  71.  
  72.       DeleteMsgPort(prj->msgport);
  73.       prj->msgport = NULL;
  74.  
  75.       prj->editnotes = TRUE;
  76.       return;
  77.       }
  78.  
  79.    refreshnotes();
  80.  
  81.    /***   Answer messages   ***/
  82.  
  83.    while (!done)
  84.       {
  85.       Wait (1 << prj->msgport->mp_SigBit);
  86.           
  87.       while (nextmessage = (struct IntuiMessage *)GetMsg(prj->msgport))
  88.          {
  89.          prj->currmessage.Code = nextmessage->Code;
  90.          prj->currmessage.Class = nextmessage->Class;
  91.          prj->currmessage.IDCMPWindow = nextmessage->IDCMPWindow;
  92.          prj->currmessage.Seconds = nextmessage->Seconds;
  93.          prj->currmessage.Micros = nextmessage->Micros;
  94.  
  95.          ReplyMsg ((struct Message *)nextmessage);
  96.  
  97.          ndptr = findnodefromwin(prj->currmessage.IDCMPWindow);
  98.  
  99.          currnote = ndptr->data;
  100.  
  101.          switch(prj->currmessage.Class)
  102.             {
  103.             case IDCMP_MOUSEBUTTONS:
  104.                switch (prj->currmessage.Code)
  105.                   {
  106.                   case SELECTDOWN:
  107.                      if (DoubleClick(currnote->leftseconds,currnote->leftmicros,
  108.                         prj->currmessage.Seconds,prj->currmessage.Micros))
  109.                         {
  110.                         removenote(ndptr);
  111.  
  112.                         currnote->show = FALSE;
  113.                         }
  114.                      else
  115.                         {
  116.                         currnote->leftseconds = prj->currmessage.Seconds;
  117.                         currnote->leftmicros =  prj->currmessage.Micros;
  118.                         currnote->rightseconds = 0;
  119.                         currnote->rightmicros = 0;
  120.                         }
  121.                      break;
  122.                   case MENUUP:
  123.                      prj->editnotes = TRUE;
  124.                      done = TRUE;
  125.                      break;
  126.                   default:
  127.                      break;
  128.                   }
  129.                break;
  130.             case IDCMP_REFRESHWINDOW:
  131.                BeginRefresh(prj->currmessage.IDCMPWindow);
  132.                refreshnote(ndptr);
  133.                EndRefresh(prj->currmessage.IDCMPWindow,TRUE);
  134.                break;
  135.             case IDCMP_CHANGEWINDOW:
  136.                currnote->xpos = (int)prj->currmessage.IDCMPWindow->LeftEdge;
  137.                currnote->ypos = (int)prj->currmessage.IDCMPWindow->TopEdge;
  138.                break;
  139.             case IDCMP_VANILLAKEY:
  140.                switch (prj->currmessage.Code)
  141.                   {
  142.                   case 'F':
  143.                   case 'f':
  144.                      WindowToFront(prj->currmessage.IDCMPWindow);
  145.                      break;
  146.                   case 'B':
  147.                   case 'b':
  148.                      WindowToBack(prj->currmessage.IDCMPWindow);
  149.                      break;
  150.                   case 'E':
  151.                   case 'e':
  152.                      prj->editnotes = TRUE;
  153.                      done = TRUE;
  154.                      break;
  155.                   case 'H':
  156.                   case 'h':
  157.                      removenote(ndptr);
  158.                      currnote->show = FALSE;
  159.                      break;
  160.                   }
  161.                break;
  162.             default:
  163.                break;
  164.             }
  165.          }
  166.       if (prj->no_notes_visable == 0)
  167.          {
  168.          prj->editnotes = TRUE;
  169.          done = TRUE;
  170.          }
  171.       }
  172.  
  173.    /***   Remove note that we clicked on first   ***/
  174.  
  175.    removenote(ndptr);
  176.  
  177.    removenotes();
  178.  
  179.    /***   Remove message port   ***/
  180.  
  181.    DeleteMsgPort(prj->msgport);
  182.    prj->msgport = NULL;
  183.  
  184.    /***  Set current note to the last one we clicked on   ***/
  185.  
  186.    prj->currnode = ndptr;
  187.    }
  188.  
  189. void opennotes()
  190.    {
  191.    struct ndnode *ndptr;
  192.    nteptr currnote;
  193.  
  194.    /***   Open all windows, draw back to front   ***/
  195.  
  196.    ndptr = prj->notes_end->prev;
  197.  
  198.    while (ndptr->prev)
  199.       {
  200.       currnote = ndptr->data;
  201.  
  202.       if (currnote->show)
  203.          {
  204.          currnote->win = OpenWindowTags( NULL,
  205.             WA_Left,        currnote->xpos,
  206.             WA_Top,         currnote->ypos,
  207.             WA_Width,       prj->notewidth,
  208.             WA_Height,      prj->noteheight,
  209.             WA_IDCMP,       NULL,
  210.             WA_Flags,       WFLG_DRAGBAR|WFLG_DEPTHGADGET|
  211.                   WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP,
  212.             WA_Title,       currnote->title,
  213.             WA_ScreenTitle, "StickIt V1.02 2nd August '93 ©1993 Andy Dean",
  214.                   TAG_DONE );
  215.  
  216.          if (!currnote->win)
  217.             error("opennotes; Can't open note",ERR_FATAL);
  218.  
  219.          /***   Must connect window to message port   ***/
  220.  
  221.          currnote->win->UserPort = prj->msgport;
  222.          ModifyIDCMP(currnote->win,IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY|
  223.                         IDCMP_MOUSEBUTTONS|IDCMP_CHANGEWINDOW);
  224.  
  225.          /***   Set font to user's choice   ***/
  226.  
  227.          SetFont(currnote->win->RPort,prj->notefontptr);
  228.  
  229.          /***   Reset DoubleClick info   ***/
  230.  
  231.          currnote->leftseconds = currnote->leftmicros = 0;
  232.          currnote->rightseconds = currnote->rightmicros = 0;
  233.  
  234.          /***   Clear the note   ***/
  235.  
  236.          SetAPen(currnote->win->RPort,prj->backcolour);
  237.          RectFill(currnote->win->RPort,(LONG)currnote->win->BorderLeft,
  238.             (LONG)currnote->win->BorderTop,
  239.             currnote->win->Width - (currnote->win->BorderRight + 1),
  240.             currnote->win->Height - (currnote->win->BorderBottom + 1));
  241.          SetAPen(currnote->win->RPort,prj->textcolour);
  242.  
  243.          /***   Increment note count   ***/
  244.  
  245.          prj->no_notes_visable++;
  246.          }
  247.       else
  248.          currnote->win = NULL;
  249.  
  250.       ndptr = ndptr->prev;
  251.       }
  252.    }
  253.  
  254. void refreshnotes()
  255.    {
  256.    struct ndnode *ndptr;
  257.    nteptr currnote;
  258.  
  259.    /***   Refresh the text in each window   ***/
  260.  
  261.    ndptr = prj->notes_start->next;
  262.           
  263.    while (ndptr->next)
  264.       {
  265.       currnote = ndptr->data;
  266.  
  267.       refreshnote(ndptr);
  268.  
  269.       ndptr = ndptr->next;
  270.       }
  271.    }
  272.  
  273. void removenotes()
  274.    {
  275.    struct ndnode *ndptr;
  276.    nteptr currnote;
  277.  
  278.    /***   Remove all open notes and remove shared message port   ***/
  279.    /***   and close font                                         ***/
  280.  
  281.    ndptr = prj->notes_start->next;
  282.  
  283.    while (ndptr->next)
  284.       {
  285.       currnote = ndptr->data;
  286.  
  287.       removenote(ndptr);
  288.  
  289.       ndptr = ndptr->next;
  290.       }
  291.    }
  292.  
  293. void removenote(struct ndnode *ndptr)
  294.    {
  295.    nteptr currnote;
  296.  
  297.    currnote = ndptr->data;
  298.  
  299.    if (currnote->win)
  300.       {
  301.       CloseWindowSafely(currnote->win);
  302.       currnote->win = NULL;
  303.  
  304.       prj->no_notes_visable--;
  305.       }
  306.    }
  307.  
  308. void refreshnote(struct ndnode *ndptr)
  309.    {
  310.    struct Window *w;
  311.    struct TextFont *myfont;
  312.    struct RastPort *myrp;
  313.    struct TextExtent resultextent;
  314.  
  315.    nteptr currnote;
  316.  
  317.    LONG fit,count = 0,printable,l;
  318.    UWORD wtbarheight;
  319.  
  320.    currnote = ndptr->data;
  321.  
  322.    if (!currnote->win)   /* Old refresh message */
  323.       return;
  324.  
  325.    if (!currnote->show)
  326.       return;
  327.  
  328.    /***   Clear the note   ***/
  329.  
  330.    SetAPen(currnote->win->RPort,prj->backcolour);
  331.    RectFill(currnote->win->RPort,(LONG)currnote->win->BorderLeft,
  332.       (LONG)currnote->win->BorderTop,
  333.       currnote->win->Width - (currnote->win->BorderRight + 1),
  334.       currnote->win->Height - (currnote->win->BorderBottom + 1));
  335.    SetAPen(currnote->win->RPort,prj->textcolour);
  336.  
  337.    /***   Blatently nick Commodore's text routines !   ***/
  338.  
  339.    w = currnote->win;
  340.    myfont = prj->notefontptr;
  341.    myrp = currnote->win->RPort;
  342.  
  343.    wtbarheight = w->WScreen->BarHeight + myfont->tf_Baseline + 2;
  344.  
  345.    SetDrMd(myrp,JAM1);
  346.    SetBPen(myrp,prj->backcolour);
  347.  
  348.    Move(myrp,w->WScreen->WBorLeft,wtbarheight);
  349.  
  350.    /***   Emulate the Commodore code   ***/
  351.  
  352.    printable = strlen(currnote->note);
  353.  
  354.    while (count < printable)
  355.       {
  356.       fit = TextFit(myrp,&(currnote->note[count]),
  357.          (printable - count), &resultextent,
  358.          NULL, 1, 
  359.          (w->Width - (myrp->cp_x + w->BorderLeft +
  360.          w->BorderRight)), myfont->tf_YSize + 1);
  361.  
  362.       if (fit == 0)
  363.          Move (myrp,w->BorderLeft,myrp->cp_y +
  364.             myfont->tf_YSize + 1);
  365.       else
  366.          {
  367.          if ((fit + count) != printable)   /* Need to wrap */
  368.             {
  369.             for (l = count + fit; ((l > count) &&
  370.                (currnote->note[l] != ' ')); l--);
  371.  
  372.             if ((l - count) != 0) /* If can't wrap */
  373.                fit = l - count + 1;
  374.             }
  375.  
  376.          Text(myrp,&(currnote->note[count]),fit);
  377.          count += fit;
  378.  
  379.          /***  Strip space from start of line   ***/
  380.  
  381.          while ((count <= printable) &&
  382.             (currnote->note[count] == ' '))
  383.             count++;
  384.  
  385.          /***  Newline   ***/
  386.  
  387.          Move (myrp,w->BorderLeft,myrp->cp_y +
  388.             myfont->tf_YSize + 1);
  389.          }
  390.  
  391.       if (myrp->cp_y > (w->Height - (w->BorderBottom + 2)))
  392.          return;   /* Reached bottom */
  393.       }
  394.    }
  395.  
  396.  
  397. struct ndnode *findnodefromwin(struct Window *win)
  398.    {
  399.    struct ndnode *ndptr;
  400.    nteptr currnote;
  401.  
  402.    ndptr = prj->notes_start->next;
  403.           
  404.    while (ndptr->next)
  405.       {
  406.       currnote = ndptr->data;
  407.  
  408.       if (currnote->win == win)
  409.          return(ndptr);
  410.  
  411.       ndptr = ndptr->next;
  412.       }
  413.  
  414.    return (NULL);
  415.    }
  416.  
  417.  
  418.