PropertyList.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. m_Dirty = true;
  125. Invalidate();
  126. }
  127. return i;
  128. }
  129. }
  130. // if it is not found, then create a new one
  131. if(!pItem)
  132. {
  133. pItem = new CPropertyItem(name, value, helpString, type, comboItems);
  134. }
  135. return this->AddPropItem(pItem);
  136. }
  137. int CPropertyList::OnCreate(LPCREATESTRUCT lpCreateStruct)
  138. {
  139. if (CListBox::OnCreate(lpCreateStruct) == -1)
  140. return -1;
  141. m_bDivIsSet = FALSE;
  142. m_nDivider = 0;
  143. m_bTracking = FALSE;
  144. m_hCursorSize = AfxGetApp()->LoadStandardCursor(IDC_SIZEWE);
  145. m_hCursorArrow = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
  146. m_SSerif8Font.CreatePointFont(80,_T("MS Sans Serif"));
  147. return 0;
  148. }
  149. void CPropertyList::OnSelchange()
  150. {
  151. CRect rect;
  152. CString lBoxSelText;
  153. GetItemRect(m_curSel,rect);
  154. rect.left = m_nDivider;
  155. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  156. if (m_btnCtrl)
  157. m_btnCtrl.ShowWindow(SW_HIDE);
  158. if (m_CheckBoxControl)
  159. m_CheckBoxControl.ShowWindow(SW_HIDE);
  160. if (pItem->m_nItemType==CPropertyList::COMBO)
  161. {
  162. //display the combo box. If the combo box has already been
  163. //created then simply move it to the new location, else create it
  164. m_nLastBox = 0;
  165. if (m_cmbBox)
  166. m_cmbBox.MoveWindow(rect);
  167. else
  168. {
  169. rect.bottom += 100;
  170. m_cmbBox.Create(CBS_DROPDOWNLIST
  171. | CBS_NOINTEGRALHEIGHT | WS_VISIBLE
  172. | WS_CHILD | WS_BORDER,
  173. rect,this,IDC_PROPCMBBOX);
  174. m_cmbBox.SetFont(&m_SSerif8Font);
  175. }
  176. //add the choices for this particular property
  177. CString cmbItems = pItem->m_cmbItems;
  178. lBoxSelText = pItem->m_curValue;
  179. m_cmbBox.ResetContent();
  180. int i,i2;
  181. i=0;
  182. while ((i2=cmbItems.Find('|',i)) != -1)
  183. {
  184. m_cmbBox.AddString(cmbItems.Mid(i,i2-i));
  185. i=i2+1;
  186. }
  187. if(i != 0)
  188. m_cmbBox.AddString(cmbItems.Mid(i));
  189. m_cmbBox.ShowWindow(SW_SHOW);
  190. m_cmbBox.SetFocus();
  191. //jump to the property's current value in the combo box
  192. int j = m_cmbBox.FindStringExact(0,lBoxSelText);
  193. if (j != CB_ERR)
  194. m_cmbBox.SetCurSel(j);
  195. else
  196. m_cmbBox.SetCurSel(0);
  197. }
  198. else if (pItem->m_nItemType==CPropertyList::EDIT)
  199. {
  200. //display edit box
  201. m_nLastBox = 1;
  202. m_prevSel = m_curSel;
  203. rect.bottom -= 3;
  204. if (m_editBox)
  205. m_editBox.MoveWindow(rect);
  206. else
  207. {
  208. m_editBox.Create(ES_LEFT | ES_AUTOHSCROLL | WS_VISIBLE
  209. | WS_CHILD | WS_BORDER,
  210. rect,this,IDC_PROPEDITBOX);
  211. m_editBox.SetFont(&m_SSerif8Font);
  212. }
  213. lBoxSelText = pItem->m_curValue;
  214. m_editBox.ShowWindow(SW_SHOW);
  215. m_editBox.SetFocus();
  216. //set the text in the edit box to the property's current value
  217. m_editBox.SetWindowText(lBoxSelText);
  218. }
  219. else if (pItem->m_nItemType == CPropertyList::CHECKBOX)
  220. {
  221. rect.bottom -= 3;
  222. if (m_CheckBoxControl)
  223. m_CheckBoxControl.MoveWindow(rect);
  224. else
  225. {
  226. m_CheckBoxControl.Create("check",BS_CHECKBOX
  227. | BM_SETCHECK |BS_LEFTTEXT
  228. | WS_VISIBLE | WS_CHILD,
  229. rect,this,IDC_PROPCHECKBOXCTRL);
  230. m_CheckBoxControl.SetFont(&m_SSerif8Font);
  231. }
  232. lBoxSelText = pItem->m_curValue;
  233. m_CheckBoxControl.ShowWindow(SW_SHOW);
  234. m_CheckBoxControl.SetFocus();
  235. //set the text in the edit box to the property's current value
  236. if(lBoxSelText == "ON")
  237. {
  238. m_CheckBoxControl.SetCheck(1);
  239. }
  240. else
  241. {
  242. m_CheckBoxControl.SetCheck(0);
  243. }
  244. }
  245. else
  246. DisplayButton(rect);
  247. }
  248. void CPropertyList::DisplayButton(CRect region)
  249. {
  250. //displays a button if the property is a file/color/font chooser
  251. m_nLastBox = 2;
  252. m_prevSel = m_curSel;
  253. if (region.Width() > 25)
  254. region.left = region.right - 25;
  255. region.bottom -= 3;
  256. if (m_btnCtrl)
  257. m_btnCtrl.MoveWindow(region);
  258. else
  259. {
  260. m_btnCtrl.Create("...",BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD,
  261. region,this,IDC_PROPBTNCTRL);
  262. m_btnCtrl.SetFont(&m_SSerif8Font);
  263. }
  264. m_btnCtrl.ShowWindow(SW_SHOW);
  265. m_btnCtrl.SetFocus();
  266. }
  267. void CPropertyList::OnKillFocus(CWnd* pNewWnd)
  268. {
  269. //m_btnCtrl.ShowWindow(SW_HIDE);
  270. CListBox::OnKillFocus(pNewWnd);
  271. }
  272. void CPropertyList::OnKillfocusCmbBox()
  273. {
  274. m_cmbBox.ShowWindow(SW_HIDE);
  275. Invalidate();
  276. }
  277. void CPropertyList::OnKillfocusEditBox()
  278. {
  279. CString newStr;
  280. m_editBox.ShowWindow(SW_HIDE);
  281. Invalidate();
  282. }
  283. void CPropertyList::OnSelchangeCmbBox()
  284. {
  285. CString selStr;
  286. if (m_cmbBox)
  287. {
  288. m_cmbBox.GetLBText(m_cmbBox.GetCurSel(),selStr);
  289. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  290. pItem->m_curValue = selStr;
  291. m_Dirty = true;
  292. }
  293. }
  294. void CPropertyList::OnChangeEditBox()
  295. {
  296. CString newStr;
  297. m_editBox.GetWindowText(newStr);
  298. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  299. pItem->m_curValue = newStr;
  300. m_Dirty = true;
  301. }
  302. void CPropertyList::OnCheckBox()
  303. {
  304. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  305. if(m_CheckBoxControl.GetCheck())
  306. {
  307. pItem->m_curValue = "ON";
  308. }
  309. else
  310. {
  311. pItem->m_curValue = "OFF";
  312. }
  313. m_Dirty = true;
  314. }
  315. void CPropertyList::OnButton()
  316. {
  317. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  318. //display the appropriate common dialog depending on what type
  319. //of chooser is associated with the property
  320. if (pItem->m_nItemType == CPropertyList::FILE)
  321. {
  322. CString SelectedFile;
  323. CString Filter("All Files (*.*)||");
  324. CFileDialog FileDlg(TRUE, NULL, NULL, NULL,
  325. Filter);
  326. CString initialDir;
  327. CString currPath = pItem->m_curValue;
  328. if (currPath.GetLength() > 0)
  329. {
  330. int endSlash = currPath.ReverseFind('\\');
  331. if(endSlash == -1)
  332. {
  333. endSlash = currPath.ReverseFind('/');
  334. }
  335. initialDir = currPath.Left(endSlash);
  336. }
  337. initialDir.Replace("/", "\\");
  338. FileDlg.m_ofn.lpstrTitle = "Select file";
  339. if (currPath.GetLength() > 0)
  340. FileDlg.m_ofn.lpstrInitialDir = initialDir;
  341. if(IDOK == FileDlg.DoModal())
  342. {
  343. SelectedFile = FileDlg.GetPathName();
  344. m_btnCtrl.ShowWindow(SW_HIDE);
  345. std::string path = SelectedFile;
  346. cmSystemTools::ConvertToUnixSlashes(path);
  347. pItem->m_curValue = path.c_str();
  348. m_Dirty = true;
  349. Invalidate();
  350. }
  351. }
  352. else if (pItem->m_nItemType == CPropertyList::PATH)
  353. {
  354. CString initialDir = pItem->m_curValue;
  355. // convert back to windos style path
  356. initialDir.Replace("/", "\\");
  357. CString title = "Setting Cache Value: ";
  358. title += pItem->m_propName;
  359. CPathDialog dlg("Select Path", title, initialDir);
  360. if(dlg.DoModal()==IDOK)
  361. {
  362. CString SelectedFile = dlg.GetPathName();
  363. m_btnCtrl.ShowWindow(SW_HIDE);
  364. std::string path = SelectedFile;
  365. cmSystemTools::ConvertToUnixSlashes(path);
  366. pItem->m_curValue = path.c_str();
  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.AppendMenu(MF_STRING | MF_ENABLED, 43, "Help For Cache Entry");
  481. menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
  482. rect.TopLeft().x + point.x,
  483. rect.TopLeft().y + point.y, this, NULL);
  484. }
  485. void CPropertyList::OnDelete()
  486. {
  487. if(m_curSel == -1 || this->GetCount() <= 0)
  488. {
  489. return;
  490. }
  491. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  492. cmCacheManager::GetInstance()->RemoveCacheEntry(pItem->m_propName);
  493. m_PropertyItems.erase(pItem);
  494. delete pItem;
  495. this->DeleteString(m_curSel);
  496. Invalidate();
  497. }
  498. void CPropertyList::OnHelp()
  499. {
  500. if(m_curSel == -1 || this->GetCount() <= 0)
  501. {
  502. return;
  503. }
  504. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(m_curSel);
  505. MessageBox(pItem->m_HelpString, pItem->m_propName, MB_OK|MB_ICONINFORMATION);
  506. }
  507. void CPropertyList::RemoveAll()
  508. {
  509. int c = this->GetCount();
  510. for(int i =0; i < c; ++i)
  511. {
  512. CPropertyItem* pItem = (CPropertyItem*) GetItemDataPtr(0);
  513. cmCacheManager::GetInstance()->RemoveCacheEntry(pItem->m_propName);
  514. m_PropertyItems.erase(pItem);
  515. delete pItem;
  516. this->DeleteString(0);
  517. }
  518. Invalidate();
  519. }