📄 memoryeditorlist.cpp
字号:
/////////////////////////////////////////////////////////////////////////////
//
// FILE: MemoryEditorList.cpp : implementation file
//
// See "_README.CPP"
//
// Implements a owner data (callback) style CListCtrl
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "MemoryEditorList.h"
#include "EditMemoryDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CHAR * PLCViewerFormats[7] =
{
"",
"%d", // VIEWFORMAT_DECIMAL =1,
"%04X", // VIEWFORMAT_HEX,
"%u", // VIEWFORMAT_WORD,
"%u", // VIEWFORMAT_DWORD,
"%ld", // VIEWFORMAT_LONG,
"%f" // VIEWFORMAT_FLOAT
};
CHAR * ViewerFormatNames[7] =
{
"",
"decimal", // VIEWFORMAT_DECIMAL =1,
"hex", // VIEWFORMAT_HEX,
"word 16", // VIEWFORMAT_WORD,
"dword 32", // VIEWFORMAT_DWORD,
"long 32", // VIEWFORMAT_LONG,
"float 32" // VIEWFORMAT_FLOAT
};
/*
// --------------- SwopWords (Global Generic) --------------------------------
// PURPOSE : Converts DWORD from large endian to small endian
// (and back if called again)
DWORD SwopWords(DWORD * x)
{
DWORD loWord;
DWORD hiWord;
loWord = LOWORD(*x);
hiWord = HIWORD(*x);
*x = (loWord<<16) + hiWord ;
return(*x);
} // SwopWords*/
// ------------------------- GetPLCMemoryLimit --------------------------------
DWORD CMOD_simDlg::GetPLCMemoryLimit(DWORD area)
{
//if (m_busyCreatingServers)
// return(0);
return PLCMemory[area].GetSize();
}
// ---------------------------- GetPLCMemoryValue -----------------------------
DWORD CMOD_simDlg::GetPLCMemoryValue(DWORD area,DWORD offset, WORD type)
{
DWORD dwValue=0;
ASSERT(area < (DWORD)GetNumMemoryAreas());
if (offset >= MAX_MOD_MEMWORDS)
return (0);
//ASSERT(offset < MAX_MOD_MEMWORDS);
switch(type)
{
case CMemoryEditorList::VIEWFORMAT_DECIMAL:
case CMemoryEditorList::VIEWFORMAT_HEX:
case CMemoryEditorList::VIEWFORMAT_WORD:
dwValue = PLCMemory[area][offset];
break;
case CMemoryEditorList::VIEWFORMAT_DWORD:
case CMemoryEditorList::VIEWFORMAT_LONG:
dwValue = PLCMemory[area][offset]<<16;
if (offset < MAX_MOD_MEMWORDS)
dwValue += PLCMemory[area][offset+1];
break;
case CMemoryEditorList::VIEWFORMAT_FLOAT:
dwValue = PLCMemory[area][offset]<<16;
if (offset < MAX_MOD_MEMWORDS)
dwValue += PLCMemory[area][offset+1];
if (pGlobalDialog->IsClone())
SwopWords(&dwValue); //clone PLC's have a swapped float
break;
default:
ASSERT(0);
break;
}
return (dwValue);
} // GetPLCMemoryValue
/////////////////////////////////////////////////////////////////////////////
// CMemoryEditorList
class CMOD_simDlg;
extern CMOD_simDlg *pGlobalDialog;
CMemoryEditorList::CMemoryEditorList()
{
for (int i=0;i<sizeof(m_selectedListPos)/sizeof(m_selectedListPos[0])/*MAX_MOD_MEMTYPES */;i++)
m_selectedListPos[i] = 0;
m_memAreaDisplayed = (DWORD)-1;
SetAreaDisplayed(MODBUS_MEM_REGISTERS);
SetDisplayedWidth(10);
m_addressFormatHEX = FALSE;
SetDataViewFormat(VIEWFORMAT_DECIMAL);
m_backSysColor = GetSysColor(COLOR_WINDOW);
int red= GetRValue(m_backSysColor),
blue= GetBValue(m_backSysColor),
green= GetGValue(m_backSysColor);
if ((red+blue+green) >= (250*3))
{
red=blue=green=155;
}
int ave = (256-(int)((red+blue+green)/6.0));
red += ave;
green += ave;
blue += ave;
m_backSysColorFade = RGB(red,green, blue);
}
CMemoryEditorList::~CMemoryEditorList()
{
}
// ------------------------------ SetAreaDisplayed -----------------------------
LONG CMemoryEditorList::SetAreaDisplayed(LONG PLCArea)
{
// save the current place.
POSITION pos;
// make sure that the control at least exists
if ((DWORD)-1 !=m_memAreaDisplayed)
{
pos = GetFirstSelectedItemPosition();
m_selectedListPos[m_memAreaDisplayed] = GetNextSelectedItem(pos);
#ifdef _UI_DEBUGGING
CString msg;
msg.Format("Selection was at %d in %d.\n", m_selectedListPos[m_memAreaDisplayed],m_memAreaDisplayed);
OutputDebugString(msg);
#endif // _UI_DEBUGGING
}
else
m_selectedListPos[m_memAreaDisplayed] = 0; // first time in here
m_memAreaDisplayed = PLCArea;
return (m_memAreaDisplayed);
} // SetAreaDisplayed
// ---------------------------------- GetViewFormat ----------------------------
// return a suitable format for the current data type
CMemoryEditorList::ListViewFormats_ CMemoryEditorList::GetViewFormat()
{
if ((pGlobalDialog->m_selectedProtocol == PROTOCOL_SELABMASTER232) ||
(pGlobalDialog->m_selectedProtocol == PROTOCOL_SELAB232))
return(m_viewFormat);
//
switch (m_memAreaDisplayed)
{
case MODBUS_MEM_OUTPUTS:
case MODBUS_MEM_INPUTS:
return (VIEWFORMAT_DECIMAL);
break;
default:
break;
}
return (m_viewFormat);
}
// ----------------------------- GetViewFormatString ---------------------------
LPCTSTR CMemoryEditorList::GetViewFormatString()
{
// call our member Fn
return (PLCViewerFormats[GetViewFormat()]);
}
// --------------------------------- GetFormatName ----------------------------
LPCTSTR CMemoryEditorList::GetFormatName()
{
// call our member Fn
return (ViewerFormatNames[GetViewFormat()]);
}
// ------------------------------ SetupColumns --------------------------------
// populate the control's headdings
void CMemoryEditorList::SetupColumns(LONG width, BOOL cloneAddressing)
{
int nColumnCount;
m_displayableWidth = (WORD)width-1;
m_cloneAddressing = cloneAddressing;
// start out fresh
nColumnCount = GetHeaderCtrl()->GetItemCount();
// Delete all of the columns.
for (int i=0;i < nColumnCount;i++)
{
DeleteColumn(0);
}
// set up the column headings on the list-view control
{
LV_COLUMN lvColumn;
int i;
TCHAR szString[NUMBER_LISTCOLS][20] = {TEXT(" Address"),
TEXT(" +0"),
TEXT(" +1"),
TEXT(" +2"),
TEXT(" +3"),
TEXT(" +4"),
TEXT(" +5"),
TEXT(" +6"),
TEXT(" +7"),
TEXT(" +8"),
TEXT(" +9")};
if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
{
// provide headdings for bit addresses
strcpy(szString[0], TEXT(" Address") );
for (int i=0 ; i<=15 ; i++)
{
if (cloneAddressing)
sprintf(szString[i+1], TEXT(" +%d"), i );
else
sprintf(szString[i+1], TEXT(" +%d"), 15-i );
}
strcpy(szString[i+1], TEXT(" Total") );
m_displayableWidth++;
}
// initialize the columns
lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvColumn.fmt = LVCFMT_LEFT;
lvColumn.cx = WIDTH_LISTCOL1; // 1st col is wider
for(i = 0; i < (int)m_displayableWidth+1; i++)
{
lvColumn.pszText = szString[i];
// work out width for all columns except the "address" column
if (i!=0)
{
if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
{ // col width for all bits
if (i!=17) // all columns but the last one "total"
{
lvColumn.cx = WIDTH_LISTCOLBIT; // digitals are narrower
if (strlen(szString[i])==3) // Make 0..9 narrower still
lvColumn.cx -=6; // make it narrower by 5 DLU's
}
else
lvColumn.cx = WIDTH_LISTCOLTOTAL; // "total" column
}
else
{
// col width for a register
lvColumn.cx = WIDTH_LISTCOLS;
}
}
SendMessage(LVM_INSERTCOLUMN, (WPARAM)i, (LPARAM)&lvColumn);
}
if (pGlobalDialog->PLCIsBitMemory(m_memAreaDisplayed))
m_displayableWidth--; // total column is not data
}
// set listview item count to maximum
DWORD maximumOffset = pGlobalDialog->GetPLCMemoryLimit(m_memAreaDisplayed);
WORD numListItem = (WORD)ceil(maximumOffset/*MAX_MOD_MEMWORDS*//((float)m_displayableWidth));
SendMessage(LVM_SETITEMCOUNT, (WPARAM)(numListItem), (LPARAM)LVSICF_NOINVALIDATEALL);
EnsureVisible(m_selectedListPos[m_memAreaDisplayed], FALSE);
} // SetupColumns
BEGIN_MESSAGE_MAP(CMemoryEditorList, CListCtrl)
//{{AFX_MSG_MAP(CMemoryEditorList)
ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
ON_NOTIFY_REFLECT(LVN_GETDISPINFO, OnGetdispinfo)
//}}AFX_MSG_MAP
ON_NOTIFY_REFLECT ( NM_CUSTOMDRAW, OnCustomdrawMyList )
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMemoryEditorList message handlers
// --------------------------- OnDblclk ---------------------------------
void CMemoryEditorList::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
{
POINT pt;
RECT rect;
CHAR position[80];
LVHITTESTINFO htInfo;
LONG absItemNumber;
// get cursor position
GetCursorPos(&pt);
// convert point to client area co-ordinates
rect.top = pt.y;
rect.bottom = pt.y;
rect.left = pt.x;
rect.right = pt.x;
ScreenToClient(&rect);
// do a hit test
htInfo.pt.x = rect.left;
htInfo.pt.y = rect.top;
SubItemHitTest(&htInfo); // this will return the Item
//**** 20021101 add by HF
if(htInfo.iSubItem == 0) return;
//**** 20021101 end
if (htInfo.flags & LVHT_ONITEMLABEL)
{
CString description, descrFormat;
WORD memPrefix;
CString registerName;
#ifdef _UI_DEBUGGING
sprintf(position, "\nEditable data columns =%d |", m_displayableWidth);
OutputDebugString(position);
sprintf(position, "Selected at %d, %d (%d,%d)\n", rect.left, rect.top, htInfo.iItem, htInfo.iSubItem);
OutputDebugString(position);
#endif
// absItemNumber = (htInfo.iItem * m_displayableWidth) + htInfo.iSubItem-1;// work out a zero-based index
// absItemNumber = (htInfo.iItem * (NUMBER_LISTCOLS-1)) + htInfo.iSubItem-1;
// **** 20021101 mod by HF start
absItemNumber = (htInfo.iItem * (m_displayableWidth)) + htInfo.iSubItem-1;
// absItemNumber = (htInfo.iItem * (BITS_IN_WORD-1)) + htInfo.iSubItem-1;
// **** 20021101 mod by HF end
memPrefix = PLCMemPrefixes[m_memAreaDisplayed];
descrFormat.Format("Edit %s value at %s", GetFormatName(), GetAddressFormat());
description.Format(descrFormat, (IsAddressFormatHex()?0:memPrefix*10000) + absItemNumber+1);
if ((pGlobalDialog->m_seperateRegisters) && (PROTOCOL_SELMOD232 == pGlobalDialog->m_selectedProtocol))
{ // work out which station and register # this item belongs to if we are iusing seperate modbus reg. /station
registerName.Format("Station #%d , I/O #%d", (WORD)(absItemNumber / pGlobalDialog->m_numSeperate), (WORD)(absItemNumber % pGlobalDialog->m_numSeperate));
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -