home *** CD-ROM | disk | FTP | other *** search
- /* Author: Everett Anderson
- CIS: 74630,632
- WWIVlink: 1@15101
-
- Through 1994: BBS at (501) 663-2789 */
-
- /* Written and compiled with TC++ v1.01 */
-
- /* Improvements welcome! */
-
- /* High level, home made video functions for quick select bar programs */
-
- /* I wrote these in the process of exploring C's color text abilities. You
- can tell that I'm a fairly inexperienced programmer in some of the
- functions. Most of them follow more of a human math process than what
- the language is probably capable of. I decided not to make a real
- window structure, as the line[][] array seemed so much more simple.
- Hopefully, I'll come back to these lowly routines and write
- them in C++. */
-
- /* They have their limits. I made the whole selection system based on a
- character, not an integer. This way, it works with both quick switch()
- statements, and also lets the user enter a character rather than using
- the select bar. Currently, no Ctrl or Alt characters are supported. */
-
- /* The whole system is based on a "feed string". I think this is a fairly
- basic way to handle it (dare I say original or intelligent). All you
- have to give my putbox function is the feed string, the top and left
- position on a 1,1,80,25 screen that you wish it to be, and an optional
- header. The width, length, and the selection process are all
- automatically factored in, without you worrying about it. */
-
- /* Here's an example feed string:
-
- sprintf(s,"[1]. Eat\n[2]. Drink\n[3]. Sleep");
-
- The window will be made, a selection bar will be used, and the possible
- options are 1 2 3. The user can use a select bar or just enter the
- highlighted option. Okay, so maybe that's more than you wanted to give
- this lowly function system, but you've downloaded it, so improve on it!
- You can also incorporate the selection anywhere in the phrase, such as
-
- sprintf(s,"[E]at\nDr[i]nk\nSl[e]ep");
- */
-
- /* If you use these, just cut out the main function and link the obj. I'm just
- combining them so that they don't get separated, and so that I don't have
- to write a makefile to upload this. */
-
- /* Final note: There is no system (as there could be in C++) of easily going
- back to the menu that you may have had five windows ago. Putbox() just
- makes and controls one window at a time. I commented out the three
- lines that would let a user abort with an ESC and return an ESC value. */
-
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
-
- int NORMAL = 15; /* Default colors */
- int SELECT = 14;
- int BORDER = 11;
- int BACKGROUND = 1;
- int BAR = 3;
-
- #define UP 72
- #define DOWN 80
- //#define ESC 27
-
- int lines, winright;
- char line[26][85], options[36]; /* Main concept of line[][] and options[] */
-
- /* Prototypes */
- void cls(void);
- void strchar(char *string, const char ch);
- void box(int left, int top, int right, int bottom);
- int get_box_width(char *s);
- void putline(int y, char *s);
- void putbar(int y, int textline);
- void delbar(int y, int textline);
- char getbar(void);
- void putheader(int width, char *header);
- void format_lines(char *s);
- void format_options(void);
- char selection(void);
- void new_window(void);
- char putbox(char *string, int left, int top, char *header);
-
- void cls(void)
- /* Clears the screen, setting a text window */
- {
- textcolor(LIGHTGRAY);
- textbackground(BLACK);
- window(1,1,80,25);
- clrscr();
- }
-
- void strchar(char *string, const char ch)
- /* This acts in the same way as strcat except it combines a string and
- a single character, updating the null at the end. */
- {
- int last;
-
- last=strlen(string);
- string[last]=ch;
- string[last+1]=0;
- }
-
- void box(int left, int top, int right, int bottom)
- /* Only works with lines 1-24. Line 25 scrolls */
- /* This makes a single-line border box */
- {
- int x, y;
-
- window(1, 1, 80, 25);
-
- window(left, top, right, bottom+1);
-
- winright=right-left;
-
- x=1;
- y=1;
-
- gotoxy(1, 1);
- cputs("┌");
- while (x++<right-left)
- cputs("─");
- cputs("┐");
- while (y++<bottom-top-1) {
- gotoxy(1, y);
- cputs("│");
- clreol();
- gotoxy(right-left+1, y);
- cputs("│");
- }
- gotoxy(1, bottom-1);
- cputs("└");
- x=1;
- while (x++<right-left)
- cputs("─");
- cputs("┘");
- window(left, top, right, bottom);
- }
-
- int get_box_width(char *s)
- /* Calculates the necessary box width when given a feed string to work
- from */
- {
- int pos, length, largest;
-
- pos=0;
- length=0;
- largest=0;
-
- while (pos<strlen(s)) {
- if (s[pos]=='[') {
- pos+=3;
- length+=1;
- continue;
- }
- if ((s[pos]=='\n') || (s[pos]=='\r')) {
- if (length>largest) {
- largest=length;
- length=0;
- pos++;
- continue;
- }
- length=0;
- } else
- length++;
- pos++;
- }
- return(largest);
- }
-
- void putline(int y, char *s)
- /* Puts text s on line y in the current text window, using correct color */
- {
- int pos, done, option;
-
- gotoxy(3, y);
- cputs(" ");
- pos=0;
- done=0;
- option=0;
-
- while (!done) {
- if (s[pos]=='[') {
- textcolor(SELECT);
- pos++;
- cprintf("%c",s[pos]);
- pos+=2;
- textcolor(NORMAL);
- option=1;
- }
- if (s[pos]!='\n')
- cprintf("%c",s[pos]);
- pos++;
- if (pos==strlen(s))
- done=1;
- }
- if (option)
- for (done=pos; done<winright-3; done++)
- cputs(" ");
- }
-
- void putbar(int y, int textline)
- /* Positions the select bar at line y over the string line[textline] */
- {
- textbackground(BAR);
- putline(y, line[textline]);
- }
-
- void delbar(int y, int textline)
- /* Removes the select bar from line y, rewriting line[textline] */
- {
- textbackground(BACKGROUND);
- putline(y, line[textline]);
- }
-
- char getbar(void)
- /* Performs all bar movement operations until the selection is made w/
- the enter key or with a possible selection. Returns the correct
- character selection. */
- {
- int y, textline, ch, mods, i;
- char check, *ptr;
-
- y=3;
- textline=0;
- ch=0;
- while (!strchr(line[textline],'[')) {
- y++;
- textline++;
- if (y>lines+2) {
- y=3;
- textline=0;
- break;
- }
- }
-
- while (ch==0) {
- putbar(y, textline);
-
- ch=getch();
-
- if ((ch==UP) || (ch==DOWN)) {
- if (ch==UP) {
- delbar(y, textline);
- y--;
- textline--;
- while (!strchr(line[textline],'[')) {
- y--;
- textline--;
- if (y<3) {
- y=lines+2;
- textline=lines-1;
- }
- }
- }
- if (ch==DOWN) {
- delbar(y, textline);
- y++;
- textline++;
- while (!strchr(line[textline],'[')) {
- y++;
- textline++;
- if (y>lines+2) {
- y=3;
- textline=0;
- }
- }
- }
- if (y<3) {
- y=lines+2;
- textline=lines-1;
- }
- if (y>lines+2) {
- y=3;
- textline=0;
- }
- ch=0;
- continue;
- }
- if ((ch=='\n') || (ch=='\r')) {
- ptr=strchr(line[textline],'[');
- check=line[textline][ptr-line[textline]+1];
- ch=check;
- continue;
- }
- // if (ch==ESC)
- // continue;
- if ((ch > '`') && (ch < '{'))
- ch = ch - 32;
- if (!strchr(options,ch))
- ch=0;
- }
-
- delbar(y, textline);
- return(ch);
- }
-
- void putheader(int width, char *header)
- /* Attempts to put a centered text header on the current box */
- {
- int start, size;
-
- if (header[0]==0)
- return;
- size=strlen(header)+2;
- if (size>width-4)
- return;
- start=(width/2)-(size/2); /* Adding 1 usually corrects some header
- misalignments */
- gotoxy(start, 1);
- textcolor(SELECT);
- cprintf(" %s ",header);
- textcolor(NORMAL);
- }
-
- void format_lines(char *s)
- /* Processes the feed string into line[][] format */
- {
- int pos;
-
- lines=0;
-
- for(pos=0; pos<=strlen(s); pos++)
- if ((s[pos]!='\n') && (s[pos]!='\r'))
- strchar(line[lines],s[pos]);
- else
- lines++;
- lines++;
- for (pos=lines; pos<=25; pos++)
- line[pos][0]=0;
- }
-
- void format_options()
- /* Processes the feed string to get the specified selection options */
- {
- int pos, i;
- char *ptr;
-
- for(pos=0; pos<sizeof(options); pos++)
- options[pos]=0;
-
- pos=0;
-
- for(i=0; i<=lines; i++) {
- ptr=strchr(line[i],'[');
- if (!ptr)
- continue;
- pos=ptr-line[i];
- ptr=NULL;
- pos++;
- strchar(options,line[i][pos]);
- pos=0;
- }
- }
-
- char selection()
- /* Puts the text lines into the window and then calls getbar() */
- {
- char ch;
- int y, done;
-
- y=3;
-
- lines=0;
- done=0;
-
- while (!done) {
- if (line[lines][0]==0) {
- done=1;
- continue;
- }
- putline(y, line[lines]);
- lines++;
- y++;
- }
-
- if (options[0]==0)
- return(0);
- ch=getbar();
- return(ch);
- }
-
- void new_window(void)
- /* Sets all line[][] values to 0 */
- {
- int i;
-
- for (lines=0; lines<=25; lines++)
- for (i=0; i<=sizeof(line[lines]); i++)
- line[lines][i]=0;
- for (i=0; i<=sizeof(options); i++)
- options[i]=0;
-
- lines=0;
- }
-
- char putbox(char *string, int left, int top, char *header)
- /* The main calling function. *String is the feed string, with
- left and top being the desired starting point. The width and
- length of the box are automatically considered and made. The
- header is also positioned (if possible). */
-
- /* Note: This leaves the whole box & text without the selection bar
- afterwards. It also keeps the window() setting at the same
- settings of box(). */
- {
- int width, length;
- char ch;
-
- _setcursortype(_NOCURSOR); // Turns off the visual cursor
- new_window(); // Sets all line[][] to 0
- format_lines(string); // Sets all line[][] from feed string
- width=get_box_width(string); // Gets needed width
- length=top+lines+4; // Gets needed length
- format_options(); // Sets all options from feed string
- window(1,1,80,25); // New text window, full screen
- textcolor(BORDER); // Colors for the box
- textbackground(BACKGROUND); // Colors for the box
- box(left,top,width+8+left-1,length); // Make a single-line box w/ calcs.
- putheader(width+8, header); // Attempt the header
- ch=selection(); // Do complete bar selection
- _setcursortype(_NORMALCURSOR); // Restore cursor
- return(ch); // Return character selected
- }
-
- int main(void)
- {
- char s[200], ch;
-
- cls();
- textcolor(BORDER);
- textbackground(BACKGROUND);
- _setcursortype(_NOCURSOR);
- box(1, 1, 80, 11);
- textcolor(NORMAL);
- putline(3, "This is a quick example of the functions available from this file.");
- putline(4, "These functions were written by Everett Anderson (74630,632).");
- putline(6, "This section is made with box() and putline()'s.");
- textcolor(SELECT);
- putline(8, "Press any key to continue");
- getch();
- _setcursortype(_NORMALCURSOR);
-
- /* The above is somewhat cumbersome, but look below: */
-
- do {
- cls();
- sprintf(s, "[1]. Run your favorite programs\n[2]. Use this system further\n3. Invalid selection\n4. Invalid selection\n[Q]. Quit to DOS");
- ch=putbox(s, 1, 1, "Main Menu");
- cls();
- printf("You selected %c as an option.\n\nPress any key to continue.", ch);
- getch();
- } while (ch!='Q');
-
- return(0);
- }