home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
-
- PROGRAM: SqlPM.c
- Copyright (C) 1988-1990 Microsoft Corp.
-
- PURPOSE: SqlPM sample Presentation Manager application
-
- FUNCTIONS:
-
- SqlPMInit() - initializes window data and registers window
- SqlPMWndProc() - processes messages
- AboutSQL() - processes messages for "About" dialog box
- SelectSQL() - processes input of author name
- ConnectSQL() - processes input of server name and connects to server
-
- COMMENTS:
-
- Windows can have several copies of your application running at the
- same time. The variable hInst keeps track of which instance this
- application is so that processing will be to the correct window.
-
- You only need to initialize the application once. After it is
- initialized, all other copies of the application will use the same
- window class, and do not need to be separately initialized.
-
- ****************************************************************************/
- #define INCL_PM
- #include "os2.h" /* required for all Windows applications*/
- #define DBMSOS2
- #include "sqlfront.h"
- #include "sqldb.h"
- #include "sqlpm.h"
-
- DBPROCESS *dbproc = (DBPROCESS *)NULL;
- /* dbprocess pointer for dblib connection*/
- HWND errhWnd; /* global window handle for current error*/
- HAB hab;
-
- /****************************************************************************
-
- FUNCTION: main()
-
- PURPOSE: calls initialization function, processes message loop
-
- COMMENTS:
-
- This will initialize the window class if it is the first time this
- application is run. It then creates the window, and processes the
- message loop until a WinPostQuitMessage is received. It exits the
- application by returning the value passed by the WinPostQuitMessage.
-
- ****************************************************************************/
-
- int main(void)
- {
- HWND hWndFrame,hWnd; /* window handle */
- HMQ hmq;
- QMSG qmsg;
- static CHAR *ClientClass = "SQLPM";
- static ULONG flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU |
- FCF_SIZEBORDER | FCF_MINMAX |
- FCF_SHELLPOSITION | FCF_TASKLIST |
- FCF_MENU | FCF_ICON ;
- hab = WinInitialize(0);
- hmq = WinCreateMsgQueue(hab,0);
- WinRegisterClass(hab,
- ClientClass,
- SqlPMWndProc,
- 0L,
- 0);
-
- hWndFrame = WinCreateStdWindow(
- HWND_DESKTOP,
- WS_VISIBLE,
- &flFrameFlags,
- ClientClass,
- "SQLPM Test Program",
- 0L,
- NULL,
- ID_RESOURCE,
- &hWnd);
-
- if (!hWnd) /* Was the window created? */
- return (0);
-
- WinSetWindowText(hWndFrame,"SQL PM Test Program");
-
- errhWnd = hWnd;
- while(WinGetMsg(hab, &qmsg, NULL,0,0))
- WinDispatchMsg(hab,&qmsg);
-
- WinDestroyWindow(hWndFrame); /* Sends WM_PAINT message */
- WinDestroyMsgQueue(hmq);
- WinTerminate(hab);
- return 0;
- }
-
-
- /****************************************************************************
-
- FUNCTION: SqlPMWndProc(HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages
-
- MESSAGES:
-
- WM_SYSCOMMAND - system menu (About dialog box)
- WM_CREATE - create window
- WM_DESTROY - destroy window
- WM_COMMAND - application menus (Connect and Select dialog boxes
-
- COMMENTS:
-
- To process the ID_ABOUTSQL message Call WinDlgBox
- which will create the box according to the information in your
- Sqlpm.rc file and turn control over to the AboutSQL() function.
- This same action will take place for the two menu items Connect and
- Select.
-
-
- ****************************************************************************/
-
- MRESULT EXPENTRY SqlPMWndProc(hWnd, message, wParam, lParam)
- HWND hWnd; /* window handle */
- USHORT message; /* type of message */
- MPARAM wParam; /* additional information */
- MPARAM lParam; /* additional information */
- {
- extern SHORT FAR dbPMMessageHandler();
- extern SHORT FAR dbPMErrorHandler();
-
- switch (message) {
- case WM_CREATE: /* message: window being created */
-
- /* Now make the message and error */
- /* handler instances */
- dberrhandle(dbPMErrorHandler);
- dbmsghandle(dbPMMessageHandler);
- WinSendMsg(hWnd,WM_ERASEBACKGROUND, (MPARAM)NULL,(MPARAM)NULL);
- break;
-
- case WM_ERASEBACKGROUND :
- return TRUE;
- case WM_COMMAND : /* menu selections generate */
- switch(COMMANDMSG(&message)->cmd)
- {
- case IDM_ABOUTSQL :
- WinDlgBox(HWND_DESKTOP, /* current instance */
- hWnd, /* resource to use */
- AboutSQL, /* parent handle */
- NULL,
- ID_ABOUTSQLDLG,
- NULL); /* ConnectSQL() instance address */
- break;
-
- case IDM_CONNECT : /* connect to server */
- WinDlgBox(HWND_DESKTOP, /* current instance */
- hWnd, /* resource to use */
- ConnectSQL, /* parent handle */
- NULL,
- ID_CONNECTDLG,
- NULL); /* ConnectSQL() instance address */
- break;
- case IDM_SELECT : /* select an author */
- WinDlgBox(HWND_DESKTOP, /* current instance */
- hWnd, /* resource to use */
- SelectSQL, /* parent handle */
- NULL,
- ID_SELECTDLG,
- (MPARAM)&hWnd); /* ConnectSQL() instance address */
-
- break;
- /* the WM_COMMAND message */
- }
- break;
- case WM_DBRESULTS : /* a select has been issued */
- SqlPMProcessResults(hWnd); /* process results */
- break;
-
- case WM_DESTROY: /* message: window being destroyed */
- if(dbproc != (DBPROCESS *)NULL)
- dbclose(dbproc);
- break;
-
- default: /* Passes it on if unproccessed */
- return WinDefWindowProc(hWnd,message,wParam,lParam);
- }
- return (0);
- }
- /****************************************************************************
-
- FUNCTION: AboutSQL(HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages for "AboutSQL" dialog box
-
- MESSAGES:
-
- WM_INITDLG - initialize dialog box
- WM_COMMAND - Input received
-
- COMMENTS:
-
- No initialization is needed for this particular dialog box, but TRUE
- must be returned to Windows.
-
- Wait for user to click on "Ok" button, then close the dialog box.
-
- ****************************************************************************/
-
- MRESULT FAR PASCAL AboutSQL(hDlg, message, wParam, lParam)
- HWND hDlg;
- USHORT message;
- MPARAM wParam;
- MPARAM lParam;
- {
- switch (message) {
- case WM_INITDLG :
- WinSetFocus(HWND_DESKTOP, /* set focus to edit control */
- WinWindowFromID(hDlg,DID_OK));
- break;
-
- case WM_COMMAND: /* message: received a command */
- if(SHORT1FROMMP(wParam)==DID_OK) {
- WinDismissDlg(hDlg, NULL); /* Exits the dialog box */
- return (0L);
- }
- break;
- }
- return (WinDefDlgProc(hDlg, message, wParam, lParam));
- }
-
-
- /****************************************************************************
-
- FUNCTION: SelectSQL(HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages for "SelectSQL" dialog box
-
- MESSAGES:
-
- WM_INITDLG - initialize dialog box
- WM_COMMAND - Input received
-
- COMMENTS:
-
- No initialization is needed for this particular dialog box, but TRUE
- must be returned to Windows.
-
- Let user input into edit control the name of an author (the select
- IS case sensitive). When user presses OK, format the select statement
- then send it to the server and execute it via dbsqlexec(). If the
- dbsqlexec() SUCCEED's post a WM_DBRESULTS message so the results
- may be retrieved and processed.
-
- Wait for user to click on "Ok" button, then close the dialog box.
-
- ****************************************************************************/
- MRESULT FAR PASCAL SelectSQL(hDlg, message, wParam, lParam)
- HWND hDlg;
- USHORT message;
- MPARAM wParam;
- MPARAM lParam;
- {
- char szSelectAuthor[41]; /* string for authors name */
- char szServerMess[45]; /* string for server response */
- char szAName[40]; /* format string for author */
- static HWND parhWnd;
- switch (message) {
- case WM_INITDLG: /* message: initialize dialog box */
- WinSendDlgItemMsg(hDlg, /* limit input to 40 characters */
- AUTHORNAME,EM_SETTEXTLIMIT,(MPARAM)40,(MPARAM)0);
- parhWnd = *((PHWND)(PVOIDFROMMP(lParam)));
- WinSetFocus(HWND_DESKTOP, /* set focus to edit control */
- WinWindowFromID(hDlg,AUTHORNAME));
- return (TRUE);
- case WM_COMMAND: /* message: received a command */
- switch(SHORT1FROMMP(wParam))
- {
- case DID_OK : /* "OK" box selected? */
- *szSelectAuthor = NULL; /* Null author */
-
- WinQueryDlgItemText(hDlg,AUTHORNAME, /* get input name */
- MAX_ANAME,(CHAR FAR *)szSelectAuthor);
-
- if(dbproc == (DBPROCESS *)NULL) /* if not a valid process*/
- {
- /* No server to query */
- WinMessageBox(HWND_DESKTOP,hDlg,
- "No SQL Server Connected to Query",
- "SQL Test",0, MB_ICONHAND | MB_OK);
- }
- else if(*szSelectAuthor != NULL) /* if a name exists */
- {
- dbcmd(dbproc,
- (CHAR FAR *)"select au_id, au_lname,"
- "au_fname, phone, address, city, state, zip");
- dbcmd(dbproc, (CHAR FAR *)" from pubs..authors");
- dbcmd(dbproc, (CHAR FAR *)" where au_lname = ");
- sprintf(szAName,"'%s' ",szSelectAuthor);
- dbcmd(dbproc,(CHAR FAR *)szAName);
- if(dbsqlexec(dbproc) == FAIL)
- {
- sprintf(szServerMess, /* error, not in db */
- "%s not found in database pubs",
- szSelectAuthor);
- WinMessageBox(HWND_DESKTOP,hDlg,
- (CHAR FAR *)szServerMess,(CHAR FAR *)"SQL Test",
- 0,MB_ICONHAND | MB_OK);
- }
- else /* query SUCCEEDed so */
- { /* post message to process results */
- WinPostMsg(parhWnd,WM_DBRESULTS,(MPARAM)0,(MPARAM)0);
- }
- }
- WinDismissDlg(hDlg, NULL); /* Exits the dialog box */
- return (0L);
- break;
- case DID_CANCEL :
- WinDismissDlg(hDlg, NULL); /* cancelled select */
- return(0L);
- break;
- }
- break;
- }
- return (WinDefDlgProc(hDlg, message, wParam, lParam));
- }
- /****************************************************************************
-
- FUNCTION: ConnectSQL(HWND, unsigned, WORD, LONG)
-
- PURPOSE: Processes messages for "Connect" dialog box
-
- MESSAGES:
-
- WM_INITDLG - initialize dialog box
- WM_COMMAND - Input received
-
- COMMENTS:
-
- No initialization is needed for this particular dialog box.
-
- Wait for user to click on "Ok" button, then close the dialog box.
-
- ****************************************************************************/
-
- MRESULT FAR PASCAL ConnectSQL(hDlg, message, wParam, lParam)
- HWND hDlg;
- USHORT message;
- MPARAM wParam;
- MPARAM lParam;
- {
- char szSQLServer[31];
- LOGINREC *LoginRec;
- SHORT len;
-
- *szSQLServer = NULL;
- switch (message) {
- case WM_INITDLG: /* message: initialize dialog box*/
- WinSendDlgItemMsg(hDlg, /* limit input to 30 characters */
- SQL_SERVER,EM_SETTEXTLIMIT,(MPARAM)30,(MPARAM)0);
- WinSetFocus(HWND_DESKTOP, /* set focus to edit control */
- WinWindowFromID(hDlg,SQL_SERVER));
- return (TRUE);
-
- case WM_COMMAND: /* message: received a command*/
- switch(SHORT1FROMMP(wParam))
- {
- case DID_OK : /* "OK" box selected? */
- len = WinQueryDlgItemText(hDlg,SQL_SERVER,
- MAX_SERVERNAME, (CHAR FAR *)szSQLServer); /* get Server name */
- szSQLServer[len] = NULL;
- if(dbproc != (DBPROCESS *)NULL) /* if an active */
- dbclose(dbproc); /* proc, close it */
- if(*szSQLServer != NULL) /* was something input */
- {
- if((LoginRec = dblogin()) != (LOGINREC *)NULL) /* get loginrec */
- {
- DBSETLUSER(LoginRec,"sa"); /* set user */
- /* now open the connection to server */
- if((dbproc = dbopen(LoginRec,szSQLServer))
- == (DBPROCESS *)NULL)
- {
- /* if NULL couldn't connect */
- dbfreelogin(LoginRec);
- }
- else /* got connect so use the pubs database */
- {
- dbuse(dbproc,"pubs");
- dbfreelogin(LoginRec);
- }
- }
- else /* memory allocation problem */
- WinMessageBox(HWND_DESKTOP,errhWnd,
- "Could not allocate Login Record","DBLIB Error",
- 0, MB_ICONHAND | MB_OK);
- }
- WinDismissDlg(hDlg, NULL); /* Exits the dialog box */
- return (0L);
- case DID_CANCEL :
- WinDismissDlg(hDlg, NULL);
- return(0L);
- }
- break;
- }
- return (WinDefDlgProc(hDlg, message, wParam, lParam));
- }
-
- /****************************************************************************
-
-
-
- /****************************************************************************
-
- FUNCTION: CheckForScroll(HWND, int, int, int)
-
- PURPOSE: Check if next output line will be out of client area
-
- PARAMETERS: hWnd - Handle to the window.
- CurrentPosition - Current y coordinate for the line of
- text just written to the client area.
- Spacing - The height of the line (including the space
- separating lines) of the text just written.
- Length - The length of the line just written in device
- units.
-
- RETURN: Returns the Y coordinate for the next line of text.
-
- COMMENTS:
-
- Will determine if the next line of text will be out of the client
- area. If so will scroll the window for the next line. Also validates
- the current line of text so that a WM_PAINT will not clear it.
-
- ****************************************************************************/
- SHORT CheckForScroll(hWnd,CurrentPosition,Spacing, Length)
- HWND hWnd;
- LONG CurrentPosition;
- LONG Spacing;
- LONG Length;
- {
- RECTL rect; /* RECT structure for validation */
- rect.yTop = CurrentPosition; /* top of last line of text */
- rect.yBottom = CurrentPosition-Spacing-1; /* bottom of last line */
- rect.xLeft = 1; /* left most column of line */
- rect.xRight = Length+1; /* right most column of line */
- WinValidateRect(hWnd,(PRECTL)&rect,FALSE); /* validate line so that it is */
- /* not blanked on next paint */
-
- WinQueryWindowRect(hWnd,(PRECTL)&rect); /* get rect for current client */
- if(CurrentPosition - (Spacing*2) < rect.yBottom) /* will line fit */
- {
- /* if not scroll window and */
- /* update client window */
- WinScrollWindow(hWnd,0,+(Spacing+1),NULL,NULL,NULL,NULL,0);
- WinUpdateWindow(hWnd);
- return(CurrentPosition);
- }
- return(CurrentPosition-Spacing);
- }
-
- /****************************************************************************
-
- FUNCTION: SqlPMProcessResults(HWND)
-
- PURPOSE: If a valid dbprocess is present process all results from pending
- select statement, output each field to client area. Whenever
- a new line is written to client area it is checked to see if
- the client area needs to be scrolled.
-
- PARAMETERS: hWnd - Handle to the window.
-
- RETURN: Nothing
-
- COMMENTS:
- This function will bind the fields in the select statement
- to local variables, format an output string then
- write that string to the client area via GpiCharStringAt.
- It is called by the main message processing loop
- SqlPMWndProc via the message WM_DBRESULTS.
-
- ****************************************************************************/
- void SqlPMProcessResults(hWnd)
- HWND hWnd;
- {
- HPS hPS; /* display context */
- FONTMETRICS tm; /* text metric structure */
- char szId[12]; /* Author ID for binding */
- char szLastName[41]; /* Author last name for binding */
- char szFirstName[21]; /* Author first name for binding */
- char szPhone[13]; /* Author phone for binding */
- char szAddress[41]; /* Author address for binding */
- char szCity[21]; /* Author city for binding */
- char szState[3]; /* Author state for binding */
- char szZip[6]; /* Author zipcode for binding */
- char szOutputString[81]; /* general output string */
- RETCODE result_code; /* results code from dbresults */
- LONG Y; /* Y coordinate for text output */
- LONG Spacing; /* Spacing between lines */
- POINTL pointl;
- RECTL rect;
-
- errhWnd = hWnd;
- WinQueryWindowRect(hWnd,(PRECTL)&rect); /* get rect for current client */
- hPS = WinGetPS(hWnd);
- GpiQueryFontMetrics(hPS, (LONG)sizeof(FONTMETRICS), (PFONTMETRICS)&tm);
- Spacing = tm.lExternalLeading+tm.lMaxAscender;
- pointl.x = 1;
- Y = pointl.y = rect.yTop - (1 + Spacing);
- if(dbproc == (DBPROCESS *)NULL) /* if process null, no results */
- {
- WinReleasePS(hPS); /* free resources and return */
- return;
- }
- WinCls(hWnd);
- while(((result_code = dbresults(dbproc)) != NO_MORE_RESULTS) && result_code != FAIL)
- {
- if(result_code == SUCCEED) /* if results ready */
- {
- /* Bind all data of interest */
- dbbind(dbproc,1,NTBSTRINGBIND, 12L, szId);
- dbbind(dbproc,2,NTBSTRINGBIND, 41L, szLastName);
- dbbind(dbproc,3,NTBSTRINGBIND, 21L, szFirstName);
- dbbind(dbproc,4,NTBSTRINGBIND, 13L, szPhone);
- dbbind(dbproc,5,NTBSTRINGBIND, 41L, szAddress);
- dbbind(dbproc,6,NTBSTRINGBIND, 21L, szCity);
- dbbind(dbproc,7,NTBSTRINGBIND, 3L, szState);
- dbbind(dbproc,8,NTBSTRINGBIND, 6L, szZip);
- while(dbnextrow(dbproc) != NO_MORE_ROWS) /* get all rows */
- {
- /* here we format each field and write it to client */
- /* area checking to see if the client area needs to */
- /* be scrolled after each line is written */
-
- sprintf(szOutputString,"Author ID: %s",szId);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
- sprintf(szOutputString,"Last Name: %s",szLastName);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
-
- sprintf(szOutputString,"First Name: %s",szFirstName);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
- sprintf(szOutputString,"Address: %s",szAddress);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
- sprintf(szOutputString,"City: %s",szCity);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
- sprintf(szOutputString,"State: %s",szState);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
- sprintf(szOutputString,"ZipCode: %s",szZip);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
- sprintf(szOutputString,"Telephone: %s",szPhone);
- GpiCharStringAt(hPS,(PPOINTL)&pointl,(LONG)strlen(szOutputString),szOutputString);
- Y = CheckForScroll(hWnd,Y,Spacing,(LONG)strlen(szOutputString) * tm.lEmInc);
- pointl.y = Y;
-
- Y = CheckForScroll(hWnd,Y,Spacing,0L); /* insert blank line */
- pointl.y = Y;
- }
- }
- }
- WinReleasePS(hPS); /* free resource */
- return;
- }
- /* WinCls() - clears the displayed screen */
- void WinCls(hWnd)
- HWND hWnd;
- {
- HPS hPS;
-
- hPS = WinGetPS(hWnd);
- GpiErase(hPS);
- WinReleasePS(hPS);
- }
-
- /****************************************************************************
-
- FUNCTION: dbPMMessageHandler(DBPROCESS *, DBINT, DBSMALLINT, DBSMALLINT,
- CHAR *)
-
- PURPOSE: When the Data Server returns a message to dblib this function
- will be called to process that message. You must return 0 to
- dblib.
-
- RETURN: Return 0
-
- COMMENTS:
-
- ****************************************************************************/
-
- SHORT FAR dbPMMessageHandler(dbproc, msgno, msgstate, severity, msgtext)
- DBPROCESS *dbproc;
- DBINT msgno;
- DBSMALLINT msgstate;
- DBSMALLINT severity;
- CHAR *msgtext;
- {
- WinMessageBox(HWND_DESKTOP,errhWnd,
- msgtext,"SQL DataServer Message",0,MB_OK);
- return(0);
- }
-
- /****************************************************************************
-
- FUNCTION: dbPMErrorHandler(DBPROCESS *, int, int, int, CHAR *, CHAR *)
-
- PURPOSE: When dblib returns an error message to the application this
- function will be called to process that error. You must return
- either INT_CANCEL, INT_CONTINUE, or INT_EXIT to dblib.
-
- RETURN: Return continuation code.
-
- COMMENTS:
-
- ****************************************************************************/
-
- SHORT FAR dbPMErrorHandler(dbproc, severity, errno, oserr, dberrstr, oserrstr)
- DBPROCESS *dbproc;
- int severity;
- int errno;
- int oserr;
- CHAR *dberrstr;
- CHAR *oserrstr;
- {
- WinMessageBox(HWND_DESKTOP,errhWnd,
- dberrstr,"DB-LIBRARY error",0, MB_ICONHAND | MB_OK);
-
- if (oserr != -1) /* os error */
- {
- WinMessageBox(HWND_DESKTOP,errhWnd,
- oserrstr,"Operating-System error",0,MB_ICONHAND | MB_OK);
- }
-
- return(INT_CANCEL); /* cancel command */
- }
-