campo-sirio/al/examples/ex03win.c
alex 714dd74636 Archive Library versione 2.00
git-svn-id: svn://10.65.10.50/trunk@5350 c028cbd2-c16b-5b4b-a496-9718f37d4682
1997-10-09 16:09:54 +00:00

434 lines
15 KiB
C
Executable File

/*
* EX03WIN.C
*
* C/Windows Example program for ArchiveLib 2.0
*
* Copyright (c) Greenleaf Software, Inc. 1994 - 1996
* All Rights Reserved
*
* MEMBERS/FUNCTIONS DEMONSTRATED
*
* ALArchiveDelete()
*
* DESCRIPTION
*
* EX03WIN.CPP demonstrates ALArchiveDelete(). It lets you
* select a list of files to delete from an archive, then performs
* the deletion. The list boxes are updated right afterwards so you
* can see the results.
*
* This is a very simple Windows program. It works by creating a modeless
* dialog box, then wrapping a framing window around it. By using a framing
* window as the main window we are able to handle accelerator keys, menus,
* and icons a bit easier. Because of this approach, WinMain() and
* MainWndProc() are not very exciting, and have been moved down to the
* bottom of the file. MainWndProc() does handle menus and accelerator
* keys, so it comes first.
*
* This program is functionally equivalent to EX03WIN.CPP. The C version
* uses the translation layer functions to get the job done, but if
* you put them side by side, you won't see too much difference.
*
* REVISION HISTORY
*
* February 1, 1996 2.0A : Second release
*
*/
#define STRICT
#include <windows.h>
#include <stdarg.h>
#include "al.h"
#include "algauge.h"
#include "ex03win.h"
int iInstanceNumber;
HINSTANCE hInstance;
HWND hDlgMain = 0;
hALArchive hArchive = 0;
void Delete( HWND hDlg );
void SizeFramingWindow( HWND hDlg );
/*
* This function gets the name of an archive, then uses the
* ALArchiveFillListBoxDialog() function to fill it up with
* names.
*/
void ReadArchive( HWND hDlg, int archive_name_id, int list_box_id )
{
hALArchive archive;
char input_name[ 128 ];
GetDlgItemText( hDlg, archive_name_id, input_name, 128 );
SendDlgItemMessage( hDlg, archive_name_id, (UINT) EM_SETSEL, (WPARAM) -1, (LPARAM) -1 );
#if defined( ZIP )
archive = newALPkArchive( input_name );
#else
archive = newALGlArchive( input_name );
#endif
ALArchiveFillListBox( archive, hDlg, list_box_id );
deleteALArchive( archive );
}
/*
* This function performs the deletion. it gets the name of the archive
* from a text box, then gets the list, then sets marks on the list based
* on the names set in the list box. The archive delete function then takes
* care of the hard work.
*
* There is one tricky bit here. The hArchive handle is a global variable.
* If we are in the middle of updating or deleting, the other functions
* in this program can figure that out by looking at the handle. They
* can also set the error member in hArchive, causing an abort of the
* delete process.
*/
void Delete( HWND hDlg )
{
char input_name[ 128 ];
hALMonitor hMonitor;
hALEntryList hList;
int count;
GetDlgItemText( hDlg, AL_INPUT, input_name, 128 );
hMonitor = newALWindowsMessage( AL_MONITOR_JOB,
GetDlgItem( hDlg, AL_PROGRESS_TEXT ),
AL_SEND_RATIO,
GetDlgItem( hDlg, AL_PROGRESS_BAR ),
ALGaugeSetPosition );
#if defined( ZIP )
hArchive = newALPkArchive( input_name );
hList = newALListPkTools( hMonitor, 6, 13, 6 );
#else
hArchive = newALGlArchive( input_name );
hList = newALListGlTools( hMonitor, AL_GREENLEAF_LEVEL_3 );
#endif
if ( hList && hArchive ) {
hALArchive output_archive;
ALArchiveReadDirectory( hArchive, hList );
ALEntryListClearMarks( hList, 0 );
count = ALEntryListSetMarksFromListBox( hList, hDlg, AL_INPUT_LIST );
EditDisplay( hDlg,
AL_DEBUG,
"%d file%s selected\r\n",
count,
(LPSTR) (count == 1 ? " is" : "s are" ) );
/*
* I pass ALArchiveDelete an output archive with a blank name. This tells
* the delet function to to rename it after the deletion is done.
*/
#if defined( ZIP )
output_archive = newALPkArchive( "" );
#else
output_archive = newALGlArchive( "" );
#endif
ALArchiveDelete( hArchive, hList, output_archive );
SetDlgItemText( hDlg,
AL_OUTPUT,
ALStorageGetName( ALArchiveGetStorage( hArchive ) ) );
ReadArchive( hDlg, AL_INPUT, AL_INPUT_LIST );
ReadArchive( hDlg, AL_OUTPUT, AL_OUTPUT_LIST );
}
if ( hArchive ) {
deleteALArchive( hArchive );
hArchive = 0;
}
if ( hList )
deleteALEntryList( hList );
}
/*
* This is the window procedure for the modeless dialog. It is where
* all the commands and so on are handled. Most of the activity in
* the program is invoked as a results of button presses in the
* main dialog windows.
*/
BOOL AL_EXPORT CALLBACK MainDialogProc( HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam )
{
char buf[ 81 ];
AL_UNUSED_PARAMETER( lParam );
switch ( message ) {
/*
* On initialization, we size our framing windows, then set up the window
* caption, followd by setting up the two text boxes in this dialog.
*/
case WM_INITDIALOG :
SizeFramingWindow( hDlg );
wsprintf( buf, "Windows Example 03 <%d>", iInstanceNumber );
SetWindowText( GetParent( hDlg ), buf );
#if defined( ZIP )
wsprintf( buf, "win%02d.zip", iInstanceNumber );
#else
wsprintf( buf, "win%02d.gal", iInstanceNumber );
#endif
SetDlgItemText( hDlg, AL_INPUT, buf );
SetDlgItemText( hDlg, AL_OUTPUT, "" );
return( TRUE );
/*
* WM_COMMAND message are generated for most of the button presses and
* keystrokes that we are concerned with.
*/
case WM_COMMAND :
switch ( wParam ) {
/*
* IDOK indicatest that the user pressed enter. We process that as
* a command to read the input archive if the focus is in the right spot.
*/
case IDOK : /*User has pressed enter*/
if ( GetFocus() == GetDlgItem( hDlg, AL_INPUT ) ) {
ReadArchive( hDlg, AL_INPUT, AL_INPUT_LIST );
ReadArchive( hDlg, AL_OUTPUT, AL_OUTPUT_LIST );
}
break;
/*
* When we are supposed to read the input archive, it usually means there
* is a new filename there. That means we read in the archive using
* the function provided here, then read in the contents of the backup
* archive, if one exists.
*/
case AL_READ_INPUT :
ReadArchive( hDlg, AL_INPUT, AL_INPUT_LIST );
ReadArchive( hDlg, AL_OUTPUT, AL_OUTPUT_LIST );
return TRUE;
/*
* Before we perform the deletion, we have to check to see if another
* deletion is already in progress.
*/
case AL_DELETE :
if ( !hArchive ) {
Delete( hDlg );
if ( hArchive ) {
deleteALArchive( hArchive );
hArchive = 0;
}
}
return TRUE;
/*
* Normally a request to exit is no big deal. But if I am already in the
* process of performing a deletion, I don't want to try and exist.
* In that case, I just set the error status for the archive, so it will
* abort at its own sweet pace.
*/
case AL_EXIT :
case WM_QUIT :
case WM_DESTROY :
if ( hArchive ) {
hALStorage hFile = ALArchiveGetStorage( hArchive );
ALStorageSetError( hFile,
AL_USER_ABORT,
"User pressed abort key" );
FlashWindow( GetParent( hDlg ), 1 );
FlashWindow( GetParent( hDlg ), 0 );
return FALSE;
}
PostMessage( GetParent( hDlg ),WM_DESTROY, 0, 0 );
DestroyWindow( hDlgMain );
hDlgMain = 0;
return TRUE;
/*
* To abort a deletion in progress, we just call the set error routine
* and set the archive's storage object to an error state. The delete
* function will see this as soon as it comes up for air, and it will
* exit prematurely.
*/
case AL_ABORT :
if ( hArchive ) {
hALStorage hFile = ALArchiveGetStorage( hArchive );
ALStorageSetError( hFile,
AL_USER_ABORT,
"User pressed abort key" );
}
return TRUE;
default :
break;
}
break;
}
return FALSE;
}
/*
* The about box dialog procedure is pretty boring. The most interesting
* thing it does is center itself on the screen.
*/
BOOL AL_EXPORT CALLBACK
AboutDialogProc( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
RECT rc;
AL_UNUSED_PARAMETER( lParam );
switch ( message ) {
case WM_INITDIALOG :
GetWindowRect( hDlg, &rc );
SetWindowPos( hDlg,
0,
((GetSystemMetrics(SM_CXSCREEN) - (rc.right - rc.left)) / 2),
((GetSystemMetrics(SM_CYSCREEN) - (rc.bottom - rc.top)) / 2),
0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
break;
case WM_COMMAND :
switch ( wParam ) {
case IDOK :
case WM_QUIT :
case WM_DESTROY :
EndDialog( hDlg, TRUE );
return TRUE;
}
break;
}
return FALSE;
}
/*
* The main window is nothing more than a shell that exists to create
* the dialog box, take menu commands, and process accelerator keys.
*/
LONG AL_EXPORT CALLBACK MainWndProc( HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam )
{
switch ( message ) {
case WM_COMMAND:
switch( wParam ) {
case AL_GOTO_INPUT :
if ( hDlgMain)
SetFocus( GetDlgItem( hDlgMain, AL_INPUT ) );
break;
case AL_ABOUT :
DialogBox( hInstance, "ALAboutDialog", 0, (DLGPROC) AboutDialogProc );
return TRUE;
case AL_EXIT :
PostQuitMessage( 0 );
}
break;
case WM_SETFOCUS:
SetFocus( hDlgMain ); /* insure that the Dialog Box has the focus */
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
}
return DefWindowProc( hWnd, message, wParam, lParam );
}
/*
* WinMain calls the initialization routine for the progress gauge,
* registers our class. It then creates the dialog, and starts the
* message pump.
*/
int PASCAL WinMain( HINSTANCE instance,
HINSTANCE previous_instance,
LPSTR cmd_line,
int nCmdShow )
{
HWND hWnd;
FARPROC lpfn;
MSG msg;
HACCEL hAccel;
AL_UNUSED_PARAMETER( cmd_line );
hInstance = instance;
if ( !ALGaugeInit( instance, previous_instance ) )
return (FALSE);
if ( previous_instance == 0 ) {
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon( hInstance, "ALIcon" );
wc.hCursor = LoadCursor(0,IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = "ALMenu";
wc.lpszClassName = "GreenleafDialogClass";
if ( !RegisterClass( &wc ) )
return FALSE;
iInstanceNumber = 0;
}
#if !defined( AL_FLAT_MODEL )
else {
GetInstanceData( previous_instance,
(PBYTE) &iInstanceNumber,
sizeof iInstanceNumber );
iInstanceNumber++;
}
#endif
hWnd = CreateWindow( "GreenleafDialogClass",
"Greenleaf Example Program",
WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT,
0,
0,
0,
0,
0,
hInstance,
0 );
lpfn = MakeProcInstance( (FARPROC) MainDialogProc, hInstance );
hDlgMain = CreateDialog( hInstance, "ALMainDialog", hWnd, (DLGPROC) lpfn );
ShowWindow( hWnd, nCmdShow );
ShowWindow( hDlgMain, SW_SHOW );
hAccel = LoadAccelerators( hInstance, "ALAccelerator" );
while ( GetMessage ( &msg, 0, 0, 0) ) {
HWND hWndAccel = GetActiveWindow();
if ( !( hWndAccel && TranslateAccelerator( hWndAccel, hAccel, &msg ) ) )
{
if ( hDlgMain == NULL || !IsDialogMessage( hDlgMain,&msg ) ) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
}
return (msg.wParam);
}
/*
* I need this routine for one purpose only. After the dialog is loaded,
* it calls this routine, which adjusts the size of the framing window to
* fit exactly around the dialog box. It also moves it to the center of
* the screen.
*/
void SizeFramingWindow( HWND hDlg )
{
RECT rect;
int x;
int y;
GetWindowRect( hDlg, &rect );
y = GetSystemMetrics( SM_CYSCREEN );
y -= ( rect.bottom - rect.top );
y -= GetSystemMetrics( SM_CYMENU ) + GetSystemMetrics( SM_CYCAPTION );
y /= 2;
x = GetSystemMetrics( SM_CXSCREEN );
x -= rect.right - rect.left;
x /= 2;
SetWindowPos( GetParent( hDlg ), 0, x, y,
rect.right-rect.left,
rect.bottom-rect.top +
GetSystemMetrics( SM_CYMENU ) +
GetSystemMetrics( SM_CYCAPTION ),
SWP_NOZORDER );
}