DLG_Find.c
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:6k
源码类别:

VC书籍

开发平台:

Visual C++

  1. ////////////////////////////////////////////////////////////////////////////////
  2. #include "stdafx.h"
  3. #include "globals.h"
  4. #include "DLG_Find.h"
  5. #include "CPI_Playlist.h"
  6. #include "CPI_PlaylistItem.h"
  7. ////////////////////////////////////////////////////////////////////////////////
  8. //
  9. void Search_SelectItems();
  10. //
  11. ////////////////////////////////////////////////////////////////////////////////
  12. ////////////////////////////////////////////////////////////////////////////////
  13. //
  14. //
  15. //
  16. BOOL CALLBACK wp_FindDialog(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
  17. {
  18.     switch(uMessage)
  19.     {
  20.     case WM_INITDIALOG:
  21.         // Setup type initial state
  22.         {
  23.             UINT uiDlgItemToSet;
  24.             if(options.m_enQuickFindTerm == qftTitle)
  25.                 uiDlgItemToSet = IDC_QFND_TITLES;
  26.             else if(options.m_enQuickFindTerm == qftArtist)
  27.                 uiDlgItemToSet = IDC_QFND_ARTISTS;
  28.             else
  29.                 uiDlgItemToSet = IDC_QFND_ALBUMS;
  30.             SendDlgItemMessage(hWnd, uiDlgItemToSet, BM_SETCHECK, (WPARAM)BST_CHECKED, 0L);
  31.         }
  32.         // Setup position
  33.         if(globals.m_bQuickFindWindowPos_Valid == TRUE)
  34.         {
  35.             SetWindowPos(hWnd, NULL,
  36.                          globals.m_ptQuickFindWindowPos.x, globals.m_ptQuickFindWindowPos.y,
  37.                          0, 0,
  38.                          SWP_NOSIZE | SWP_NOZORDER);
  39.         }
  40.         return TRUE;
  41.     case WM_CLOSE:
  42.         DestroyWindow(hWnd);
  43.         windows.m_hWndFindDialog = NULL;
  44.         SetFocus(CLV_GetHWND(globals.m_hPlaylistViewControl));
  45.         break;
  46.     case WM_COMMAND:
  47.         switch(LOWORD(wParam))
  48.         {
  49.         case IDOK:
  50.         case IDCANCEL:
  51.             SendMessage(hWnd, WM_CLOSE, 0L, 0L);
  52.             break;
  53.         case IDC_FND_TEXT:
  54.             if(HIWORD(wParam) == EN_CHANGE)
  55.                 Search_SelectItems();
  56.             break;
  57.         case IDC_QFND_TITLES:
  58.         case IDC_QFND_ARTISTS:
  59.         case IDC_QFND_ALBUMS:
  60.             if(HIWORD(wParam) == BN_CLICKED)
  61.             {
  62.                 // Set the search defaults
  63.                 if(LOWORD(wParam) == IDC_QFND_TITLES)
  64.                     options.m_enQuickFindTerm = qftTitle;
  65.                 else if(LOWORD(wParam) == IDC_QFND_ARTISTS)
  66.                     options.m_enQuickFindTerm = qftArtist;
  67.                 else
  68.                     options.m_enQuickFindTerm = qftAlbum;
  69.                 // Research
  70.                 Search_SelectItems();
  71.             }
  72.             break;
  73.         }
  74.         break;
  75.     case WM_WINDOWPOSCHANGED:
  76.         // Store the window pos for next time
  77.         {
  78.             const WINDOWPOS* pWinPos = (const WINDOWPOS*)lParam;
  79.             globals.m_bQuickFindWindowPos_Valid = TRUE;
  80.             globals.m_ptQuickFindWindowPos.x = pWinPos->x;
  81.             globals.m_ptQuickFindWindowPos.y = pWinPos->y;
  82.         }
  83.         break;
  84.     }
  85.     return FALSE;
  86. }
  87. //
  88. //
  89. //
  90. char* CP_stristr(const char *pcString1, const char *pcString2)
  91. {
  92.     char *pCompareStart = (char *)pcString1;
  93.     char *pCursor_S1, *pCursor_S2;
  94.     char cSrc, cDst;
  95.     // If there is a null source string - this is a "no match"
  96.     if(!pcString1)
  97.         return NULL;
  98.     // Null length string 2 - this is a "no match"
  99.     if(!*pcString2)
  100.         return NULL;
  101.     // Search from every start pos in the source string
  102.     while(*pCompareStart)
  103.     {
  104.         pCursor_S1 = pCompareStart;
  105.         pCursor_S2 = (char *)pcString2;
  106.         // Scan both string
  107.         while(*pCursor_S1 && *pCursor_S2)
  108.         {
  109.             cSrc = *pCursor_S1;
  110.             cDst = *pCursor_S2;
  111.             // Correct case
  112.             if( (cSrc >= 'A') && (cSrc <= 'Z') )
  113.                 cSrc -= ('A' - 'a');
  114.             if( (cDst >= 'A') && (cDst <= 'Z') )
  115.                 cDst -= ('A' - 'a');
  116.             if(cSrc != cDst)
  117.                 break;
  118.             pCursor_S1++;
  119.             pCursor_S2++;
  120.         }
  121.         // If string 2 is exhausted - there is a match
  122.         if(!*pCursor_S2)
  123.             return(pCompareStart);
  124.         // Offset source and continue
  125.         pCompareStart++;
  126.     }
  127.     return NULL;
  128. }
  129. //
  130. //
  131. //
  132. void Search_SelectItems()
  133. {
  134.     CP_HPLAYLISTITEM hCursor;
  135.     BOOL bFirstMatch = TRUE;
  136.     char* pcTerm;
  137.     unsigned int iEditTextLength;
  138.     BOOL bTitles, bAlbums, bArtists;
  139.     // Get search term
  140.     iEditTextLength = SendDlgItemMessage(windows.m_hWndFindDialog, IDC_FND_TEXT, WM_GETTEXTLENGTH, 0L, 0L);
  141.     pcTerm = (char*)malloc(iEditTextLength + 1);
  142.     SendDlgItemMessage(windows.m_hWndFindDialog, IDC_FND_TEXT, WM_GETTEXT, (WPARAM)(iEditTextLength + 1), (LPARAM)pcTerm);
  143.     // Get search filter
  144.     bTitles = SendDlgItemMessage(windows.m_hWndFindDialog, IDC_QFND_TITLES, BM_GETCHECK, 0L, 0L) == BST_CHECKED ? TRUE : FALSE;
  145.     bArtists = SendDlgItemMessage(windows.m_hWndFindDialog, IDC_QFND_ARTISTS, BM_GETCHECK, 0L, 0L) == BST_CHECKED ? TRUE : FALSE;
  146.     bAlbums = SendDlgItemMessage(windows.m_hWndFindDialog, IDC_QFND_ALBUMS, BM_GETCHECK, 0L, 0L) == BST_CHECKED ? TRUE : FALSE;
  147.     // Perform select/search
  148.     for(hCursor = CPL_GetFirstItem(globals.m_hPlaylist); hCursor; hCursor = CPLI_Next(hCursor))
  149.     {
  150.         const int iItemIDX = CPLI_GetCookie(hCursor);
  151.         const char* pcMatchTerm;
  152.         BOOL bSelect = FALSE;
  153.         // Perform searches
  154.         if(bTitles)
  155.             pcMatchTerm = CPLI_GetTrackName(hCursor);
  156.         else if(bArtists)
  157.             pcMatchTerm = CPLI_GetArtist(hCursor);
  158.         else
  159.             pcMatchTerm = CPLI_GetAlbum(hCursor);
  160.         if( CP_stristr(pcMatchTerm, pcTerm) )
  161.             bSelect = TRUE;
  162.         // If this is the first match - focus (and scroll on) the item
  163.         if(bSelect && bFirstMatch == TRUE)
  164.         {
  165.             bFirstMatch = FALSE;
  166.             CLV_SetFocusItem(globals.m_hPlaylistViewControl, iItemIDX);
  167.             CLV_EnsureVisible(globals.m_hPlaylistViewControl, iItemIDX);
  168.         }
  169.         // Update selected state
  170.         CLV_SetItemSelected(globals.m_hPlaylistViewControl, iItemIDX, bSelect);
  171.     }
  172.     // Cleanup
  173.     free(pcTerm);
  174. }
  175. //
  176. //
  177. //