Category : C Source Code
Archive   : MEWELDT.ZIP
Filename : DESKTOP.C

 
Output of file : DESKTOP.C contained in archive : MEWELDT.ZIP
/*===========================================================================*/
/* */
/* File : DESKTOP.C */
/* */
/* Purpose : Demo program for MEWEL which emulates some of the functions */
/* and look-and-feel of the MS Windows 2.1 MS-DOS Executive. */
/* */
/* History : Written by Marc Adler 12/8/89 */
/* */
/* (C) Copyright 1989 Marc Adler/Magma Systems All Rights Reserved */
/*===========================================================================*/

#if MSC
#include /* for getcwd() */
#include /* for malloc() */
#elif TC
#include /* for getcwd() */
#include /* for malloc() */
#endif

#include
#include
#include
#include
#include "window.h"
#include "keys.h"
#include "rc.h"

#include "desktop.h"


HWND hMain,
hWndDrives,
hWndPath,
hWndDir;

extern HWND hWndCalc;

/*************************************************************************\
| Variables for the directory lister |
\*************************************************************************/
char *szFileNames[MAXFILES];
char szFileSpec[MAXPATHLEN];
WORD iCurrDrive;
DWORD ulDriveMap;
struct dirwndinfo DirWndInfo;


#ifdef TC
/* File attribute constants */
#define _A_NORMAL 0x00
#define _A_RDONLY 0x01
#define _A_HIDDEN 0x02
#define _A_SYSTEM 0x04
#define _A_VOLID 0x08
#define _A_SUBDIR 0x10
#define _A_ARCH 0x20

#define _dos_findfirst(spec, mask, fi) findfirst(spec, fi, mask)
#define _dos_findnext(fi) findnext(fi)
#define wr_time ff_ftime
#define wr_date ff_fdate
#define name ff_name
#define attrib ff_attrib
#define ffblk find_t

#define _dos_setdrive(d,n) setdisk((d) - 1)
#endif





main(argc, argv)
int argc;
char **argv;
{
EVENT event;
WNDCLASS wndClass;
WORD hAccel;

/*
Initialize the window system - this *must* be done once in every appl
*/
WinInit();
WinUseSysColors(NULLHWND, TRUE);

/*
Set up the accelerator table and keys
*/
hAccel = AcceleratorCreate();
AcceleratorSetFocus(hAccel);
AcceleratorSetKey(ID_EXIT, VK_ALT_X);
AcceleratorSetKey(ID_HELP, VK_F1);

strcpy(szFileSpec, "*.*");
DirWndInfo.flags = VIEW_ALL;

/*
Create the main window and set the main window procedure
*/
hMain = CreateWindow("Normal", /* class */
"MS-DOS Executive", /* title */
WIN_TITLEBAR | WIN_SYSMENU | WIN_MINMAXBOX |
WIN_HAS_BORDER,
0,0, 80,25, /* x,y,width,height */
SYSTEM_COLOR, /* attr */
0, /* id */
NULLHWND, /* hParent */
NULLHWND, /* hMenu */
0, /* hInst */
0L); /* lpParam */
OldMainWinProc = WinSetWinProc(hMain, MainWndProc);
AcceleratorSetWnd(hMain);
BuildMenu(hMain);
/*
Show the main window first cause the drive and file info may take a
while to fill in.
*/
ShowWindow(hMain, TRUE);

wndClass.szClassName = "Directory";
wndClass.szBaseClass = "Normal";
wndClass.pfnWndProc = DirWndProc;
RegisterClass(&wndClass);
hWndDir = CreateWindow("Directory", /* class */
NULL, /* title */
0L, /* dwStyle */
1,3, 78,21, /* x,y,width,height */
SYSTEM_COLOR, /* attr */
1, /* id */
hMain, /* hParent */
NULLHWND, /* hMenu */
0, /* hInst */
0L); /* lpParam */
WinSetWinProc(hWndDir, DirWndProc);
ShowWindow(hWndDir, TRUE);

hWndDrives = CreateWindow("Normal", /* class */
NULL, /* title */
0L, /* dwStyle */
2,2, 30,1, /* x,y,width,height */
SYSTEM_COLOR, /* attr */
2, /* id */
hMain, /* hParent */
NULLHWND, /* hMenu */
0, /* hInst */
0L); /* lpParam */
WinSetWinProc(hWndDrives, DriveWndProc);
ShowWindow(hWndDrives, TRUE);

hWndPath = CreateWindow("Text", /* class */
"", /* title */
0L, /* dwStyle */
32,2,45,1, /* x,y,width,height */
SYSTEM_COLOR, /* attr */
3, /* id */
hMain, /* hParent */
NULLHWND, /* hMenu */
0, /* hInst */
0L); /* lpParam */
WinSetWinProc(hWndPath, PathWndProc);
ShowWindow(hWndPath, TRUE);

SetFocus(hWndDir);

/*
Main message loop
*/
while (GetMessage(&event) != WM_QUIT)
{
#if MODELESS
if (hWndCalc && IsDialogMessage(hWndCalc, &event))
continue;
#endif
TranslateAccelerator(&event);
DispatchMessage(&event);
}

exit(0);
}



/*===========================================================================*/
/* BuildMenu - create the main menu bar and all of the pulldowns */
/*===========================================================================*/
BuildMenu(hParent)
HWND hParent;
{
WORD hMod;
HMENU hBar;

/*
Load the main menubar in from the external resource file DESKTOP.RES.
Alternatively, we can also do a bunch of CreateMenu()'s, but using
a resource file lets us modify our menus more easily.
*/
hMod = OpenResourceFile(DESKTOP_RESFILE);
if (!hMod)
{
printf("Can't load main menu\n");
exit(1);
}
hBar = LoadMenu(hMod, ID_MENUBAR, hMain);
SetMenu(hMain, hBar);
CloseResourceFile(hMod);
}



/*
MainWndProc - window procedure for the main window
*/
int pascal MainWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;
WORD wParam;
DWORD lParam;
{
switch (message)
{
case WM_COMMAND :
switch (wParam)
{
case ID_RUN :
GenericDialogDriver(DLG_RUN, RunDialogProc);
break;
case ID_DELETE:
GenericDialogDriver(DLG_DELETE, DeleteDialogProc);
break;
case ID_CHGDIR :
GenericDialogDriver(DLG_CHGDIR, ChgDirDialogProc);
break;
case ID_CREATEDIR :
GenericDialogDriver(DLG_CREATEDIR, CreateDirDialogProc);
break;
case ID_FORMAT :
GenericDialogDriver(DLG_FORMAT, FormatDialogProc);
break;
case ID_SYSDISK :
GenericDialogDriver(DLG_SYSDISK,SystemDialogProc);
break;
case ID_VOLUME :
GenericDialogDriver(DLG_VOLUME, VolumeDialogProc);
break;
case ID_VIEW_PARTIAL :
GenericDialogDriver(DLG_VIEW_PARTIAL,PartialDialogProc);
break;


case ID_RENAME:
GenericDialogDriver(DLG_RENAME, RenameDialogProc);
break;
case ID_COPY :
GenericDialogDriver(DLG_COPY, CopyDialogProc);
break;

case ID_PRINT :
MessageBox("This function is not", "implemented in the",
"demo program.", "Outta luck", MB_OK | MB_ICONEXCLAMATION);
break;

case ID_GETINFO :
GetInfo();
break;

case ID_VIEW_LONG:
DirWndInfo.flags |= VIEW_LONG;
CheckMenuItem(GetMenu(hMain), ID_VIEW_SHORT,MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), ID_VIEW_LONG, MF_CHECKED|MF_BYCOMMAND);
RefreshDirectory();
break;
case ID_VIEW_SHORT:
DirWndInfo.flags &= ~VIEW_LONG;
CheckMenuItem(GetMenu(hMain), ID_VIEW_SHORT,MF_CHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), ID_VIEW_LONG, MF_UNCHECKED|MF_BYCOMMAND);
RefreshDirectory();
break;

case ID_BYDATE :
DirWndInfo.flags &= ~SORT_MASK;
DirWndInfo.flags |= SORT_TIME;
goto refresh;
case ID_BYNAME :
DirWndInfo.flags &= ~SORT_MASK;
goto refresh;
case ID_BYSIZE :
DirWndInfo.flags &= ~SORT_MASK;
DirWndInfo.flags |= SORT_SIZE;
goto refresh;
case ID_BYKIND :
DirWndInfo.flags &= ~SORT_MASK;
DirWndInfo.flags |= SORT_EXTENSION;
refresh:
CheckMenuItem(GetMenu(hMain), ID_BYDATE, MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), ID_BYSIZE, MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), ID_BYKIND, MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), ID_BYNAME, MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), wParam, MF_CHECKED |MF_BYCOMMAND);
RefreshDirectory();
break;

case ID_VIEW_ALL :
if (DirWndInfo.flags & VIEW_PARTIAL)
CheckMenuItem(GetMenu(hMain), ID_VIEW_PARTIAL, MF_UNCHECKED|MF_BYCOMMAND);
else if (DirWndInfo.flags & VIEW_PROGRAMS)
CheckMenuItem(GetMenu(hMain), ID_VIEW_PROGRAMS, MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), ID_VIEW_ALL, MF_CHECKED|MF_BYCOMMAND);
DirWndInfo.flags &= ~(VIEW_PROGRAMS | VIEW_PARTIAL);
DirWndInfo.flags |= VIEW_ALL;
strcpy(szFileSpec, "*.*");
RefreshDirectory();
break;

case ID_VIEW_PROGRAMS :
if (DirWndInfo.flags & VIEW_PARTIAL)
CheckMenuItem(GetMenu(hMain), ID_VIEW_PARTIAL, MF_UNCHECKED|MF_BYCOMMAND);
else if (DirWndInfo.flags & VIEW_ALL)
CheckMenuItem(GetMenu(hMain), ID_VIEW_ALL, MF_UNCHECKED|MF_BYCOMMAND);
CheckMenuItem(GetMenu(hMain), ID_VIEW_PROGRAMS, MF_CHECKED|MF_BYCOMMAND);
DirWndInfo.flags &= ~(VIEW_PARTIAL | VIEW_ALL);
DirWndInfo.flags |= VIEW_PROGRAMS;
strcpy(szFileSpec, "*.exe!*.com!*.bat");
RefreshDirectory();
break;

case ID_EXIT:
PostQuitMessage(0);
break;

case ID_HELP :
goto do_help;

case ID_ABOUT:
MessageBox("MEWEL Window System", "Magma Systems",
"(201) 912-0192", "About MEWEL", MB_OK | MB_ICONASTERISK);
break;

case ID_CALC :
Calculator();
break;
}
break;

case WM_SYSCOMMAND :
switch (wParam)
{
case SC_CLOSE :
PostQuitMessage(0);
break;
}
break;

case WM_HELP :
do_help:
GenericDialogDriver(DLG_HELP, HelpDialogProc);
break;


case WM_HSCROLL :
switch (wParam)
{
case SB_LINEUP :
wParam = VK_LEFT;
break;
case SB_LINEDOWN :
wParam = VK_RIGHT;
break;
case SB_PAGEUP :
wParam = VK_LEFT;
break;
case SB_PAGEDOWN :
wParam = VK_RIGHT;
break;
case SB_THUMBTRACK :
{
RECT r, rectDir;
int col = LOWORD(lParam);
int currpos;
int iHeight, oldLogCol;

if (DirWndInfo.flags & VIEW_LONG)
return TRUE;

GetClientRect(hWndDir, &rectDir);
iHeight = RECT_HEIGHT(rectDir);
oldLogCol = DirWndInfo.leftLogCol;

/*
Determine the approximate position of the thumb.
Use this ratio to determine currpos :
(col-leftcol) / (bottomcol-leftcol+1) = currpos / (maxpos-minpos+1)
*/
r = WinGetClient(hWndDir);/* Get the coordinates of the scrollbar */
DirWndInfo.leftLogCol = (col-r.left) *
(DirWndInfo.nFiles / iHeight + 1) / (r.right-r.left-1);
DirWndInfo.idxCurrSel += (DirWndInfo.leftLogCol - oldLogCol) * iHeight;
if (DirWndInfo.idxCurrSel < 0)
DirWndInfo.idxCurrSel = 0;
else
if (DirWndInfo.idxCurrSel >= DirWndInfo.nFiles)
DirWndInfo.idxCurrSel = DirWndInfo.nFiles - 1;
SetScrollPos((HWND) HIWORD(lParam), DirWndInfo.idxCurrSel + 1, TRUE);
ShowWindow(hWndDir, TRUE);
SendMessage(hWndDir, WM_SETFOCUS, TRUE, 0L);
return TRUE;
}
}
SendMessage(hWndDir, WM_CHAR, wParam, 0L);
break;


case WM_VSCROLL :
switch (wParam)
{
case SB_LINEUP :
wParam = VK_UP;
break;
case SB_LINEDOWN :
wParam = VK_DOWN;
break;
case SB_PAGEUP :
wParam = VK_PGUP;
break;
case SB_PAGEDOWN :
wParam = VK_PGDN;
break;
case SB_THUMBTRACK :
{
RECT r;
int row = LOWORD(lParam);
int currpos;
/*
Determine the approximate position of the thumb.
Use this ratio to determine currpos :
(row-toprow) / (bottomrow-toprow+1) = currpos / (maxpos-minpos+1)
*/
r = WinGetClient(hWndDir);/* Get the coordinates of the scrollbar */
DirWndInfo.idxCurrSel = (row-r.top) * DirWndInfo.nFiles / (r.bottom-r.top-1);
SetScrollPos((HWND) HIWORD(lParam), DirWndInfo.idxCurrSel, TRUE);
DirWndInfo.idxTopFile = DirWndInfo.idxCurrSel;
ShowWindow(hWndDir, TRUE);
return TRUE;
}
}
SendMessage(hWndDir, WM_CHAR, wParam, 0L);
break;

case WM_SETFOCUS :
SetFocus(hWndDir);
break;

default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
}


int pascal DriveWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;
WORD wParam;
DWORD lParam;
{
char buf[MAXPATHLEN];
WORD attr;
WORD i, iDriveCol;
DWORD ulMap;
extern DWORD pascal _DOSEnumDisks(void);

switch (message)
{
case WM_PAINT :
{
attr = WinGetAttr(hWnd);

getcwd(buf, sizeof(buf));
iCurrDrive = toupper(buf[0]);

ulDriveMap = ulMap = _DOSEnumDisks();
iDriveCol = 0;
for (i = 'A'; i <= 'Z'; i++)
{
if (ulMap & 0x01)
{
sprintf(buf, "[-%c-]", i);
WinPuts(hWnd, 0, iDriveCol, buf,
(i == iCurrDrive) ? MAKE_HIGHLITE(attr) : attr);
iDriveCol += SPACE_PER_DRIVE;
}
ulMap >>= 1;
}
break;
}

case WM_LBUTTONDOWN :
{
int mousecol = LOWORD(lParam);
int idxDrive, drives;
RECT rClient;

/*
Translate screen coords to client coords
*/
GetClientRect(hWnd, &rClient);
mousecol -= rClient.left;
idxDrive = mousecol / SPACE_PER_DRIVE;

ulMap = ulDriveMap;
for (i = 'A'; i <= 'Z'; i++)
{
if (ulMap & 0x01)
{
if (idxDrive-- == 0)
{
_dos_setdrive(i - 'A' + 1, &drives);
CheckCriticalError(i);
RefreshDirectory();
return TRUE;
}
}
ulMap >>= 1;
}
return TRUE;
}


default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
}


int pascal PathWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;
WORD wParam;
DWORD lParam;
{
char buf[MAXPATHLEN+1];

switch (message)
{
case WM_PAINT :
getcwd(buf, sizeof(buf));
WinPuts(hWnd, 0, 0, buf, WinGetAttr(hWnd));
break;

case WM_LBUTTONDOWN :
{
int mousecol = LOWORD(lParam);
RECT rClient;

/*
Translate screen coords to client coords
*/
GetClientRect(hWnd, &rClient);
mousecol -= rClient.left;
getcwd(buf, sizeof(buf));

if (mousecol < strlen(buf))
{
while (buf[mousecol] && buf[mousecol] != '\\')
mousecol++;
if (buf[mousecol-1] == ':') /* C:\ */
mousecol++;
buf[mousecol] = '\0';
if (chdir(buf) >= 0)
RefreshDirectory();
}

return TRUE;
}

default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
}



int pascal DirWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;
WORD wParam;
DWORD lParam;
{
char buf[COL_WIDTH];
POINT ptDir;
RECT rectDir;
int iHeight, iWidth;
int newCurrSel, i;
WORD attr;

switch (message)
{
case WM_CREATE :
DirWndInfo.idxCurrSel = 0;
DirWndInfo.leftLogCol = 0;
DirWndInfo.idxTopFile = 0;
DirGetFiles();
break;

case WM_PAINT :
if (lParam != 0L) /* don't respond to WM_PAINT sent by WinScrollWindow */
break;

GetClientRect(hWnd, &rectDir);
iHeight = RECT_HEIGHT(rectDir);
iWidth = (DirWndInfo.flags & VIEW_LONG) ? 1 : RECT_WIDTH(rectDir);
attr = WinGetAttr(hWnd);

ptDir.x = ptDir.y = 0;
if ((DirWndInfo.flags & VIEW_LONG))
i = DirWndInfo.idxTopFile;
else
i = DirWndInfo.leftLogCol * iHeight;
for ( ; i < DirWndInfo.nFiles; i++)
{
OutputFileName(hWnd, ptDir.y, ptDir.x, szFileNames[i], attr);
if (++ptDir.y >= iHeight)
{
ptDir.y = 0;
ptDir.x += COL_WIDTH;
if (ptDir.x >= iWidth)
{
RECT rMain, rMainClient;
HWND hHSB, hVSB;

GetWindowRect(hMain, &rMain);
GetClientRect(hMain, &rMainClient);
WinGetScrollbars(hMain, &hHSB, &hVSB);

if (!hHSB)
hHSB = ScrollBarCreate(hMain,
rMain.bottom, rMainClient.left,
rMain.bottom, rMainClient.right,
SYSTEM_COLOR,
(WORD) WIN_HSCROLL, 0);
SetScrollRange(hHSB, 1, (DirWndInfo.nFiles / iHeight) + 1, TRUE);

if ((DirWndInfo.flags & VIEW_LONG) && DirWndInfo.nFiles >= iHeight)
{
if (!hVSB)
hVSB = ScrollBarCreate(hMain,
rMainClient.top, rMain.right,
rMainClient.bottom, rMain.right,
SYSTEM_COLOR,
(WORD) WIN_VSCROLL, 0);
SetScrollRange(hVSB, 1, DirWndInfo.nFiles, TRUE);
}
break;
}
}
}
DirSetScrollPos();
break;


case WM_SETFOCUS :
attr = WinGetAttr(hWnd);
if ((DirWndInfo.flags & VIEW_LONG))
OutputFileName(hWnd, DirWndInfo.idxCurrSel - DirWndInfo.idxTopFile, 0,
szFileNames[DirWndInfo.idxCurrSel], MAKE_HIGHLITE(attr));
else
{
GetClientRect(hWnd, &rectDir);
iHeight = RECT_HEIGHT(rectDir);
OutputFileName(hWnd, DirWndInfo.idxCurrSel % iHeight,
(DirWndInfo.idxCurrSel/iHeight - DirWndInfo.leftLogCol) * COL_WIDTH,
szFileNames[DirWndInfo.idxCurrSel], MAKE_HIGHLITE(attr));
}
break;


case WM_LBUTTONDOWN :
case WM_LBUTTONDBLCLK:
{
int mouserow = HIWORD(lParam); /* screen coords of mouse click */
int mousecol = LOWORD(lParam);
RECT rClient;

/*
Translate screen coords to client coords
*/
GetClientRect(hWnd, &rClient);
mouserow -= rClient.top;
mousecol -= rClient.left;
iHeight = RECT_HEIGHT(rClient);
iWidth = RECT_WIDTH(rClient); /* need the next two for the goto */
attr = WinGetAttr(hWnd);

if (DirWndInfo.flags & VIEW_LONG)
newCurrSel = DirWndInfo.idxTopFile + mouserow;
else
newCurrSel = (mousecol/COL_WIDTH + DirWndInfo.leftLogCol) * iHeight + mouserow;

if (message == WM_LBUTTONDBLCLK)
PostMessage(hWnd, WM_CHAR, '\r', 0L);
goto change_sel;
}

case WM_CHAR :
GetClientRect(hWnd, &rectDir);
iHeight = RECT_HEIGHT(rectDir);
iWidth = RECT_WIDTH(rectDir);
attr = WinGetAttr(hWnd);
newCurrSel = DirWndInfo.idxCurrSel;

switch (wParam)
{
case VK_DOWN :
if (DirWndInfo.idxCurrSel + 1 < DirWndInfo.nFiles)
newCurrSel = DirWndInfo.idxCurrSel + 1;
break;

case VK_UP :
if (DirWndInfo.idxCurrSel > 0)
newCurrSel = DirWndInfo.idxCurrSel - 1;
break;

case VK_LEFT :
case VK_BACKTAB :
if ((DirWndInfo.flags & VIEW_LONG))
break;
if (DirWndInfo.idxCurrSel - iHeight >= 0)
newCurrSel = DirWndInfo.idxCurrSel - iHeight;
break;

case VK_RIGHT :
case VK_TAB :
if ((DirWndInfo.flags & VIEW_LONG))
break;
if (DirWndInfo.idxCurrSel + iHeight < DirWndInfo.nFiles)
newCurrSel = DirWndInfo.idxCurrSel + iHeight;
break;

case VK_PGUP :
if (!(DirWndInfo.flags & VIEW_LONG))
break;
if (DirWndInfo.idxCurrSel - iHeight >= 0)
newCurrSel = DirWndInfo.idxCurrSel - iHeight;
else
newCurrSel = 0;
break;

case VK_PGDN :
if (!(DirWndInfo.flags & VIEW_LONG))
break;
if (DirWndInfo.idxCurrSel + iHeight < DirWndInfo.nFiles)
newCurrSel = DirWndInfo.idxCurrSel + iHeight;
else
newCurrSel = DirWndInfo.nFiles - 1;
break;


case VK_BACKSPACE :
if (chdir("..") >= 0)
{
RefreshDirectory();
return TRUE;
}
break;

case '\r':
{
char fname[MAXPATHLEN];
strcpy(fname, szFileNames[DirWndInfo.idxCurrSel]);
if (fname[0] == '[')
{
*strchr(fname, ']') = '\0';
if (chdir(fname+1) >= 0)
{
RefreshDirectory();
return TRUE;
}
}
else
RunCurrFile();
break;
}

default :
if (wParam < VK_CTRL_Z)
{
DWORD ulMap = ulDriveMap;
int i;
int drives;

for (i = wParam; --i; ulMap >>= 1)
;
if (ulMap & 0x01)
{
_dos_setdrive(wParam, &drives);
CheckCriticalError(iCurrDrive);
RefreshDirectory();
return TRUE;
}
}
break;
}

change_sel:
if (newCurrSel != DirWndInfo.idxCurrSel)
{
if ((DirWndInfo.flags & VIEW_LONG))
OutputFileName(hWnd, DirWndInfo.idxCurrSel - DirWndInfo.idxTopFile, 0,
szFileNames[DirWndInfo.idxCurrSel], attr);
else
OutputFileName(hWnd, DirWndInfo.idxCurrSel % iHeight,
(DirWndInfo.idxCurrSel/iHeight - DirWndInfo.leftLogCol) * COL_WIDTH,
szFileNames[DirWndInfo.idxCurrSel], attr);

DirWndInfo.idxCurrSel = newCurrSel;

if ((DirWndInfo.flags & VIEW_LONG))
{
if (newCurrSel < DirWndInfo.idxTopFile)
{
if (wParam == VK_UP)
{
DirWndInfo.idxTopFile--;
WinScrollWindow(hWnd, 0, 1, (RECT *) NULL);
}
else
{
DirWndInfo.idxTopFile = newCurrSel;
ShowWindow(hWnd, TRUE);
}
}
else if (newCurrSel >= DirWndInfo.idxTopFile + iHeight)
{
if (wParam == VK_DOWN)
{
DirWndInfo.idxTopFile++;
WinScrollWindow(hWnd, 0, -1, (RECT *) NULL);
}
else
{
DirWndInfo.idxTopFile = newCurrSel;
ShowWindow(hWnd, TRUE);
}
}
}
else
{
if ((DirWndInfo.idxCurrSel/iHeight - DirWndInfo.leftLogCol) * COL_WIDTH >= iWidth)
{
DirWndInfo.leftLogCol++;
ShowWindow(hWnd, TRUE);
}
else if (DirWndInfo.idxCurrSel/iHeight < DirWndInfo.leftLogCol &&
DirWndInfo.leftLogCol > 0)
{
DirWndInfo.leftLogCol--;
ShowWindow(hWnd, TRUE);
}
}

if ((DirWndInfo.flags & VIEW_LONG))
OutputFileName(hWnd, DirWndInfo.idxCurrSel - DirWndInfo.idxTopFile, 0,
szFileNames[DirWndInfo.idxCurrSel], MAKE_HIGHLITE(attr));
else
OutputFileName(hWnd, DirWndInfo.idxCurrSel % iHeight,
(DirWndInfo.idxCurrSel/iHeight - DirWndInfo.leftLogCol) * COL_WIDTH,
szFileNames[DirWndInfo.idxCurrSel], MAKE_HIGHLITE(attr));

DirSetScrollPos();
}
break;


default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
}


/****************************************************************************/
/* */
/* ROUTINE TO GATHER UP ALL OF THE FILES IN THE */
/* CURRENT DIRECTORY. */
/* */
/****************************************************************************/
DirGetFiles()
{
struct find_t fileinfo;
char buf[80];
char spec[80], *pSpec;
WORD mask;

while (--DirWndInfo.nFiles >= 0)
if (szFileNames[DirWndInfo.nFiles])
free(szFileNames[DirWndInfo.nFiles]);

DirWndInfo.nFiles = 0;

/*
FILL IN THE SUBDIRECTORIES
*/
mask = _A_SUBDIR;
if (mask && _dos_findfirst("*.*", mask, &fileinfo) == 0)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR) &&
(fileinfo.name[0] != '.' || fileinfo.name[1]))
{
if (DirWndInfo.nFiles < MAXFILES)
{
sprintf(buf, "[%s]", fileinfo.name);
szFileNames[DirWndInfo.nFiles++] = strsave(buf);
}
}
} while (_dos_findnext(&fileinfo) == 0);
}

/*
Fill in the normal files
*/
mask = (_A_NORMAL | _A_RDONLY | _A_HIDDEN | _A_SYSTEM);


pSpec = szFileSpec;
for (;;)
{
char *pBang = strchr(pSpec, '!');
if (pBang)
*pBang = '\0';

if (_dos_findfirst(pSpec, mask, &fileinfo) == 0)
{
do
{
if (DirWndInfo.nFiles < MAXFILES)
{
sprintf(buf, "%-12s", fileinfo.name);
sprintf(buf+12, " %7ld ", fileinfo.size);
sprintf(buf+21, " %2d-%02d-%02d %2d:%02d:%02d",
((fileinfo.wr_date >> 9) & 0x7F) + 80,
(fileinfo.wr_date >> 5) & 0x0F,
(fileinfo.wr_date) & 0x1F,
(fileinfo.wr_time >> 11) & 0x1F,
(fileinfo.wr_time >> 5) & 0x3F,
(fileinfo.wr_time << 5) & 0x3E);
szFileNames[DirWndInfo.nFiles++] = strcpy(emalloc(42), buf);
}
} while (_dos_findnext(&fileinfo) == 0);
}

if (pBang)
{
*pBang = '!';
pSpec = pBang + 1;
}
else
break;
}

qsort(szFileNames, DirWndInfo.nFiles, sizeof(char *), CompDriver);
return TRUE;
}


RefreshDirectory()
{
PostMessage(hWndDir, WM_CREATE, 0, 0L);
PostMessage(hMain, WM_SHOW, TRUE, 0L);
PostMessage(hWndDir, WM_SETFOCUS, 0, 0L);
}

DirSetScrollPos()
{
HWND hHSB, hVSB;

WinGetScrollbars(hMain, &hHSB, &hVSB);
if (hVSB)
SetScrollPos(hVSB, DirWndInfo.idxCurrSel + 1, TRUE);
if (hHSB)
SetScrollPos(hHSB, DirWndInfo.leftLogCol + 1, TRUE);
}


OutputFileName(HWND hWnd, int row, int col, char *szFile, WORD attr)
{
char szBlanks[32];
int sLen;

if ((DirWndInfo.flags & VIEW_LONG))
{
if (szFile[0] == '[')
WinEraseEOL(hWnd, row, col, WinGetAttr(hWnd));
WinPuts(hWnd, row, col, szFile, attr);
}
else
{
if (szFile[0] != '[')
szFile[12] = '\0';
WinPuts(hWnd, row, col, szFile, attr);
/*
If the calculator is active, we want to erase any calculator characters
*/
#if 0
if (hWndCalc && (sLen = strlen(szFile)) < COL_WIDTH)
{
memset(szBlanks, ' ', COL_WIDTH - sLen);
szBlanks[COL_WIDTH-sLen] = '\0';
WinPuts(hWnd, row, col + sLen, szBlanks, WinGetAttr(hWnd));
}
#endif
if (szFile[0] != '[')
szFile[12] = ' ';
}
}


/****************************************************************************/
/* */
/* ROUTINE TO RUN THE CURRENTLY SELECTED FILE */
/* */
/****************************************************************************/

RunCurrFile()
{
char fname[MAXPATHLEN];
char *szExt;

strncpy(fname, szFileNames[DirWndInfo.idxCurrSel], 12);
fname[12] = '\0';
if (fname[0] == '[')
return FALSE;
szExt = strchr(fname, '.');

if (szExt && *++szExt)
{
szExt[3] = '\0';
if (!strcmp(szExt, "EXE") || !strcmp(szExt, "COM") ||
!strcmp(szExt, "BAT"))
{
system(fname);
RefreshDirectory();
}
}
return TRUE;
}


/****************************************************************************/
/* */
/* COMPARISON ROUTINES FOR DIRECTORY SORTING */
/* */
/****************************************************************************/

int CompDriver(char **psz1, char **psz2)
{
if (DirWndInfo.flags & SORT_EXTENSION)
return CompExtension(psz1, psz2);
else if (DirWndInfo.flags & SORT_TIME)
return CompTime(psz1, psz2);

else if (DirWndInfo.flags & SORT_SIZE)
return CompSize(psz1, psz2);
else
return CompName(psz1, psz2);
}


CompName(char **psz1, char **psz2)
{
char *s1 = *psz1;
char *s2 = *psz2;

if (*s1 == '[' && *s2 != '[')
return -1;
if (*s1 != '[' && *s2 == '[')
return 1;

return strcmp(*psz1, *psz2);
}


CompSize(char **psz1, char **psz2)
{
char *s1 = *psz1;
char *s2 = *psz2;
int rc;

if (*s1 == '[' && *s2 != '[') /* Comparing a directory and a file? */
return -1; /* The directory always is the lower. */
if (*s1 != '[' && *s2 == '[')
return 1;
if (*s1 == '[' && *s2 == '[') /* Comparing two directories? Just */
return strcmp(s1, s2); /* compare the names. */

/* Advance past the name and go to the size field. */
s1 += 13;
s2 += 13;

if ((rc = strncmp(s1, s2, 7)) == 0)
rc = strcmp(*psz1, *psz2); /* equal in size? Then compare the names */
return rc;
}


CompTime(char **psz1, char **psz2)
{
char *s1 = *psz1;
char *s2 = *psz2;
int rc;

if (*s1 == '[' && *s2 != '[') /* Comparing a directory and a file? */
return -1; /* The directory always is the lower. */
if (*s1 != '[' && *s2 == '[')
return 1;
if (*s1 == '[' && *s2 == '[') /* Comparing two directories? Just */
return strcmp(s1, s2); /* compare the names. */

/* Advance past the name and size, and go to the time field. */
s1 += 21;
s2 += 21;

return strncmp(s1, s2, 17) * -1;
}


CompExtension(char **psz1, char **psz2)
{
char *s1 = *psz1;
char *s2 = *psz2;
int c1, c2;
int rc;

if (*s1 == '[' && *s2 != '[')
return -1;
if (*s1 != '[' && *s2 == '[')
return 1;
if (*s1 == '[' && *s2 == '[') /* Comparing two directories? Just */
return strcmp(s1, s2); /* compare the names. */

for (c1 = 1; *s1 && *s1 != '.' && *s1 != ' '; s1++, c1++)
;
for (c2 = 1; *s2 && *s2 != '.' && *s2 != ' '; s2++, c2++)
;

if ((rc = strncmp(s1, s2, min(c1, c2))))
return rc;

for (s1 = *psz1, s2 = *psz2; *s1 == *s2 && *s1 && *s1 != '.'; s1++, s2++)
;

return ((*s1 == '.' ? 0 : *s1) - (*s2 == '.' ? 0 : *s2));
}


/****************************************************************************/
/* */
/* GET-INFO ROUTINES */
/* */
/****************************************************************************/

GetInfo()
{
char buf[80];
HWND hWnd;

strcpy(buf, szFileNames[DirWndInfo.idxCurrSel]);
if (buf[0] == '[')
strcat(buf, " ");

hWnd = CreateWindow("Normal", /* class */
"Get Info", /* title */
WIN_HAS_BORDER | WIN_SYSMENU | WIN_TITLEBAR | WS_CLIP,
2,5, strlen(buf)+2,3, /* x,y,width,height */
SYSTEM_COLOR, /* attr */
2, /* id */
hWndDir, /* hParent */
NULLHWND, /* hMenu */
0, /* hInst */
0L); /* lpParam */
WinSetWinProc(hWnd, GetInfoWndProc);

SetWindowExtra(hWnd, sizeof(char *));
SetWindowPtr(hWnd, 0, strsave(buf));

ShowWindow(hWnd, TRUE);
}


int pascal GetInfoWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;
WORD wParam;
DWORD lParam;
{
char buf[MAXPATHLEN+1];

switch (message)
{
case WM_SYSCOMMAND :
switch (wParam)
{
case SC_CLOSE :
DestroyWindow(hWnd);
SetFocus(hWndDir);
break;
}
break;

case WM_PAINT :
strcpy(buf, GetWindowPtr(hWnd, 0));
WinPuts(hWnd, 0, 0, buf, WinGetAttr(hWnd));

break;

case WM_LBUTTONDOWN:
{
RECT rWindow;
int mouserow = HIWORD(lParam);
int mousecol = LOWORD(lParam);

GetWindowRect(hWnd, &rWindow);
if (mouserow == rWindow.top)
{
if (mousecol == rWindow.left)
DefWinProc(hWnd, message, wParam, lParam);
else
/*
If we click on the upper left corner of the dialog box,
we want to move it....
*/
Rubberband(hWnd, TRUE);

break;
}
break;
}

default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
}


/*
Rubberband()
Lets the user drag around (or stretch) an outline of a window as the
user resizes or moves the window.
*/
Rubberband(hWnd, bIsMoving)
HWND hWnd;
int bIsMoving;
{
RECT r, newr, rMain;
char *szSavedRect;
EVENT event;

if (!hWnd)
return FALSE;

rMain = WinGetClient(hMain);
r = WinGetRect(hWnd);
szSavedRect = emalloc(RectSize(&rMain) * 2);
WinSaveRect(hMain, &rMain, szSavedRect);
VidFrame(r.top, r.left, r.bottom, r.right, WinGetAttr(hWnd), BORDER_DASHED);
SetCapture(hWnd);

for (;;)
{
if (GetMessage(&event) == WM_LBUTTONUP)
break;

if (event.message == WM_MOUSEMOVE)
{
/* Get the 0-based screen coords of the mouse */
int mouserow = HIWORD(event.lParam);
int mousecol = LOWORD(event.lParam);

if (RectContainsPoint(rMain, mouserow, mousecol))
{
if (bIsMoving)
{
newr.top = mouserow;
newr.left = mousecol;
newr.bottom = mouserow + (r.bottom - r.top);
newr.right = mousecol + (r.right - r.left);
}
else /* bIsResizing */
{
newr.top = r.top;
newr.left = r.left;
newr.bottom = mouserow;
newr.right = mousecol;
}


/*
Sigh! This would be a whole lot easier if WinMove() restricted
the area of the child window to the client rectangle of the parent...
*/
if (newr.top <= rMain.top || newr.bottom >= rMain.bottom ||
newr.left < rMain.left || newr.right > rMain.right)
continue;
}
else
continue;
}

else if (event.message == WM_CHAR)
{
newr = WinGetRect(hWnd);
switch (event.wParam)
{
case VK_LEFT :
if ((!bIsMoving || newr.left > 0) && newr.right > newr.left+1)
{
newr.right--;
if (bIsMoving)
newr.left--;
}
break;
case VK_RIGHT:
if (newr.right < VideoInfo.width-1)
{
newr.right++;
if (bIsMoving)
newr.left++;
}
break;
case VK_UP :
if ((!bIsMoving || newr.top > 2) && newr.bottom > newr.top+1)
{
newr.bottom--;
if (bIsMoving)
newr.top--;
}
break;
case VK_DOWN :
if (newr.bottom < VideoInfo.length - 1)
{
newr.bottom++;
if (bIsMoving)
newr.top++;
}
break;
case VK_ESC :
case '\r' :
goto bye;
default :
break;
}
}

WinRestoreRect(hMain, &rMain, szSavedRect);
VidFrame(newr.top, newr.left, newr.bottom, newr.right, WinGetAttr(hWnd), BORDER_DASHED);
if (bIsMoving)
WinMove(hWnd, newr.top, newr.left);
else
WinSetSize(hWnd, RECT_HEIGHT(newr), RECT_WIDTH(newr));
}

bye:
free(szSavedRect);
ShowWindow(hMain, TRUE);
ReleaseCapture();
}



  3 Responses to “Category : C Source Code
Archive   : MEWELDT.ZIP
Filename : DESKTOP.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/