home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / Chip_2002-02_cd1.bin / sharewar / apaths / APSOURCE.ZIP / PathTab.c < prev    next >
C/C++ Source or Header  |  2001-03-26  |  11KB  |  420 lines

  1. /* PathTab - March 26th, 2001
  2. **
  3. **      Copyright (c) 1997-2001 by Gregory Braun. All rights reserved.
  4. **
  5. **      This routine displays a list of the AppPaths found in the
  6. **      MS Windows 95/NT System Registry.
  7. **
  8. **      Private (static) routines are included to initialize the
  9. **      property page sheet when first invoked as well as handle
  10. **      [Create], [Modify] and [Remove] user requests.
  11. **
  12. **      Called:     dlg = handle to the dialog window.
  13. **                  msg = the MS Windows WM_xxxxxx message to be handled.
  14. **                  wp  = the WPARAM specific to the message being sent
  15. **                  lp  = the LPARAM specific to the message being sent
  16. **
  17. **      Returns:    TRUE upon success, or FALSE if the user pressed [Cancel],
  18. **                  or an internal error exists.
  19. */
  20.  
  21. #include "AppPaths.h"
  22.  
  23. #define KILL_QUERY          "Program:\t%s\r" \
  24.                             "Location:\t%s\r" \
  25.                             "Path:\t%s\r\r" \
  26.                             "There is no UNDO available for the Remove command.\r" \
  27.                             "Are you sure you want to delete this registery entry?\r"
  28.  
  29. #define CREATE_FAILURE      "The specified MS Windows System Registry\r" \
  30.                             "AppPath key could not be created."
  31.  
  32. #define MODIFY_FAILURE      "The selected MS Windows System Registry\r" \
  33.                             "AppPath key could not be modified."
  34.  
  35. #define KILL_FAILURE        "The selected MS Windows System Registry\r" \
  36.                             "AppPath key could not be removed."
  37.  
  38. #define MEMORY_ERROR        "Memory Error\r\r" \
  39.                             "Windows can not allocate a temporary memory\r" \
  40.                             "buffer for the requested sorting operations."
  41.  
  42.  
  43. static int                  sort = COLUMN_1;
  44.  
  45. static BOOL                 initiate        (HWND dlg);
  46. static BOOL                 selection       (HWND dlg,WPARAM wp,LPARAM lp);
  47. static BOOL                 verify          (HWND dlg);
  48.  
  49. static BOOL                 create          (HWND dlg);
  50. static BOOL                 modify          (HWND dlg);
  51. static BOOL                 kill            (HWND dlg);
  52.  
  53. static BOOL                 reflect         (HWND dlg,BOOL hit);
  54.  
  55. static BOOL                 order           (HWND list,int column);
  56.  
  57. static int                  by_name         (const void * k1,const void * k2);
  58. static int                  by_application  (const void * k1,const void * k2);
  59. static int                  by_path         (const void * k1,const void * k2);
  60.  
  61. static int                  cmp_name        (LPCSTR n1,LPCSTR n2);
  62. static int                  cmp_application (LPCSTR a1,LPCSTR a2);
  63. static int                  cmp_path        (LPCSTR p1,LPCSTR p2);
  64.  
  65.  
  66. extern UINT CALLBACK PathTab (HWND dlg,UINT msg,WPARAM wp,LPARAM lp)
  67. {
  68.     auto int                item    = LOWORD (wp);
  69.  
  70.     auto LPNMHDR            hdr     = (LPNMHDR) lp;
  71.     auto NM_LISTVIEW *      cmd     = (NM_LISTVIEW *) lp;
  72.  
  73.     auto HWND               list;
  74.     auto BOOL               good;
  75.  
  76.     switch (msg) {
  77.  
  78.         case WM_COMMAND :
  79.  
  80.              switch (item) {
  81.  
  82.                  case PATH_CREATE_BTN :
  83.                       create (dlg);
  84.                       break;
  85.  
  86.                  case PATH_MODIFY_BTN :
  87.                       modify (dlg);
  88.                       break;
  89.  
  90.                  case PATH_REMOVE_BTN :
  91.                       kill (dlg);
  92.                       break;
  93.  
  94.                  default :
  95.                       break;
  96.                       }
  97.  
  98.              break;
  99.  
  100.         case WM_HELP :
  101.              HelpTip (dlg,((LPHELPINFO) lp)->iCtrlId);
  102.              break;
  103.  
  104.         case WM_NOTIFY :
  105.  
  106.              switch (((LPNMHDR) lp)->code) {
  107.  
  108.                 case LVN_COLUMNCLICK :
  109.                      list = hdr->hwndFrom;
  110.                      sort = cmd->iSubItem;
  111.                      order (list,sort);
  112.                      break;
  113.  
  114.                 case PSN_SETACTIVE :
  115.                      initiate (dlg);
  116.                      break;
  117.  
  118.                 case PSN_KILLACTIVE :
  119.                      good = verify (dlg);
  120.                      SetWindowLong (dlg,DWL_MSGRESULT,!good);
  121.                      return ((good) ? FALSE : TRUE);
  122.                      break;
  123.  
  124.                 case PSN_APPLY :
  125.                      good = verify (dlg);
  126.                      SetWindowLong (dlg,DWL_MSGRESULT,good);
  127.                      return ((good) ? PSNRET_NOERROR : PSNRET_INVALID_NOCHANGEPAGE);
  128.                      break;
  129.  
  130.                 case PSN_HELP :
  131.                      HelpTopic (dlg,PATH_TAB);
  132.                      break;
  133.  
  134.                 default :
  135.                      selection (dlg,wp,lp);
  136.                      break;
  137.                      }
  138.  
  139.              break;
  140.  
  141.         break;
  142.         }
  143.  
  144.      return (FALSE);
  145. }
  146.  
  147. static BOOL initiate (HWND dlg)
  148. {
  149.     static BOOL     created = FALSE;
  150.  
  151.     auto LPSTR      label[] = { "Application", "Folder Location", "Path" };
  152.     auto int        width[] = { 115,150,150 };
  153.  
  154.     auto HWND       ctl     = GetDlgItem (dlg,PATH_MEMBER_LST);
  155.  
  156.     auto LV_COLUMN  col     = { NIL };
  157.  
  158.     auto int        inx;
  159.  
  160.     if (!created) {
  161.  
  162.         col.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  163.         col.fmt  = LVCFMT_LEFT;
  164.  
  165.         for (inx = NIL; inx < PATH_COLS_MAX; inx++) {
  166.             col.iSubItem = inx;
  167.             col.pszText = label[inx];
  168.             col.cx      = width[inx];
  169.             ListView_InsertColumn (ctl,inx,&col);
  170.             }
  171.  
  172.         created = TRUE;
  173.         }
  174.  
  175.     GetPKeys (ctl);
  176.  
  177.     order (ctl,sort);
  178.     reflect (dlg,TRUE);
  179.  
  180.     return (TRUE);
  181. }
  182.  
  183. static BOOL selection (HWND dlg,WPARAM wp,LPARAM lp)
  184. {
  185.     auto int            item    = (int) wp;
  186.  
  187.     auto LPNMHDR        hdr     = (LPNMHDR) lp;
  188.     auto UINT           code    = hdr->code;
  189.     auto HWND           list    = hdr->hwndFrom;
  190.  
  191.     auto int            hit;
  192.  
  193.     if (item != PATH_MEMBER_LST)
  194.         return (FALSE);
  195.  
  196.     switch (code) {
  197.  
  198.         case NM_CLICK :
  199.         case NM_DBLCLK :
  200.              if ((hit = LV_RowHit (list)) >= NIL) {
  201.                  reflect (dlg,TRUE);
  202.                  if (code == NM_DBLCLK)
  203.                      modify (dlg);
  204.                  }
  205.              else reflect (dlg,FALSE);
  206.              break;
  207.  
  208.         default :
  209.              break;
  210.              }
  211.  
  212.     return (TRUE);
  213. }
  214.  
  215. static BOOL verify (HWND dlg)
  216. {
  217.     UNUSED_ARG (dlg);
  218.  
  219.     return (TRUE);
  220. }
  221.  
  222. static BOOL create (HWND dlg)
  223. {
  224.     auto PATH   path    = { NIL };
  225.  
  226.     auto HWND   list    = GetDlgItem (dlg,PATH_MEMBER_LST);
  227.  
  228.     if (!EditPath (dlg,&path))
  229.         return (FALSE);
  230.  
  231.     if (!MakePKey (&path))
  232.         return (Error (dlg,NULL,CREATE_FAILURE));
  233.  
  234.     PL_AddItem (list,&path);
  235.  
  236.     return (TRUE);
  237. }
  238.  
  239. static BOOL modify (HWND dlg)
  240. {
  241.     auto PATH   last    = { NIL };
  242.     auto PATH   path    = { NIL };
  243.  
  244.     auto HWND   list    = GetDlgItem (dlg,PATH_MEMBER_LST);
  245.     auto int    index   = LV_GetCurSel (list);
  246.  
  247.     ListView_EnsureVisible (list,index,FALSE);
  248.  
  249.     PL_GetItem (list,index,&path);
  250.     last = path;
  251.  
  252.     if (!EditPath (dlg,&path))
  253.         return (FALSE);
  254.  
  255.     if (!KillPKey (&last))
  256.         return (Error (dlg,NULL,KILL_FAILURE));
  257.  
  258.     if (!MakePKey (&path))
  259.         return (Error (dlg,NULL,MODIFY_FAILURE));
  260.  
  261.     PL_PutItem (list,index,&path);
  262.  
  263.     return (TRUE);
  264. }
  265.  
  266. static BOOL kill (HWND dlg)
  267. {
  268.     auto PATH   path    = { NIL };
  269.  
  270.     auto HWND   list    = GetDlgItem (dlg,PATH_MEMBER_LST);
  271.     auto int    index   = LV_GetCurSel (list);
  272.  
  273.     auto char   msg[MSTRING];
  274.  
  275.     ListView_EnsureVisible (list,index,FALSE);
  276.  
  277.     PL_GetItem (list,index,&path);
  278.  
  279.     wsprintf (msg,KILL_QUERY,path.name,path.application,path.path);
  280.  
  281.     if (!Query (dlg,NULL,msg))
  282.         return (FALSE);
  283.  
  284.     if (!KillPKey (&path))
  285.         return (Error (dlg,NULL,KILL_FAILURE));
  286.  
  287.     ListView_DeleteItem (list,index);
  288.     reflect (dlg,FALSE);
  289.  
  290.     return (TRUE);
  291. }
  292.  
  293. static BOOL reflect (HWND dlg,BOOL hit)
  294. {
  295.     Button_Enable (GetDlgItem (dlg,PATH_CREATE_BTN),TRUE);
  296.  
  297.     Button_Enable (GetDlgItem (dlg,PATH_MODIFY_BTN),hit);
  298.     Button_Enable (GetDlgItem (dlg,PATH_REMOVE_BTN),hit);
  299.  
  300.     return (TRUE);
  301. }
  302.  
  303. static BOOL order (HWND list,int column)
  304. {
  305.     auto int        count   = ListView_GetItemCount (list);
  306.     auto LPPATH     path    = GlobalAllocPtr (GHND,count * sizeof (PATH));
  307.  
  308.     auto HCURSOR    old;
  309.     auto int        inx;
  310.  
  311.     if (!path) return (Error (list,NULL,MEMORY_ERROR));
  312.  
  313.     old = SetCursor (LoadCursor (NULL,IDC_WAIT));
  314.  
  315.     SetWindowRedraw (list,FALSE);
  316.  
  317.     for (inx = NIL; inx < count; inx++)
  318.          PL_GetItem (list,inx,&path[inx]);
  319.  
  320.     switch (column) {
  321.  
  322.         case COLUMN_2 :
  323.              qsort (path,count,sizeof (PATH),by_application);
  324.              break;
  325.  
  326.         case COLUMN_3 :
  327.              qsort (path,count,sizeof (PATH),by_path);
  328.              break;
  329.  
  330.         default :
  331.              qsort (path,count,sizeof (PATH),by_name);
  332.              break;
  333.              }
  334.  
  335.     for (inx = NIL; inx < count; inx++)
  336.          PL_PutItem (list,inx,&path[inx]);
  337.  
  338.     SetWindowRedraw (list,TRUE);
  339.  
  340.     LV_SetCurSel (list,NIL);
  341.  
  342.     GlobalFreePtr (path);
  343.  
  344.     SetCursor (old);
  345.  
  346.     return (TRUE);
  347. }
  348.  
  349. static int by_name (const void * k1,const void * k2)
  350. {
  351.     auto LPPATH     p1      = (LPPATH) k1;
  352.     auto LPPATH     p2      = (LPPATH) k2;
  353.     auto int        result  = cmp_name (p1->name,p2->name);
  354.  
  355.     if (result) return (result);
  356.  
  357.     return (cmp_application (p1->application,p2->application));
  358. }
  359.  
  360. static int by_application (const void * k1,const void * k2)
  361. {
  362.     auto LPPATH     p1      = (LPPATH) k1;
  363.     auto LPPATH     p2      = (LPPATH) k2;
  364.     auto int        result  = cmp_application (p1->application,p2->application);
  365.  
  366.     if (result) return (result);
  367.  
  368.     return (cmp_name (p1->name,p2->name));
  369. }
  370.  
  371. static int by_path (const void * k1,const void * k2)
  372. {
  373.     auto LPPATH     p1      = (LPPATH) k1;
  374.     auto LPPATH     p2      = (LPPATH) k2;
  375.     auto int        result  = cmp_path (p1->path,p2->path);
  376.  
  377.     if (result) return (result);
  378.  
  379.     result = cmp_application (p1->application,p2->application);
  380.  
  381.     if (result) return (result);
  382.  
  383.     return (cmp_name (p1->name,p2->name));
  384. }
  385.  
  386. static int cmp_name (LPCSTR n1,LPCSTR n2)
  387. {
  388.     if (!*n1 && !*n2) return (NIL);
  389.  
  390.     if (!*n1) return (1);
  391.  
  392.     if (!*n2) return (-1);
  393.  
  394.     return (lstrcmpi (n1,n2));
  395. }
  396.  
  397. static int cmp_application (LPCSTR a1,LPCSTR a2)
  398. {
  399.     if (!*a1 && !*a2) return (NIL);
  400.  
  401.     if (!*a1) return (1);
  402.  
  403.     if (!*a2) return (-1);
  404.  
  405.     return (lstrcmpi (a1,a2));
  406. }
  407.  
  408. static int cmp_path (LPCSTR p1,LPCSTR p2)
  409. {
  410.     if (!*p1 && !*p2) return (NIL);
  411.  
  412.     if (!*p1) return (1);
  413.  
  414.     if (!*p2) return (-1);
  415.  
  416.     return (lstrcmpi (p1,p2));
  417. }
  418.  
  419. /* end of PathTab.c - written by Gregory Braun */
  420.