home *** CD-ROM | disk | FTP | other *** search
- /*
- demotty.c
-
- jmd 8/06/89
- Fun with tty cmap windows
-
- C-scape 3.2 Example Program
- Copyright (c) 1990 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- This program demonstrates the use of a character map (cmap) window.
- The function cmap_PlotTTY is called to output text to the window in
- TTY fashion. See cmap_PlotTTY in the OWL file CMWINTTY.C for details.
-
- Also demostrated here is attaching a border to the window; setting the
- border title and prompt; enabling mouse features in the border;
- positioning the window; and connecting an auxiliary function to a window
- to process keystrokes via the WINA_GO message.
-
- You may call this program with an ASCII file as an argument and see
- the text roll thru the window:
-
- demotty mytext.txt
-
- Or, you may invoke it with no argument and enter text from the
- keyboard.
-
- Revision History:
- -----------------
- 1/31/90 jmd added use of wingo_func
- 4/01/90 jmd ansi-fied
- 6/05/90 jmd added more commentary on go_funcs
- 6/06/90 jmd changed main to return an int
- 9/14/90 bkd changed to use exit(0) instead of return(0).
- 9/24/90 jmd added use of aux funcs
- 10/19/90 pmcm included ostdlib.h for exit(), added return(1)
- 12/01/90 ted added oak_notused() macro to suppress warnings.
- 12/04/90 ted restored "" includes for C-scape headers (not <> includes).
- 12/6/90 bkd switched order of oak.h and ostdlib.h
- ---------------------------------------------------------------------------
-
- Seds are windows of the sedwin_Class. This is the most common C-scape
- window. There are other window types available in the Oakland Windowing
- Library (OWL), however.
-
- One of these other windows is a cmap window, or cmwin. The forthcoming
- Oakland Advanced Programming Guide is the manual for the OWL and will
- provide a more in-depth look at window classes and objects. Here we
- present the rudiments.
-
- Character map (cmap) windows are of the cmwin_Class. A character map is
- a two-dimensional array of characters and attributes.
-
- You can create a window with win_Open. This routine creates an object of
- class winclass. The starting size and position (in display coordinates) is
- given by the ocbox cboxp. The window is placed in the unemployed window
- list. win_Open returns a pointer to the newly created window or NULL if
- unsuccessful.
-
- win_type win_Open(class_fptr winclass, ocbox *cboxp);
-
-
- You employ the window - painting it to the display - with win_Employ.
-
- boolean win_Employ(win_tye win);
-
-
- The window is fired - removing it from the display without destroying it -
- by calling with win_UnEmploy.
-
- boolean win_UnEmploy(win_type win);
-
-
- You can destroy the window with win_Close. This removes the window from
- the display and deallocates its storage.
-
- void win_Close(win_type win);
-
- You can connect a auxiliary function to a window with obj_SetAux().
- When you call win_Go on a non-sed window, its aux function gets
- sent a WINA_GO message:
-
- obj_SetAux(win, my_aux);
- win_Go(win);
-
- Note that a window is a kind of "object" and that all "objects"
- have aux functions. We must use the function obj_SetAux to attach an
- aux function to the window. sed_SetAux is simply a macro to
- obj_SetAux
-
- When the aux function receives the WINA_GO message it should
- handle input to the window (similar to a C-scape fkey function):
-
- int my_aux(win_type win, int msg, VOID *indata, VOID *outdata)
- {
- boolean quit = FALSE;
- int baton = 0;
-
- switch(msg) {
-
- case WINA_GO:
- while (!quit) {
-
- kb_Read();
- ...
- }
-
- return(baton);
-
- default:
- return(1);
- }
-
- The return value of the aux function is returned from win_Go (just
- as a baton value is returned from sed_Go()).
-
- Some common additional functions enable you to attached a border, set the
- window position, and create a bob from the window so that you may nest it
- within a sed.
-
- boolean win_SetBorder(win_type win, class_fptr bd);
-
- void win_SetPosition(win_type win, int row, int col);
-
-
- bob_type win_CreateBob(win_type win, int depend);
-
-
- depend is either BOB_DEPEND or BOB_INDEPENDENT
- bobs move when the parent is moved
- a dependent bob will be painted with its parent
- an independent bob must be painted separately
-
-
- You can display text in the cmap window by drawing the text string in the
- cmap:
-
- char *cmwin_DrawString(win_type cmwin,
- int row,
- int col,
- char *string,
- byte attr,
- int slen);
-
- Places string within window's cmap, clipping against cmap edges.
- Puts blanks from end of string to slen if string was too short.
- String is not read after a newline or a '\0'.
- Coords are relative to cmap.
- For blanks past the end of the string, attribute 'attr' is used.
- Returns pointer to char after last one put into cmap.
-
-
-
- You initially paint the window to the display with win_Employ, as mentioned
- above.
-
- You can refresh the display of a cmap window with win_Paint.
- win_Paint only paints the window. If you wish to paint the border, any
- window shadow, and the window then call bord_Paint. In general, win_ calls
- affect only the window; bord_ calls affect the window and border. If you
- have nested it by the use of win_CreateBob, call sed_Repaint.
-
- void win_Paint(win_type win);
-
- void bord_Paint(win_type win);
-
-
- cmwins have an associated data store (cmap) that determine what is painted
- to the display. (seds are similar, in that the sed, menu, and field data
- structures tell a sed how to paint itself).
-
-
- When a cmwin is opened the size of its cmap is determined by the dimensions
- of the window. By default, when the cmwin is made re-sized smaller the
- cmap stays anchored in the upper left corner of the window. Its re-
- size flag is FALSE. You may change this behaviour so that the anchor is
- in the lower right corner by setting its resize flag TRUE. These
- functions get and set the resize flag.
-
- boolean cmwin_GetResize(win_type cmwin);
-
- void cmwin_SetResize(win_type cmwin, boolean b);
-
-
- You may consult the source for the cmwin_Class in CMWIN.C.
- The headers CMWINOBJ.H will give you some listing of function macros
- pertinent to this window class.
-
- */
-
- /*
- if you had included cscape.h you wouldn't have to include oak.h,
- odisp.h or bordobj.h.
- */
-
- #include <stdio.h>
- #include <ctype.h> /* for isprint() */
-
- #include "oak.h"
- #include "ostdlib.h" /* for exit() */
- #include "odisp.h"
-
- #include "cmwinobj.h" /* for cmwin stuff */
- #include "bordobj.h" /* for border stuff */
-
- #include "scancode.h" /* for referring to keyboard */
-
- /*** Function prototypes ***/
-
- /* Turbo C++ complains if main is prototyped */
- #ifndef TCP
- int main(int argc, char *argv[]);
- #endif
-
- aux_func (aux_TTY);
-
- #define FEATURE (BD_MOVE | BD_RESIZE | BD_OUTLINE)
- #define BORDER bd_prompt
-
- /* file pointer: global so that gofunc can refer to it */
- FILE *fp = NULL;
-
- int main(int argc, char *argv[])
- {
- win_type win;
- ocbox cbox;
-
- /* open file specified on command line for reading */
-
- if (argc > 1) {
- fp = fopen(argv[1], "r");
- }
-
- /* use the current video mode */
- disp_Init(def_ModeCurrent, FNULL);
-
- /* initialize the mouse */
- hard_InitMouse();
- cmwin_MouseInit();
-
- /* set up for a window with half as many row & cols as display */
- cbox.toprow = 1;
- cbox.leftcol = 1;
- cbox.botrow = (int) (disp_GetHeight() / 2);
- cbox.rightcol = (int) (disp_GetWidth() / 2);
-
- /* create the window and attach the border */
-
- /* Create a cmap window */
- win = win_Open(cmwin_Class, &cbox);
- win_SetPosition(win, (int)(disp_GetHeight() / 4), (int)(disp_GetWidth() / 4));
-
- /* attach a border to the window */
- win_SetBorder(win, BORDER);
- bord_SetFeature(win, FEATURE);
- bord_SendMsg(win, BDM_SETTITLE, "Character map (cmap) TTY window", NULL);
-
- /* attach our auxilary function to our window */
- obj_SetAux(win, aux_TTY);
-
- /* employ the window and paint it to the display */
- win_Employ(win);
-
- /* call go on the window (this passes control to our wingo_func) */
- win_Go(win);
-
- /* close the OWL and the input file, if present */
- disp_Close();
-
- if (fp != NULL) {
- fclose(fp);
- }
-
- exit(0);
- return(0);
- }
-
- int aux_TTY(win_type win, int msg, VOID *indata, VOID *outdata)
- /*
- The auxiliary function for our cmap window.
- This function gets called with a WINA_GO message
- when we call win_Go on the cmap window.
-
- It handles the character input.
- (similar to a C-scape fkey function)
-
- Note: this is the same as a regular C-scape auxiliary function.
- */
- {
- char buffer[501];
- boolean done;
- SIZE_T len;
- int scancode;
-
- oak_notused(indata);
- oak_notused(outdata);
-
- if (msg == WINA_GO) {
-
- if (fp == NULL) {
- done = FALSE; /* read keys if no input file given */
- bord_SendMsg(win, BDM_PROMPT, "Ready for keybd input! ESC=quit", NULL);
- while(!done) {
- switch (scancode = kb_Read()) {
- case ESC:
- done = TRUE; /* ESC to quit */
- buffer[0] = '\0';
- break;
-
- case ENTER: /* CR and LF */
- buffer[0] = '\n';
- buffer[1] = '\0';
- break;
-
- case BACKSPACE: /* back up one col in row */
- buffer[0] = '\b';
- buffer[1] = '\0';
- break;
-
- case UP: /* reverse linefeed */
- buffer[0] = '\v';
- buffer[1] = '\0';
- break;
-
- case RIGHT: /* move left one col in row */
- buffer[0] = ' ';
- buffer[1] = '\0';
- break;
-
- case TAB: /* tab */
- buffer[0] = '\t';
- buffer[1] = '\0';
- break;
-
- case PGDN: /* form feed (the height of win) */
- buffer[0] = '\f';
- buffer[1] = '\0';
- break;
- /* print whats printable */
- default:
- buffer[0] = (isprint(ascii(scancode))) ? (char) ascii(scancode) : '\0';
- buffer[1] = '\0';
- break;
- }
- cmwin_PlotTTY(win, buffer);
- }
- }
-
- else { /* if we have an input file */
-
- bord_SendMsg(win, BDM_PROMPT, "Press a key to begin!", NULL);
- kb_Read(); /* wait for a starting keypress */
- bord_SendMsg(win, BDM_PROMPT, "ESC=quit", NULL);
-
- do { /* then read file 500 at a shot */
- len = fread(buffer, 1, 500, fp);
- buffer[len] = '\0';
- cmwin_PlotTTY(win, buffer); /* and show */
-
- } while (len == 500); /* till done */
-
- kb_Read();
- }
-
- /* the return value is returned via win_Go */
- return(0);
- }
-
- /* all other messages, return 1 */
- return(1);
- }
-
-