// SOLWIN.C

// This file includes methods for:

// SOLWIN (subclass of WIN)
// SOLBWIN (subclass of BWIN)

#include <solipeg.g>
#include <solipeg.rsg>
#include <hwim.h>
#include <epoc.h>
#include <p_gen.h>
#include <solglob.h>



#pragma save, METHOD_CALL


//
//  C_SOLBWIN methods
//

#define hBMPSprite          (self->solbwin.hBMPSprite)
#define iSpriteID           (self->solbwin.iSpriteID)
#define wSpriteOffset       (self->solbwin.wSpriteOffset)


METHOD VOID solbwin_wn_init(PR_SOLBWIN *self) {
    W_WINDATA       wd ;

    // Connect to window server with no backup bitmap
    wd.extent.tl.x = 0 ;
    wd.extent.tl.y = 0 ;
    wd.extent.width = wd.extent.height = wserv_channel->conn.info.pixels.y ;
    wd.background = W_WIN_BACK_CLR | W_WIN_BACK_GREY_CLR ;
    p_send5(    self,
                O_WN_CONNECT,
                NULL,
                W_WIN_EXTENT | W_WIN_BACKGROUND,
                &wd) ;

    // Reposition the window according to preferences stored in the engine
    p_send2(self, O_WN_REPOS) ;

    self->solbwin.swn = f_newsend(  CAT_SOLIPEG_SOLIPEG,
                                    C_SOLWIN,
                                    O_WN_INIT,
                                    self ) ;

    // Make the windows visible
    p_send3(self, O_WN_VISIBLE, WV_INITVIS) ;
}

// Place the cursor at the current position if a sprite exists
METHOD VOID solbwin_wn_cursor(PR_SOLBWIN *self) {
    if ( !iSpriteID ) {
        W_SPRITE    sprite[1] = {   {   0, 0, 0,
                                        0, 0, 0,
                                        {0, 0},
                                        0 }
                                } ;
        P_RECT      rect = {    {0, 0},
                                {DESIGN_SIZE, DESIGN_SIZE} } ;

        hBMPSprite = gCreateBit(0, &rect.br) ;
        gCreateTempGC0(hBMPSprite) ;
        gClrRect(&rect, G_TRMODE_CLR) ;
        gBorder(W_BORD_CORNER_1) ;
        gFreeTempGC() ;

        rect.br.x = wSpriteOffset + BOARD_X(3) ;
        rect.br.y = BOARD_Y(5) ;
        sprite[0].bit_set = hBMPSprite ;
        iSpriteID = wCreateSprite(  self->win.id,
                                    &rect.br,
                                    0,
                                    1,
                                    sprite ) ;
    }
    else {
        P_POINT     pos ;
        UWORD       uwPos ;

        uwPos = p_send2(w_ws->wserv.com, O_SCM_GPOS) ;
        pos.x = wSpriteOffset + BORDER_WIDTH + BOARD_X(X(uwPos)) ;
        pos.y = BORDER_WIDTH + BOARD_Y(Y(uwPos)) ;
        wSetSprite(iSpriteID, &pos, 0, NULL) ;
    }
}

// Reposition the window
METHOD VOID solbwin_wn_repos(PR_SOLBWIN *self) {
    W_WINDATA       wd ;
    P_EXTENT        ext ;

    wInquireWindow(self->win.id, &wd) ;
    wInquireStatusWindow(W_STATUS_WINDOW_BIG, &ext) ;
    if ( options.winpos == 0 ) {
        wd.extent.tl.x = 0 ;
    }
    else if ( options.winpos == 1 ) {
        wd.extent.tl.x = (  wserv_channel->conn.info.pixels.x -
                            wd.extent.width ) / 2 ;
    }
    else {
        wd.extent.tl.x =    wserv_channel->conn.info.pixels.x -
                            wd.extent.width -
                            ext.width ;
    }
    wSetWindow( self->win.id,
                W_WIN_EXTENT,
                &wd ) ;
    wSpriteOffset = wd.extent.tl.x ;
    p_send2(self, O_WN_CURSOR) ;
}

// Pass the key stroke to the command manager
METHOD VOID solbwin_wn_key(PR_SOLBWIN *self, INT keycode, INT mods) {
    // Tell the command manager it's got a keypress
    p_send4(w_ws->wserv.com, O_SCM_KEY, keycode, mods) ;
}

// Draw the window - border and board
METHOD VOID solbwin_wn_draw(PR_SOLBWIN *self) {
    gBorder2(W_BORDER_TYPE_1, W_BORD_CORNER_1 | W_BORD_SHADOW_ON) ;
}

//
//  C_SOLWIN methods
//

#define hBMP                (self->solwin.hBMP)
#define hBMPGrey            (self->solwin.hBMPGrey)


METHOD VOID solwin_wn_init(PR_SOLWIN *self, PR_WIN *par) {
    W_WINDATA       wd ;
    TEXT            strFilename[P_FNAMESIZE] = "" ;
    P_POINT         pt ;

    f_fparse(strFilename, DatCommandPtr, strFilename, NULL) ;

    self->solwin.parent = par ;

    // Connect to window server with no backup bitmap
    wInquireWindow(par->win.id, &wd) ;
    wd.extent.tl.x = BORDER_WIDTH ;
    wd.extent.tl.y = BORDER_WIDTH ;
    wd.extent.width -= 2 * BORDER_WIDTH ;
    wd.extent.height -= 2 * BORDER_WIDTH ;
    wd.background = W_WIN_BACK_NONE | W_WIN_BACK_GREY_NONE ;
    p_send5(    self,
                O_WN_CONNECT,
                par,
                W_WIN_EXTENT | W_WIN_BACKGROUND,
                &wd) ;

    hBMP = gOpenBit(strFilename, 2, 0, &pt) ;
    hBMPGrey = gOpenBit(strFilename, 3, 0, &pt) ;
}

// Draw the window - border and board
METHOD VOID solwin_wn_draw(PR_SOLWIN *self) {
    // Tell the engine to redraw the board
    p_send3(soleng, O_ENG_DRAW_BOARD, self) ;
}

