home *** CD-ROM | disk | FTP | other *** search
- /*
- ** DEMO.C - DESQview API demonstration program.
- ** IBM Personal Computer - DESQview C Interface Demo.
- ** Written for Borland Turbo C v1.5.
- */
-
- #include <stdio.h>
- #include <dir.h>
- #include <bios.h>
- #include <io.h>
- #include <string.h>
- #include <stdlib.h>
-
- #include "tvapi.h" /* types and function prototypes */
- #include "tvstream.h"
- #include "dvp.h"
- #include "tvui.h"
-
- #if __TURBOC__ != 1 && __TURBOC__ != 0x0100
- unsigned _heaplen = 8192 ; /* only need 8K heap */
- #endif
-
- /*===============================================================*/
- /* global variables */
- /*===============================================================*/
-
- char old_title[24] ;
- int old_frattr ;
- int orig_rows,orig_cols ;
- int orig_row,orig_col ;
- int org_row, org_col ;
- int orig_lrows,orig_lcols ;
-
- /*===============================================================*/
- /* write an integer out to the current task's main window as a */
- /* decimal value */
- /* We can't use itoa(), because that assumes SS == DS in small */
- /* data models, which winds up clobbering stuff when called from */
- /* inside the notify handler */
- /*===============================================================*/
-
- void write_int(int i)
- {
- static char s[8] ; /* must be in DS, but SS != DS inside notify handler */
- int neg = FALSE ;
- int count = 0 ;
-
- if (i < 0) /* if number negative, then display a minus sign, */
- { /* followed by the additive inverse of the number */
- neg = TRUE ;
- i = -i ;
- } ;
- do /* convert the number to a string of chars */
- { /* by repeatedly dividing by 10 and */
- s[count++] = (i % 10) + '0' ; /* converting the remainder to a character */
- i /= 10 ; /* produces reverse of char string for # */
- }
- while (i > 0) ;
- if (neg)
- TVputchar(NIL,'-',7) ;
- for ( count-- ; count >= 0 ; ) /* print the reverse of the stored string, which */
- TVputchar(NIL,s[count--],7) ; /* results in the number in the correct order */
- }
-
- /*===============================================================*/
- /* wait for a keystroke, ensuring that the physical cursor pos */
- /* is updated first (the logical cursor may move without */
- /* necessarily moving the physical cursor) */
- /*===============================================================*/
-
- int get_key(void)
- {
- TVwin_hcur(NIL) ;
- return bioskey(0) ;
- }
-
- /*===============================================================*/
- /* save as much of the current state as we can, to restore later */
- /*===============================================================*/
-
- void save_state(void)
- {
- TVqry_origin(NIL,&org_row,&org_col) ;
- TVqry_position(NIL,&orig_row,&orig_col) ;
- TVqry_size(NIL,&orig_rows,&orig_cols) ;
- TVqry_lsize(NIL,&orig_lrows,&orig_lcols) ;
- TVqry_title(NIL,old_title,sizeof(old_title)) ;
- old_frattr = TVqry_frattr(NIL) ;
- }
-
- /*===============================================================*/
-
- void standard_window(void)
- {
- TVwin_resize(NIL,13,66) ;
- TVwin_move(NIL,7,5) ;
- TVwin_cursor(NIL,0,0) ;
- TVwin_origin(NIL,0,0) ;
- TVwin_clear(NIL) ;
- }
-
- /*===============================================================*/
-
- void stream_test(void)
- {
- int row, col ;
- char buffer[10] ;
- int i ;
-
- standard_window() ;
- TVwin_title(NIL,"DV-GLUE v" DVGLUE_versionSTR) ;
- TVwin_frattr(NIL,9) ;
- TVwin_redraw(NIL) ;
- TVwin_printf(NIL,"Window #%d, old name was %s, origin (%d,%d)\r\n", DVappnum(), old_title, org_row,org_col) ;
- TVqry_cursor(NIL,&row,&col) ;
- TVwin_printf(NIL,"Cursor position is (%d,%d)\r\n",row,col) ;
- TVqry_position(NIL,&row,&col) ;
- TVwin_printf(NIL,"Window is at (%d,%d) ",row,col) ;
- TVqry_size(NIL,&row,&col) ;
- TVwin_printf(NIL,"and is %dx%d",row,col) ;
- TVqry_lsize(NIL,&row,&col) ;
- TVwin_printf(NIL,"/%dx%d in physical/logical size\r\n",row,col) ;
- TVwin_printf(NIL,"The current output attribute is %d\r\n",TVqry_attr(NIL)) ;
- TVwin_printf(NIL,"The old frame attribute was %d\r\n",old_frattr) ;
- TVwin_printf(NIL,"Control code processing is %s",TVqry_ctrl(NIL)?"on":"off") ;
- TVwin_printf(NIL," and we are \r\n %s when writing\r\n",TVqry_leave(NIL)?
- "leaving attributes untouched":"changing attributes") ;
- TVwin_printf(NIL,"Using %s attributes\r\n",TVqry_logattr(NIL)?"logical":"physical") ;
- TVwin_hcur(NIL) ;
- TVwin_atread(NIL,FALSE) ;
- TVqry_cursor(NIL,&row,&col) ;
- TVwin_cursor(NIL,0,0) ;
- TVwin_nread(NIL,(void *)buffer,sizeof(buffer)) ;
- TVwin_cursor(NIL,row,col) ;
- TVwin_printf(NIL,"First method read '%s' from screen\n",buffer) ;
- TVwin_cursor(NIL,0,0) ;
- TVwin_read(NIL,(void *)buffer,sizeof(buffer)) ;
- TVwin_gotoxy(NIL,col,row+1) ;
- TVwin_printf(NIL,"Second method read '%s' from screen\n",buffer) ;
-
- TVwin_swrite(NIL,"\r\nPress a key to hide window, then another to unhide it....");
- get_key() ;
- TVwin_hide(NIL) ;
- get_key() ;
- TVwin_unhide(NIL) ;
- TVwin_swrite(NIL,"\r\nPress a key to continue..." ) ;
- get_key() ;
-
- /* now scroll the visible portion of the virtual screen left until it is clear */
-
- TVqry_size(NIL,&row,&col) ;
- for (i = 0 ; i < col ; i++)
- {
- TVsleep(5) ;
- TVwin_scroll(NIL,0,0,row,col,SCRL_LEFT) ;
- }
- }
-
- /*===============================================================*/
-
- void timer_test(void)
- {
- OBJECT k, t, w ;
- int left, elapsed ;
-
- standard_window() ;
- w = TVwin_new(NIL,5,46) ;
- TVwin_clear(w) ;
- TVposwin(w,NIL,PW_HCENTER|PW_VCENTER,0,0) ;
- TVwin_cursor(w,0,0) ;
- TVwin_origin(w,0,0) ;
- TVwin_top(w) ;
- TVwin_printf(w,"Press a few keys during the next ten seconds:"
- "\r\n(this doesn't work as expected)") ;
- k = TVkbd_new() ;
- TVkbd_write(k,"a",1,0) ; /* make sure we allocate a buffer */
- TVkbd_open(k,w) ; /* before connecting to window */
- /* (to force one buffer per keystroke) */
- TVkbd_setflags(k,KBD_ACTIVE) ;
- t = TVtimer_new() ;
- TVtimer_begin(t,1000) ; /* 10 seconds */
- while ((left = (int) TVtimer_len(t)) > 0)
- {
- elapsed = (int) TVtimer_elapsed(t) ;
- TVwin_cursor(w,3,0) ;
- TVwin_printf(w,"Time left: %2d.%02d Buffers used: %d\r\n Elapsed: %2d.%02d",
- left/100, left%100, TVkbd_messages(k), elapsed/100,
- elapsed%100) ;
- }
- TVtimer_free(t) ;
- TVkbd_clear(k) ; /* get rid of typeahead */
- TVwin_cursor(w,0,0) ;
- TVwin_printf(w,"%5d buffers used after clearing typeahead \r\n",TVkbd_messages(k));
- while (bioskey(1)) /* brute-force keyboard clear */
- bioskey(0) ;
- TVsleep(100) ;
- TVkbd_free(k) ;
- TVwin_free(w) ;
- TVkbd_setflags(NIL,KBD_ACTIVE) ; /* open and make active again */
- }
-
- /*===============================================================*/
-
- void notify_func(NOTIFY_MSG far *msg)
- {
- static char mail[2] ;
-
- /* at this point, we are on the private stack, not our own */
- /* switching to our prior stack causes a crash */
-
- /* NOTE: can't use TVwin_printf unless we can ensure that it isn't used in */
- /* any routines which might be interrupted for this notification */
- /* also need to be careful with any routines that need SS == DS in small */
- /* data models */
-
- TVwin_swrite(NIL,"\r\n \r") ;
- switch (msg->event)
- {
- case TV_HMOVE:
- case TV_VMOVE:
- TVwin_swrite(NIL,"Hey, don't move me! Now at ") ;
- write_int(msg->msg.movement.row) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.movement.col) ;
- break ;
- case TV_HSIZE:
- case TV_VSIZE:
- TVwin_swrite(NIL,"I liked my previous size! New size ") ;
- write_int(msg->msg.resizing.rows) ;
- TVwin_swrite(NIL," by ") ;
- write_int(msg->msg.resizing.cols) ;
- break ;
- case TV_HSCROLL:
- TVwin_swrite(NIL,"You scrolled me ") ;
- if (msg->msg.scrolling.amount > 0)
- TVwin_swrite(NIL,"right") ;
- else if (msg->msg.scrolling.amount < 0)
- TVwin_swrite(NIL,"left") ;
- else
- TVwin_swrite(NIL,"[done]") ;
- break ;
- case TV_VSCROLL:
- TVwin_swrite(NIL,"You scrolled me ") ;
- if (msg->msg.scrolling.amount > 0)
- TVwin_swrite(NIL,"down") ;
- else if (msg->msg.scrolling.amount < 0)
- TVwin_swrite(NIL,"up") ;
- else
- TVwin_swrite(NIL,"[done]") ;
- break ;
- case TV_CLOSE:
- TVwin_swrite(NIL,"You can't close me yet! Next time WILL close me") ;
- TVwin_cancel(NIL,TV_CLOSE) ;
- break ;
- case TV_HIDE:
- TVwin_swrite(NIL,"You hid me!") ;
- break ;
- case TV_HELP:
- TVwin_swrite(NIL,"Sorry, no help available. Mouse at (") ;
- write_int(msg->msg.help.mouse_row) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.help.mouse_col) ;
- TVwin_swrite(NIL,")") ;
- break ;
- case TV_COLORS:
- TVwin_swrite(NIL,"Was the color changed?") ;
- break ;
- case TV_SWITCHEDTO:
- TVwin_swrite(NIL,"Thanks for making me active.") ;
- break ;
- case TV_SWITCHEDAWAY:
- TVwin_swrite(NIL,"Hey, I want to stay active!") ;
- break ;
- case TV_VIDEOMODE:
- TVwin_swrite(NIL,"New video mode is ") ;
- write_int(msg->msg.video_mode) ;
- break ;
- case TV_SCISSORS_CUT:
- /* while (TVmbx_size(msg->msg.scissors.mailbox) > 0)
- TVmbx_write(msg->msg.scissors.mailbox,0,0,"",0) ; /* keep DV happy */
- TVwin_swrite(NIL,"Sorry, don't know how to cut (") ;
- write_int(msg->msg.scissors.up_left_row) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.scissors.up_left_col) ;
- TVwin_swrite(NIL,") to (") ;
- write_int(msg->msg.scissors.up_left_row + msg->msg.scissors.height - 1) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.scissors.up_left_col + msg->msg.scissors.width - 1) ;
- TVwin_swrite(NIL,")") ;
- break ;
- case TV_SCISSORS_COPY:
- /* while (TVmbx_size(msg->msg.scissors.mailbox) > 0)
- TVmbx_write(msg->msg.scissors.mailbox,0,0,"",0) ; /* keep DV happy */
- TVwin_swrite(NIL,"Sorry, don't know how to copy (") ;
- write_int(msg->msg.scissors.up_left_row) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.scissors.up_left_col) ;
- TVwin_swrite(NIL,") to (") ;
- write_int(msg->msg.scissors.up_left_row + msg->msg.scissors.height - 1) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.scissors.up_left_col + msg->msg.scissors.width - 1) ;
- TVwin_swrite(NIL,")") ;
- break ;
- case TV_SCISSORS_PASTE:
- /* (void) TVreadmail(msg->msg.scissors.mailbox,mail,0) ; /* keep DV happy */
- TVwin_swrite(NIL,"Sorry, don't know how to paste (") ;
- write_int(msg->msg.scissors.up_left_row) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.scissors.up_left_col) ;
- TVwin_swrite(NIL,") to (") ;
- write_int(msg->msg.scissors.up_left_row + msg->msg.scissors.height - 1) ;
- TVwin_swrite(NIL,",") ;
- write_int(msg->msg.scissors.up_left_col + msg->msg.scissors.width - 1) ;
- TVwin_swrite(NIL,")") ;
- break ;
- case TV_MAINMENU:
- TVwin_swrite(NIL,"Main menu popped up") ;
- break ;
- case TV_MENU_END:
- TVwin_swrite(NIL,"Main menu put away") ;
- break ;
- default: TVwin_swrite(NIL,"Unknown event ") ;
- write_int(msg->event) ;
- TVwin_swrite(NIL,"! What happened?") ;
- break ;
- }
- }
-
- void far notify_handler(void)
- {
- static NOTIFY_MSG msg ; /* TVreadmail needs to have msg in DS, which is != SS */
-
- /* at this point, we are on the private stack, not our own */
- /* switching to our prior stack causes a crash */
- while (TVmbx_size(NIL))
- if (TVreadmail(NIL,(char *)&msg,sizeof(msg)) > 0)
- {
- msg.event -= MS_NOTIFY ;
- notify_func((NOTIFY_MSG far *)&msg) ; /* far to ensure proper segment, */
- /* since DS != SS in small models */
- }
- }
-
- void notify_test(void)
- {
- int i ;
- char far *scrnbuf ;
- int size, flag ;
- int row, col ;
-
- standard_window() ;
- TVwin_redraw(NIL) ;
- /* select whether to use TVwin_notify or UIsignal. Change the 0 to 1 to use */
- /* TVwin_notify */
- #if 0
- TVwin_async(NIL,notify_handler) ;
- for (i = TV_HMOVE ; i <= TV_SWITCH ; i++)
- {
- TVwin_allow(NIL,i) ;
- TVwin_notify(NIL,i) ;
- }
- #else
- for (i = TV_HMOVE ; i <= TV_MENU_END ; i++)
- UIsignal(i,NIL,notify_func) ;
- #endif 0
- TVwin_disallow(NIL,TV_QUIT) ;
- TVwin_swrite(NIL,"\r\nJust try to do something to me! (including closing me)\r\n"
- "(Rearrange/Hide after using Scissors may lock up your system)") ;
- TVwin_cursor(NIL,4,0) ;
- TVwin_swrite(NIL,"Press a key when done....\r\n") ;
- TVwin_hcur(NIL) ;
- TVgetbuf(NIL,&scrnbuf,&size,&flag) ;
- while (bioskey(1) == 0)
- {
- switch (scrnbuf[0])
- {
- case '|': scrnbuf[0] = '/' ;
- break ;
- case '/': scrnbuf[0] = '-' ;
- break ;
- case '-': scrnbuf[0] = '\\' ;
- break ;
- default: scrnbuf[0] = '|' ;
- }
- TVqry_cursor(NIL,&row,&col) ;
- if (row > 20)
- TVwin_cursor(NIL,5,0) ;
- TVsleep(20) ;
- }
- bioskey(0) ;
- for (i = TV_HMOVE ; i <= TV_SWITCH ; i++)
- TVwin_cancel(NIL,i) ; /* cancel notification */
- TVwin_allow(NIL,TV_QUIT) ;
- TVwin_clear(NIL) ;
- }
-
- /*===============================================================*/
-
- void pointer_test(void)
- {
- OBJECT p ;
- int i, rows, cols ;
-
- TVwin_title(NIL,"Mouse Demo") ;
- standard_window() ;
- TVwin_move(NIL,1,1) ;
- TVwin_top(NIL) ;
- TVwin_redraw(NIL) ;
- p = TVptr_new() ;
- TVptr_open(p,NIL) ;
- TVptr_icon(p,'+') ; /* this is actually ignored in DV, I assume it does work */
- /* as advertised under TopView */
- TVwin_disallow(NIL,TV_HSIZE) ;
- TVwin_disallow(NIL,TV_VSIZE) ;
- if (TVqry_kmouse()) /* if we are using a keyboard mouse */
- TVapi_kmouse(TRUE) ; /* make sure it is on */
- TVwin_swrite(NIL,"\r\nMoving mouse pointer to screen origin") ;
- TVsleep(100) ;
- TVqry_position(NIL,&rows,&cols) ;
- TVptr_goto(p,-rows,-cols) ;
- TVsleep(100) ;
- TVwin_swrite(NIL,"\r\nMoving mouse pointer to positions relative to window\r\n");
- TVqry_size(NIL,&rows,&cols) ;
- for (i = 0 ; i < rows ; i++)
- {
- TVptr_goto(p,i,i/2+1) ;
- TVsleep(25) ;
- }
- TVwin_allow(NIL,TV_VSIZE) ;
- TVwin_allow(NIL,TV_HSIZE) ;
- TVptr_getscale(p,&rows,&cols) ;
- TVwin_printf(NIL,"\r\nPointer scaling: %d rows and %d columns",rows,cols) ;
- TVsleep(100) ;
- TVptr_free(p) ;
- if (TVqry_kmouse()) /* if we are using a keyboard mouse */
- TVapi_kmouse(FALSE) ; /* make sure it is off */
- TVwin_bottom(NIL) ;
- }
-
- void mouse_drawing_program(void)
- {
- OBJECT p ;
- POINTER_MSG msg ;
- int left_button = FALSE ;
- int right_button = FALSE ;
-
- TVwin_clear(NIL) ;
- TVwin_cursor(NIL,0,0) ;
- TVwin_top(NIL) ;
- TVwin_resize(NIL,23,47) ;
- TVwin_move(NIL,1,10) ;
- TVwin_swrite(NIL,"Drawing Program. Press key to quit\r\n"
- " Left button sets block, right button erases\r\n"
- " Row: 4 Col: 0 Buttons: \r\n"
- "----------------------------------------------\r\n") ;
- TVwin_redraw(NIL) ;
- p = TVptr_new() ;
- TVptr_open(p,NIL) ;
- TVptr_setflags(p,PTR_RELEASE|PTR_NOTTOP) ;
- if (TVqry_kmouse()) /* if we are using a keyboard mouse */
- TVapi_kmouse(TRUE) ; /* make sure it is on */
- TVptr_goto(p,4,0) ;
- msg.row = 4 ; msg.column = 0 ; msg.button_state = 0 ;
- while (bioskey(1) == 0)
- if (TVptr_messages(p) != 0) /* only read pointer if there is input */
- {
- TVptr_read(p,&msg) ;
- if (msg.button_state & 1)
- {
- if (msg.button_state & 0x80)
- left_button = TRUE ;
- else if (msg.button_state & 0x40)
- left_button = FALSE ;
- }
- else if (msg.button_state & 2)
- if (msg.button_state & 0x80)
- right_button = TRUE ;
- else if (msg.button_state & 0x40)
- right_button = FALSE ;
- if (msg.row >= 4 && msg.row < 23 && msg.column >= 0 && msg.column < 46)
- {
- TVwin_cursor(NIL,msg.row,msg.column) ;
- if (left_button)
- TVputchar(NIL,'█',7) ;
- else if (right_button)
- TVputchar(NIL,' ',7) ;
- }
- if (msg.row < 4)
- {
- TVptr_goto(p,4,msg.column) ;
- msg.row = 4 ;
- }
- TVwin_cursor(NIL,2,7) ;
- TVwin_printf(NIL,"Row: %4d Col: %4d Buttons: %c%c", msg.row, msg.column,
- left_button ? 'L' : ' ',right_button ? 'R' : ' ') ;
- }
- bioskey(0) ; /* swallow the keystroke */
- TVptr_close(p) ;
- TVptr_free(p) ;
- if (TVisobj(p))
- TVerrormsg(0,"Error--Pointer wasn't freed!",1,0,0,0) ;
- else
- {
- TVwin_cursor(NIL,22,0) ;
- TVwin_swrite(NIL,"Press a key to continue....") ;
- get_key() ;
- }
- if (TVqry_kmouse()) /* if we are using a keyboard mouse */
- TVapi_kmouse(FALSE) ; /* make sure it is off */
- TVwin_bottom(NIL) ;
- }
-
- /*===============================================================*/
-
- void multiwindow_test(void)
- {
- OBJECT win1 = TVwin_new(NIL,10,15) ;
- OBJECT win2 = TVwin_new(NIL,5,20) ;
- OBJECT win3 = TVwin_new(NIL,8,30) ;
-
- standard_window() ;
- TVwin_redraw(NIL) ;
- TVwin_move(win1,6,60) ;
- TVwin_title(win1,"Window 1") ;
- TVwin_redraw(win1) ;
- TVwin_move(win2,1,52) ;
- TVwin_title(win2,"Window 2") ;
- TVwin_redraw(win2) ;
- TVwin_move(win3,4,40) ;
- TVwin_title(win3,"Window 3") ;
- TVwin_redraw(win3) ;
- TVwin_title(NIL,"Multiwindow Demo") ;
- TVwin_redraw(NIL) ;
- TVsleep(150) ;
- TVwin_reorder(win1,OBJSEG(win3),OBJSEG(win2),OBJSEG(TVmywindow()),OBJSEG(win1),0);
- TVwin_swrite(win1,"Window 1 on top") ;
- TVsleep(150) ;
- TVwin_reorder(win3,OBJSEG(win2),OBJSEG(win1),OBJSEG(TVmywindow()),OBJSEG(win3),0);
- TVwin_swrite(win3,"Window 3 on top") ;
- TVsleep(150) ;
- TVwin_reorder(win2,OBJSEG(win3),OBJSEG(TVmywindow()),OBJSEG(win1),OBJSEG(win2),0);
- TVwin_swrite(win2,"Window 2 on top") ;
- TVsleep(150) ;
- TVwin_reorder(NIL,OBJSEG(win2),OBJSEG(win3),OBJSEG(win1),OBJSEG(TVmywindow()),0);
- TVwin_swrite(NIL,"\r\nWindow 0 on top") ;
- TVsleep(100) ;
- TVwin_top(win3) ;
- TVwin_swrite(win3,"\n\rWindow 3 on top again") ;
- TVsleep(100) ;
- TVwin_topsys(win2) ;
- TVwin_swrite(win2,"\n\rWindow 2 now topmost" ) ;
- TVsleep(100) ;
- TVwin_top(NIL) ;
- TVwin_swrite(NIL,"\n\rBack to window 0" ) ;
- TVsleep(100) ;
- TVwin_swrite(NIL,"\r\nPlease open another window, then press a key....") ;
- TVwin_top(NIL) ; /* need to be topmost to have keyboard attached */
- get_key() ;
- TVwin_swrite(NIL,"\n\rAbout to hide and then unhide all my windows....") ;
- TVsleep(150) ;
- TVapp_hide(NIL) ; /* also puts us in background, */
- TVsleep(100) ;
- TVapp_show(NIL) ;
- TVsleep(10) ;
- TVapp_gofore(NIL) ; /* so have to force ourself into foreground */
- TVwin_swrite(NIL,"\r\nDone!") ;
- TVwin_redraw(NIL) ;
- TVsleep(50) ;
- TVwin_swrite(NIL,"\r\n\nI am about to put myself in the background, and"
- "\r\nthen back into the foreground") ;
- TVsleep(200) ;
- TVapp_goback(NIL) ;
- TVsleep(100) ;
- TVapp_gofore(NIL) ;
- TVwin_swrite(NIL,"\r\nDone!") ;
- TVsleep(50) ;
- TVwin_free(win3) ;
- TVsleep(50) ;
- TVwin_free(win2) ;
- TVsleep(50) ;
- TVwin_free(win1) ;
- TVsleep(50) ;
- TVwin_frattr(NIL,0x70) ; /* reverse video */
- TVwin_top(NIL) ; /* make window active */
- TVwin_redraw(NIL) ; /* make sure physical screen is updated */
- TVwin_swrite(NIL,"\r\nPress a key to continue....") ;
- get_key() ;
- }
-
- /*===============================================================*/
-
- void subtask1(int parent)
- {
- char kbd_input[16] ;
- char mbx_input[32] ;
- int mbx_status ;
- OBJECT mbx ;
-
- do {
- TVkbd_read(NIL,kbd_input,sizeof(kbd_input)) ;
- if (kbd_input[0] != 0x1B)
- TVwin_swrite(NIL,kbd_input) ;
- } while (kbd_input[0] != 0x1B) ;
- TVwin_clear(NIL) ;
- do {
- TVreadmail(NIL,mbx_input, sizeof(mbx_input)) ;
- mbx_status = (int) TVmbx_status(NIL) ;
- if (mbx_input[0] == '\0')
- TVwin_swrite(NIL,"\r\nThat's all, folks!" ) ;
- else
- {
- TVwin_printf(NIL, "\r\nRead: '%s' with status %d",mbx_input,mbx_status) ;
- if (OBJSEG(TVmbx_sender(NIL)) == parent)
- TVwin_swrite(NIL," from parent") ;
- else
- TVwin_swrite(NIL," from a stranger") ;
- }
- } while (mbx_input[0] != '\0') ;
- TVsleep(250) ;
- TVwin_clear(NIL) ;
- TVwin_swrite(NIL,"Test of named mailboxes:\r\n") ;
- mbx = TVmbx_new() ;
- TVmbx_open(mbx);
- TVmbx_name(mbx,"Test Mailbox") ;
- TVmbx_clear(mbx) ;
- TVmbx_write(TVmbxof(MK_OBJ(parent)),FALSE,0,"Ready",5) ;
- TVreadmail(mbx,mbx_input,sizeof(mbx_input)) ;
- TVwin_swrite(NIL,"Got '") ;
- TVwin_swrite(NIL,mbx_input) ;
- TVwin_swrite(NIL,"'") ;
- TVsleep(150) ; /* don't ACK right away */
- TVmbx_write(TVmbxof(MK_OBJ(parent)),FALSE,0,"Done",4) ;
- TVreadmail(NIL,mbx_input,sizeof(mbx_input)) ;
- TVmbx_close(mbx) ;
- TVmbx_free(mbx) ;
- }
-
- void subtask2(int parent)
- {
- int row = 0, col = 0 ;
- int drow, dcol ;
- int rows, cols ;
- int rep ;
-
- (void) parent ; /* get rid of TurboC's warning about unused parameters */
- TVwin_cursor(NIL,0,0) ;
- TVwin_hcur(NIL) ;
- drow = 1 ;
- dcol = 1 ;
- for (rep = 0 ; rep < 30 ; rep++)
- do {
- if (TVmbx_size(NIL) != 0)
- {
- TVmbx_clear(NIL) ; /* discard messages, only used to notify us */
- TVtask_stop(NIL) ; /* and suspend ourselves */
- }
- TVwin_cursor(NIL,row,col) ;
- TVwin_swrite(NIL," ") ;
- TVqry_size(NIL, &rows, &cols) ;
- if (row < 0 || row >= rows-1)
- drow = -drow ;
- if (col < 0 || col >= cols-1)
- dcol = -dcol ;
- row += drow ;
- col += dcol ;
- TVwin_cursor(NIL,row,col) ;
- TVwin_swrite(NIL,"+") ;
- TVsleep(10) ;
- } while (row != 0 || col != 0) ;
- }
-
- void stripCRLF(char *buf)
- {
- char *s = buf + strlen(buf) -1 ;
-
- while (s >= buf && (*s == '\r' || *s == '\n'))
- s-- ;
- s[1] = '\0' ;
- }
-
- DVP_file DVPbuffer ;
- char prog[40], arg1[40] ;
-
- void app_new_test(void)
- {
- char *progname ;
- OBJECT new_task ;
-
- TVwin_clear(NIL) ;
- TVwin_cursor(NIL,0,0) ;
- TVwin_swrite(NIL,"\r\nOK, now let's load a user program.\r\nProgram's name, including extension: ");
- TVwin_hcur(NIL) ;
- fgets(prog,sizeof(prog),stdin) ;
- stripCRLF(prog) ;
- TVwin_swrite(NIL,"\r\nThe program's argument: " ) ;
- TVwin_hcur(NIL) ;
- fgets(arg1,sizeof(arg1),stdin) ;
- stripCRLF(arg1) ;
- progname = searchpath(prog) ;
- if (progname)
- {
- TVwin_printf(NIL,"\nInvoking '%s %s'\n",progname,arg1) ;
- new_task = TVapp_new(NIL,10,1,13,78,TRUE,progname,progname,arg1,NULL) ;
- if (new_task != NIL)
- {
- TVwin_swrite(NIL,"\r\nExperiment with switching between this program and "
- "\r\nthe one you just invoked. Press a key when done") ;
- get_key() ;
- TVtask_free(new_task) ;
- }
- else
- TVerrormsg(0,"Error loading--not enough memory? ESC to continue",1,0,0,0) ;
- }
- else
- {
- TVwin_printf(NIL,"\n\n\nCouldn't find %s!\n", prog ) ;
- TVsleep(150) ;
- }
- }
-
- void app_start_test(void)
- {
- int size ;
- FILE *fp ;
-
- standard_window() ;
- TVwin_title(NIL,"Create new Window") ;
- TVwin_swrite(NIL,"This next test may cause your system to lock up.\r\n"
- "It is known to cause a lockup the next time you\r\n"
- "try to open a window under DV 2.00 (6-16-87)\r\n"
- "\r\n"
- "Type the keys from the open menu, or just Return to\r\n"
- "skip this test\r\n"
- "\r\n"
- "Keys on open menu: ") ;
- TVwin_redraw(NIL) ;
- fgets(arg1,sizeof(arg1),stdin) ;
- if (arg1[0] && arg1[0] != '\n')
- {
- arg1[2] = '\0' ;
- TVwin_swrite(NIL,"DV directory (i.e. C:\\DV): ") ;
- TVwin_hcur(NIL) ;
- fflush(stdin) ;
- fgets(prog,sizeof(prog)-11,stdin) ;
- if (prog[strlen(prog)-1] == '\n')
- prog[strlen(prog)-1] = '\0' ;
- strcat(prog,"\\") ;
- strcat(prog,arg1) ;
- strcat(prog,"-PIF.DVP") ;
- if ((fp = fopen(prog,"rb")) == NULL)
- TVwin_printf(NIL,"\r\nUnable to open %s!\r\n",prog) ;
- else
- {
- size = fread(&DVPbuffer,1,sizeof(DVPbuffer),fp) ;
- if (DVapp_start(&DVPbuffer,size) == 0)
- TVwin_swrite(NIL,"Unable to open!\r\n") ;
- fclose(fp) ;
- }
- TVwin_swrite(NIL,"Press a key....") ;
- get_key() ;
- }
- }
-
- void fork_test(void)
- {
- char key, keys[2] ;
- OBJECT task1, task2 ;
- int count = 0 ;
- char mail[10] ;
-
- standard_window() ;
- TVwin_resize(NIL,8,66) ;
- TVwin_move(NIL,5,11) ;
- TVwin_redraw(NIL) ;
- task1 = TVtask_new(NIL,"Echo keys",1,39,3,40,NULL,1000,subtask1,FALSE) ;
- if (task1 == NIL)
- {
- TVerrormsg(0,"Insufficient memory or other error--Press ESC",1,0,0,0);
- return ;
- }
- task2 = TVtask_new(NIL,"Another task",14,1,10,78,NULL,1000,subtask2,FALSE) ;
- if (task2 == NIL)
- {
- TVerrormsg(0,"Insufficient memory or other error--Press ESC",1,0,0,0) ;
- TVtask_free(task1) ;
- return ;
- }
- TVwin_top(NIL) ; /* put the cursor back in our window */
- TVwin_swrite(NIL,"Type characters to echo in other window, ESC to quit") ;
- do {
- key = get_key() ;
- keys[0] = key ;
- TVkbd_write(TVkbdof(task1),keys,1,0) ;
- } while (key != 0x1B) ;
- TVsleep(25) ; /* allow time for other window to clear */
- TVwin_swrite(NIL,"\r\nType strings to echo in other window, press just RETURN to quit\r\n") ;
- TVwin_hcur(NIL) ;
- do {
- fgets(arg1, sizeof(arg1), stdin) ;
- stripCRLF(arg1) ;
- TVmbx_write(TVmbxof(task1),FALSE,count++,arg1, strlen(arg1)) ;
- } while (strlen(arg1) > 0) ;
-
- TVmbx_clear(NIL) ;
- TVwin_swrite(NIL,"\r\nTesting named mailboxes\r\n") ;
- TVreadmail(NIL,mail,sizeof(mail)) ; /* wait for other task to set up mbx */
- TVwin_swrite(NIL,"sending... " ) ;
- TVmbx_write(TVmbx_find("Test Mailbox"),FALSE,0,"Test successful",15) ;
- TVreadmail(NIL,mail,sizeof(mail)) ; /* wait for ACK */
- TVwin_printf(NIL,"got ACK: '%s'",mail) ;
- TVmbx_write(TVmbxof(task1),FALSE,0,"Quit",4) ;
-
- TVsleep(100) ; /* allow time for other window to close */
- TVwin_swrite(NIL,"\r\n\r\nPress a key to continue....") ;
- get_key() ;
- TVwin_move(NIL,1,1) ;
- TVwin_resize(NIL,9,66) ;
- TVwin_redraw(NIL) ; /* update window's size and position on the screen */
- TVsendmail(TVmbxof(task2),"",0) ; /* would like to do TVtask_stop(task2) */
- /* but DV 2.00 only allows suspending */
- /* the current process, not others */
- TVwin_clear(NIL) ;
- TVwin_cursor(NIL,0,0) ;
- TVwin_swrite(NIL,"\r\nThe 'bouncing' plus sign in the other window should"
- "\r\nhave stopped moving. Press a key....") ;
- get_key() ;
- TVtask_start(task2) ;
- TVwin_swrite(NIL,"\r\nIt should have started moving again. Press a key....") ;
- get_key() ;
- TVtask_free(task1) ;
- TVtask_free(task2) ; /* make sure the subtasks are both terminated */
- app_new_test() ;
- app_start_test() ;
- TVwin_clear(NIL) ;
- }
-
- /*===============================================================*/
-
- void far second_level_interrupt(void)
- {
- /* note that in this routine, DS/ES/SS are unknown */
- TVsound(1000,10) ;
- }
-
- void interrupt_test(void)
- {
- WORD bit = TVgetbit(second_level_interrupt) ;
-
- standard_window() ;
- TVwin_swrite(NIL,"\r\n\r\nPress a key to test second level interrupts") ;
- TVwin_redraw(NIL) ;
- get_key() ;
- TVwin_swrite(NIL,"\r\n\r\nBeeping....") ;
- TVsetbit(bit) ;
- TVsleep(100) ;
- TVfreebit(bit) ;
- TVwin_swrite(NIL,"Done!\r\n") ;
- }
-
- /*===============================================================*/
-
- void objectq_test(void)
- {
- OBJECT p = TVptr_new() ;
- OBJECT obj ;
- char buffer[100] ;
- int i, count ;
- POINTER_MSG msg ;
-
- standard_window() ;
- TVwin_redraw(NIL) ;
- TVobq_open(NIL) ;
- TVptr_open(p,NIL) ;
- TVptr_setflags(p,PTR_NOTFORE|PTR_NOTTOP|PTR_RELEASE) ;
- TVptr_erase(p) ;
- TVobq_add(NIL,p) ;
- TVkbd_clear(NIL) ;
- TVkbd_setflags(NIL,KBD_CONCURRENT) ;
- TVobq_add(NIL,TVmykbd()) ;
- TVwin_clear(NIL) ;
- TVwin_title(NIL,"OBJECTQ demo") ;
- TVwin_cursor(NIL,0,0) ;
- TVwin_origin(NIL,0,0) ;
- TVwin_swrite(NIL,"Waiting for input from either the keyboard or a mouse\r\n") ;
- TVwin_swrite(NIL,"(a keyboard mouse doesn't count as mouse input)") ;
- obj = TVobq_read(NIL) ;
- if (obj == p)
- {
- TVptr_read(obj,&msg) ;
- TVwin_printf(NIL,"Got row: %d col: %d buttons: %02.2x\n",msg.row,
- msg.column,msg.button_state) ;
- }
- else
- {
- count = TVkbd_read(obj,buffer,sizeof(buffer)) ;
- TVwin_swrite(NIL,"\r\nGot: ") ;
- for (i = 0 ; i < count ; i++)
- TVwin_printf(NIL,"%02x ",buffer[i]) ;
- }
- TVobq_remove(NIL,p) ; /* don't take input from mouse anymore */
- TVptr_close(p) ; /* don't need the object, so free it */
- TVptr_free(p) ;
- TVsleep(100) ;
- TVobq_close(NIL) ;
- }
-
- /*===============================================================*/
-
- extern WORD _restore_DS(void) ;
-
- void far kbd_filter(void)
- {
- WORD ax = _AX ; /* store the parameters which were passed in registers */
- WORD bx = _BX ;
- WORD cx = _CX ;
- WORD dx = _DX ;
- WORD ds = _DS ; /* and DS, which we will be clobbering */
-
- _restore_DS() ; /* get back TurboC's data segment (not necessary in this */
- /* trivial filter function, though) */
- /* tell DESQview to ignore non-numeric characters in numeric fields */
- if ((dx & F_NUMBER) != 0 && ax >= ' ' && ax <= 0xFF)
- ax = (ax >= '0' && ax <= '9') ? 0 : 0x100 ;
- else if (ax > 0xFF)
- ax = 0 ; /* we want to use the extended-ASCII keystrokes */
- _DX = dx ;
- _CX = cx ;
- _BX = bx ;
- _DS = ds ;
- _AX = ax ;
- }
-
- /*===============================================================*/
-
- void field_test(void)
- {
- char buffer[50] ;
- BYTE *fields ;
- int status, len ;
-
- TVwin_title(NIL,"Field Demo") ;
- standard_window() ;
- TVwin_swrite(NIL,"Field-mode test\r\n"
- "Cursor should be \031 there\r\n"
- "Enter a number: Enter a string:\r\n"
- "Forced uppercase string: With value: \r\n"
- "Press RETURN when done, or click here: ") ;
- fields = TVfld_build_header(5,F_ALLOWKBD|F_READARRAY,0x70,0x0F) ;
- TVfld_build_entry(fields,1,2,16,2,18,F_FILLIN,F_NEXT|F_NUMBER|F_CLEAR,1,0) ;
- TVfld_build_entry(fields,2,2,35,2,50,F_FILLIN,F_NEXT|F_CLEAR|4,1,0) ;
- TVfld_build_entry(fields,3,3,25,3,30,F_FILLIN,F_NEXT|F_UPPER|F_CLEAR,1,0) ;
- TVfld_build_entry(fields,4,3,44,3,57,F_FILLIN,F_NEXT|F_CLEAR,1,0) ;
- TVfld_build_entry(fields,5,4,39,4,42,F_MENU,'\r',0x70,0) ;
- TVwin_stream(NIL,fields) ;
- TVfld_swrite(NIL,4,"default") ;
- TVfld_attr(NIL,4,0x70) ;
- TVfld_cursor(NIL,1) ; /* position cursor in first field */
- TVwin_redraw(NIL) ;
- TVfld_altmode(NIL,TRUE) ;
- TVkbd_setflags(NIL,KBD_FILTERALL) ;
- TVkbd_setesc(NIL,kbd_filter) ;
- len = TVkbd_read(NIL,buffer,sizeof(buffer)) ;
- status = (int) TVkbd_status(NIL) ;
- TVfld_altmode(NIL,FALSE) ;
- free(fields) ; /* now we can finally free the stream */
- TVwin_gotoxy(NIL,0,6) ;
- TVwin_printf(NIL,"Got '%.*s'\r\nStatus was %d\r\nPress a key....",len,buffer,status) ;
- get_key() ;
- }
-
- /*===============================================================*/
- /*===============================================================*/
-
- void open_window(void)
- {
- OBJECT w ;
- int row, col, rows, cols ;
-
- standard_window() ;
- TVwin_title(NIL,"UIwin_open() test") ;
- TVwin_swrite(NIL,"About to ask you to select a window's size and position" ) ;
- TVwin_redraw(NIL) ;
- TVsleep(100) ;
- w = UIwin_open(NIL,2,2,25,70,5,20) ;
- TVwin_clear(NIL) ;
- TVwin_cursor(NIL,0,0) ;
- if (w)
- {
- TVqry_position(w,&row,&col) ;
- TVqry_size(w,&rows,&cols) ;
- TVwin_cursor(w,0,0) ;
- TVwin_printf(w,"New window's handle: %Fp\r\n", w) ;
- TVwin_printf(w,"Size: %dx%d\r\nPosition: (%d,%d)\r\nPress a key...",rows,cols,row,col) ;
- TVwin_top(w) ;
- TVwin_redraw(w) ;
- get_key() ;
- TVwin_free(w) ;
- }
- else
- {
- TVwin_swrite(NIL,"Cancelled! Press a key...") ;
- get_key() ;
- }
- }
-
- /*===============================================================*/
- /* restore as much of the old state as we've been able to save */
- /*===============================================================*/
-
- void restore_state(void)
- {
- TVwin_lsize(NIL,orig_lrows,orig_lcols) ;
- TVwin_title(NIL,old_title) ;
- TVwin_frattr(NIL,old_frattr) ;
- TVwin_move(NIL,orig_row,orig_col) ;
- TVwin_resize(NIL,orig_rows,orig_cols) ;
- TVwin_origin(NIL,org_row,org_col) ;
- TVwin_redraw(NIL) ; /* make sure screen is updated */
- }
-
- /*===============================================================*/
-
- void do_all(void)
- {
- stream_test() ;
- TVerrormsg(0,"TEST ERROR MESSAGE - press ESC",1,0,0,0);
- notify_test() ;
- timer_test() ;
- pointer_test();
- mouse_drawing_program() ;
- field_test() ;
- fork_test() ;
- interrupt_test() ;
- objectq_test() ;
- multiwindow_test() ;
- open_window() ;
- TVsound(200,5) ;
- TVsound(350,5) ;
- TVsound(500,5) ;
- TVsound(650,5) ;
- }
-
- /*===============================================================*/
-
- MENU_ITEM main_menu_items[] = {
- { M_SPECIAL,"Do everything \021─┘", '\r',0, FALSE },
- { M_SEP, 0, 205, 0, FALSE },
- { M_HCENTER,"Single Tests", 0, 0, FALSE },
- { M_ITEM, "Streams", 'S', 0, FALSE },
- { M_ITEM, "Notification", 'N', 0, FALSE },
- { M_ITEM, "Timers", 'T', 0, FALSE },
- { M_ITEM, "Pointers...", 'P', 0, FALSE },
- { M_ITEM, "Fields", 'F', 0, FALSE },
- { M_ITEM, "Multitasking", 'M', 0, FALSE },
- { M_ITEM, "Interrupts", 'I', 0, FALSE },
- { M_ITEM, "objectQ", 'Q', 0, FALSE },
- { M_ITEM, "Windows", 'W', 0, FALSE },
- { M_ITEM, "Open window", 'O', 0, FALSE },
- { M_ITEM, "soUnd...", 'U', 0, FALSE },
- { M_SEP, 0, 205, 0, FALSE },
- { M_ITEM, "eXit", 'X', 0, FALSE },
- { M_END, 0, 0, 0, FALSE }
- } ;
-
- MENU_OPTIONS main_menu_options = { " Demo ", 0, -1, FALSE, TRUE, TRUE, ' ' } ;
-
- MENU_ITEM sound_menu_items[] = {
- { M_ITEM, "200 Hz tone", '2', 0, FALSE },
- { M_ITEM, "350 Hz tone", '3', '5', FALSE },
- { M_ITEM, "500 Hz tone", '5', 0, FALSE },
- { M_ITEM, "650 Hz tone", '6', '5', FALSE },
- { M_END, 0, 0, 0, FALSE }
- } ;
-
- MENU_OPTIONS sound_menu_options = { " Sound Menu ", 16, -21, FALSE, TRUE, TRUE, ' ' } ;
-
- MENU_ITEM pointer_menu_items[] = {
- { M_ITEM, "Program pointer movement", 'P', 0, FALSE },
- { M_ITEM, "Drawing program", 'D', 0, FALSE },
- { M_END, 0, 0, 0, FALSE }
- } ;
-
- MENU_OPTIONS pointer_menu_options = { "Pointer Menu", 9, -21, FALSE, TRUE, TRUE, ' ' } ;
-
- /*===============================================================*/
-
- void sound_menu(void) ;
- void pointer_menu(void) ;
-
- void do_nothing(void)
- {
- }
-
- void (*menu_functions[])(void) =
- {
- do_all, stream_test, notify_test, timer_test, do_nothing, field_test,
- fork_test, interrupt_test, objectq_test, multiwindow_test, open_window,
- do_nothing, do_nothing
- } ;
-
- /*===============================================================*/
-
- int menu_test(OBJECT window,int status,char *results)
- {
- (void) window ;
- (void) results ;
- if (status == 2 || status == 27) /* right mouse button or ESC */
- return MA_REDO | MA_RESET ;
- else if (results[11] == 'Y') /* is choice so'U'nd menu? */
- {
- sound_menu() ;
- results[11] = 'N' ; /* reset the field */
- return MA_REDO | MA_SELECT ;
- }
- else if (results[4] == 'Y') /* is choice 'P'ointer menu? */
- {
- pointer_menu() ;
- return MA_REDO | MA_RESET ;
- }
- else
- return MA_DONE ;
- }
-
- /*===============================================================*/
-
- void sound_menu(void)
- {
- void *menu = UImenu_build(sound_menu_items,&sound_menu_options) ;
- char results[5] ; /* there are four items */
- int i ;
-
- if (menu == ME_TOOBIG)
- TVwin_printf(NIL, "Menu too big!\n") ;
- else if (menu == ME_NOMEM)
- TVwin_printf(NIL, "Not enough memory for menu!\n") ;
- else if (menu == ME_BADITEM)
- TVwin_printf(NIL, "Error in menu item! You probably forgot the M_END\n" ) ;
- else
- {
- (void) UImenu_show(menu,TRUE,NULL,results) ;
- if (strchr(results,'Y') != NULL)
- TVsound(200+150*(strchr(results,'Y')-results),4) ;
- else /* hit Esc */
- {
- for (i = 0 ; i < 30 ; i++)
- TVsound(300+100*i,3) ;
- TVnosound() ; /* test cancellation of enqueued notes */
- }
- }
- }
-
- /*===============================================================*/
-
- void pointer_menu(void)
- {
- void *menu = UImenu_build(pointer_menu_items,&pointer_menu_options) ;
- char results[3] ;
-
- if (menu == ME_TOOBIG)
- TVwin_printf(NIL, "Menu too big!\n") ;
- else if (menu == ME_NOMEM)
- TVwin_printf(NIL, "Not enough memory for menu!\n") ;
- else if (menu == ME_BADITEM)
- TVwin_printf(NIL, "Error in menu item! You probably forgot the M_END\n" ) ;
- else
- {
- (void) UImenu_show(menu,TRUE,NULL,results) ;
- if (results[0] == 'Y')
- pointer_test() ;
- else if (results[1] == 'Y')
- mouse_drawing_program() ;
- }
- }
-
- /*===============================================================*/
-
- void main_menu(void)
- {
- void *menu = UImenu_build(main_menu_items,&main_menu_options) ;
- char results[14] ; /* there are 13 menu items */
-
- if (menu == ME_TOOBIG)
- TVwin_printf(NIL, "Menu too big!\n" ) ;
- else if (menu == ME_NOMEM)
- TVwin_printf(NIL, "Not enough memory for menu!\n" ) ;
- else if (menu == ME_BADITEM)
- TVwin_printf(NIL, "Error in menu item! You probably forgot the M_END\n" ) ;
- else
- {
- do {
- save_state() ; /* save size/pos/title of window */
- (void) UImenu_show(menu,TRUE,menu_test,results) ;
- if (strchr(results,'Y') != NULL)
- (*menu_functions[strchr(results,'Y') - results])() ;
- restore_state() ; /* restore window to its previous state */
- } while (results[strlen(results)-1] == 'N') ;
- free( menu ) ;
- }
- }
-
- /*===============================================================*/
-
- void main()
- {
- /* initialize everything and return DESQview version number */
- if (DVinit() == 0)
- {
- fputs("This program requires DESQview 2.0 or later\n",stderr);
- exit(1);
- }
- main_menu() ;
- TVwin_clear(NIL) ;
- /* quit DESQview API */
- DVexit() ;
- }
-
- /* End of DEMO.C */
-