//
//      Square.c  
//

#include "AEEModGen.h"  // khai bao module interface
#include "AEEAppGen.h"  // khai bao applet interface 
#include "AEEShell.h"   // khai bao shell interface

#include "Square.bid"    // include Class ID c?a Square applet 

// khai bao phia tr??c
static boolean Square_HandleEvent(IApplet * pi, AEEEvent eCode,
                                  uint16 wParam, uint32 dwParam);
static void Square_OnAppStart(AEEApplet* app);

// 
//  function t?o BREW applet
//  
int AEEClsCreateInstance(
    AEECLSID ClsId,     // class ID c?a applet t?o ra
    IShell * pIShell,   // shell object
    IModule * po,       // module object
    void ** ppObj)      // tr? v? applet t?o ra
{
    *ppObj = NULL;
    
    if (ClsId == AEECLSID_SQUARE) {
        if (AEEApplet_New(sizeof(AEEApplet), ClsId, pIShell, po, (IApplet**)ppObj,
            (AEEHANDLER)Square_HandleEvent,NULL) == TRUE) {
            return (AEE_SUCCESS);
        }
    }
    return (EFAILED);
}

//
//  Event handler c?a Square applet
//
static boolean Square_HandleEvent(
    IApplet * pi,
    AEEEvent eCode,
    uint16 wParam,
    uint32 dwParam)
{  
    switch (eCode) 
    {
    case EVT_APP_START:                        
        Square_OnAppStart((AEEApplet*) pi);
        return TRUE;
            
    case EVT_APP_STOP:
        return TRUE;
        
    default:
        break;
    }
    return FALSE;
}

//
//  s? ???c g?i khi kh?i ??ng application
//
static void Square_OnAppStart(AEEApplet* app)
{
    IShell* shell;
    IDisplay* disp;
    AEEDeviceInfo devinfo;
    AEERect whole_rect;
    AEERect red_rect;
    RGBVAL light_blue   = MAKE_RGB(200, 200, 255);
    RGBVAL red          = MAKE_RGB(255, 0, 0);

    // get IShell object
    shell = app->m_pIShell;

    // get IDisplay object
    disp  = app->m_pIDisplay;

    // get size of mobile screen
    ISHELL_GetDeviceInfo(shell, &devinfo);

    // t?o hinh ch? nh?t co kich th??c b?ng man hinh
    whole_rect.x    = 0;                    // t?o ?? X c?a ?i?m trai tren
    whole_rect.y    = 0;                    // t?o ?? Y c?a ?i?m trai tren
    whole_rect.dx   = devinfo.cxScreen;     // width
    whole_rect.dy   = devinfo.cyScreen;     // hight

    // to mau xanh toan b? man hinh
    IDISPLAY_FillRect(disp, &whole_rect, light_blue);

    // v? khung c?a hinh ch? nh?t mau ??
    red_rect.x      = 20;
    red_rect.y      = 20;
    red_rect.dx     = 40;
    red_rect.dy     = 30;
    IDISPLAY_DrawRect(disp, &red_rect, red, 0, IDF_RECT_FRAME);

    // hi?n th? th?c t? c?a s? v? ra bitmap man hinh
    IDISPLAY_Update(disp);
}

