PropertyList.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // PropertyList.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "PropertyList.h"
  5. #define IDC_PROPCMBBOX 712
  6. #define IDC_PROPEDITBOX 713
  7. #define IDC_PROPBTNCTRL 714
  8. #define IDC_PROPCHECKBOXCTRL 715
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CPropertyList
  11. CPropertyList::CPropertyList()
  12. {
  13. m_Dirty = false;
  14. }
  15. CPropertyList::~CPropertyList()
  16. {
  17. for(std::set<CPropertyItem*>::iterator i = m_PropertyItems.begin();
  18. i != m_PropertyItems.end(); ++i)
  19. {
  20. delete *i;
  21. }
  22. }
  23. BEGIN_MESSAGE_MAP(CPropertyList, CListBox)
  24. //{{AFX_MSG_MAP(CPropertyList)
  25. ON_WM_CREATE()
  26. ON_CONTROL_REFLECT(LBN_SELCHANGE, OnSelchange)
  27. ON_WM_LBUTTONUP()
  28. ON_WM_KILLFOCUS()
  29. ON_WM_LBUTTONDOWN()
  30. ON_WM_RBUTTONUP()
  31. ON_WM_MOUSEMOVE()
  32. //}}AFX_MSG_MAP
  33. ON_CBN_KILLFOCUS(IDC_PROPCMBBOX, OnKillfocusCmbBox)
  34. ON_CBN_SELCHANGE(IDC_PROPCMBBOX, OnSelchangeCmbBox)
  35. ON_EN_KILLFOCUS(IDC_PROPEDITBOX, OnKillfocusEditBox)
  36. ON_EN_CHANGE(IDC_PROPEDITBOX, OnChangeEditBox)
  37. ON_BN_CLICKED(IDC_PROPBTNCTRL, OnButton)
  38. ON_BN_CLICKED(IDC_PROPCHECKBOXCTRL, OnCheckBox)
  39. ON_COMMAND(42, OnDelete)
  40. END_MESSAGE_MAP()
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CPropertyList message handlers
  43. BOOL CPropertyList::PreCreateWindow(CREATESTRUCT& cs)
  44. {
  45. if (!CListBox::PreCreateWindow(cs))
  46. return FALSE;
  47. cs.style &= ~(LBS_OWNERDRAWVARIABLE | LBS_SORT);
  48. cs.style |= LBS_OWNERDRAWFIXED;
  49. m_bTracking = FALSE;
  50. m_nDivider = 0;
  51. m_bDivIsSet = FALSE;
  52. return TRUE;
  53. }
  54. void CPropertyList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
  55. {
  56. lpMeasureItemStruct->itemHeight = 20; //pixels
  57. }
  58. void CPropertyList::DrawItem(LPDRAWITEMSTRUCT lpDIS)
  59. {
  60. CDC dc;
  61. dc.Attach(lpDIS->hDC);
  62. CRect rectFull = lpDIS->rcItem;
  63. CRect rect = rectFull;
  64. if (m_nDivider==0)
  65. m_nDivider = rect.Width() / 2;
  66. rect.left = m_nDivider;
  67. CRect rect2 = rectFull;
  68. rect2.right = rect.left - 1;
  69. UINT nIndex = lpDIS->itemID;
  70. if (nIndex != (UINT) -1)
  71. {
  72. //draw two rectangles, one for each row column
  73. dc.FillSolidRect(rect2,RGB(192,192,192));
  74. dc.DrawEdge(rect2,EDGE_SUNKEN,BF_BOTTOMRIGHT);
  75. dc.DrawEdge(rect,EDGE_SUNKEN,BF_BOTTOM);
  76. //get the CPropertyItem for the current row
  77. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(nIndex);
  78. //write the property name in the first rectangle
  79. dc.SetBkMode(TRANSPARENT);
  80. dc.DrawText(pItem->m_propName,CRect(rect2.left+3,rect2.top+3,
  81. rect2.right-3,rect2.bottom+3),
  82. DT_LEFT | DT_SINGLELINE);
  83. //write the initial property value in the second rectangle
  84. dc.DrawText(pItem->m_curValue,CRect(rect.left+3,rect.top+3,
  85. rect.right+3,rect.bottom+3),
  86. DT_LEFT | DT_SINGLELINE);
  87. }
  88. dc.Detach();
  89. }
  90. int CPropertyList::AddItem(CString txt)
  91. {
  92. int nIndex = AddString(txt);
  93. return nIndex;
  94. }
  95. int CPropertyList::AddPropItem(CPropertyItem* pItem)
  96. {
  97. int nIndex = AddString(_T(""));
  98. SetItemDataPtr(nIndex,pItem);
  99. m_PropertyItems.insert(pItem);
  100. return nIndex;
  101. }
  102. int CPropertyList::AddProperty(const char* name,
  103. const char* value,
  104. int type,
  105. const char* comboItems)
  106. {
  107. CPropertyItem* pItem = 0;
  108. for(int i =0; i < this->GetCount(); ++i)
  109. {
  110. CPropertyItem* item = this->GetItem(i);
  111. if(item->m_propName == name)
  112. {
  113. pItem = item;
  114. if(pItem->m_curValue != value)
  115. {
  116. pItem->m_curValue = value;
  117. m_Dirty = true;
  118. Invalidate();
  119. }
  120. return i;
  121. }
  122. }
  123. // if it is not in the displayed list, then
  124. // check for it in the m_PropertyItems list as
  125. // a removed item
  126. for(std::set<CPropertyItem*>::iterator
  127. p = m_PropertyItems.begin();
  128. p != m_PropertyItems.end(); ++p)
  129. {
  130. if((*p)->m_propName == name)
  131. {
  132. pItem = *p;
  133. pItem->m_Removed = false;
  134. pItem->m_curValue = value;
  135. Invalidate();
  136. }
  137. }
  138. // if it is not found, then create a new one
  139. if(!pItem)
  140. {
  141. pItem = new CPropertyItem(name, value, type, comboItems);
  142. }
  143. return this->AddPropItem(pItem);
  144. }
  145. int CPropertyList::OnCreate(LPCREATESTRUCT lpCreateStruct)
  146. {
  147. if (CListBox::OnCreate(lpCreateStruct) == -1)
  148. return -1;
  149. m_bDivIsSet = FALSE;
  150. m_nDivider = 0;
  151. m_bTracking = FALSE;
  152. m_hCursorSize = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
  153. m_hCursorArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  154. m_SSerif8Font.CreatePointFont(80,_T("MS Sans Serif"));
  155. return 0;
  156. }
  157. void CPropertyList::OnSelchange()
  158. {
  159. CRect rect;
  160. CString lBoxSelText;
  161. GetItemRect(m_curSel,rect);
  162. rect.left = m_nDivider;
  163. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  164. if (m_btnCtrl)
  165. m_btnCtrl.ShowWindow(SW_HIDE);
  166. if (m_CheckBoxControl)
  167. m_CheckBoxControl.ShowWindow(SW_HIDE);
  168. if (pItem->m_nItemType==CPropertyList::COMBO)
  169. {
  170. //display the combo box. If the combo box has already been
  171. //created then simply move it to the new location, else create it
  172. m_nLastBox = 0;
  173. if (m_cmbBox)
  174. m_cmbBox.MoveWindow(rect);
  175. else
  176. {
  177. rect.bottom += 100;
  178. m_cmbBox.Create(CBS_DROPDOWNLIST
  179. | CBS_NOINTEGRALHEIGHT | WS_VISIBLE
  180. | WS_CHILD | WS_BORDER,
  181. rect,this,IDC_PROPCMBBOX);
  182. m_cmbBox.SetFont(&m_SSerif8Font);
  183. }
  184. //add the choices for this particular property
  185. CString cmbItems = pItem->m_cmbItems;
  186. lBoxSelText = pItem->m_curValue;
  187. m_cmbBox.ResetContent();
  188. int i,i2;
  189. i=0;
  190. while ((i2=cmbItems.Find('|',i)) != -1)
  191. {
  192. m_cmbBox.AddString(cmbItems.Mid(i,i2-i));
  193. i=i2+1;
  194. }
  195. if(i != 0)
  196. m_cmbBox.AddString(cmbItems.Mid(i));
  197. m_cmbBox.ShowWindow(SW_SHOW);
  198. m_cmbBox.SetFocus();
  199. //jump to the property's current value in the combo box
  200. int j = m_cmbBox.FindStringExact(0,lBoxSelText);
  201. if (j != CB_ERR)
  202. m_cmbBox.SetCurSel(j);
  203. else
  204. m_cmbBox.SetCurSel(0);
  205. }
  206. else if (pItem->m_nItemType==CPropertyList::EDIT)
  207. {
  208. //display edit box
  209. m_nLastBox = 1;
  210. m_prevSel = m_curSel;
  211. rect.bottom -= 3;
  212. if (m_editBox)
  213. m_editBox.MoveWindow(rect);
  214. else
  215. {
  216. m_editBox.Create(ES_LEFT | ES_AUTOHSCROLL | WS_VISIBLE
  217. | WS_CHILD | WS_BORDER,
  218. rect,this,IDC_PROPEDITBOX);
  219. m_editBox.SetFont(&m_SSerif8Font);
  220. }
  221. lBoxSelText = pItem->m_curValue;
  222. m_editBox.ShowWindow(SW_SHOW);
  223. m_editBox.SetFocus();
  224. //set the text in the edit box to the property's current value
  225. m_editBox.SetWindowText(lBoxSelText);
  226. }
  227. else if (pItem->m_nItemType == CPropertyList::CHECKBOX)
  228. {
  229. rect.bottom -= 3;
  230. if (m_CheckBoxControl)
  231. m_CheckBoxControl.MoveWindow(rect);
  232. else
  233. {
  234. m_CheckBoxControl.Create("check",BS_CHECKBOX
  235. | BM_SETCHECK |BS_LEFTTEXT
  236. | WS_VISIBLE | WS_CHILD,
  237. rect,this,IDC_PROPCHECKBOXCTRL);
  238. m_CheckBoxControl.SetFont(&m_SSerif8Font);
  239. }
  240. lBoxSelText = pItem->m_curValue;
  241. m_CheckBoxControl.ShowWindow(SW_SHOW);
  242. m_CheckBoxControl.SetFocus();
  243. //set the text in the edit box to the property's current value
  244. if(lBoxSelText == "ON")
  245. {
  246. m_CheckBoxControl.SetCheck(1);
  247. }
  248. else
  249. {
  250. m_CheckBoxControl.SetCheck(0);
  251. }
  252. }
  253. else
  254. DisplayButton(rect);
  255. }
  256. void CPropertyList::DisplayButton(CRect region)
  257. {
  258. //displays a button if the property is a file/color/font chooser
  259. m_nLastBox = 2;
  260. m_prevSel = m_curSel;
  261. if (region.Width() > 25)
  262. region.left = region.right - 25;
  263. region.bottom -= 3;
  264. if (m_btnCtrl)
  265. m_btnCtrl.MoveWindow(region);
  266. else
  267. {
  268. m_btnCtrl.Create("...",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
  269. region,this,IDC_PROPBTNCTRL);
  270. m_btnCtrl.SetFont(&m_SSerif8Font);
  271. }
  272. m_btnCtrl.ShowWindow(SW_SHOW);
  273. m_btnCtrl.SetFocus();
  274. }
  275. void CPropertyList::OnKillFocus(CWnd* pNewWnd)
  276. {
  277. //m_btnCtrl.ShowWindow(SW_HIDE);
  278. CListBox::OnKillFocus(pNewWnd);
  279. }
  280. void CPropertyList::OnKillfocusCmbBox()
  281. {
  282. m_cmbBox.ShowWindow(SW_HIDE);
  283. Invalidate();
  284. }
  285. void CPropertyList::OnKillfocusEditBox()
  286. {
  287. CString newStr;
  288. m_editBox.ShowWindow(SW_HIDE);
  289. Invalidate();
  290. }
  291. void CPropertyList::OnSelchangeCmbBox()
  292. {
  293. CString selStr;
  294. if (m_cmbBox)
  295. {
  296. m_cmbBox.GetLBText(m_cmbBox.GetCurSel(),selStr);
  297. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  298. pItem->m_curValue = selStr;
  299. m_Dirty = true;
  300. }
  301. }
  302. void CPropertyList::OnChangeEditBox()
  303. {
  304. CString newStr;
  305. m_editBox.GetWindowText(newStr);
  306. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  307. pItem->m_curValue = newStr;
  308. m_Dirty = true;
  309. }
  310. void CPropertyList::OnCheckBox()
  311. {
  312. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  313. if(m_CheckBoxControl.GetCheck())
  314. {
  315. pItem->m_curValue = "ON";
  316. }
  317. else
  318. {
  319. pItem->m_curValue = "OFF";
  320. }
  321. m_Dirty = true;
  322. }
  323. void CPropertyList::OnButton()
  324. {
  325. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  326. //display the appropriate common dialog depending on what type
  327. //of chooser is associated with the property
  328. if (pItem->m_nItemType == CPropertyList::COLOR)
  329. {
  330. COLORREF initClr;
  331. CString currClr = pItem->m_curValue;
  332. //parse the property's current color value
  333. if (currClr.Find("RGB") > -1)
  334. {
  335. int j = currClr.Find(',',3);
  336. CString bufr = currClr.Mid(4,j-4);
  337. int RVal = atoi(bufr);
  338. int j2 = currClr.Find(',',j+1);
  339. bufr = currClr.Mid(j+1,j2-(j+1));
  340. int GVal = atoi(bufr);
  341. int j3 = currClr.Find(')',j2+1);
  342. bufr = currClr.Mid(j2+1,j3-(j2+1));
  343. int BVal = atoi(bufr);
  344. initClr = RGB(RVal,GVal,BVal);
  345. }
  346. else
  347. initClr = 0;
  348. CColorDialog ClrDlg(initClr);
  349. if (IDOK == ClrDlg.DoModal())
  350. {
  351. COLORREF selClr = ClrDlg.GetColor();
  352. CString clrStr;
  353. clrStr.Format("RGB(%d,%d,%d)",GetRValue(selClr),
  354. GetGValue(selClr),GetBValue(selClr));
  355. m_btnCtrl.ShowWindow(SW_HIDE);
  356. pItem->m_curValue = clrStr;
  357. m_Dirty = true;
  358. Invalidate();
  359. }
  360. }
  361. else if (pItem->m_nItemType == CPropertyList::FILE)
  362. {
  363. CString SelectedFile;
  364. CString Filter("Gif Files (*.gif)|*.gif||");
  365. CFileDialog FileDlg(TRUE, NULL, NULL, NULL,
  366. Filter);
  367. CString currPath = pItem->m_curValue;
  368. FileDlg.m_ofn.lpstrTitle = "Select file";
  369. if (currPath.GetLength() > 0)
  370. FileDlg.m_ofn.lpstrInitialDir = currPath.Left(
  371. currPath.GetLength() - currPath.ReverseFind('\\'));
  372. if(IDOK == FileDlg.DoModal())
  373. {
  374. SelectedFile = FileDlg.GetPathName();
  375. m_btnCtrl.ShowWindow(SW_HIDE);
  376. pItem->m_curValue = SelectedFile;
  377. m_Dirty = true;
  378. Invalidate();
  379. }
  380. }
  381. else if (pItem->m_nItemType == CPropertyList::PATH)
  382. {
  383. char szPathName[4096];
  384. BROWSEINFO bi;
  385. bi.hwndOwner = m_hWnd;
  386. bi.pidlRoot = NULL;
  387. bi.pszDisplayName = (LPTSTR)szPathName;
  388. bi.lpszTitle = "Select Directory";
  389. bi.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS;
  390. bi.lpfn = NULL;
  391. LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
  392. BOOL bSuccess = SHGetPathFromIDList(pidl, szPathName);
  393. CString SelectedFile;
  394. if(bSuccess)
  395. {
  396. SelectedFile = szPathName;
  397. m_btnCtrl.ShowWindow(SW_HIDE);
  398. pItem->m_curValue = SelectedFile;
  399. m_Dirty = true;
  400. Invalidate();
  401. }
  402. }
  403. else if (pItem->m_nItemType == CPropertyList::FONT)
  404. {
  405. CFontDialog FontDlg(NULL,CF_EFFECTS | CF_SCREENFONTS,NULL,this);
  406. if(IDOK == FontDlg.DoModal())
  407. {
  408. CString faceName = FontDlg.GetFaceName();
  409. m_btnCtrl.ShowWindow(SW_HIDE);
  410. pItem->m_curValue = faceName;
  411. m_Dirty = true;
  412. Invalidate();
  413. }
  414. }
  415. }
  416. void CPropertyList::OnLButtonUp(UINT nFlags, CPoint point)
  417. {
  418. if (m_bTracking)
  419. {
  420. //if columns were being resized then this indicates
  421. //that mouse is up so resizing is done. Need to redraw
  422. //columns to reflect their new widths.
  423. m_bTracking = FALSE;
  424. //if mouse was captured then release it
  425. if (GetCapture()==this)
  426. ::ReleaseCapture();
  427. ::ClipCursor(NULL);
  428. CClientDC dc(this);
  429. InvertLine(&dc,CPoint(point.x,m_nDivTop),CPoint(point.x,m_nDivBtm));
  430. //set the divider position to the new value
  431. m_nDivider = point.x;
  432. //redraw
  433. Invalidate();
  434. }
  435. else
  436. {
  437. BOOL loc;
  438. int i = ItemFromPoint(point,loc);
  439. m_curSel = i;
  440. CListBox::OnLButtonUp(nFlags, point);
  441. }
  442. }
  443. void CPropertyList::OnLButtonDown(UINT nFlags, CPoint point)
  444. {
  445. if ((point.x>=m_nDivider-5) && (point.x<=m_nDivider+5))
  446. {
  447. //if mouse clicked on divider line, then start resizing
  448. ::SetCursor(m_hCursorSize);
  449. CRect windowRect;
  450. GetWindowRect(windowRect);
  451. windowRect.left += 10; windowRect.right -= 10;
  452. //do not let mouse leave the list box boundary
  453. ::ClipCursor(windowRect);
  454. if (m_cmbBox)
  455. m_cmbBox.ShowWindow(SW_HIDE);
  456. if (m_editBox)
  457. m_editBox.ShowWindow(SW_HIDE);
  458. CRect clientRect;
  459. GetClientRect(clientRect);
  460. m_bTracking = TRUE;
  461. m_nDivTop = clientRect.top;
  462. m_nDivBtm = clientRect.bottom;
  463. m_nOldDivX = point.x;
  464. CClientDC dc(this);
  465. InvertLine(&dc,CPoint(m_nOldDivX,m_nDivTop),CPoint(m_nOldDivX,m_nDivBtm));
  466. //capture the mouse
  467. SetCapture();
  468. }
  469. else
  470. {
  471. m_bTracking = FALSE;
  472. CListBox::OnLButtonDown(nFlags, point);
  473. }
  474. }
  475. void CPropertyList::OnMouseMove(UINT nFlags, CPoint point)
  476. {
  477. if (m_bTracking)
  478. {
  479. //move divider line to the mouse pos. if columns are
  480. //currently being resized
  481. CClientDC dc(this);
  482. //remove old divider line
  483. InvertLine(&dc,CPoint(m_nOldDivX,m_nDivTop),CPoint(m_nOldDivX,m_nDivBtm));
  484. //draw new divider line
  485. InvertLine(&dc,CPoint(point.x,m_nDivTop),CPoint(point.x,m_nDivBtm));
  486. m_nOldDivX = point.x;
  487. }
  488. else if ((point.x >= m_nDivider-5) && (point.x <= m_nDivider+5))
  489. //set the cursor to a sizing cursor if the cursor is over the row divider
  490. ::SetCursor(m_hCursorSize);
  491. else
  492. CListBox::OnMouseMove(nFlags, point);
  493. }
  494. void CPropertyList::InvertLine(CDC* pDC,CPoint ptFrom,CPoint ptTo)
  495. {
  496. int nOldMode = pDC->SetROP2(R2_NOT);
  497. pDC->MoveTo(ptFrom);
  498. pDC->LineTo(ptTo);
  499. pDC->SetROP2(nOldMode);
  500. }
  501. void CPropertyList::PreSubclassWindow()
  502. {
  503. m_bDivIsSet = FALSE;
  504. m_nDivider = 0;
  505. m_bTracking = FALSE;
  506. m_curSel = 1;
  507. m_hCursorSize = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
  508. m_hCursorArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  509. m_SSerif8Font.CreatePointFont(80,_T("MS Sans Serif"));
  510. }
  511. CPropertyItem* CPropertyList::GetItem(int index)
  512. {
  513. return (CPropertyItem*)GetItemDataPtr(index);
  514. }
  515. void CPropertyList::OnRButtonUp( UINT nFlags, CPoint point )
  516. {
  517. CMenu menu;
  518. CRect rect;
  519. this->GetWindowRect(&rect);
  520. BOOL loc;
  521. m_curSel = ItemFromPoint(point,loc);
  522. menu.CreatePopupMenu();
  523. menu.AppendMenu(MF_STRING | MF_ENABLED, 42, "Delete Cache Entry");
  524. menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
  525. rect.TopLeft().x + point.x,
  526. rect.TopLeft().y + point.y, this, NULL);
  527. }
  528. void CPropertyList::OnDelete()
  529. {
  530. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  531. pItem->m_Removed = true;
  532. this->DeleteString(m_curSel);
  533. Invalidate();
  534. }
  535. void CPropertyList::RemoveAll()
  536. {
  537. int c = this->GetCount();
  538. for(int i =0; i < c; ++i)
  539. {
  540. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(0);
  541. pItem->m_Removed = true;
  542. this->DeleteString(0);
  543. }
  544. Invalidate();
  545. }