home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2002 February
/
Chip_2002-02_cd1.bin
/
sharewar
/
apaths
/
APSOURCE.ZIP
/
ServiceTab.c
< prev
next >
Wrap
C/C++ Source or Header
|
2001-03-26
|
10KB
|
388 lines
/* ServiceTab - March 26th, 2001
**
** Copyright (c) 1997-2001 by Gregory Braun. All rights reserved.
**
** This routine displays a list of the RunServices keys found in the
** MS Windows 95/NT System Registry.
**
** Private (static) routines are included to initialize the
** property page sheet when first invoked as well as handle
** [Create], [Modify] and [Remove] user requests.
**
** Called: dlg = handle to the dialog window.
** msg = the MS Windows WM_xxxxxx message to be handled.
** wp = the WPARAM specific to the message being sent
** lp = the LPARAM specific to the message being sent
**
** Returns: TRUE upon success, or FALSE if the user pressed [Cancel],
** or an internal error exists.
*/
#include "AppPaths.h"
#define KILL_QUERY "Title:\t%s\r" \
"Cmd:\t%s\r\r" \
"There is no UNDO available for the Remove command.\r" \
"Are you sure you want to delete this registery entry?\r"
#define CREATE_FAILURE "The selected MS Windows System Registry\r" \
"Service key could not be created."
#define KILL_FAILURE "The selected MS Windows System Registry\r" \
"Service key could not be removed."
#define MODIFY_FAILURE "The selected MS Windows System Registry\r" \
"Service key could not be modified."
#define MEMORY_ERROR "Memory Error\r\r" \
"Windows can not allocate a temporary memory\r" \
"buffer for the requested sorting operations."
static int sort = COLUMN_1;
static BOOL initiate (HWND dlg);
static BOOL selection (HWND dlg,WPARAM wp,LPARAM lp);
static BOOL verify (HWND dlg);
static BOOL create (HWND dlg);
static BOOL modify (HWND dlg);
static BOOL kill (HWND dlg);
static BOOL reflect (HWND dlg,BOOL hit);
static BOOL order (HWND list,int column);
static int by_name (const void * k1,const void * k2);
static int by_path (const void * k1,const void * k2);
static int cmp_name (LPCSTR n1,LPCSTR n2);
static int cmp_path (LPCSTR p1,LPCSTR p2);
extern UINT CALLBACK ServiceTab (HWND dlg,UINT msg,WPARAM wp,LPARAM lp)
{
auto int item = LOWORD (wp);
auto LPNMHDR hdr = (LPNMHDR) lp;
auto NM_LISTVIEW * cmd = (NM_LISTVIEW *) lp;
auto HWND list;
auto BOOL good;
switch (msg) {
case WM_COMMAND :
switch (item) {
case SRV_CREATE_BTN :
create (dlg);
break;
case SRV_MODIFY_BTN :
modify (dlg);
break;
case SRV_REMOVE_BTN :
kill (dlg);
break;
default :
break;
}
break;
case WM_HELP :
HelpTip (dlg,((LPHELPINFO) lp)->iCtrlId);
break;
case WM_NOTIFY :
switch (((LPNMHDR) lp)->code) {
case LVN_COLUMNCLICK :
list = hdr->hwndFrom;
sort = cmd->iSubItem;
order (list,sort);
break;
case PSN_SETACTIVE :
initiate (dlg);
break;
case PSN_KILLACTIVE :
good = verify (dlg);
SetWindowLong (dlg,DWL_MSGRESULT,!good);
return ((good) ? FALSE : TRUE);
break;
case PSN_APPLY :
good = verify (dlg);
SetWindowLong (dlg,DWL_MSGRESULT,good);
return ((good) ? PSNRET_NOERROR : PSNRET_INVALID_NOCHANGEPAGE);
break;
case PSN_HELP :
HelpTopic (dlg,SRV_TAB);
break;
default :
selection (dlg,wp,lp);
break;
}
break;
break;
}
return (FALSE);
}
static BOOL initiate (HWND dlg)
{
static BOOL created = FALSE;
auto LPSTR label[] = { "Title", "Command" };
auto int width[] = { 115,300 };
auto HWND ctl = GetDlgItem (dlg,SRV_MEMBER_LST);
auto LV_COLUMN col = { NIL };
auto int inx;
if (!created) {
col.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
col.fmt = LVCFMT_LEFT;
for (inx = NIL; inx < SRV_COLS_MAX; inx++) {
col.iSubItem = inx;
col.pszText = label[inx];
col.cx = width[inx];
ListView_InsertColumn (ctl,inx,&col);
}
created = TRUE;
}
GetSKeys (ctl);
order (ctl,sort);
reflect (dlg,TRUE);
return (TRUE);
}
static BOOL selection (HWND dlg,WPARAM wp,LPARAM lp)
{
auto int item = (int) wp;
auto LPNMHDR hdr = (LPNMHDR) lp;
auto UINT code = hdr->code;
auto HWND list = hdr->hwndFrom;
auto int hit;
if (item != SRV_MEMBER_LST)
return (FALSE);
switch (code) {
case NM_CLICK :
case NM_DBLCLK :
if ((hit = LV_RowHit (list)) >= NIL) {
reflect (dlg,TRUE);
if (code == NM_DBLCLK)
modify (dlg);
}
else reflect (dlg,FALSE);
break;
default :
break;
}
return (TRUE);
}
static BOOL verify (HWND dlg)
{
UNUSED_ARG (dlg);
return (TRUE);
}
static BOOL create (HWND dlg)
{
auto SRV srv = { NIL };
auto HWND list = GetDlgItem (dlg,SRV_MEMBER_LST);
if (!EditService (dlg,&srv))
return (FALSE);
if (!MakeSKey (&srv))
return (Error (dlg,NULL,CREATE_FAILURE));
SL_AddItem (list,&srv);
return (TRUE);
}
static BOOL modify (HWND dlg)
{
auto SRV was = { NIL };
auto SRV srv = { NIL };
auto HWND list = GetDlgItem (dlg,SRV_MEMBER_LST);
auto int index = LV_GetCurSel (list);
ListView_EnsureVisible (list,index,FALSE);
SL_GetItem (list,index,&srv);
was = srv;
if (!EditService (dlg,&srv))
return (FALSE);
if (!KillSKey (&was))
return (Error (dlg,NULL,KILL_FAILURE));
if (!MakeSKey (&srv))
return (Error (dlg,NULL,MODIFY_FAILURE));
SL_PutItem (list,index,&srv);
return (TRUE);
}
static BOOL kill (HWND dlg)
{
auto SRV srv = { NIL };
auto HWND list = GetDlgItem (dlg,SRV_MEMBER_LST);
auto int index = LV_GetCurSel (list);
auto char msg[MSTRING];
ListView_EnsureVisible (list,index,FALSE);
SL_GetItem (list,index,&srv);
wsprintf (msg,KILL_QUERY,srv.name,srv.path);
if (!Query (dlg,NULL,msg))
return (FALSE);
if (!KillSKey (&srv))
return (Error (dlg,NULL,KILL_FAILURE));
ListView_DeleteItem (list,index);
reflect (dlg,FALSE);
return (TRUE);
}
static BOOL reflect (HWND dlg,BOOL hit)
{
Button_Enable (GetDlgItem (dlg,SRV_CREATE_BTN),TRUE);
Button_Enable (GetDlgItem (dlg,SRV_MODIFY_BTN),hit);
Button_Enable (GetDlgItem (dlg,SRV_REMOVE_BTN),hit);
return (TRUE);
}
static BOOL order (HWND list,int column)
{
auto int count = ListView_GetItemCount (list);
auto LPSRV srv = (count) ? GlobalAllocPtr (GHND,count * sizeof (SRV)) : NULL;
auto HCURSOR old;
auto int inx;
if (!count) return (FALSE);
if (!srv) return (Error (list,NULL,MEMORY_ERROR));
old = SetCursor (LoadCursor (NULL,IDC_WAIT));
SetWindowRedraw (list,FALSE);
for (inx = NIL; inx < count; inx++)
SL_GetItem (list,inx,&srv[inx]);
switch (column) {
case COLUMN_2 :
qsort (srv,count,sizeof (SRV),by_path);
break;
default :
qsort (srv,count,sizeof (SRV),by_name);
break;
}
for (inx = NIL; inx < count; inx++)
SL_PutItem (list,inx,&srv[inx]);
SetWindowRedraw (list,TRUE);
LV_SetCurSel (list,NIL);
GlobalFreePtr (srv);
SetCursor (old);
return (TRUE);
}
static int by_name (const void * k1,const void * k2)
{
auto LPSRV p1 = (LPSRV) k1;
auto LPSRV p2 = (LPSRV) k2;
auto int result = cmp_name (p1->name,p2->name);
if (result) return (result);
return (cmp_path (p1->path,p2->path));
}
static int by_path (const void * k1,const void * k2)
{
auto LPSRV p1 = (LPSRV) k1;
auto LPSRV p2 = (LPSRV) k2;
auto int result = cmp_path (p1->path,p2->path);
if (result) return (result);
return (cmp_name (p1->name,p2->name));
}
static int cmp_name (LPCSTR n1,LPCSTR n2)
{
if (!*n1 && !*n2) return (NIL);
if (!*n1) return (1);
if (!*n2) return (-1);
return (lstrcmpi (n1,n2));
}
static int cmp_path (LPCSTR p1,LPCSTR p2)
{
if (!*p1 && !*p2) return (NIL);
if (!*p1) return (1);
if (!*p2) return (-1);
return (lstrcmpi (p1,p2));
}
/* end of ServiceTab.c - written by Gregory Braun */