// CustomDrawGridDlg.cpp // // (c)1998-2025 Codejock Software, All Rights Reserved. // // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN // CONSENT OF CODEJOCK SOFTWARE. // // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A // SINGLE COMPUTER. // // CONTACT INFORMATION: // support@codejock.com // http://www.codejock.com // ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CustomDrawGrid.h" #include "CustomDrawGridDlg.h" #include "MessageRecord.h" #include "XTPCustomDrawGridPaintManager.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CCustomDrawGridDlg dialog CCustomDrawGridDlg::CCustomDrawGridDlg(CWnd* pParent /*=NULL*/) : CXTPResizeDialog(CCustomDrawGridDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCustomDrawGridDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_nDrawingMethod = 1; } void CCustomDrawGridDlg::DoDataExchange(CDataExchange* pDX) { CXTPResizeDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCustomDrawGridDlg) DDX_Control(pDX, IDC_GRIDCTRL, m_wndGridCtrl); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCustomDrawGridDlg, CXTPResizeDialog) //{{AFX_MSG_MAP(CCustomDrawGridDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_DESTROY() ON_COMMAND(IDM_USE_DRAWFRAMECONTROL, OnUseDrawframecontrol) ON_COMMAND(IDM_USE_DRAWSTATE, OnUseDrawstate) ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCustomDrawGridDlg message handlers enum { GRIDCOLUMN_NAME, GRIDCOLUMN_ATTR1, GRIDCOLUMN_ATTR2, GRIDCOLUMN_ATTR3, }; const int COLUMN_MAIL_ICON = 0; const int COLUMN_CHECK_ICON = 2; BOOL CCustomDrawGridDlg::OnInitDialog() { CXTPResizeDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. _ASSERTE((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); _ASSERTE(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // // create main menu. // CMenu* pMenu = GetMenu(); if (pMenu) { CMenu* pSubMenu = pMenu->GetSubMenu(0); if (pSubMenu) pSubMenu->CheckMenuRadioItem(IDM_USE_DRAWFRAMECONTROL, IDM_USE_DRAWSTATE, IDM_USE_DRAWFRAMECONTROL, MF_BYCOMMAND); } // // load the image bitmap and set the image list for the // Grid control. // CBitmap bitmap; int cx = 0, cy = 0; int dpi = XTPDpiHelper()->GetDPIX(); if (96 <= dpi && dpi < 120) { cx = cy = 16; VERIFY(bitmap.LoadBitmap(IDB_CONTROLS_16)); } else if (120 <= dpi && dpi < 144) { cx = cy = 20; VERIFY(bitmap.LoadBitmap(IDB_CONTROLS_20)); } else if (144 <= dpi && dpi < 192) { cx = cy = 24; VERIFY(bitmap.LoadBitmap(IDB_CONTROLS_24)); } else { cx = cy = 32; VERIFY(bitmap.LoadBitmap(IDB_CONTROLS_32)); } // // create the image lists used by the Grid control. // if (!m_ilControls.Create(cx, cy, ILC_COLOR24 | ILC_MASK, 0, 1)) return -1; m_ilControls.Add(&bitmap, RGB(255, 0, 255)); // // Add sample columns // m_wndGridCtrl.AddColumn(new CXTPGridColumn(GRIDCOLUMN_NAME, _T("Name"), 150)); m_wndGridCtrl.AddColumn(new CXTPGridColumn(GRIDCOLUMN_ATTR1, _T("Attribute 1"), 150)); m_wndGridCtrl.AddColumn(new CXTPGridColumn(GRIDCOLUMN_ATTR2, _T("Attribute 2"), 150)); m_wndGridCtrl.AddColumn(new CXTPGridColumn(GRIDCOLUMN_ATTR3, _T("Attribute 3"), 150)); m_wndGridCtrl.GetColumns()->GetAt(GRIDCOLUMN_ATTR3)->SetAlignment(DT_VCENTER); // // Add sample records in tree // CString strName; CString strProgress; for (int i = 0; i < 10; i++) { strName.Format(_T("Parameter %d"), i); strProgress.Format(_T("Progress %d: (%d%%)"), i, 0); m_wndGridCtrl.AddRecord(new CMessageRecord(strName, 4, i, 4, 1 << (i & 3), strProgress, RGB(0, 0, 100), 15, 0, 100, i + 1, 0)); } // // define style attributes for the Grid control. // m_wndGridCtrl.GetColumns()->Find(GRIDCOLUMN_NAME)->SetTreeColumn(TRUE); CXTPCustomDrawGridPaintManager* pPaintManager = new CXTPCustomDrawGridPaintManager(this); m_wndGridCtrl.SetPaintManager(pPaintManager); // // after columns and data have been added call Populate to // populate all of the date for the control. // m_wndGridCtrl.Populate(); m_wndGridCtrl.EnableDragDrop(_T("CustomDrawGrid"), xtpGridAllowDrag | xtpGridAllowDrop); #ifdef _XTP_INCLUDE_CONTROLS // Set control resizing. SetResize(IDC_GRIDCTRL, XTP_ANCHOR_TOPLEFT, XTP_ANCHOR_BOTTOMRIGHT); SetResize(IDCANCEL, XTP_ANCHOR_BOTTOMRIGHT, XTP_ANCHOR_BOTTOMRIGHT); SetResize(IDOK, XTP_ANCHOR_BOTTOMRIGHT, XTP_ANCHOR_BOTTOMRIGHT); // Load window placement LoadPlacement(_T("CCustomDrawGridDlg")); // start timer SetTimer(0, 250, NULL); #endif return TRUE; // return TRUE unless you set the focus to a control } void CCustomDrawGridDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CXTPResizeDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CCustomDrawGridDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM)dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CXTPResizeDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CCustomDrawGridDlg::OnQueryDragIcon() { return (HCURSOR)m_hIcon; } void CCustomDrawGridDlg::OnDestroy() { CXTPResizeDialog::OnDestroy(); #ifdef _XTP_INCLUDE_CONTROLS // Save window placement SavePlacement(_T("CCustomDrawGridDlg")); #endif } unsigned int CCustomDrawGridDlg::GetControlsNumber(CXTPGridRecordItem* pItem) { if (pItem && pItem->GetIndex() == 1) return ((CMessageRecordItemCheckGroup*)pItem)->GetControlsNumber(); else if (pItem && pItem->GetIndex() == 2) return ((CMessageRecordItemRadioGroup*)pItem)->GetControlsNumber(); return 0; } unsigned int CCustomDrawGridDlg::GetControlsNumber(CXTPGridRow* pRow) { if (!pRow) return 0; unsigned int unControls = 0; CXTPGridRecordItem* pItem = pRow->GetRecord()->GetItem(1); unControls = GetControlsNumber(pItem); pItem = pRow->GetRecord()->GetItem(2); unControls = max(unControls, GetControlsNumber(pItem)); return unControls; } int CCustomDrawGridDlg::GetRowHeight(CDC* /*pDC*/, CXTPGridRow* pRow) { unsigned int unControls = 0; CXTPGridRecordItem* pItem = pRow->GetRecord()->GetItem(1); if (pItem) unControls = max(unControls, ((CMessageRecordItemCheckGroup*)pItem)->GetControlsNumber()); pItem = pRow->GetRecord()->GetItem(2); if (pItem) unControls = max(unControls, ((CMessageRecordItemRadioGroup*)pItem)->GetControlsNumber()); if (!pRow->IsGroupRow()) { return unControls * XTP_DPI_X(15) + (m_wndGridCtrl.GetPaintManager()->IsGridVisible(FALSE) ? XTP_DPI_X(1) : XTP_DPI_X(0)); } if (m_wndGridCtrl.GetPaintManager()->m_bShadeGroupHeadings) return unControls * XTP_DPI_X(15) + XTP_DPI_X(6); return unControls * XTP_DPI_X(15) + XTP_DPI_X(16); } BOOL CCustomDrawGridDlg::DrawItem(XTP_GRIDRECORDITEM_DRAWARGS* pDrawArgs, XTP_GRIDRECORDITEM_METRICS* /*pMetrics*/) { if (pDrawArgs->pColumn->GetIndex() != GRIDCOLUMN_ATTR1 && pDrawArgs->pColumn->GetIndex() != GRIDCOLUMN_ATTR2) return FALSE; CRect rc = pDrawArgs->pRow->GetItemRect(pDrawArgs->pItem); rc.left += XTP_DPI_X(5); rc.right = rc.left + XTP_DPI_X(14); rc.bottom = rc.top + XTP_DPI_Y(14); for (unsigned int i = 0; i < GetControlsNumber(pDrawArgs->pItem); i++) { UINT unFlags; UINT unIconId; CString strText; if (pDrawArgs->pColumn->GetIndex() == 1) { strText.Format(_T("%s%d"), _T("T"), i + 1); unFlags = DFCS_BUTTONCHECK; CMessageRecordItemCheckGroup* pRecord = (CMessageRecordItemCheckGroup*)pDrawArgs->pItem; if (pRecord->GetValue() & (1 << i)) { unFlags |= DFCS_CHECKED; unIconId = 1; } else { unFlags &= ~DFCS_CHECKED; unIconId = 0; } } else { strText.Format(_T("%s%d"), _T("V"), i + 1); unFlags = DFCS_BUTTONRADIO; CMessageRecordItemRadioGroup* pRecord = (CMessageRecordItemRadioGroup*)pDrawArgs->pItem; if (pRecord->GetValue() & (1 << i)) { unFlags |= DFCS_CHECKED; unIconId = 3; } else { unFlags &= ~DFCS_CHECKED; unIconId = 2; } } if (m_nDrawingMethod == 1) ::DrawFrameControl(pDrawArgs->pDC->GetSafeHdc(), rc, DFC_BUTTON, unFlags); else m_ilControls.Draw(pDrawArgs->pDC, unIconId, rc.TopLeft(), ILD_NORMAL); TextOut(pDrawArgs->pDC->GetSafeHdc(), rc.left + XTP_DPI_X(20), rc.top, strText, strText.GetLength()); rc.top += XTP_DPI_Y(15); rc.bottom += XTP_DPI_Y(15); } return TRUE; } BOOL CCustomDrawGridDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_LBUTTONDOWN) { CPoint pt = pMsg->pt; m_wndGridCtrl.ScreenToClient(&pt); CXTPGridRow* pRow = m_wndGridCtrl.HitTest(pt); if (pRow) { CXTPGridRecordItem* pItem = pRow->HitTest(pt); if (pItem) { if (pItem->GetIndex() < 1) return 0; unsigned int unControls = GetControlsNumber(pItem); if (unControls == 0) return 0; CRect rc = pRow->GetItemRect(pItem); rc.right = rc.left + XTP_DPI_X(14) + XTP_DPI_X(25); rc.bottom = rc.top + XTP_DPI_Y(14); for (unsigned int i = 0; i < unControls; i++) { if (rc.PtInRect(pt)) { if (pItem->GetIndex() == 1) { ((CMessageRecordItemCheckGroup*)pItem)->ToggleBit(i); } else { ((CMessageRecordItemRadioGroup*)pItem)->SetValue(1 << i); } } rc.OffsetRect(XTP_DPI_X(0), XTP_DPI_Y(15)); } } } } return CXTPResizeDialog::PreTranslateMessage(pMsg); } void CCustomDrawGridDlg::OnUseDrawframecontrol() { if (m_nDrawingMethod == 1) return; m_nDrawingMethod = 1; CMenu* pMenu = GetMenu(); if (pMenu) { CMenu* pSubMenu = pMenu->GetSubMenu(0); if (pSubMenu) pSubMenu->CheckMenuRadioItem(IDM_USE_DRAWFRAMECONTROL, IDM_USE_DRAWSTATE, IDM_USE_DRAWFRAMECONTROL, MF_BYCOMMAND); } m_wndGridCtrl.RedrawControl(); } void CCustomDrawGridDlg::OnUseDrawstate() { if (m_nDrawingMethod == 2) return; m_nDrawingMethod = 2; CMenu* pMenu = GetMenu(); if (pMenu) { CMenu* pSubMenu = pMenu->GetSubMenu(0); if (pSubMenu) pSubMenu->CheckMenuRadioItem(IDM_USE_DRAWFRAMECONTROL, IDM_USE_DRAWSTATE, IDM_USE_DRAWSTATE, MF_BYCOMMAND); } m_wndGridCtrl.RedrawControl(); } void CCustomDrawGridDlg::OnTimer(UINT_PTR nIDEvent) { // advance progresses for (int i = 0; i < m_wndGridCtrl.GetRecords()->GetCount(); i++) { CXTPGridRecordItemProgress* pItem = DYNAMIC_DOWNCAST(CXTPGridRecordItemProgress, m_wndGridCtrl.GetRecords()->GetAt(i)->GetItem(GRIDCOLUMN_ATTR3)); pItem->StepProgressPos(); int nLower, nUpper; pItem->GetProgressRange(nLower, nUpper); if (pItem->GetProgressPos() == nLower || pItem->GetProgressPos() == nUpper) pItem->SetProgressStep(-pItem->GetProgressStep()); CString strProgress; strProgress.Format(_T("Progress %d: (%d%%)"), i, pItem->GetProgressPosPercent()); pItem->SetValue(strProgress); } m_wndGridCtrl.RedrawControl(); CXTPResizeDialog::OnTimer(nIDEvent); }