// SOLDLG.C

// This file includes methods for:

// SOLDLG (subclasses of DLGBOX, 5 dialogs in SOLIPEG)

#include <solipeg.g>
#include <solipeg.rsg>
#include <hwim.h>
#include <solglob.h>



#pragma METHOD_CALL


/************************************/
/* Dialog Subclass Methods          */
/************************************/

// Initialization for Open/Save dialogs
METHOD VOID soldlg_open_dl_dyn_init(PR_DLGBOX *self) {
    // Set the filename to the one passed in the rbuf
    p_send4(    DatDialogPtr,
                O_WN_SET,
                1,
                ((SOLRBUF *) self->dlgbox.rbuf)->strFilename ) ;

    // Set the context sensitive help
    self->dlgbox.helprid = RH_LIBRARY ;
}

// Keypress handling for ENTER in Open/Save Dialog
METHOD INT soldlg_open_dl_key(  PR_DLGBOX *     self,
                                INT             index,
                                INT             keycode,
                                INT             actbut ) {
    // Set the name in rbuf to that accepted in dialog
    p_send4(    DatDialogPtr,
                O_WN_SENSE,
                1,
                ((SOLRBUF *) self->dlgbox.rbuf)->strFilename ) ;
    // Change accepted
    return(WN_KEY_CHANGED) ;
}

// Initialization for Options dialog
METHOD VOID soldlg_options_dl_dyn_init(PR_DLGBOX *self) {
    INT     iLp ;

    // Set the dialog choices as currently recorded in the engine
    for ( iLp = 0 ; iLp < 5 ; iLp++ ) {
        hDlgSetChlist(iLp + 1, *(&options.shape + iLp)) ;
    }

    // Set the context-sensitive help
    self->dlgbox.helprid = RH_OPTIONS ;
}

// Keypress handling for ENTER in Options dialog
METHOD INT soldlg_options_dl_key(   PR_DLGBOX *     self,
                                    INT             index,
                                    INT             keycode,
                                    INT             actbut ) {
    INT     iLp ;

    // Set the dialog choices into the engine settings
    for ( iLp = 0 ; iLp < 5 ; iLp++ ) {
        *(&options.shape + iLp) = hDlgSenseChlist(iLp + 1) ;
    }

    // Change accepted
    return(WN_KEY_CHANGED) ;
}

// Initialization for Library dialog
METHOD VOID soldlg_library_dl_dyn_init(PR_DLGBOX *self) {
    // Set the dialog choices as currently recorded in the engine
    hDlgSetChlist(1, options.ridLast) ;

    // Set the context-sensitive help
    self->dlgbox.helprid = RH_LIBRARY ;
}

// Keypress handling for ENTER in Library dialog
METHOD INT soldlg_library_dl_key(   PR_DLGBOX *     self,
                                    INT             index,
                                    INT             keycode,
                                    INT             actbut ) {
    // Tell the engine to load the resource chosen
    p_send3(    soleng,
                O_ENG_INTERNAL_LIBRARY,
                options.ridLast = hDlgSenseChlist(1) ) ;

    // Change accepted
    return(WN_KEY_CHANGED) ;
}

// External library choice list dynamic initialization
METHOD VOID soldlg_ext_lib_dl_dyn_init(PR_DLGBOX * self) {
    TEXT            buffer[P_FNAMESIZE] ;
    SE_CHLIST       sc ;

    // Read the external library's name
    p_send4(    ((PR_SOLEL *) self->dlgbox.rbuf)->solel.pRsc,
                O_RS_READ_BUF,
                3,
                buffer ) ;
    if ( buffer[0] ) {
        hDlgSetText(0, buffer) ;
    }
    sc.set_flags = SE_CHLIST_DATA | SE_CHLIST_RETAIN ;

    // Set choice list to the list loaded from the library by solel
    sc.data = ((PR_SOLEL *) self->dlgbox.rbuf)->solel.pData ;
    hDlgSet(1, &sc) ;

    // Set the context-sensitive help
    self->dlgbox.helprid = RH_LIBRARY ;
}

// External library choice list accept
METHOD INT soldlg_ext_lib_dl_key(   PR_DLGBOX *     self,
                                    INT             index,
                                    INT             keycode,
                                    INT             actbut ) {
    // Tell the engine to load the resource chosen
    p_send4(    soleng,
                O_ENG_EXTERNAL_LIBRARY,
                self->dlgbox.rbuf,
                hDlgSenseChlist(1) ) ;

    // Change accepted
    return(WN_KEY_CHANGED) ;
}

// Statistics dialog dynamic initialization
METHOD VOID soldlg_stats_dl_dyn_init(PR_DLGBOX * self) {
    TEXT            buffer[32] ;
    INT             iLp ;
    INT             fFirst = TRUE ;
    TEXT *          p ;

    // Display statistics from the engine (persistently stored in an
    //  environment variable (DatApp1 stores the name)
    buffer[p_gltob(buffer, options.games, 10)] = '\x0' ;
    hDlgSetText(1, buffer) ;
    if ( options.games ) {
        buffer[p_gltob(buffer, options.pegs / options.games, 10)] = '\x0' ;
        hDlgSetText(2, buffer) ;
    }

    for ( buffer[0] = '\x0', p = &buffer[0], iLp = 0 ; iLp < 5 ; iLp++ ) {
        if ( options.results[iLp] ) {
            if ( !fFirst ) {
                *p++ = ',' ;
                *p++ = ' ' ;
            }
            fFirst = FALSE ;
            p += p_gtob(p, options.results[iLp], 10) ;
            *p = '\x0' ;
        }
    }
    hDlgSetText(3, buffer) ;

    // Set the context-sensitive help
    self->dlgbox.helprid = RH_MAIN ;
}

