PropertyList.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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::list<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.push_back(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 found, then create a new one
  125. pItem = new CPropertyItem(name, value, type, comboItems);
  126. return this->AddPropItem(pItem);
  127. }
  128. int CPropertyList::OnCreate(LPCREATESTRUCT lpCreateStruct)
  129. {
  130. if (CListBox::OnCreate(lpCreateStruct) == -1)
  131. return -1;
  132. m_bDivIsSet = FALSE;
  133. m_nDivider = 0;
  134. m_bTracking = FALSE;
  135. m_hCursorSize = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
  136. m_hCursorArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  137. m_SSerif8Font.CreatePointFont(80,_T("MS Sans Serif"));
  138. return 0;
  139. }
  140. void CPropertyList::OnSelchange()
  141. {
  142. CRect rect;
  143. CString lBoxSelText;
  144. GetItemRect(m_curSel,rect);
  145. rect.left = m_nDivider;
  146. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  147. if (m_btnCtrl)
  148. m_btnCtrl.ShowWindow(SW_HIDE);
  149. if (m_CheckBoxControl)
  150. m_CheckBoxControl.ShowWindow(SW_HIDE);
  151. if (pItem->m_nItemType==PIT_COMBO)
  152. {
  153. //display the combo box. If the combo box has already been
  154. //created then simply move it to the new location, else create it
  155. m_nLastBox = 0;
  156. if (m_cmbBox)
  157. m_cmbBox.MoveWindow(rect);
  158. else
  159. {
  160. rect.bottom += 100;
  161. m_cmbBox.Create(CBS_DROPDOWNLIST | CBS_NOINTEGRALHEIGHT | WS_VISIBLE | WS_CHILD | WS_BORDER,
  162. rect,this,IDC_PROPCMBBOX);
  163. m_cmbBox.SetFont(&m_SSerif8Font);
  164. }
  165. //add the choices for this particular property
  166. CString cmbItems = pItem->m_cmbItems;
  167. lBoxSelText = pItem->m_curValue;
  168. m_cmbBox.ResetContent();
  169. int i,i2;
  170. i=0;
  171. while ((i2=cmbItems.Find('|',i)) != -1)
  172. {
  173. m_cmbBox.AddString(cmbItems.Mid(i,i2-i));
  174. i=i2+1;
  175. }
  176. if(i != 0)
  177. m_cmbBox.AddString(cmbItems.Mid(i));
  178. m_cmbBox.ShowWindow(SW_SHOW);
  179. m_cmbBox.SetFocus();
  180. //jump to the property's current value in the combo box
  181. int j = m_cmbBox.FindStringExact(0,lBoxSelText);
  182. if (j != CB_ERR)
  183. m_cmbBox.SetCurSel(j);
  184. else
  185. m_cmbBox.SetCurSel(0);
  186. }
  187. else if (pItem->m_nItemType==PIT_EDIT)
  188. {
  189. //display edit box
  190. m_nLastBox = 1;
  191. m_prevSel = m_curSel;
  192. rect.bottom -= 3;
  193. if (m_editBox)
  194. m_editBox.MoveWindow(rect);
  195. else
  196. {
  197. m_editBox.Create(ES_LEFT | ES_AUTOHSCROLL | WS_VISIBLE | WS_CHILD | WS_BORDER,
  198. rect,this,IDC_PROPEDITBOX);
  199. m_editBox.SetFont(&m_SSerif8Font);
  200. }
  201. lBoxSelText = pItem->m_curValue;
  202. m_editBox.ShowWindow(SW_SHOW);
  203. m_editBox.SetFocus();
  204. //set the text in the edit box to the property's current value
  205. m_editBox.SetWindowText(lBoxSelText);
  206. }
  207. else if (pItem->m_nItemType == PIT_CHECKBOX)
  208. {
  209. rect.bottom -= 3;
  210. if (m_CheckBoxControl)
  211. m_CheckBoxControl.MoveWindow(rect);
  212. else
  213. {
  214. m_CheckBoxControl.Create("check",BS_CHECKBOX | BM_SETCHECK |BS_LEFTTEXT | WS_VISIBLE | WS_CHILD,
  215. rect,this,IDC_PROPCHECKBOXCTRL);
  216. m_CheckBoxControl.SetFont(&m_SSerif8Font);
  217. }
  218. lBoxSelText = pItem->m_curValue;
  219. m_CheckBoxControl.ShowWindow(SW_SHOW);
  220. m_CheckBoxControl.SetFocus();
  221. //set the text in the edit box to the property's current value
  222. if(lBoxSelText == "ON")
  223. {
  224. m_CheckBoxControl.SetCheck(1);
  225. }
  226. else
  227. {
  228. m_CheckBoxControl.SetCheck(0);
  229. }
  230. }
  231. else
  232. DisplayButton(rect);
  233. }
  234. void CPropertyList::DisplayButton(CRect region)
  235. {
  236. //displays a button if the property is a file/color/font chooser
  237. m_nLastBox = 2;
  238. m_prevSel = m_curSel;
  239. if (region.Width() > 25)
  240. region.left = region.right - 25;
  241. region.bottom -= 3;
  242. if (m_btnCtrl)
  243. m_btnCtrl.MoveWindow(region);
  244. else
  245. {
  246. m_btnCtrl.Create("...",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
  247. region,this,IDC_PROPBTNCTRL);
  248. m_btnCtrl.SetFont(&m_SSerif8Font);
  249. }
  250. m_btnCtrl.ShowWindow(SW_SHOW);
  251. m_btnCtrl.SetFocus();
  252. }
  253. void CPropertyList::OnKillFocus(CWnd* pNewWnd)
  254. {
  255. //m_btnCtrl.ShowWindow(SW_HIDE);
  256. CListBox::OnKillFocus(pNewWnd);
  257. }
  258. void CPropertyList::OnKillfocusCmbBox()
  259. {
  260. m_cmbBox.ShowWindow(SW_HIDE);
  261. Invalidate();
  262. }
  263. void CPropertyList::OnKillfocusEditBox()
  264. {
  265. CString newStr;
  266. m_editBox.ShowWindow(SW_HIDE);
  267. Invalidate();
  268. }
  269. void CPropertyList::OnSelchangeCmbBox()
  270. {
  271. CString selStr;
  272. if (m_cmbBox)
  273. {
  274. m_cmbBox.GetLBText(m_cmbBox.GetCurSel(),selStr);
  275. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  276. pItem->m_curValue = selStr;
  277. m_Dirty = true;
  278. }
  279. }
  280. void CPropertyList::OnChangeEditBox()
  281. {
  282. CString newStr;
  283. m_editBox.GetWindowText(newStr);
  284. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  285. pItem->m_curValue = newStr;
  286. m_Dirty = true;
  287. }
  288. void CPropertyList::OnCheckBox()
  289. {
  290. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  291. if(m_CheckBoxControl.GetCheck())
  292. {
  293. pItem->m_curValue = "ON";
  294. }
  295. else
  296. {
  297. pItem->m_curValue = "OFF";
  298. }
  299. m_Dirty = true;
  300. }
  301. void CPropertyList::OnButton()
  302. {
  303. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  304. //display the appropriate common dialog depending on what type
  305. //of chooser is associated with the property
  306. if (pItem->m_nItemType == PIT_COLOR)
  307. {
  308. COLORREF initClr;
  309. CString currClr = pItem->m_curValue;
  310. //parse the property's current color value
  311. if (currClr.Find("RGB") > -1)
  312. {
  313. int j = currClr.Find(',',3);
  314. CString bufr = currClr.Mid(4,j-4);
  315. int RVal = atoi(bufr);
  316. int j2 = currClr.Find(',',j+1);
  317. bufr = currClr.Mid(j+1,j2-(j+1));
  318. int GVal = atoi(bufr);
  319. int j3 = currClr.Find(')',j2+1);
  320. bufr = currClr.Mid(j2+1,j3-(j2+1));
  321. int BVal = atoi(bufr);
  322. initClr = RGB(RVal,GVal,BVal);
  323. }
  324. else
  325. initClr = 0;
  326. CColorDialog ClrDlg(initClr);
  327. if (IDOK == ClrDlg.DoModal())
  328. {
  329. COLORREF selClr = ClrDlg.GetColor();
  330. CString clrStr;
  331. clrStr.Format("RGB(%d,%d,%d)",GetRValue(selClr),
  332. GetGValue(selClr),GetBValue(selClr));
  333. m_btnCtrl.ShowWindow(SW_HIDE);
  334. pItem->m_curValue = clrStr;
  335. m_Dirty = true;
  336. Invalidate();
  337. }
  338. }
  339. else if (pItem->m_nItemType == PIT_FILE)
  340. {
  341. CString SelectedFile;
  342. CString Filter("Gif Files (*.gif)|*.gif||");
  343. CFileDialog FileDlg(TRUE, NULL, NULL, NULL,
  344. Filter);
  345. CString currPath = pItem->m_curValue;
  346. FileDlg.m_ofn.lpstrTitle = "Select file";
  347. if (currPath.GetLength() > 0)
  348. FileDlg.m_ofn.lpstrInitialDir = currPath.Left(
  349. currPath.GetLength() - currPath.ReverseFind('\\'));
  350. if(IDOK == FileDlg.DoModal())
  351. {
  352. SelectedFile = FileDlg.GetPathName();
  353. m_btnCtrl.ShowWindow(SW_HIDE);
  354. pItem->m_curValue = SelectedFile;
  355. m_Dirty = true;
  356. Invalidate();
  357. }
  358. }
  359. else if (pItem->m_nItemType == PIT_FONT)
  360. {
  361. CFontDialog FontDlg(NULL,CF_EFFECTS | CF_SCREENFONTS,NULL,this);
  362. if(IDOK == FontDlg.DoModal())
  363. {
  364. CString faceName = FontDlg.GetFaceName();
  365. m_btnCtrl.ShowWindow(SW_HIDE);
  366. pItem->m_curValue = faceName;
  367. m_Dirty = true;
  368. Invalidate();
  369. }
  370. }
  371. }
  372. void CPropertyList::OnLButtonUp(UINT nFlags, CPoint point)
  373. {
  374. if (m_bTracking)
  375. {
  376. //if columns were being resized then this indicates
  377. //that mouse is up so resizing is done. Need to redraw
  378. //columns to reflect their new widths.
  379. m_bTracking = FALSE;
  380. //if mouse was captured then release it
  381. if (GetCapture()==this)
  382. ::ReleaseCapture();
  383. ::ClipCursor(NULL);
  384. CClientDC dc(this);
  385. InvertLine(&dc,CPoint(point.x,m_nDivTop),CPoint(point.x,m_nDivBtm));
  386. //set the divider position to the new value
  387. m_nDivider = point.x;
  388. //redraw
  389. Invalidate();
  390. }
  391. else
  392. {
  393. BOOL loc;
  394. int i = ItemFromPoint(point,loc);
  395. m_curSel = i;
  396. CListBox::OnLButtonUp(nFlags, point);
  397. }
  398. }
  399. void CPropertyList::OnLButtonDown(UINT nFlags, CPoint point)
  400. {
  401. if ((point.x>=m_nDivider-5) && (point.x<=m_nDivider+5))
  402. {
  403. //if mouse clicked on divider line, then start resizing
  404. ::SetCursor(m_hCursorSize);
  405. CRect windowRect;
  406. GetWindowRect(windowRect);
  407. windowRect.left += 10; windowRect.right -= 10;
  408. //do not let mouse leave the list box boundary
  409. ::ClipCursor(windowRect);
  410. if (m_cmbBox)
  411. m_cmbBox.ShowWindow(SW_HIDE);
  412. if (m_editBox)
  413. m_editBox.ShowWindow(SW_HIDE);
  414. CRect clientRect;
  415. GetClientRect(clientRect);
  416. m_bTracking = TRUE;
  417. m_nDivTop = clientRect.top;
  418. m_nDivBtm = clientRect.bottom;
  419. m_nOldDivX = point.x;
  420. CClientDC dc(this);
  421. InvertLine(&dc,CPoint(m_nOldDivX,m_nDivTop),CPoint(m_nOldDivX,m_nDivBtm));
  422. //capture the mouse
  423. SetCapture();
  424. }
  425. else
  426. {
  427. m_bTracking = FALSE;
  428. CListBox::OnLButtonDown(nFlags, point);
  429. }
  430. }
  431. void CPropertyList::OnMouseMove(UINT nFlags, CPoint point)
  432. {
  433. if (m_bTracking)
  434. {
  435. //move divider line to the mouse pos. if columns are
  436. //currently being resized
  437. CClientDC dc(this);
  438. //remove old divider line
  439. InvertLine(&dc,CPoint(m_nOldDivX,m_nDivTop),CPoint(m_nOldDivX,m_nDivBtm));
  440. //draw new divider line
  441. InvertLine(&dc,CPoint(point.x,m_nDivTop),CPoint(point.x,m_nDivBtm));
  442. m_nOldDivX = point.x;
  443. }
  444. else if ((point.x >= m_nDivider-5) && (point.x <= m_nDivider+5))
  445. //set the cursor to a sizing cursor if the cursor is over the row divider
  446. ::SetCursor(m_hCursorSize);
  447. else
  448. CListBox::OnMouseMove(nFlags, point);
  449. }
  450. void CPropertyList::InvertLine(CDC* pDC,CPoint ptFrom,CPoint ptTo)
  451. {
  452. int nOldMode = pDC->SetROP2(R2_NOT);
  453. pDC->MoveTo(ptFrom);
  454. pDC->LineTo(ptTo);
  455. pDC->SetROP2(nOldMode);
  456. }
  457. void CPropertyList::PreSubclassWindow()
  458. {
  459. m_bDivIsSet = FALSE;
  460. m_nDivider = 0;
  461. m_bTracking = FALSE;
  462. m_curSel = 1;
  463. m_hCursorSize = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
  464. m_hCursorArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  465. m_SSerif8Font.CreatePointFont(80,_T("MS Sans Serif"));
  466. }
  467. CPropertyItem* CPropertyList::GetItem(int index)
  468. {
  469. return (CPropertyItem*)GetItemDataPtr(index);
  470. }
  471. void CPropertyList::OnRButtonUp( UINT nFlags, CPoint point )
  472. {
  473. CMenu menu;
  474. CRect rect;
  475. this->GetWindowRect(&rect);
  476. BOOL loc;
  477. m_curSel = ItemFromPoint(point,loc);
  478. menu.CreatePopupMenu();
  479. menu.AppendMenu(MF_STRING | MF_ENABLED, 42, "Delete Cache Entry");
  480. menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
  481. rect.TopLeft().x + point.x,
  482. rect.TopLeft().y + point.y, this, NULL);
  483. }
  484. void CPropertyList::OnDelete()
  485. {
  486. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  487. pItem->m_Removed = true;
  488. this->DeleteString(m_curSel);
  489. Invalidate();
  490. }