home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
list1.zip
/
LIST1.TXT
Wrap
Text File
|
1993-08-23
|
14KB
|
400 lines
LIST BOXES
The LIST1 example program shows a very introductory list box program. This
list box has a LS_MULTIPLESEL style, and communicates with the client area
to have the selections displayed in the window.
***********************************************************
* LIST1.C *
***********************************************************
#define INCL_WIN
#define INCL_GPI
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list1.h"
MRESULT EXPENTRY ClientWndProc ( HWND hwndClient,
ULONG ulMsg,
MPARAM mpParm1,
MPARAM mpParm2 ) ;
MRESULT EXPENTRY DlgProc ( HWND hwndDlg,
ULONG ulMsg,
MPARAM mpParm1,
MPARAM mpParm2 ) ;
#define CLS_CLIENT "MyClass"
int main ( void ) ;
typedef struct {
BOOL fSelectedItems ;
USHORT ausListBoxSel [10] ;
} LISTBOXINFO, *PLISTBOXINFO ;
int main ( )
{
HMQ hmqQueue ;
HAB habAnchor ;
ULONG ulFlags ;
HWND hwndFrame ;
HWND hwndClient ;
QMSG qmMsg ;
/* initialization stuff */
habAnchor = WinInitialize ( 0 ) ;
hmqQueue = WinCreateMsgQueue ( habAnchor, 0 ) ;
WinRegisterClass ( habAnchor,
CLS_CLIENT,
ClientWndProc,
CS_SYNCPAINT,
sizeof ( PVOID )) ;
ulFlags = FCF_TITLEBAR | FCF_SYSMENU |
FCF_SIZEBORDER | FCF_MINMAX | FCF_SHELLPOSITION ;
hwndFrame = WinCreateStdWindow ( HWND_DESKTOP,
WS_VISIBLE,
&ulFlags,
CLS_CLIENT,
"Plain Listbox Example",
0,
( HMODULE ) 0,
IDM_MENU,
&hwndClient ) ;
if ( hwndFrame != NULLHANDLE ) {
while ( WinGetMsg ( habAnchor, &qmMsg, NULLHANDLE, 0, 0 ))
WinDispatchMsg ( habAnchor, &qmMsg ) ;
WinDestroyWindow ( hwndFrame ) ;
} /* endif */
/* clean - up */
WinDestroyMsgQueue ( hmqQueue ) ;
WinTerminate ( habAnchor ) ;
return 0 ;
}
MRESULT EXPENTRY ClientWndProc ( HWND hwndClient, ULONG ulMsg,
MPARAM mpParm1, MPARAM mpParm2 )
{
PLISTBOXINFO pliInfo ;
pliInfo = WinQueryWindowPtr ( hwndClient, 0 ) ;
switch ( ulMsg ) {
case WM_ERASEBACKGROUND:
return ( MRFROMLONG ( TRUE ) ) ;
case WM_CREATE:
pliInfo =
( PLISTBOXINFO ) malloc ( sizeof ( LISTBOXINFO )) ;
if ( pliInfo == ( PLISTBOXINFO ) 0 ) {
WinAlarm ( HWND_DESKTOP,
WA_ERROR ) ;
return (( MRESULT ) TRUE ) ;
} /* endif */
WinSetWindowPtr ( hwndClient,
0,
pliInfo ) ;
pliInfo -> fSelectedItems = FALSE ;
WinPostMsg ( hwndClient,
UM_LOADDLG,
( MPARAM ) 0,
( MPARAM ) 0 ) ;
break ;
case WM_DESTROY:
if ( pliInfo )
free ( pliInfo ) ;
break ;
case WM_PAINT:
{
HPS hpsPresentationSpace ;
RECTL rectInvalidRect, rclPaintRegion ;
USHORT i ;
hpsPresentationSpace = WinBeginPaint (
hwndClient,
NULLHANDLE,
&rectInvalidRect ) ;
rclPaintRegion.xLeft = rectInvalidRect.xLeft ;
rclPaintRegion.xRight = rectInvalidRect.xRight ;
rclPaintRegion.yBottom = rectInvalidRect.yBottom ;
rclPaintRegion.yTop = rectInvalidRect.yTop ;
WinFillRect ( hpsPresentationSpace,
&rectInvalidRect,
CLR_WHITE ) ;
if ( pliInfo -> fSelectedItems == TRUE ) {
rclPaintRegion.yTop -= 15 ;
WinDrawText ( hpsPresentationSpace,
- 1,
"You have selected:",
&rclPaintRegion,
0,
0,
DT_LEFT | DT_TEXTATTRS ) ;
for ( i = 0 ; i < 10 ; i ++ )
if ( pliInfo -> ausListBoxSel [i] ==
TRUE ) {
rclPaintRegion.yTop -= 15 ;
WinDrawText ( hpsPresentationSpace,
- 1,
pszListBoxEntry [i] ,
&rclPaintRegion,
0,
0,
DT_LEFT |
DT_TEXTATTRS ) ;
pliInfo -> ausListBoxSel [i] = FALSE ;
} /* end if selected */
pliInfo -> fSelectedItems = FALSE ;
} /* end if */
WinEndPaint ( hpsPresentationSpace ) ;
break ;
}
case UM_LOADDLG:
WinDlgBox ( hwndClient,
hwndClient,
DlgProc,
( HMODULE ) 0,
IDD_LISTBOX,
NULL ) ;
return ( MRFROMLONG ( TRUE ) ) ;
case WM_SIZE:
WinPostMsg ( hwndClient,
UM_LISTBOXSEL ,
( MPARAM ) 0,
( MPARAM ) 0 ) ;
break ;
case UM_LISTBOXSEL:
{
SHORT sSelect = 0 ;
USHORT usIndex = LIT_FIRST ;
RECTL rclInvalidRegion ;
while ( sSelect != LIT_NONE ) {
sSelect = ( SHORT ) WinSendDlgItemMsg (
WinWindowFromID (
hwndClient, IDD_LISTBOX ) ,
IDL_LISTBOX,
LM_QUERYSELECTION,
MPFROMSHORT ( usIndex ) ,
( MPARAM ) 0 ) ;
pliInfo -> ausListBoxSel [sSelect] = TRUE ;
usIndex = sSelect ;
}
pliInfo -> fSelectedItems = TRUE ;
WinQueryWindowRect ( hwndClient,
&rclInvalidRegion ) ;
rclInvalidRegion.xLeft =
( rclInvalidRegion.xRight -
rclInvalidRegion.xLeft ) / 3 * 2 ;
WinInvalidateRect ( hwndClient,
&rclInvalidRegion, FALSE ) ;
break ;
}
default:
return WinDefWindowProc ( hwndClient,
ulMsg, mpParm1, mpParm2 ) ;
} /* endswitch */
return ( MRFROMLONG ( FALSE )) ;
}
MRESULT EXPENTRY DlgProc ( HWND hwndDlg, ULONG ulMsg,
MPARAM mpParm1, MPARAM mpParm2 )
{
USHORT usNumItems = 10 ;
USHORT i ;
switch ( ulMsg ) {
case WM_INITDLG:
{
HWND hwndListBox ;
hwndListBox = WinWindowFromID ( hwndDlg,
IDL_LISTBOX ) ;
for ( i = 0 ; i < usNumItems ; i ++ )
WinInsertLboxItem ( hwndListBox,
LIT_END,
pszListBoxEntry [i] ) ;
WinSendDlgItemMsg ( hwndDlg,
IDL_LISTBOX,
LM_SELECTITEM,
MPFROMSHORT ( 0 ) ,
MPFROMSHORT ( TRUE )) ;
}
break ;
case WM_COMMAND:
{
SHORT sCommand ;
HWND hwndClient ;
RECTL rclRectangle ;
sCommand = SHORT1FROMMP ( mpParm1 ) ;
switch ( sCommand ) {
case DID_OK:
hwndClient = WinQueryWindow ( hwndDlg,
QW_PARENT ) ;
WinPostMsg ( hwndClient,
UM_LISTBOXSEL,
( MPARAM ) 0,
( MPARAM ) 0 ) ;
return (MRFROMLONG ( TRUE )) ;
case DID_CANCEL:
hwndClient = WinQueryWindow ( hwndDlg,
QW_PARENT ) ;
WinQueryWindowRect ( hwndClient,
&rclRectangle ) ;
WinInvalidateRect ( hwndClient,
&rclRectangle,
FALSE ) ;
WinDismissDlg ( hwndDlg, sCommand ) ;
break ;
} /* end switch sCommand */
break ;
}
default:
return ( WinDefDlgProc ( hwndDlg, ulMsg,
mpParm1, mpParm2 )) ;
} /* endswitch */
return ( MRFROMLONG ( FALSE ) ) ;
}
***********************************************************
* LIST1.RC *
***********************************************************
#include <os2.h>
#include "LIST1.H"
DLGTEMPLATE IDD_LISTBOX LOADONCALL MOVEABLE DISCARDABLE
BEGIN
DIALOG "Listbox Example", IDD_LISTBOX, 12, 6, 170, 107, WS_VISIBLE,
FCF_SYSMENU | FCF_TITLEBAR
PRESPARAMS PP_BACKGROUNDCOLORINDEX, CLR_WHITE
BEGIN
LISTBOX IDL_LISTBOX, 14, 28, 135, 63, LS_MULTIPLESEL
PRESPARAMS PP_BACKGROUNDCOLORINDEX, CLR_WHITE
PUSHBUTTON "OK", DID_OK, 3, 1, 40, 14
PUSHBUTTON "Cancel", DID_CANCEL, 48, 1, 40, 14
END
END
***********************************************************
* LIST1.H *
***********************************************************
#define UM_LOADDLG (WM_USER+1)
#define UM_LISTBOXSEL (WM_USER+2)
#define IDD_LISTBOX 200
#define IDL_LISTBOX 201
#define IDM_MENU 202
CHAR *pszListBoxEntry[] = {
"Cowboys",
"Bengals",
"Oilers",
"Bears",
"Broncos",
"Jets",
"Raiders",
"Rams",
"Giants",
"Redskins" };
***********************************************************
* LIST1.MAK *
***********************************************************
LIST1.EXE: LIST1.OBJ \
LIST1.RES
LINK386 @<<
LIST1
LIST1
LIST1
OS2386
LIST1
<<
RC LIST1.RES LIST1.EXE
LIST1.RES: LIST1.RC \
LIST1.H
RC -r LIST1.RC LIST1.RES
LIST1.OBJ: LIST1.C \
LIST1.H
ICC -C+ -Kb+ -Ss+ LIST1.C
***********************************************************
* LIST1.DEF *
***********************************************************
NAME LISTBOX WINDOWAPI
DESCRIPTION 'Listbox example.
Copyright (c) 1992 by Kathleen Panov.
All rights reserved.'
***********************************************************
In the LIST1 sample program, the dialog box will post a message,
UM_LISTBOXSEL, to the client area wwhen the OK button is pressed.
When the client area receives this message, it queries the list
box to determine which items have been selected. These items are
stored in the user-defined window word area for the client window.
Also, a flag, fSelectedItems is set to indicate items have been
selected.
The UM_LISTBOXSEL is known as a user-defined message. All user-defined
messages must use a message ID between 0x1000 (WM_USER) and 0xBFFF.
These messages give the programmer the opportunity to create a message for
an event that is not a system-defined event.
When the WM_PAINT message is received, the client area is cleared. If the
flag, fSelectedItems, is set, the items in the window word are written
to the client area.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ NOTICE +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The LIST1 example is from The Art of OS/2 C Programming
by Kathleen Panov, Arthur Panov, and Larry Salomon, Jr.,
August 1993, published by QED Publishing Group, ISBN
0-89435-446-9. The example was uploaded by the publisher
with the assistance of the authors for use by Forum
members.