home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: Product
/
Product.zip
/
sbcspsht.zip
/
SBCSPSHT.C
Wrap
C/C++ Source or Header
|
1997-03-27
|
13KB
|
327 lines
/******************************************************************************/
/* */
/* Description: Extends the VisPro Spreadsheet Object to make it more usable */
/* SubClass the SpreadSheet Class to enhance functionality */
/* OPTIMIZE the Spreadsheet so that the cols width is big enough */
/* for every entry in this col. */
/* EDIT STYLE enhace the posibilities of the edit mode */
/* e.g. start edit by simply typing in the field */
/* */
/* File Name : SBCSPSHT.C */
/* */
/* Author : THOMAS KORFHAGE */
/* */
/* Copyright 1997 (c) Thomas Korfhage, 100271.16@compuserve.com */
/* */
/* Generated using VisPro/C serial number VPC5016481 */
/* */
/* ---------------------------- ADVICE -------------------------------------- */
/*
Put this file into the subprocs folder of your project.
## At Code sections - Extra include files - add this lines:
BOOL SubClassSprShtClass ( VOID );
#define SPRM_OPTIMZE WM_USER+26
#define SPRM_SET_EDTSTYLE WM_USER+27
#define OPTIMZE_ALLCOLS -1
#define OPTIMZE_COLS 1
#define OPTIMZE_HEADER 2
#define SPRS_ENTER_EDITS 0x00000100L /* Enter Key starts editing */
#define SPRS_CHAR_EDITS 0x00000200L /* Any Char-Key starts editing */
#define SPRS_EDITFULLSEL 0x00000400L /* editing starts with full selection of entire text thats an overwrite mode */
#define SPRS_EDITFIXLEN 0x00000800L /* the edited text can not be longer than the text set in the entry field. */
## At Code section - Application initialization - add this call:
SubClassSprShtClass ();
## At Form Event When Opened set the desired Edit Stile with this call:
WinSendDlgItemMsg (hwndDlg, ID_SPREADSHEET, SPRM_SET_EDTSTYLE, MPFROMSHORT (SPRS_ENTER_EDITS), 0);
## To Optimize the whole SpeadSheet Display use this call:
WinSendDlgItemMsg (hwndDlg, ID_SPREADSHEET, SPRM_OPTIMZE, MPFROM2SHORT (OPTIMZE_ALLCOLS, OPTIMZE_COLS|OPTIMZE_HEADER), MPFROM2SHORT (ExtraSpaceX, ExtraSpaceY);
## To Optimize one column of the SpeadSheet use this call:
WinSendDlgItemMsg (hwndDlg, ID_SPREADSHEET, SPRM_OPTIMZE, MPFROM2SHORT (column, OPTIMZE_COLS), MPFROM2SHORT (ExtraSpaceX, ExtraSpaceY);
*/
/* */
/******************************************************************************/
/*INCLUDES*/
#define INCL_WIN
#define INCL_NLS
#define INCL_DOS
#define INCL_GPI
#include <os2.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <VCSPRSHT.H>
/*TYPEDEFS*/
/*MACROS*/
/*DEFINES*/
#define SPRM_OPTIMZE WM_USER+26
#define SPRM_SET_EDTSTYLE WM_USER+27
#define OPTIMZE_ALLCOLS -1
#define OPTIMZE_COLS 1
#define OPTIMZE_HEADER 2
#define SPRS_ENTER_EDITS 0x00000100L
#define SPRS_CHAR_EDITS 0x00000200L
#define SPRS_EDITFULLSEL 0x00000400L
#define SPRS_EDITFIXLEN 0x00000800L
#define SPRS_ALLEDITSTYLES 0x00000F00L
/*FTY*/
HWND FindHwndClassNr (HWND hwnd, int ClassNr);
HWND FindHwndSprShtEF (HWND hwndSpreadSheet);
USHORT GetScrollBarWidth (HWND hwnd);
VOID OptimizeSprShtDisplay (HWND hwnd, SHORT Col, SHORT flOpti, SHORT SpaceX, SHORT SpaceY);
BOOL IsSprShtEFVisible (HWND hwndSpreadSheet);
BOOL SubClassSpreadSheetEF (HWND hwndSpreadSheet);
MRESULT EXPENTRY NewSprShtEFWindowProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
MRESULT EXPENTRY fnwpVPSPRSHT (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2);
BOOL SubClassSprShtClass ( VOID );
/*externe FTY*/
/*externe DATA*/
/*DATA*/
static PFNWP fnwpDefVPSPRSHT = NULL;
static PFNWP pOldSprShtEFWindowProc = NULL;
static ULONG VPSPRSHTExtraOffset;
/*CODE*/
BOOL SubClassSprShtClass ( VOID )
{
HAB hab;
CLASSINFO ClassInfo; /* Class information structure. */
hab = WinInitialize(0);
WinQueryClassInfo (hab, WC_SPRSHT, &ClassInfo);
fnwpDefVPSPRSHT = ClassInfo.pfnWindowProc;
VPSPRSHTExtraOffset = ClassInfo.cbWindowData;
return WinRegisterClass (hab, /* Anchor block handle */
WC_SPRSHT, /* Window class name (Upper)*/
fnwpVPSPRSHT, /* Address window procedure */
ClassInfo.flClassStyle , /* Class style */
ClassInfo.cbWindowData + sizeof (USHORT) /* Extra window words */
);
}
MRESULT EXPENTRY fnwpVPSPRSHT (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
MRESULT mres;
if (msg == WM_CREATE) {
PCREATESTRUCT pCrst;
pCrst = (PCREATESTRUCT) PVOIDFROMMP (mp2);
WinSetWindowUShort (hwnd, VPSPRSHTExtraOffset, (USHORT)pCrst->flStyle&SPRS_ALLEDITSTYLES);
mres = fnwpDefVPSPRSHT (hwnd, msg, mp1, mp2);
SubClassSpreadSheetEF (hwnd);
return mres;
} else if (msg == WM_CHAR) {
USHORT EStyle = WinQueryWindowUShort (hwnd, VPSPRSHTExtraOffset);
USHORT EFAktiv = IsSprShtEFVisible (hwnd);
if ((CHARMSG (&msg)->fs & (KC_KEYUP | KC_CHAR | KC_VIRTUALKEY)) == KC_CHAR && !(CHARMSG (&msg)->vkey == VK_NEWLINE || CHARMSG (&msg)->vkey == VK_ENTER)) {
if (!EFAktiv) {
if ((EStyle & SPRS_CHAR_EDITS) != 0) {
MPARAM SavMp1 = mp1, SavMp2 = mp2;
HWND hwndEF = FindHwndSprShtEF (hwnd);
CHARMSG (&msg)->fs = KC_TOGGLE |KC_LONEKEY|KC_SCANCODE|KC_VIRTUALKEY|KC_KEYUP|KC_ALT;
CHARMSG (&msg)->vkey = VK_NEWLINE;
CHARMSG (&msg)->scancode = 0x1c;
CHARMSG (&msg)->chr = 0x1c00;
mres = fnwpDefVPSPRSHT (hwnd, msg, mp1, mp2);
if (hwndEF)
WinSendMsg (hwndEF, msg, SavMp1, SavMp2);
return mres;
} else
return FALSE;
}
}
if (!(CHARMSG (&msg)->fs & KC_KEYUP) && (CHARMSG (&msg)->vkey == VK_NEWLINE || CHARMSG (&msg)->vkey == VK_ENTER)) {
if (!EFAktiv) {
if ((EStyle & SPRS_ENTER_EDITS) != 0) {
CHARMSG (&msg)->fs = KC_TOGGLE |KC_LONEKEY|KC_SCANCODE|KC_VIRTUALKEY|KC_KEYUP|KC_ALT;
CHARMSG (&msg)->vkey = VK_NEWLINE;
CHARMSG (&msg)->scancode = 0x1c;
CHARMSG (&msg)->chr = 0x1c00;
} else
return FALSE;
}
}
} else if (msg == SPRM_SET_EDTSTYLE) {
USHORT EStyle = WinQueryWindowUShort (hwnd, VPSPRSHTExtraOffset);
EStyle &= ~SPRS_ALLEDITSTYLES;
WinSetWindowUShort (hwnd, VPSPRSHTExtraOffset, (USHORT)(SHORT1FROMMP (mp1)&SPRS_ALLEDITSTYLES)|EStyle);
} else if (msg == SPRM_OPTIMZE) { /* Optimize SpreadSheet Display */
OptimizeSprShtDisplay (hwnd, SHORT1FROMMP (mp1), SHORT2FROMMP (mp1), SHORT1FROMMP (mp2), SHORT2FROMMP (mp2));
return (MRESULT )TRUE;
}
return fnwpDefVPSPRSHT (hwnd, msg, mp1, mp2);
}
MRESULT EXPENTRY NewSprShtEFWindowProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
switch (msg) {
/* Check if Set Text */
case WM_SETWINDOWPARAMS:
{
PWNDPARAMS pwprm = (PWNDPARAMS) PVOIDFROMMP (mp1); /* pointer to WNDPARAMS */
if (pwprm->fsStatus & (WPM_TEXT | WPM_CCHTEXT)) {
HWND hwndParent = WinQueryWindow( hwnd, QW_PARENT );
/* get Extended Style of Spreadsheet */
USHORT EStyle = WinQueryWindowUShort (hwndParent, VPSPRSHTExtraOffset);
long len = strlen (pwprm->pszText);
if (len)
(*pOldSprShtEFWindowProc) (hwnd, EM_SETTEXTLIMIT, MPFROMLONG( (EStyle & SPRS_EDITFIXLEN) == 0 ? 200 : len), 0);
if ((EStyle & SPRS_EDITFULLSEL) != 0) {
MRESULT mres;
mres = (*pOldSprShtEFWindowProc) (hwnd, msg, mp1, mp2);
(*pOldSprShtEFWindowProc) (hwnd, EM_SETSEL, MPFROM2SHORT (0, 255), 0);
return mres;
}
}
}
break;
}
return (*pOldSprShtEFWindowProc) (hwnd, msg, mp1, mp2);
}
BOOL SubClassSpreadSheetEF (HWND hwndSpreadSheet)
{
HWND hwndEF;
hwndEF = FindHwndSprShtEF (hwndSpreadSheet);
if (hwndEF != (HWND)0) {
/* Spreadsheet Entryfield gefunden */
pOldSprShtEFWindowProc = WinSubclassWindow (hwndEF, NewSprShtEFWindowProc);
return pOldSprShtEFWindowProc != 0;
}
return FALSE;
}
BOOL IsSprShtEFVisible (HWND hwndSpreadSheet)
{
HWND hwndEF;
hwndEF = FindHwndSprShtEF (hwndSpreadSheet);
if (hwndEF != (HWND)0)
return WinIsWindowVisible (hwndEF);
return FALSE;
}
VOID OptimizeSprShtDisplay (HWND hwnd, SHORT Col, SHORT flOpti, SHORT SpaceX, SHORT SpaceY)
{
HPS hps = WinGetPS (hwnd);
RECTL rctl;
PSPRSHTCDATA pCtl;
int i, nrrows, nrcols;
if (hps == (HPS)0)
return;
pCtl = (PSPRSHTCDATA )((PBYTE)WinQueryWindowPtr (hwnd, 4) + 0x7C);
if (pCtl == NULL || pCtl->cb != sizeof (SPRSHTCDATA)) {
pCtl = NULL;
nrrows = 100;
nrcols = 100;
} else {
nrrows = pCtl->numCells.cy + 1;
nrcols = pCtl->numCells.cx + 1;
}
memset (&rctl, 0, sizeof (rctl));
if (flOpti & OPTIMZE_HEADER) {
rctl.yTop = rctl.xRight = 10000;
WinDrawText (hps, -1, "ATIqg~?_", &rctl, 0, 0 , DT_LEFT|DT_VCENTER|DT_QUERYEXTENT|DT_TEXTATTRS);
WinSendMsg (hwnd, SSM_SETDEFHEADERHEIGHT, MPFROMLONG (i = rctl.yTop - rctl.yBottom + SpaceY), 0);
}
if (flOpti & OPTIMZE_COLS) {
int col, maxcol, width;
col = maxcol = Col;
if (Col == OPTIMZE_ALLCOLS) {
col = 0;
maxcol = nrcols-1;
}
while (col <= maxcol) {
int row;
PSZ pCell;
int iwidth;
width = 0;
for (row = 0; row < nrrows; ++row) {
pCell = WinSendMsg (hwnd, SSM_GETCELL, MPFROM2SHORT (col, row), 0);
if (pCell == NULL || (row == 0 && atoi (pCell) == col && *pCell != '@')) // Zelle nicht vorhanden
break;
if (*pCell) {
rctl.yTop = rctl.xRight = 10000;
WinDrawText (hps, -1, pCell, &rctl, 0, 0 , DT_LEFT|DT_VCENTER|DT_QUERYEXTENT|DT_TEXTATTRS);
iwidth = rctl.xRight - rctl.xLeft;
if (iwidth > width)
width = iwidth;
}
}
if (row == 0) /* Spalte nicht vorhanden */
break;
if (width > 0) {
if (col == 0)
WinSendMsg (hwnd, SSM_SETDEFROWLABELWIDTH, MPFROMLONG (width + SpaceX), 0);
else
WinSendMsg (hwnd, SSM_SETCOLWIDTH, MPFROMLONG (col), MPFROMLONG (width += SpaceX));
}
++col;
}
if (Col == OPTIMZE_ALLCOLS) {
WinSendMsg (hwnd, SSM_SETDEFCELLWIDTH, MPFROMLONG (width), 0);
}
}
WinReleasePS (hps);
}
USHORT GetScrollBarWidth (HWND hwnd)
{
HWND hwndSB = FindHwndClassNr (hwnd, 8);
if (hwndSB) {
SWP swp;
WinQueryWindowPos( hwndSB, (PSWP)&swp );
return (USHORT)min (swp.cx, swp.cy);
}
return 0;
}
HWND FindHwndSprShtEF (HWND hwndSpreadSheet)
{
return FindHwndClassNr (hwndSpreadSheet, 6);
}
HWND FindHwndClassNr (HWND hwnd, int ClassNr)
{
HWND hwndNext; /* Handle of the window to test */
HENUM Henum; /* Enumeration handle */
SHORT sRetLen; /* returned string length */
char szClassName[32];
Henum = WinBeginEnumWindows (hwnd);
while ((hwndNext = WinGetNextWindow (Henum)) != NULLHANDLE) {
sRetLen = WinQueryClassName (hwndNext, sizeof (szClassName), szClassName);
if (sRetLen >= 2 && szClassName[0] == '#' && atoi (&szClassName[1]) == ClassNr) {
/* 1.Window of standart class with correct nr found */
WinEndEnumWindows (Henum);
return hwndNext;
}
}
WinEndEnumWindows (Henum);
return (HWND)0;
}