PropertyList.cpp 14 KB

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