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

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