home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C!T ROM 2
/
ctrom_ii_b.zip
/
ctrom_ii_b
/
PROGRAM
/
FOXPRO
/
VEDIT060
/
SOURCE
/
VEDIT8.PRG
< prev
next >
Wrap
Text File
|
1992-04-01
|
15KB
|
490 lines
*******************************************************************************
* *
* VEdit_Text_Editor() *
* *
* (c) 1992 - Jayson R. Minard *
* *
* This FUNCTION loads a VLIST with lines of text from a file, then calls *
* the internal editor routine. The editor routine is also written in *
* FORCE. *
* *
*******************************************************************************
#INCLUDE KEYS.HDR
#INCLUDE vlist.hdr
#INCLUDE vmouse.hdr
#INCLUDE vedit.hdr
#INCLUDE STRING.HDR
#INCLUDE FILEIO.HDR
#INCLUDE SYSTEM.HDR
#INCLUDE IO.HDR
VARDEF EXTERN
BYTE __color_std, __color_enhcd
ENDDEF
*******************************************************************************
* return codes *
* 0 - no errors *
* 2 - file doesn't exist *
* 3 - error reading file *
* 4 - error editting file *
* 5 - error allocating memory *
* 6 - cannot open file to save *
*******************************************************************************
FUNCTION UINT VEdit_Text_Editor
PARAMETERS CHAR filename,;
VALUE INT upper_row,;
VALUE INT upper_col,;
VALUE INT lower_row,;
VALUE INT lower_col,;
VALUE UINT start_line,;
VALUE UINT start_col,;
VALUE UINT right_margin,;
VALUE UINT hard_margin,;
VALUE BYTE m_text_color,;
VALUE LOGICAL word_wrap,;
VALUE LOGICAL disp_only,;
VALUE LOGICAL scroll_bar,;
VALUE LOGICAL stat_line,;
VALUE UINT stat_row,;
VALUE UINT stat_col,;
VALUE LOGICAL is_mouse,;
VALUE LOGICAL a_embedded,;
VALUE UINT tab_width,;
VALUE UINT undelete_lines,;
UNTYPED key_handler
VARDEF
LONG handle,;
scrap,;
undelete
FILE input_file
CHAR temp_str
CHAR( 128 ) new_filename, temp_filename
UINT ret_val
UINT top_element,;
left_col,;
temp_loc
INT open_type
LOGICAL going
LOGICAL need_wrap
LOGICAL go_ahead
UINT dum
BYTE old_std
ENDDEF
need_wrap = .F.
go_ahead = .F.
new_filename = filename
*- initialize main text file list, use 200 width so that there is work
* room for word wrap. If word wrap is never used, or the right margin
* is limited to a lower number, then this can be lowered.
*
dum = hard_margin + ( hard_margin/2 )
IF dum > 245
dum = 245
ENDIF
handle = Vlist_Init( dum )
IF handle = 0
RETURN 5
ENDIF
*- initialize scrap text buffers
scrap = Vlist_Init( dum )
IF scrap = 0
vlist_clear( handle )
RETURN 5
ENDIF
*- initialize undelete text buffers
undelete = Vlist_Init( dum )
IF undelete = 0
vlist_clear( handle )
vlist_clear( scrap )
RETURN 5
ENDIF
IF .NOT. EXIST( filename )
ELSE
*- open file
IF .NOT. F_OPEN( input_file, filename, &f_read )
Vlist_Clear( handle )
Vlist_Clear( scrap )
Vlist_Clear( undelete )
RETURN 3
ENDIF
SAVE_AREA( upper_row, upper_col, upper_row+3, upper_col+14 )
FILL( upper_row, upper_col, upper_row+3, upper_col+14,;
&double_box, " ", __color_bar, __color_tab, 6 )
old_std = __color_std
__color_std = __color_tab
CURSOR_ON()
@upper_row+1, upper_col+2 SAY "Reading..."
*- read file
DO WHILE .NOT. F_EOF( input_file )
IF .NOT. F_GETLN( input_file, temp_str )
__color_std = old_std
RESTORE_AREA()
Vlist_Clear( handle )
Vlist_Clear( scrap )
VList_Clear( undelete )
F_CLOSE( input_file )
RETURN 3
ENDIF
temp_str = LEFT( RTRIM( temp_str ), hard_margin )
temp_loc = AT( CHR( 9 ), temp_str )
DO WHILE temp_loc > 0
temp_str = LEFT( temp_str, temp_loc - 1 ) + SPACE( tab_width ) +;
SUBSTR( temp_str, temp_loc + 1, 250 )
temp_loc = AT( CHR( 9 ), temp_str )
ENDDO
IF LEN( temp_str ) > right_margin
need_wrap = .T.
ENDIF
*- pad the string out to the far right margin
temp_str = temp_str + SPACE( hard_margin - LEN( temp_str ) )
*- add one line of text to VLIST
IF .NOT. Vlist_Add( handle, temp_str )
__color_std = old_std
RESTORE_AREA()
Vlist_Clear( handle )
Vlist_Clear( scrap )
VList_Clear( undelete )
F_CLOSE( input_file )
RETURN 5
ENDIF
ENDDO
*- close file
F_CLOSE( input_file )
__color_std = old_std
RESTORE_AREA()
ENDIF
*- lets check the parameters to make sure they are valid
IF start_line > Vlist_Max( handle )
start_line = Vlist_Max( handle )
ENDIF
IF start_col = 0
start_col = 1
ENDIF
IF start_col > hard_margin
start_col = hard_margin
ENDIF
IF start_line = 0
start_line = 1
ENDIF
IF right_margin < 10
right_margin = 10
ENDIF
IF right_margin > hard_margin
right_margin = hard_margin
ENDIF
top_element = 0
left_col = 0
going = .T.
*- if word wrap on, and at least one line in the file was longer than
* the right margin, then word wrap the whole file.
IF need_wrap
IF word_wrap
SAVE_AREA( upper_row, upper_col, upper_row+3, upper_col+17 )
FILL( upper_row, upper_col, upper_row+3, upper_col+17,;
&double_box, " ", __color_bar, __color_tab, 6 )
old_std = __color_std
__color_std = __color_tab
CURSOR_ON()
@upper_row+1, upper_col+2 SAY "Formatting..."
VEdit_Word_Wrap( handle, 1, .F., right_margin, hard_margin,;
dum, dum )
__color_std = old_std
RESTORE_AREA()
ENDIF
ENDIF
DO WHILE going
*- call internal editor, NOTE: this is the same thing the MEMO editor
* calls since it just edits text in a VLIST, it does not care where
* it came from.
ret_val = VEdit_Internal_Editor( handle, scrap, undelete,;
upper_row, upper_col,;
lower_row, lower_col,;
top_element, start_line, left_col, start_col,;
right_margin, hard_margin, m_text_color, word_wrap,;
disp_only, .F., scroll_bar, stat_line, stat_row, stat_col,;
is_mouse, a_embedded,;
tab_width, undelete_lines,;
key_handler )
*- check our return cases
DO CASE
CASE ret_val = &edit_continue && continue with edit, I guess we must
&& have resized or something!
LOOP
CASE ret_val = &edit_none && exit after doing NOTHING
ret_val = 0
going = .F.
CASE ret_val = &edit_save_scrap .OR.;
ret_val = &edit_read_scrap
IF ret_val = &edit_save_scrap .AND. Vlist_Max( scrap ) = 0
LOOP
ENDIF
temp_filename = new_filename
SAVE_AREA( upper_row, upper_col, upper_row+3, upper_col+38 )
FILL( upper_row, upper_col, upper_row+3, upper_col+38,;
&double_box, " ", __color_bar, __color_tab, 6 )
old_std = __color_std
__color_std = __color_tab
CURSOR_ON()
temp_filename = LEFT( TRIM( temp_filename ), 128 )
temp_filename = temp_filename + SPACE( 128 - LEN( temp_filename ) )
@upper_row+1, upper_col+2 SAY "Enter SCRAP filename:";
GET temp_filename PICTURE "@K@S12"
READ
__color_std = old_std
RESTORE_AREA()
IF LASTKEY() = 27
LOOP
ELSE
temp_filename = TRIM( temp_filename )
new_filename = temp_filename
ENDIF
SAVE_AREA( upper_row, upper_col, upper_row+3, upper_col+14 )
FILL( upper_row, upper_col, upper_row+3, upper_col+14,;
&double_box, " ", __color_bar, __color_tab, 6 )
old_std = __color_std
__color_std = __color_tab
CURSOR_ON()
IF ret_val = &edit_save_scrap
@upper_row+1, upper_col+2 SAY "Saving..."
open_type = &f_create
ELSE
@upper_row+1, upper_col+2 SAY "Reading..."
open_type = &f_read
ENDIF
IF .NOT. F_OPEN( input_file, temp_filename, open_type )
ret_val = 6
going = .F.
ELSE
IF ret_val = &edit_save_scrap
VLIST_Top( scrap )
DO WHILE .NOT. VLIST_BOL( scrap )
temp_str = RTRIM( Vlist_Cstr( scrap ) )
F_PUT( input_file, temp_str + CHR( 13 ) + CHR( 10 ) )
Vlist_Skip( scrap, &jl_forward )
ENDDO
ELSE
* read scrap
DO WHILE .NOT. F_EOF( input_file )
IF .NOT. F_GETLN( input_file, temp_str )
__color_std = old_std
RESTORE_AREA()
F_CLOSE( input_file )
ret_val = 3
going = .F.
LOOP
ENDIF
temp_str = LEFT( RTRIM( temp_str ), hard_margin )
temp_loc = AT( CHR( 9 ), temp_str )
DO WHILE temp_loc > 0
temp_str = LEFT( temp_str, temp_loc - 1 ) + SPACE( tab_width ) +;
SUBSTR( temp_str, temp_loc + 1, 250 )
temp_loc = AT( CHR( 9 ), temp_str )
ENDDO
*- pad the string out to the far right margin
temp_str = temp_str + SPACE( hard_margin - LEN( temp_str ) )
IF .NOT. Vlist_Add( scrap, temp_str )
__color_std = old_std
RESTORE_AREA()
F_CLOSE( input_file )
ret_val = 5
going = .F.
LOOP
ENDIF
ENDDO
ENDIF
F_CLOSE( input_file )
ENDIF
__color_std = old_std
RESTORE_AREA()
CASE ret_val = &edit_save_exit .OR.;
ret_val = &edit_save_continue .OR.;
ret_val = &edit_error .OR.;
ret_val = &edit_save_as_new .OR.;
ret_val = &edit_save_rename && save
temp_filename = filename
IF ret_val = &edit_save_exit .OR.;
ret_val = &edit_error
* do they really want to exit without saving?
SAVE_AREA( upper_row, upper_col, upper_row+3, upper_col+29 )
FILL( upper_row, upper_col, upper_row+3, upper_col+29,;
&double_box, " ", __color_bar, __color_tab, 6 )
old_std = __color_std
__color_std = __color_tab
CURSOR_ON()
IF ret_val = &edit_error
@upper_row+1, upper_col+2 SAY "ERROR! Save text (Y/N)?"
ELSE
@upper_row+1, upper_col+2 SAY "Save before exit (Y/N)?"
ENDIF
DO WHILE INKEY() <> 0
ENDDO
REPEAT
dum = GET_KEY()
UNTIL CHR( dum ) $ "YyNn" .OR. dum=27
__color_std = old_std
RESTORE_AREA()
IF dum = 27
LOOP
ENDIF
IF dum <> 'Y' .AND. dum <> 'y'
go_ahead = .F.
ELSE
go_ahead = .T.
ENDIF
ELSE
go_ahead = .T.
IF ret_val = &edit_save_as_new .OR.;
ret_val = &edit_save_rename
IF ret_val = &edit_save_as_new
temp_filename = new_filename
ELSE
temp_filename = filename
ENDIF
SAVE_AREA( upper_row, upper_col, upper_row+3, upper_col+38 )
FILL( upper_row, upper_col, upper_row+3, upper_col+38,;
&double_box, " ", __color_bar, __color_tab, 6 )
old_std = __color_std
__color_std = __color_tab
CURSOR_ON()
temp_filename = LEFT( TRIM( temp_filename ), 128 )
temp_filename = temp_filename + SPACE( 128 - LEN( temp_filename ) )
IF ret_val = &edit_save_as_new
@upper_row+1, upper_col+2 SAY "Enter NEW filename:";
GET temp_filename PICTURE "@K@S12"
ELSE
@upper_row+1, upper_col+2 SAY "Enter RENAME filename:";
GET temp_filename PICTURE "@K@S12"
ENDIF
READ
__color_std = old_std
RESTORE_AREA()
IF LASTKEY() = 27
LOOP
ELSE
temp_filename = TRIM( temp_filename )
IF ret_val = &edit_save_as_new
new_filename = temp_filename
ELSE
filename = temp_filename
ENDIF
ENDIF
ENDIF
ENDIF
IF go_ahead
SAVE_AREA( upper_row, upper_col, upper_row+3, upper_col+13 )
FILL( upper_row, upper_col, upper_row+3, upper_col+13,;
&double_box, " ", __color_bar, __color_tab, 6 )
old_std = __color_std
__color_std = __color_tab
CURSOR_ON()
@upper_row+1, upper_col+2 SAY "Saving..."
* save the sucker!
IF .NOT. F_OPEN( input_file, temp_filename, &f_create )
ret_val = 6
going = .F.
__color_std = old_std
RESTORE_AREA()
LOOP
ELSE
VLIST_Top( handle )
DO WHILE .NOT. VList_BOL( handle )
temp_str = RTRIM( Vlist_Cstr( handle ) )
F_PUT( input_file, temp_str + CHR( 13 ) + CHR( 10 ) )
Vlist_Skip( handle, &jl_forward )
ENDDO
F_CLOSE( input_file )
ENDIF
__color_std = old_std
RESTORE_AREA()
ENDIF
IF ret_val = &edit_save_exit
going = .F.
ret_val = 0
ELSE
IF ret_val = &edit_error
ret_val = 4
going = .F.
ELSE
going = .T.
ENDIF
ENDIF
ENDCASE
ENDDO
*- clean up memory
Vlist_Clear( handle )
Vlist_Clear( scrap )
VList_Clear( undelete )
RETURN ret_val
ENDPRO