home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / monitors / rsys / rsyssrc.lha / RSysSearch.c < prev    next >
C/C++ Source or Header  |  1993-09-25  |  9KB  |  404 lines

  1. /*
  2. ***************************************************************************
  3. *
  4. * Datei:
  5. *    RSysSearch.c
  6. *
  7. * Inhalt:
  8. *
  9. *      --- Globale Routinen ---
  10. *
  11. *    void RSysFindNext ( void );
  12. *    void RSysFindPrev ( void );
  13. *    void RSysSearch ( void );
  14. *
  15. *      --- Lokale  Routinen ---
  16. *
  17. *    static int HandleSearchStrIDCMP ( void );
  18. *    static int OpenSearchStrWindow ( void );
  19. *    static int SearchCancelGadClicked ( void );
  20. *    static int SearchOkGadClicked ( void );
  21. *    static int SearchSGadClicked ( void );
  22. *    static int SearchStrCloseWindow ( void );
  23. *    static int SearchStrVanillaKey ( void );
  24. *    static void EnterSearchString ( void );
  25. *    static void InfoMsg ( char *fmt );
  26. *    static void StrFound ( struct Node *node );
  27. *    static void StrNotFound ( int num );
  28. *
  29. * Bemerkungen:
  30. *    Enthält die Routinen zum Suchen nach einem Teilstring
  31. *    in der Liste des Hauptfensters.
  32. *
  33. * Erstellungsdatum:
  34. *    25-Sep-93    Rolf Böhme
  35. *
  36. * Änderungen:
  37. *    25-Sep-93    Rolf Böhme    Erstellung
  38. *
  39. ***************************************************************************
  40. */
  41.  
  42. #include "RSys.h"
  43.  
  44. extern int SearchSGadClicked( void );
  45. extern int SearchOkGadClicked( void );
  46. extern int SearchCancelGadClicked( void );
  47.  
  48. extern int HandleSearchStrIDCMP( void );
  49. extern int SearchStrCloseWindow();
  50. extern int SearchStrVanillaKey();
  51. extern int OpenSearchStrWindow( void );
  52. extern void CloseSearchStrWindow( void );
  53. extern void EnterSearchString(void);
  54.  
  55. struct Node *actualfindnode;
  56. long actualfindnodenum = 0;
  57. static char searchstr[BUFSIZE];
  58. static startsearch = FALSE;
  59.  
  60. static struct Window         *SearchStrWnd = NULL;
  61. static struct Gadget         *SearchStrGList = NULL;
  62. struct IntuiMessage    SearchStrMsg;
  63. static struct Gadget         *SearchStrGadgets[SearchStr_CNT];
  64. static UWORD                  SearchStrLeft = 81;
  65. static UWORD                  SearchStrTop = 66;
  66. static UWORD                  SearchStrWidth = 259;
  67. static UWORD                  SearchStrHeight = 57;
  68. static UBYTE                 *SearchStrWdt = (UBYTE *)NAME " - Search string";
  69.  
  70. static UWORD SearchStrGTypes[] = {
  71.     STRING_KIND,
  72.     BUTTON_KIND,
  73.     BUTTON_KIND
  74. };
  75.  
  76. static struct NewGadget SearchStrNGad[] = {
  77.     22, 15, 208, 13, (UBYTE *)"Search String", NULL, GD_SearchSGad, PLACETEXT_ABOVE, NULL, (APTR)SearchSGadClicked,
  78.     22, 34, 91, 13, (UBYTE *)"_Search", NULL, GD_SearchOkGad, PLACETEXT_IN, NULL, (APTR)SearchOkGadClicked,
  79.     139, 34, 91, 13, (UBYTE *)"Cancel", NULL, GD_SearchCancelGad, PLACETEXT_IN, NULL, (APTR)SearchCancelGadClicked
  80. };
  81.  
  82. static ULONG *SearchStrGTags[] = {
  83.     (ULONG *)(GTST_MaxChars), (ULONG *)40, (ULONG *)(TAG_DONE),
  84.     (ULONG *)(GT_Underscore), (ULONG *)'_', (ULONG *)(TAG_DONE),
  85.     (ULONG *)(TAG_DONE)
  86. };
  87.  
  88. static int SearchSGadClicked( void )
  89. {
  90.    char *gadtext;
  91.  
  92.     /* routine when gadget "Search String" is clicked. */
  93.    gadtext = gadgetbuff(SearchStrGadgets[GD_SearchSGad - GD_SearchSGad]);
  94.  
  95.    if(strlen(gadtext) != 0)
  96.    {
  97.       strncpy(searchstr, gadtext, BUFSIZE);
  98.       startsearch = TRUE;
  99.  
  100.       return TRUE;
  101.    }
  102.  
  103.    searchstr[0] = STRINGEND;
  104.  
  105.    startsearch = FALSE;
  106.  
  107.    return TRUE;
  108. }
  109.  
  110. static int SearchOkGadClicked( void )
  111. {
  112.     /* routine when gadget "_Search" is clicked. */
  113.    startsearch = TRUE;
  114.  
  115.    return FALSE;
  116. }
  117.  
  118. static int SearchCancelGadClicked( void )
  119. {
  120.     /* routine when gadget "Cancel" is clicked. */
  121.    startsearch = FALSE;
  122.  
  123.    return FALSE;
  124. }
  125.  
  126. static int SearchStrCloseWindow( void )
  127. {
  128.     /* routine for "IDCMP_CLOSEWINDOW". */
  129.    startsearch = FALSE;
  130.  
  131.    return FALSE;
  132. }
  133.  
  134. static int SearchStrVanillaKey( void )
  135. {
  136.     /* routine for "IDCMP_VANILLAKEY". */
  137.  
  138.    if(SearchStrMsg.Code == ESC)
  139.    {
  140.       startsearch = FALSE;
  141.  
  142.       return FALSE;
  143.    }
  144.  
  145.    if(ToUpper((ULONG)SearchStrMsg.Code) == (UBYTE)'S')
  146.    {
  147.       startsearch = TRUE;
  148.  
  149.       return FALSE;
  150.    }
  151.  
  152.    return TRUE;
  153. }
  154.  
  155. static int HandleSearchStrIDCMP( void )
  156. {
  157.     struct IntuiMessage    *m;
  158.     int            (*func)(void);
  159.     BOOL            running = TRUE;
  160.  
  161.     while( m = GT_GetIMsg( SearchStrWnd->UserPort ))
  162.    {
  163.         CopyMem(( char * )m, ( char * )&SearchStrMsg, (long)sizeof( struct IntuiMessage ));
  164.  
  165.         GT_ReplyIMsg( m );
  166.  
  167.         switch ( SearchStrMsg.Class )
  168.       {
  169.             case    IDCMP_REFRESHWINDOW:
  170.             MakeWindowRefresh(SearchStrWnd);
  171.                 break;
  172.  
  173.             case    IDCMP_CLOSEWINDOW:
  174.                 running = SearchStrCloseWindow();
  175.                 break;
  176.  
  177.             case    IDCMP_VANILLAKEY:
  178.                 running = SearchStrVanillaKey();
  179.                 break;
  180.  
  181.             case    IDCMP_GADGETUP:
  182.             HandleHelp((( struct Gadget * )SearchStrMsg.IAddress)->GadgetID);
  183.  
  184.                 func = ( void * )(( struct Gadget * )SearchStrMsg.IAddress )->UserData;
  185.                 running = func();
  186.                 break;
  187.         }
  188.     }
  189.  
  190.     return( running );
  191. }
  192.  
  193. static int OpenSearchStrWindow( void )
  194. {
  195.     struct NewGadget    ng;
  196.     struct Gadget    *g;
  197.     UWORD        lc, tc;
  198.    UWORD wleft = SearchStrLeft,
  199.          wtop = SearchStrTop,
  200.          ww,
  201.          wh;
  202.    int   gl[]={GD_SearchSGad - GD_SearchSGad};
  203.  
  204.    AdjustWindowDimensions(Scr, SearchStrLeft, SearchStrTop,
  205.                           SearchStrWidth, SearchStrHeight,
  206.                           &wleft, &wtop, &ww, &wh);
  207.  
  208.     if ( ! ( g = CreateContext( &SearchStrGList ))) return 1L;
  209.  
  210.     for( lc = 0, tc = 0; lc < SearchStr_CNT; lc++ )
  211.    {
  212.  
  213.         CopyMem((char * )&SearchStrNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));
  214.  
  215.         ng.ng_VisualInfo = VisualInfo;
  216.         ng.ng_TextAttr   = Font;
  217.       ng.ng_LeftEdge = OffX + ComputeX(ng.ng_LeftEdge);
  218.       ng.ng_TopEdge = OffY + ComputeY(ng.ng_TopEdge);
  219.       ng.ng_Width = ComputeX(ng.ng_Width);
  220.       ng.ng_Height = ComputeY(ng.ng_Height);
  221.  
  222.         SearchStrGadgets[ lc ] = g = CreateGadgetA((ULONG)SearchStrGTypes[ lc ], g, &ng, ( struct TagItem * )&SearchStrGTags[ tc ] );
  223.  
  224.         while( SearchStrGTags[ tc ] ) tc += 2;
  225.         tc++;
  226.  
  227.         if ( NOT g ) return 2L;
  228.     }
  229.  
  230.     if ( ! ( SearchStrWnd = OpenWindowTags( NULL,
  231.             WA_Left, wleft,
  232.             WA_Top, wtop,
  233.             WA_Width, ww,
  234.             WA_Height, wh,
  235.                 WA_IDCMP,    STRINGIDCMP|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
  236.                 WA_Flags,    WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE|WFLG_RMBTRAP,
  237.                 WA_Title,    SearchStrWdt,
  238.             WA_PubScreenFallBack, TRUE,
  239.             WA_PubScreen, Scr,
  240.                 TAG_DONE )))
  241.     return 4L;
  242.  
  243.    RefreshRastPort(SearchStrWnd, SearchStrGadgets, gl, 1, FALSE, SearchStrGList);
  244.  
  245.     return NULL;
  246. }
  247.  
  248. static void
  249. InfoMsg(char *fmt)
  250. {
  251.    char msg[BUFSIZE];
  252.  
  253.    sprintf(msg, fmt, searchstr);
  254.    PrintInfo(msg, SPEAK, 30);
  255.  
  256.    return;
  257. }
  258.  
  259. static void
  260. StrNotFound(int num)
  261. {
  262.    actualfindnode = GetNode(&ListeLVList, (ULONG)num);
  263.    actualfindnodenum = num;
  264.  
  265.    topentry = actualfindnodenum;
  266.    SetMainLVTop(actualfindnodenum);
  267.  
  268.    InfoMsg("\"%s\" not found...");
  269.  
  270.    PrintStatistics();
  271.  
  272.    return;
  273. }
  274.  
  275. static void
  276. StrFound(struct Node *node)
  277. {
  278.    actualfindnode = node;
  279.  
  280.    topentry = actualfindnodenum;
  281.    SetMainLVTop(topentry);
  282.  
  283.    InfoMsg("\"%s\" found...");
  284.  
  285.    PrintStatistics();
  286.  
  287.    return;
  288. }
  289.  
  290. void
  291. RSysFindNext(void)
  292. {
  293.    struct Node *node;
  294.    int lastnum = actualfindnodenum;
  295.  
  296.    HandleHelp(MN_FindNext);
  297.  
  298.    if(actualfindnodenum >= (countentries - newlvh)) return;
  299.  
  300.    if(strlen(searchstr) == 0)
  301.    {
  302.       EnterSearchString();
  303.  
  304.       if(!startsearch) return;
  305.    }
  306.  
  307.    InfoMsg("Find next \"%s\"...");
  308.  
  309.    for(node = actualfindnode->ln_Succ, actualfindnodenum++;
  310.        node->ln_Succ; node = node->ln_Succ, actualfindnodenum++)
  311.       if(strstr(node->ln_Name, searchstr))
  312.       {
  313.          StrFound(node);
  314.  
  315.          return;
  316.       }
  317.  
  318.    StrNotFound(lastnum);
  319.  
  320.    return;
  321. }
  322.  
  323. void
  324. RSysFindPrev(void)
  325. {
  326.    struct Node *node;
  327.    int lastnum = actualfindnodenum;
  328.  
  329.    HandleHelp(MN_FindPrev);
  330.  
  331.    if(actualfindnodenum <= 0) return;
  332.  
  333.    if(strlen(searchstr) == 0)
  334.    {
  335.       EnterSearchString();
  336.  
  337.       if(!startsearch) return;
  338.    }
  339.  
  340.    InfoMsg("Find prev: \"%s\"...");
  341.  
  342.    actualfindnodenum--;
  343.  
  344.    for(node = actualfindnode->ln_Pred; node->ln_Pred; node = node->ln_Pred, actualfindnodenum--)
  345.       if(strstr(node->ln_Name, searchstr))
  346.       {
  347.          StrFound(node);
  348.          return;
  349.       }
  350.  
  351.    StrNotFound(lastnum);
  352.  
  353.    return;
  354. }
  355.  
  356. static void
  357. EnterSearchString(void)
  358. {
  359.    PrintInfo("Search string...", SPEAK, 0);
  360.  
  361.    if (OpenASysWindow(OpenSearchStrWindow, NO_KILL))
  362.    {
  363.       LockMainWindow(LOCK);
  364.  
  365.        GT_SetGadgetAttrs(SearchStrGadgets[GD_SearchSGad - GD_SearchSGad], SearchStrWnd,
  366.                                NULL,
  367.                                GTST_String, (UBYTE *) searchstr,
  368.                                TAG_DONE);
  369.  
  370.       ActivateGadget(SearchStrGadgets[GD_SearchSGad - GD_SearchSGad], SearchStrWnd, NULL);
  371.  
  372.       while (HandleSearchStrIDCMP()) ;
  373.  
  374.       CloseASysWindow(&SearchStrWnd, &SearchStrGList, NULL);
  375.  
  376.       LockMainWindow(UNLOCK);
  377.    }
  378.  
  379.    return;
  380. }
  381.  
  382. void
  383. RSysSearch(void)
  384. {
  385.    HandleHelp(MN_Search);
  386.  
  387.    EnterSearchString();
  388.  
  389.    if(startsearch && strlen(searchstr) == 0L) startsearch = FALSE;
  390.  
  391.    if(startsearch)
  392.    {
  393.       startsearch = FALSE;
  394.       actualfindnode = ListeLVList.lh_Head;
  395.       actualfindnodenum = 0;
  396.  
  397.       RSysFindNext();
  398.    }
  399.  
  400.    PrintStatistics();
  401.  
  402.    return;
  403. }
  404.