#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windom.h>
#include "text.h"
#include "tut_2.h"
static
void __CDECL evnt_draw_text( WINDOW *win, short buff[8], void *data) {
short x, y, w, h;
short hcell, hcar;
short i, attr[10];
short y_max, h_offset;
TEXT *ptext = (TEXT*) data;
WindGet( win, WF_WORKXYWH, &x, &y, &w, &h);
y_max = MIN( y + h, win->graf->clip.g_y + win->graf->clip.g_h) - 1;
vqt_attributes( win->graf->handle, attr);
hcell = attr[9];
hcar = attr[7];
vswr_mode( win->graf->handle, MD_TRANS);
h_offset = hcell - hcar;
for( i=win->ypos; i<win->ypos_max ; i++) {
y += hcell;
if( y < win->graf->clip.g_y)
continue;
if( strlen( ptext->line[ i]) > win->xpos)
v_gtext(win->graf->handle, x, y - h_offset, ptext->line[i] + win->xpos);
if (y > y_max)
break;
}
}
static
void __CDECL evnt_destroy_text( WINDOW *win, short buff[8], void *text) {
free_text( text);
}
WINDOW *OpenText( TEXT *text) {
WINDOW *win;
short attrib[10];
win = WindCreate( WAT_NOINFO, app.x, app.y, app.w, app.h);
EvntDataAdd( win, WM_REDRAW, evnt_draw_text, text, EV_BOT);
EvntDataAdd( win, WM_DESTROY, evnt_destroy_text, text, EV_TOP);
win -> xpos_max = 255;
win -> ypos_max = text->maxline;
vqt_attributes( win->graf->handle, attrib);
win -> w_u = attrib[8];
win -> h_u = attrib[9];
text-> wchar = attrib[6];
text-> hchar = attrib[7];
WindOpen( win, app.x, app.y, app.w, app.h);
WindSlider( win, HSLIDER|VSLIDER);
return win;
}
int endprog = FALSE;
static
void evnt_apterm( ) {
while( wglb.first) {
ApplWrite( _AESapid, WM_DESTROY, wglb.first->handle, 0, 0, 0, 0);
EvntWindom( MU_MESAG);
}
endprog = TRUE;
}
OBJECT *get_tree( int index) {
OBJECT *tree;
rsrc_gaddr( 0, index, &tree);
return tree;
}
static void menudesk_open( WINDOW *null, int index, int title, void *data) {
TEXT *text = NULL;
WINDOW *win;
static char path[256]="", name[64]="";
if( FselInput(path, name, "*.*", "View text file", NULL, NULL)) {
char filename[256];
strcpy( filename, path);
strcat( filename, "\\");
strcat( filename, name);
text = load_text( filename);
if( text) win = OpenText( text);
}
if( text == NULL) FormAlert( 1, FA_ERROR "[Error: file not found][OK]");
MenuTnormal( NULL, title, 1);
}
static void menudesk_info( WINDOW *null, int index, int title, void *data) {
FormAlert( 1, FA_INFO "[Tutorial WinDom| " __FILE__ "|(c) 2004][Ok]");
MenuTnormal( NULL, title, 1);
}
int main( void) {
ApplInit();
if( RsrcLoad( "tut_2.rsc") == 0) {
FormAlert( 1, FA_ERROR "[Can not locate tut-2.rsc][Fatal]");
ApplExit();
return -1;
}
MenuBar( get_tree( MENUDESK), 1);
menu_register( _AESapid, " Tutorial WinDom 2-2 ");
ObjcAttachMenuFunc( NULL, MENUDESK_OPEN, menudesk_open, NULL);
ObjcAttachMenuFunc( NULL, MENUDESK_INFO, menudesk_info, NULL);
ObjcAttachMenuFunc( NULL, MENUDESK_QUIT, evnt_apterm, NULL);
EvntAttach( NULL, AP_TERM, evnt_apterm);
while( !endprog) EvntWindom( MU_MESAG + MU_KEYBD);
MenuBar( get_tree(MENUDESK), 0);
RsrcFree();
ApplExit();
return EXIT_SUCCESS;
}