#include <Symbol.h>
#include <MyFrame.h>


extern char* strGLOBAL_Bmp_Desktop_Bmp;

extern WAVE_PROCESS_TCB msAfxWaveProcess[];
extern int SystemFonSize;

static void FAR OnTime_AfxTaskDialog(CObject* pCurObj)
{
    static WORD i2 = 0;

    extern int wAfxTimerDivide;
    if (((++i2) % (18 * wAfxTimerDivide)) != 0)
    {
        return;
    }

    CDialog* pDialog = (CDialog *)pCurObj;
    CListBox* pListBox = (CListBox *)pDialog->ObjectFromID(LISTBASE + 1 - 1);

    DWORD dwsCounter[MAX_PROCESS_COUNT];
    char buf[80], buf2[20];
    DWORD dwSum = 0;

    asm cli
    for (int i = 0; i < MAX_PROCESS_COUNT; i++)
    {
	dwsCounter[i] = msAfxWaveProcess[i].dwCounter;
        dwSum += dwsCounter[i];
    }
    asm sti

    WAVE_PROCESS_TCB* pTcb;
    for (i = 0; i < MAX_PROCESS_COUNT; i++)
    {
    	pTcb = &msAfxWaveProcess[i];

        float fBei = 0.0f;
        if (dwSum == 0)
        {
        	fBei = 100.0f;
        }
        else
        {
            fBei = 100.0f * dwsCounter[i] / dwSum;
        }

	if (pTcb->bExist)
        {
            sprintf(buf, "任务(%2d)::%s%%,%05u/%05u,%lu",
				i + 1, ::SetDisplayBufferDot(buf2, fBei),
				pTcb->wStackSize - pTcb->wSp -
					(WORD)pTcb->pStack, pTcb->wStackSize, dwsCounter[i]);
        }
        else
        {
            sprintf(buf, "任务(%2d) =", i + 1);

        }
        pListBox->SetSel(i, buf);
    }
    asm sti

    pListBox->Paint();

}


void AfxTaskDialog()
{
        char *pStr = "任务时间列表";

	CDialog* pDialog = new CDialog;
	pDialog->CreateWindow(0,0,468,291, pStr);
	pDialog->Center();
        pDialog->SetTimeFc(OnTime_AfxTaskDialog);

	new CCloseButton(pDialog);

	CButton* pButton1 = new CButton(pDialog);
	pButton1->CreateObject(145,255,291,24,"退出");

	CListBox* pListBox1 = new CListBox(pDialog);
	pListBox1->CreateList(127,68,330,177,NULL);
	pListBox1->SetID(LISTBASE + 1 - 1);

	CLabel* pLabel1 = new CLabel(pDialog);
	pLabel1->CreateObject(130,41,73,12, pStr);

	CLabel* pLabel2 = new CLabel(pDialog);
	pLabel2->CreateObject(0,20,123,263,NULL);
	pLabel2->SetBitmapFlags(TRUE);
	pLabel2->SetBackColorDisplay(TRUE);
	pLabel2->SetBitmapFile(strGLOBAL_Bmp_Desktop_Bmp);

        pListBox1->SetCount(MAX_PROCESS_COUNT);
        for (int i = 0; i < MAX_PROCESS_COUNT; i++)
        {
                pListBox1->SetSel(i,"");
        }

        pButton1->SetFc(ClickCancelButton);

	pDialog->ShowWindow();
	pDialog->DoModal();
	delete pDialog;
}


void AfxDeviceDialog()
{
    char *pStr = "驱动程序管理器";
	CDialog* pDialog = new CDialog;
	pDialog->CreateWindow(0,0,468,291,pStr);
	pDialog->Center();

	new CCloseButton(pDialog);

	CButton* pButton1 = new CButton(pDialog);
	pButton1->CreateObject(145,255,291,24,"退出");

	CListBox* pListBox1 = new CListBox(pDialog);
	pListBox1->CreateList(127,68,330,177,NULL);
	pListBox1->SetID(LISTBASE + 1 - 1);

	CLabel* pLabel1 = new CLabel(pDialog);
	pLabel1->CreateObject(130,41,73,12,pStr);

	CLabel* pLabel2 = new CLabel(pDialog);
	pLabel2->CreateObject(0,20,123,263,NULL);
	pLabel2->SetBitmapFlags(TRUE);
	pLabel2->SetBackColorDisplay(TRUE);
	pLabel2->SetBitmapFile(strGLOBAL_Bmp_Desktop_Bmp);


        REGS mRegs;
        SREGS mSregs;
        mRegs.x.ax = 0x5200;
        ::intdosx(&mRegs,&mRegs,&mSregs);
        DEVICEHEAD* pFc = (DEVICEHEAD *)MK_FP(mSregs.es, mRegs.x.bx + 0x22);

        char buf[100], buf1[50];
        int nCount = 0;
        while (pFc != NULL &&  0xFFFF != (WORD)pFc->pNext)
        {
                nCount++;
                pFc  = (DEVICEHEAD *)pFc->pNext;
        }

        pListBox1->SetCount(nCount);

        int i = 0;
        pFc = (DEVICEHEAD *)MK_FP(mSregs.es, mRegs.x.bx + 0x22);
        while (pFc != NULL &&  0xFFFF != (WORD)pFc->pNext)
        {
                _fmemcpy(buf1, pFc->chDeviceName, 8);
                buf1[8] = '\0';
                CRegister::TrimRight(buf1);

                sprintf(buf, "Device%d::%s", i + 1, buf1);

                pListBox1->SetSel(i++, buf);
                pFc  = (DEVICEHEAD *)pFc->pNext;
        }

        pButton1->SetFc(ClickCancelButton);

	pDialog->ShowWindow();
	pDialog->DoModal();
	delete pDialog;
}

