/***************************************************************************
**
**    $Header:   D:/ECB2S/SRC/LOG/TOOLSDLG.CPP   1.2.1.4   17 Apr 1997 16:06:42   ZJRD  $
**
**    $Log:   D:/ECB2S/SRC/LOG/TOOLSDLG.CPP  $
** 
**    Rev 1.2.1.4   17 Apr 1997 16:06:42   ZJRD
** No change.
** 
**    Rev 1.2.1.3   16 Apr 1997 10:41:36   ZJRD
** 2.09F
** 
****************************************************************************/

// toolsdlg.cpp : implementation file
//

#include "stdafx.h" 
#include "resource.h" 
#include "toolsdlg.h"
#include "btnbar.h"         
#include "uicom.h"
//#include "hlistbox.h"
//#include "control.h"  
//#include "auxdata.h" 
COLORREF clrBtnFace,clrBtnShadow,clrBtnHilite,clrWindowFrame;

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif                   


//These static varible is for "Available Button" in Toolbar dialog
//	-- by chris, 4/7/97
//static int Main[]={0,3,30,12,9,10,29,11,13,14,15,31,26,33,34};
static int Main[]={0,3,30,12,9,10,29,11,13,14,15,31,26,33,34,35};
//static int CPU[]={0,3,30,12,9,10,29,11,13,14,15,31,26,3,3,3,3};
static int Source[]={0,1,2,3,4,5,6,37,12,9,10,29,30,11,13,14,15,31,26,33,34,35};
static int Stack[]={0,3,12,9,10,29,11,13,14,15,20,4,31,26,33,34};
static int Trace[]={0,3,12,9,10,29,11,13,14,15,21,22,23,7,8,16,32,31,26,33,34,35,36};
static int Variable[]={0,3,12,9,10,29,11,13,14,15,24,25,4,31,26,33,34,35};



extern UINT buttons[];

static void NEAR PASCAL PatB(HDC hDC, int x, int y, int dx, int dy, COLORREF rgb)
{
	SetBkColor(hDC, rgb);
	CRect rect(x, y, x+dx, y+dy);
	ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
}

static HBITMAP PASCAL CreateDitherBitmap()
{
	struct  // BITMAPINFO with 16 colors
	{
		BITMAPINFOHEADER bmiHeader;
		RGBQUAD      bmiColors[16];
	} bmi;
	memset(&bmi, 0, sizeof(bmi));

	bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmi.bmiHeader.biWidth = 8;
	bmi.bmiHeader.biHeight = 8;
	bmi.bmiHeader.biPlanes = 1;
	bmi.bmiHeader.biBitCount = 1;
	bmi.bmiHeader.biCompression = BI_RGB;

	COLORREF clr = ::GetSysColor(COLOR_BTNFACE);
	bmi.bmiColors[0].rgbBlue = GetBValue(clr);
	bmi.bmiColors[0].rgbGreen = GetGValue(clr);
	bmi.bmiColors[0].rgbRed = GetRValue(clr);

	if (1)
		clr = ::GetSysColor(COLOR_BTNHIGHLIGHT);
	else
		clr = RGB(255, 255, 255);
	bmi.bmiColors[1].rgbBlue = GetBValue(clr);
	bmi.bmiColors[1].rgbGreen = GetGValue(clr);
	bmi.bmiColors[1].rgbRed = GetRValue(clr);

	// initialize the brushes
	long patGray[8];
	for (int i = 0; i < 8; i++)
	   patGray[i] = (i & 1) ? 0xAAAA5555L : 0x5555AAAAL;

	HDC hDC = GetDC(NULL);
	HBITMAP hbm = CreateDIBitmap(hDC, &bmi.bmiHeader, CBM_INIT,
		(LPBYTE)patGray, (LPBITMAPINFO)&bmi, DIB_RGB_COLORS);
	ReleaseDC(NULL, hDC);

	return hbm;
}


// create a mono bitmap mask:



/////////////////////////////////////////////////////////////////////////////
// CToolsDlg dialog


CToolsDlg::CToolsDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CToolsDlg::IDD, pParent)
{
	m_pdcMem=new CDC;
	m_strText="  ";
	m_strText2="  ";
/*	if(!m_Add.LoadBitmaps("IDB_ADD","IDB_ADD1")   )
	{
		TRACE("Failed to load bitmaps for buttons\n");
		AfxThrowResourceException();
	}   
*/	//{{AFX_DATA_INIT(CToolsDlg)
	//}}AFX_DATA_INIT
}                                            

CToolsDlg::~CToolsDlg()
{
   m_pHList->DestroyWindow();
   delete m_pHList;
   ReleaseDC(m_pdcMem);
   delete m_pdcMem;
}


void CToolsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CToolsDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CToolsDlg, CDialog)
	//{{AFX_MSG_MAP(CToolsDlg)
	ON_BN_CLICKED(IDOK2, OnRemove)
	ON_BN_CLICKED(IDOK3, OnReplace)
	ON_BN_CLICKED(ID_INSERT, OnInsert)
	ON_LBN_SELCHANGE(IDC_LISTBOX1, OnSelchangeListbox1)
	ON_LBN_SELCHANGE(IDC_LISTBOX2, OnSelchangeListbox2)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_APPLY, OnApply)
	ON_EN_KILLFOCUS(IDC_COMM_EDIT, OnKillfocusCommEdit)
	ON_MESSAGE(XM_HLISTSELECTCHANGE,OnXmHListSelectChange)
	ON_EN_CHANGE(IDC_COMM_EDIT, OnChangeCommEdit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CToolsDlg message handlers






/////////////////////////////////////////////////////////////////////////////
// Custom Listbox - containing colors

void CToolListBox::AddToolItem(int item)
{
	// add a listbox item
	AddString((LPCSTR) item);
		// Listbox does not have the LBS_HASSTRINGS style, so the
		//  normal listbox string is used to store an RGB color
}

/////////////////////////////////////////////////////////////////////////////

#define COLOR_ITEM_HEIGHT   21

void CToolListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{
	// all items are of fixed size
	// must use LBS_OWNERDRAWVARIABLE for this to work
	lpMIS->itemHeight = COLOR_ITEM_HEIGHT;
}

void CToolListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
	CDC* pDC = CDC::FromHandle(lpDIS->hDC);
	int item = int(lpDIS->itemData); 
    pDC->SetBkMode(TRANSPARENT);
	if ((lpDIS->itemState & ODS_SELECTED) &&
		(lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
	{
		// item has been selected - hilite frame
		COLORREF crHilite = RGB(0,0,255);
		CBrush br(crHilite);
        pDC->FillRect(&lpDIS->rcItem,&br);
		DrawButton(pDC,((CToolsDlg*)GetParent())->m_pdcMem,lpDIS->rcItem,item);			

	}

	if (!(lpDIS->itemState & ODS_SELECTED) &&
		(lpDIS->itemAction & ODA_SELECT))
	{
		// Item has been de-selected -- remove frame
		CBrush br(RGB(255,255,255));
        pDC->FillRect(&lpDIS->rcItem,&br);
		DrawButton(pDC,((CToolsDlg*)GetParent())->m_pdcMem,lpDIS->rcItem,item);			
	}

	if (lpDIS->itemAction & ODA_DRAWENTIRE)
	{
		// Paint the color item in the color requested
		DrawButton(pDC,((CToolsDlg*)GetParent())->m_pdcMem,lpDIS->rcItem,item);
	}

}

void CToolListBox::DrawButton(CDC*pDC,CDC*pdcMem,CRect rect,int item)
{
	ASSERT(pDC != NULL);

	// make the coordinates the interior of the button
	rect.left+=2;
	rect.top+=3;
	int dx = 17 ;
	int dy = 16 ;

	// border around button
	PatB(pDC->m_hDC, rect.left,rect.top-1,dx, 1,  clrWindowFrame);
	PatB(pDC->m_hDC, rect.left,rect.top+dy,dx, 1,  clrWindowFrame);
	PatB(pDC->m_hDC, rect.left-1,rect.top,1,dy, clrWindowFrame);
	PatB(pDC->m_hDC, rect.left+dx,rect.top,1,dy, clrWindowFrame);

	// interior grey
	PatB(pDC->m_hDC, rect.left,rect.top, dx, dy, clrBtnFace);

	CPoint ptOffset;
	ptOffset.x = (dx -16 - 1) >> 1;
	ptOffset.y = (dy -15) >> 1;
	// regular button look
	PatB(pDC->m_hDC, rect.left,rect.top, 1, dy - 1, clrBtnHilite);
	PatB(pDC->m_hDC, rect.left,rect.top, dx - 1, 1, clrBtnHilite);

	PatB(pDC->m_hDC, rect.left + dx - 1, rect.top, 1, dy, clrBtnShadow);
	PatB(pDC->m_hDC, rect.left,rect.top + dy-1, dx, 1,   clrBtnShadow);

	PatB(pDC->m_hDC, rect.left + 1 + dx - 3, rect.top + 1, 1, dy-2, clrBtnShadow);
	PatB(pDC->m_hDC, rect.left + 1, rect.top + dy - 2, dx - 2, 1, clrBtnShadow);
	
	
	pDC->BitBlt(rect.left+2,rect.top+2, 15, 14, pdcMem, 16*item,0,SRCCOPY);
	
	if(item==MAX_TOOLBAR_BUTTON)      //27
	  	pDC->DrawText("Search ComBox",13,rect+CPoint(20,0),DT_LEFT );			
	else if(item==-1)
	    pDC->DrawText("Separator",9,rect+CPoint(20,0),DT_LEFT );			
	else 
	{
		CString str;
		str.LoadString(buttons[item]);
		int n,m=str.Find('\n');
		n=str.GetLength();
		CString strText;
		strText=str.Mid(m+1,n-m);
		pDC->DrawText(strText.GetBuffer(n-m),n-m-1,rect+CPoint(20,0),DT_LEFT );			
		str.ReleaseBuffer();
	}	
} 



BOOL CToolsDlg::OnInitDialog() 
{
	CDialog::OnInitDialog(); 
	
	CenterWindow();
	
	CRect rectHList;
	GetDlgItem(IDC_COMM_EDIT)->GetWindowRect(&rectHList);
	ScreenToClient(&rectHList);
	rectHList+=CPoint(0,45);
	rectHList.InflateRect(0,12);
	m_pHList=new CHListBox;
	m_pHList->Create(this,rectHList,IDU_HLIST) ; 
	CString str=((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->\
                 GetDlgItem(AFX_IDW_TOOLBAR)))->\
                 m_strUsrDefComm[m_pHList->m_nActiveItem];
    GetDlgItem(IDC_COMM_EDIT)->SetWindowText((str) );
    
// /////////////////////////////////////////////   
	clrBtnFace = ::GetSysColor(COLOR_BTNFACE);
	clrBtnShadow = ::GetSysColor(COLOR_BTNSHADOW);
	clrBtnHilite = ::GetSysColor(COLOR_BTNHIGHLIGHT);
	clrWindowFrame = ::GetSysColor(COLOR_WINDOWFRAME);    
///////////////////////////////////////////////////////////
    
	VERIFY(m_tools.SubclassDlgItem(IDC_LISTBOX1, this));  
	VERIFY(m_idletools.SubclassDlgItem(IDC_LISTBOX2, this));
    CBitmap * pBitmap=new CBitmap;
    pBitmap->LoadBitmap(IDR_MAINFRAME);  
    CDC *pDC;
	CBitmap *pbmpMyBitmap, *pbmpOld;
	pDC = GetDlgItem(IDC_LISTBOX1)->GetDC();
	
	m_pdcMem->CreateCompatibleDC(pDC); 
	pbmpOld = m_pdcMem->SelectObject( pBitmap);
    GetDlgItem(IDC_LISTBOX1)->ReleaseDC(pDC); 
   delete pBitmap; 
    
//added on 22/5                            
    CString strTitle;                                           
    CMDIChildWnd * pMDIWnd=((CMDIFrameWnd*)(AfxGetApp()->m_pMainWnd))->MDIGetActive();
    if(pMDIWnd==NULL) strTitle="Main";
    else pMDIWnd->GetWindowText(strTitle);    
    strTitle="ToolBar--"+strTitle;
    SetWindowText( (strTitle));
//////////////////////////////////    
    
    UINT nActiveWin=((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->\
                 GetDlgItem(AFX_IDW_TOOLBAR)))->nActiveId;

    UINT item=0; 
    UINT CountT;
	int* pButtonArray;
    switch(nActiveWin){
    case  WID_MAIN: 
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMainCount;
    	pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMain;
	    item=0;
 	    while(item<sizeof(Main)/sizeof(int) ){     //27
    		if(buttons[ Main[item] ]!=0){
        		m_idletools.AddToolItem(Main[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Main[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
		break;
    case  WID_CPU: 
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nCPUCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nCPU;
	    item=0;
 	    while(item<sizeof(Main)/sizeof(int) ){     //27
    		if(buttons[ Main[item] ]!=0){
        		m_idletools.AddToolItem(Main[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Main[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);    	
    	
    	break;
    case WID_SHELL:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nShellCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nShell;
	    item=0;
 	    while(item<sizeof(Main)/sizeof(int) ){     //27
    		if(buttons[ Main[item] ]!=0){
        		m_idletools.AddToolItem(Main[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Main[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);	    
	    break;
    case WID_TRACE:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nTraceCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nTrace;
	    item=0;
 	    while(item<sizeof(Trace)/sizeof(int) ){     //27
    		if(buttons[ Trace[item] ]!=0){
        		m_idletools.AddToolItem(Trace[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Trace[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;
    case WID_MEMORY: 
    case WID_MEM2:
    case WID_MEM3:
		CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMemCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMem;
	    item=0;
 	    while(item<sizeof(Main)/sizeof(int) ){     //27
    		if(buttons[ Main[item] ]!=0){
        		m_idletools.AddToolItem(Main[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Main[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;
    case WID_SOURCE:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nSourceCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nSource;
	    item=0;
 	    while(item<sizeof(Source)/sizeof(int) ){     //27
    		if(buttons[ Source[item] ]!=0){
        		m_idletools.AddToolItem(Source[item]);
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Source[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;
    case WID_BROWSE:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nBrowseCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nBrowse;
	    item=0;
 	    while(item<sizeof(Source)/sizeof(int) ){     //27
    		if(buttons[ Source[item] ]!=0){
        		m_idletools.AddToolItem(Source[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Source[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;

    case WID_STACK:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nStackCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nStack;
	    item=0;
 	    while(item<sizeof(Stack)/sizeof(int) ){     //27
    		if(buttons[ Stack[item] ]!=0){
        		m_idletools.AddToolItem(Stack[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Stack[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;

    case WID_VARIABLE:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nVarCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nVar;
	    item=0;
 	    while(item<sizeof(Variable)/sizeof(int) ){     //27
    		if(buttons[ Variable[item] ]!=0){
        		m_idletools.AddToolItem(Variable[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Variable[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;

    case WID_BMEM:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nBMemCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nBMem;
	    item=0;
 	    while(item<sizeof(Main)/sizeof(int) ){     //27
    		if(buttons[ Main[item] ]!=0){
        		m_idletools.AddToolItem(Main[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Main[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;

    case WID_PERI:
        CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nPeriCount;
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nPeri;
	    item=0;
 	    while(item<sizeof(Main)/sizeof(int) ){     //27
    		if(buttons[ Main[item] ]!=0){
        		m_idletools.AddToolItem(Main[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Main[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
	    break;

    default:
   		CountT=((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMainCount;
   		pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMain;
	    item=0;
 	    while(item<sizeof(Main)/sizeof(int) ){     //27
    		if(buttons[ Main[item] ]!=0){
        		m_idletools.AddToolItem(Main[item]);               
        		m_idletools.SetItemData(m_idletools.GetCount(),buttons[ Main[item] ]);
        	}	        
        	item++;
    	}        
    	m_idletools.AddToolItem(-1);               
    	m_idletools.SetItemData(m_idletools.GetCount(),-1);
    	break;
}
    item=0;  
    while(item<CountT){
        m_tools.AddToolItem(pButtonArray[item]); 
        item++;
    }  
    m_idletools.SetCurSel(0);
    m_tools.SetCurSel(0);

	m_tools.SetHorizontalExtent(200 );
	m_idletools.SetHorizontalExtent( 200 );

	GetDlgItem(IDC_APPLY)->EnableWindow(
      !(((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->\
        m_strUsrDefComm[0]).IsEmpty());
	return TRUE;
	 // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE

}

void CToolsDlg::OnRemove() 
{
	// TODO: Add your control notification handler code here
	
//	m_tools.GetCount()<=1
	int item=m_tools.GetCurSel(); 
    if(item==LB_ERR) return;
    //int item=m_tools.GetItemData(cursel);
	((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))\
	          ->RemoveButton(item); 
	m_tools.DeleteString(item);  
	
	m_tools.SetCurSel( min(item,m_tools.GetCount()-1 ) );               
    if(m_tools.GetCount()<MAX_BUTTON_NUM-1)
		GetDlgItem(ID_INSERT)->EnableWindow(1);
	if(m_tools.GetCount()<=1)
		GetDlgItem(IDOK2)->EnableWindow(0);     	
	
}


void CToolsDlg::OnReplace() 
{
	// TODO: Add your control notification handler code here
	int item=m_tools.GetCurSel();              
	int idlecursel=m_idletools.GetCurSel();   
	if(item==LB_ERR||idlecursel==LB_ERR) return ;

	int idleitem=(int)m_idletools.GetItemData(idlecursel);	
    ((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))\
	          ->ReplaceButton(item,idleitem);
    m_tools.DeleteString(item);
    m_tools.InsertString(item,(LPCSTR)(idleitem));  
    m_tools.SetCurSel(item);
    if(m_tools.GetCount()<MAX_BUTTON_NUM-1)
		GetDlgItem(IDC_APPLY)->EnableWindow(1); 
		
}


void CToolsDlg::OnInsert() 
{
	// TODO: Add your control notification handler code here
	int cursel=m_tools.GetCurSel();              
	int idlecursel=m_idletools.GetCurSel();   
	if(idlecursel==LB_ERR) return ;
	if(cursel==LB_ERR) cursel=m_tools.GetCount()-1;
//int item=(int)m_tools.GetItemData(cursel);
	int idleitem=(int)m_idletools.GetItemData(idlecursel);	

// 8/8/96
    if( (((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))\
	          ->m_isCmbSrch)&&
    	(idleitem==((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))\
	          ->BTN_SRCHCOM) ) ;
	else          
	{	          
	    m_tools.InsertString(cursel+1,(LPCSTR)(idleitem)); 
    	m_tools.SetCurSel(cursel+1);
    }
    ((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))\
	          ->InsertButton(cursel,idleitem,0);
    
    if(m_tools.GetCount()>=MAX_BUTTON_NUM-1)
		GetDlgItem(ID_INSERT)->EnableWindow(0);    	
    if(m_tools.GetCount()>=MAX_BUTTON_NUM-1)
		GetDlgItem(IDC_APPLY)->EnableWindow(0); 
	if(m_tools.GetCount()>1) 			        
	    GetDlgItem(IDOK2)->EnableWindow(1); 
    	
}


void CToolsDlg::OnSelchangeListbox1() 
{
	// TODO: Add your control notification handler code here 
	int cursel=m_tools.GetCurSel();
    UINT nActiveWin=((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->\
                 GetDlgItem(AFX_IDW_TOOLBAR)))->nActiveId;
    UINT item=0; 
	int*pButtonArray;
    switch(nActiveWin){
    case  WID_MAIN: 
    	pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMain;
		break;
    case  WID_CPU: 
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nCPU;
    	break;
    case WID_SHELL:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nShell;
	    break;
    case WID_TRACE:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nTrace;
	    break;
    case WID_MEMORY: 
    case WID_MEM2:
    case WID_MEM3:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMem;
	    break;
    case WID_SOURCE:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nSource;
	    break;
    case WID_STACK:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nStack;
	    break;
    case WID_VARIABLE:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nVar;
	    break;
    case WID_BMEM:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nBMem;
	    break;
    case WID_PERI:
	    pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nPeri;
	    break;
    default:
   		pButtonArray=(int*)((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->m_nMain;
    	break;
	}

/*	CRect rect;         
	GetDlgItem(IDC_STATIC1)->GetWindowRect(&rect);
	rect=rect+CPoint(0,35);
	rect.InflateRect(10,14);
    ScreenToClient(&rect);
*/
/*    if(pButtonArray[cursel]==-1){
      m_strText="Separator";           
//      SetDlgItemText(IDC_STATIC1,(str));
	  InvalidateRect(&rect);
	  UpdateWindow();	
    }         
    else if(pButtonArray[cursel]==33) //27
    {  m_strText="Search ComboBox";
	  InvalidateRect(&rect);
 	  UpdateWindow();	

//       SetDlgItemText(IDC_STATIC1,(str));
    }   
    else
    {
       m_strText.LoadString(buttons[pButtonArray[cursel]]);
       char* token;
       int n=m_strText.GetLength();
       token=strtok(m_strText.GetBuffer(n),"\n");
       m_strText.ReleaseBuffer();
  	  InvalidateRect(&rect);
	  UpdateWindow();	
//	   SetDlgItemText(IDC_STATIC1,token);
	}
*/   
  
}

void CToolsDlg::OnSelchangeListbox2() 
{
	// TODO: Add your control notification handler code here
	int cursel=m_idletools.GetCurSel();
    int item=((CListBox*)GetDlgItem(IDC_LISTBOX2))->GetItemData(cursel);
//	CString str;
/*    CRect rect;
	GetDlgItem(IDC_STATIC2)->GetWindowRect(&rect);
	rect=rect+CPoint(0,35);
	rect.InflateRect(10,14);
    ScreenToClient(&rect);


    if(item==-1)
    {
       m_strText2="Separator";
	  InvalidateRect(&rect);
	  UpdateWindow();	

//       SetDlgItemText(IDC_STATIC2,(str));
     }  
    else if(item==33)     //27   
    {
      m_strText2="Search ComboBox";
	  InvalidateRect(&rect);
	  UpdateWindow();	

//      SetDlgItemText(IDC_STATIC2,(str));
     }  
    else
    {
       m_strText2.LoadString(buttons[item]);
       char* token;
       int n=m_strText2.GetLength();
       token=strtok(m_strText2.GetBuffer(n),"\n");
       m_strText2.ReleaseBuffer();
	  InvalidateRect(&rect);
	  UpdateWindow();	
//	   SetDlgItemText(IDC_STATIC2,token);
	}
*/}        
void CToolsDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
    CRect rectUpPane,rectLowPane,rectClient;
    GetClientRect(&rectClient);
    
    rectUpPane.top=5;
    rectUpPane.left=10;
    GetDlgItem(IDC_STATIC_TITLE)->GetWindowRect(&rectLowPane);
    ScreenToClient(&rectLowPane);
    rectUpPane.right=rectClient.right-10;
    rectUpPane.bottom=rectLowPane.top-20;
    
    dc.MoveTo(rectUpPane.TopLeft());
    dc.LineTo(rectUpPane.left,rectUpPane.bottom);
    dc.LineTo(rectUpPane.BottomRight());
    dc.LineTo(rectUpPane.right,rectUpPane.top);
    dc.LineTo(rectUpPane.TopLeft());
    
    rectUpPane.InflateRect(-1,-1);
    dc.SelectStockObject(WHITE_PEN);
    dc.MoveTo(rectUpPane.TopLeft());
    dc.LineTo(rectUpPane.left,rectUpPane.bottom);
    dc.LineTo(rectUpPane.BottomRight());
    dc.LineTo(rectUpPane.right,rectUpPane.top);
    dc.LineTo(rectUpPane.TopLeft());

    rectLowPane=rectUpPane;
    rectLowPane.top=rectUpPane.bottom+10;
    rectLowPane.bottom=rectClient.bottom-10;
    
    dc.SelectStockObject(BLACK_PEN);
    dc.MoveTo(rectLowPane.TopLeft());
    dc.LineTo(rectLowPane.left,rectLowPane.bottom);
    dc.LineTo(rectLowPane.BottomRight());
    dc.LineTo(rectLowPane.right,rectLowPane.top);
    dc.LineTo(rectLowPane.TopLeft());
    
    rectLowPane.InflateRect(-1,-1);
    dc.SelectStockObject(WHITE_PEN);
    dc.MoveTo(rectLowPane.TopLeft());
    dc.LineTo(rectLowPane.left,rectLowPane.bottom);
    dc.LineTo(rectLowPane.BottomRight());
    dc.LineTo(rectLowPane.right,rectLowPane.top);
    dc.LineTo(rectLowPane.TopLeft());
    dc.SelectStockObject(BLACK_PEN);
///	dc.SelectObject(OldFont);
	// Do not call CDialog::OnPaint() for painting messages
}

void CToolsDlg::OnApply()
{
	int cursel=m_tools.GetCurSel();              
//	int idlecursel=m_idletools.GetCurSel();   
//	if(idlecursel==LB_ERR) return ;
	if(cursel==LB_ERR) cursel=m_tools.GetCount()-1;
//int item=(int)m_tools.GetItemData(cursel);
//	int idleitem=(int)m_idletools.GetItemData(idlecursel);	

    CString strEdit;
    GetDlgItem(IDC_COMM_EDIT)->GetWindowText(strEdit);
    if(strEdit.IsEmpty() ) return;
//  m_tools.InsertString(cursel+1,(LPCSTR)((LPCSTR)(m_pHList->m_nActiveItem+MAX_TOOLBAR_BUTTON+1)));
    ((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->\
          m_strUsrDefComm[m_pHList->m_nActiveItem]=strEdit;
 
    if( ((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->\
        IsButtonExsit(m_pHList->m_nActiveItem+MAX_TOOLBAR_BUTTON+1) )//34
    {
       return;
    }    
    else
    {
    	((CSuperToolBar*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))\
	          ->InsertButton(cursel,m_pHList->m_nActiveItem+MAX_TOOLBAR_BUTTON+1,\
	              ID_USER1+m_pHList->m_nActiveItem);
   	 	m_tools.InsertString(cursel+1,(LPCSTR)(m_pHList->m_nActiveItem+MAX_TOOLBAR_BUTTON+1));
        if(m_tools.GetCount()>=MAX_BUTTON_NUM-1)
			GetDlgItem(ID_INSERT)->EnableWindow(0);    	
    	if(m_tools.GetCount()>=MAX_BUTTON_NUM-1)
			GetDlgItem(IDC_APPLY)->EnableWindow(0);    	
	}		
}

void CToolsDlg::OnCancel()
{
	// TODO: Add extra cleanup here
	CDialog::OnCancel();
}

LONG CToolsDlg::OnXmHListSelectChange(UINT w,LONG)
{               
	GetDlgItem(IDC_APPLY)->EnableWindow(
      !(((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->\
        m_strUsrDefComm[w]).IsEmpty());
	return 1;
}

void CToolsDlg::OnKillfocusCommEdit()
{
	// TODO: Add your control notification handler code here
	if(GetDlgItem(IDC_APPLY)==GetFocus()) return;
	GetDlgItem(IDC_APPLY)->EnableWindow(
      !(((CCONTROL*)(AfxGetApp()->m_pMainWnd->GetDlgItem(AFX_IDW_TOOLBAR)))->\
        m_strUsrDefComm[m_pHList->m_nActiveItem]).IsEmpty());
}

void CToolsDlg::OnChangeCommEdit()
{
	// TODO: Add your control notification handler code here
	CString str;
	GetDlgItem(IDC_COMM_EDIT)->GetWindowText(str);
	GetDlgItem(IDC_APPLY)->EnableWindow(!str.IsEmpty());	
}
