Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Read and Write application parameters in XML

, 30 Jun 2003
This article provides an easy way to load and save the parameters of an application in XML format.
// ListCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "ListCtrl.h"
#include "EditCell.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define IDC_EDITCELL 1001

/////////////////////////////////////////////////////////////////////////////
// gxListCtrl

gxListCtrl::gxListCtrl(CString Text /* = "Some Text" */)
{
    DefaultText = Text;

	 //Arnaud
	_treeWnd = 0;

   Item = SubItem = 0;
}

gxListCtrl::~gxListCtrl()
{
}

BEGIN_MESSAGE_MAP(gxListCtrl, CListCtrl)
    //{{AFX_MSG_MAP(gxListCtrl)
    ON_WM_HSCROLL()
    ON_WM_VSCROLL()
    ON_NOTIFY_REFLECT_EX(LVN_ENDLABELEDIT, OnEndLabelEdit)
    ON_WM_LBUTTONDOWN()
	 ON_WM_KEYDOWN()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// gxListCtrl message handlers

void 
gxListCtrl::Resize (int cx, int cy)
{
    CRect Rect (0, 0, cx, cy);
    MoveWindow (&Rect);
    InvalidateRect (Rect);
    SetColumnWidth (2, LVSCW_AUTOSIZE_USEHEADER);
}

CEdit* 
gxListCtrl::EditSubItem (int item, int Column)
{
    // The returned pointer should not be saved
    Item = item;
    
    // Make sure that the item is visible
    if (!EnsureVisible (Item, TRUE)) 
    {
		return NULL;
    }

    // Make sure that nCol is valid
    CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
    int nColumnCount = pHeader->GetItemCount();
    if (Column >= nColumnCount || GetColumnWidth (Column) < 5)
		return NULL;

    // Get the column offset
    int Offset = 0;
    for (int iColumn = 0; iColumn < Column; iColumn++)
		Offset += GetColumnWidth (iColumn);

    CRect Rect;
    GetItemRect (Item, &Rect, LVIR_BOUNDS);

    // Now scroll if we need to expose the column
    CRect ClientRect;
    GetClientRect (&ClientRect);
    if (Offset + Rect.left < 0 || Offset + Rect.left > ClientRect.right)
    {
		CSize Size;
		if (Offset + Rect.left > 0)
			Size.cx = -(Offset - Rect.left);
		else
			Size.cx = Offset - Rect.left;
		Size.cy = 0;
		Scroll (Size);
		Rect.left -= Size.cx;
    }

    // Get Column alignment
    LV_COLUMN lvCol;
    lvCol.mask = LVCF_FMT;
    GetColumn (Column, &lvCol);
    DWORD dwStyle;
    if ((lvCol.fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_LEFT)
		dwStyle = ES_LEFT;
    else if ((lvCol.fmt & LVCFMT_JUSTIFYMASK) == LVCFMT_RIGHT)
		dwStyle = ES_RIGHT;
    else dwStyle = ES_CENTER;

    Rect.left += Offset+4;
    Rect.right = Rect.left + GetColumnWidth (Column) - 3;
    if (Rect.right > ClientRect.right)
		Rect.right = ClientRect.right;

    dwStyle |= WS_BORDER | WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL;

	 CString text = GetItemText(Item, 0);
	 text.MakeUpper();
	 bool withButton = false;
	 if(text.Find("FILENAME")>=0)
	 {
		 withButton = true;
	 }

    SubItem = Column;
    gxEditCell  *pEdit = new gxEditCell (this, Item, Column, GetItemText (Item, Column), withButton);
    pEdit->Create (dwStyle, Rect, this, IDC_EDITCELL);
	 // Give the XML infos to the edit, ARNAUD
	 pEdit->setNode(_node, GetItemText(Item, 0));
	 pEdit->setTreeWnd(_treeWnd);

    // Notify parent window
    

    return pEdit;
}

int 
gxListCtrl::HitTestEx (CPoint& Point, int* pColumn)
{
    int ColumnNum = 0;
    int Row = HitTest (Point, NULL);
    
    if (pColumn)
		*pColumn = 0;

    // Make sure that the ListView is in LVS_REPORT
    if ((GetWindowLong (m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT)
		return Row;

    // Get the top and bottom row visible
    Row = GetTopIndex();
    int Bottom = Row + GetCountPerPage();
    if (Bottom > GetItemCount())
	    Bottom = GetItemCount();
    
    // Get the number of columns
    CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
    int nColumnCount = pHeader->GetItemCount();

    // Loop through the visible rows
    for(; Row <= Bottom; Row++)
    {
		// Get bounding rect of item and check whether point falls in it.
		CRect Rect;
		GetItemRect (Row, &Rect, LVIR_BOUNDS);
		if (Rect.PtInRect (Point))
		{
			// Now find the column
			for (ColumnNum = 0; ColumnNum < nColumnCount; ColumnNum++)
			{
				int ColWidth = GetColumnWidth (ColumnNum);
				if (Point.x >= Rect.left && Point.x <= (Rect.left + ColWidth))
				{
					if (pColumn)
						*pColumn = ColumnNum;

					if(ColumnNum == 0)
					{
						return -1;
					}
					return Row;
				}
				Rect.left += ColWidth;
			}
		}
    }
    return -1;
}

BOOL 
hexNumberToInt (CString HexNumber, int& Number)
{
    char* pStopString;
    Number = strtoul (HexNumber, &pStopString, 16);
    return Number != ULONG_MAX;
} // hexNumberToInt

int 
gxListCtrl::InsertItemEx (int Item)
{
    int Result = InsertItem (Item + 1, DefaultText);
    CString ItemVal, Temp;
    if (Item == 0)
		ItemVal = "1000";
    else
	{
		int HexVal;
		Temp = GetItemText (Item - 1, 1);
		hexNumberToInt (Temp, HexVal);
		ItemVal.Format ("%x", HexVal + 1);
	}

    SetItemText (Item, 1, ItemVal);

    SetColumnWidth (2, LVSCW_AUTOSIZE_USEHEADER);

    return Result;
}

void 
gxListCtrl::OnHScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    if (GetFocus() != this) 
		SetFocus();
    
    CListCtrl::OnHScroll (nSBCode, nPos, pScrollBar);
}

void 
gxListCtrl::OnVScroll (UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
    if (GetFocus() != this) 
		SetFocus();
    
    CListCtrl::OnVScroll (nSBCode, nPos, pScrollBar);
}

BOOL 
gxListCtrl::OnEndLabelEdit (NMHDR* pNMHDR, LRESULT* pResult) 
{
    LV_DISPINFO *plvDispInfo = (LV_DISPINFO *)pNMHDR;
    LV_ITEM	*plvItem = &plvDispInfo->item;

    if (plvItem->pszText != NULL)
    {
		SetItemText (plvItem->iItem, plvItem->iSubItem, plvItem->pszText);

      GetParent()->SendMessage(WM_MODIFIED_ELEMENT);
    }
    *pResult = TRUE;

	return TRUE;
}

void 
gxListCtrl::OnLButtonDown (UINT nFlags, CPoint Point) 
{
    CListCtrl::OnLButtonDown (nFlags, Point);

    int Index;
    int ColNum;
    if ((Index = HitTestEx (Point, &ColNum)) != -1)
    {
		if (GetWindowLong (m_hWnd, GWL_STYLE) & LVS_EDITLABELS)
			EditSubItem (Index, ColNum);
    }
}

void
gxListCtrl::OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
{
    // Up and down are in the OnKeyDown so that the user can hold down the arrow
    // keys to scroll through the entries.
    BOOL Control = GetKeyState (VK_CONTROL) < 0;
    switch (nChar)
    {
		case VK_UP :
		{
			if (Item > 0)
				EditSubItem (Item - 1, SubItem);
			return;
		}
		case VK_DOWN :
		{
         if(Item < GetItemCount())
			   EditSubItem (Item + 1, SubItem);
			return;
		}
		case VK_NEXT :
		{
			int Count = GetItemCount();
			int NewItem = Item + GetCountPerPage();
			if (Count > NewItem)
				EditSubItem (NewItem, SubItem);
			else
				EditSubItem (Count - 1, SubItem);
			return;
		}
		case VK_PRIOR :
		{
			int NewItem = Item - GetCountPerPage();
			if (NewItem > 0)
				EditSubItem (NewItem, SubItem);
			else
				EditSubItem (0, SubItem);
			return;
		}
		case VK_HOME :
		{
			if (!Control)
				break;

			EditSubItem (0, SubItem);
			return;
		}
		case VK_END :
		{
			if (!Control)
				break;

			int Count = GetItemCount() - 1;
			EditSubItem (Count, SubItem);
			return;
		}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Arnaud Brejeon
Web Developer
China China
I've been living & working in Tokyo since 2000 . I'm currently working in Gentech Corp. (www.gen.co.jp), developing computer vision applications, especially in the field of face detection.
 
I've been interested in C++ for quite a while and recently discovered Ruby.
 
My hobbies are trekking, japanese food, yoga & onsens.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 1 Jul 2003
Article Copyright 2001 by Arnaud Brejeon
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid