PropertyList.cpp 15 KB

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