XCell.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. #include "StdAfx.h"
  2. #include "Xcell.h"
  3. #include "XTable.h"
  4. #ifdef _DEBUG
  5. #define new DEBUG_NEW
  6. #endif
  7. XCell::XCell(XTable* parent)
  8. {
  9. rowSpan = 0;
  10. colSpan = 0;
  11. backColor = RGB (255,255,255);
  12. borderColor = RGB (0xDC,0xDC,0xDC);
  13. borderStyle = 0;
  14. text = "";
  15. textFontSize = 14;
  16. textFontFace = "Arial";
  17. textFont = NULL;
  18. textColor = RGB (0,0,0);
  19. label = "";
  20. labelFontSize = 14;
  21. labelFontFace = "Arial";
  22. labelFont = NULL;
  23. labelColor = RGB (0,0,0);
  24. backMode = TRANSPARENT;
  25. format = 0;
  26. overlap = false;
  27. leftMargin = 4;
  28. rightMargin = 4;
  29. }
  30. XCell::~XCell()
  31. {
  32. delete textFont;
  33. delete labelFont;
  34. }
  35. XCell& XCell::operator = (XCell& cell)
  36. {
  37. if (&cell == this) return *this;
  38. rowSpan = cell.rowSpan;
  39. colSpan = cell.colSpan;
  40. text = cell.text;
  41. textColor = cell.textColor;
  42. delete textFont;
  43. textFont = NULL;
  44. if (cell.textFont)
  45. {
  46. LOGFONT logFont;
  47. cell.textFont->GetLogFont (&logFont);
  48. textFont = new CFont;
  49. textFont->CreateFontIndirect (&logFont);
  50. }
  51. textFontSize = cell.textFontSize;
  52. textFontFace = cell.textFontFace;
  53. label = cell.label;
  54. labelColor = cell.labelColor;
  55. delete labelFont;
  56. labelFont = NULL;
  57. if (cell.labelFont)
  58. {
  59. LOGFONT logFont;
  60. cell.labelFont->GetLogFont (&logFont);
  61. labelFont = new CFont;
  62. labelFont->CreateFontIndirect (&logFont);
  63. }
  64. labelFontSize = cell.labelFontSize;
  65. labelFontFace = cell.labelFontFace;
  66. format = cell.format;
  67. leftMargin = cell.leftMargin;
  68. rightMargin = cell.rightMargin;
  69. backMode = cell.backMode;
  70. backColor = cell.backColor;
  71. borderColor = cell.borderColor;
  72. borderStyle = cell.borderStyle;
  73. overlap = cell.overlap;
  74. table = cell.table;
  75. return *this;
  76. }
  77. int XCell::ToXML (CMarkup& markup)
  78. {
  79. TCHAR buf[32];
  80. if (colSpan > 1)
  81. markup.AddChildAttrib ("colSpan", colSpan);
  82. if (rowSpan > 1)
  83. markup.AddChildAttrib ("rowSpan", rowSpan);
  84. if (backColor != table->defaultCell.backColor)
  85. {
  86. sprintf (buf, "#%.2x%.2x%.2x", GetRValue(backColor), GetGValue(backColor), GetBValue(backColor));
  87. markup.AddChildAttrib ("backColor", buf);
  88. }
  89. if (textColor != table->defaultCell.textColor)
  90. {
  91. sprintf (buf, "#%.2x%.2x%.2x", GetRValue(textColor), GetGValue(textColor), GetBValue(textColor));
  92. markup.AddChildAttrib ("textColor", textColor);
  93. }
  94. if (backMode != table->defaultCell.backMode)
  95. {
  96. if (backMode == TRANSPARENT)
  97. markup.AddChildAttrib ("backMode", "transparent");
  98. else
  99. markup.AddChildAttrib ("backMode", "opaque");
  100. }
  101. if (textFontSize != table->defaultCell.textFontSize)
  102. markup.AddChildAttrib ("fontSize", textFontSize);
  103. if (textFontFace != table->defaultCell.textFontFace)
  104. markup.AddChildAttrib ("fontFace", textFontFace);
  105. if (textFontSize != table->defaultCell.textFontSize)
  106. markup.AddChildAttrib ("fontWeight", textFontSize);
  107. int align = GetAlignment();
  108. if (align != table->defaultCell.GetAlignment())
  109. {
  110. if (align & ALIGN_CENTER)
  111. markup.AddChildAttrib("align", "center");
  112. else if (align & ALIGN_RIGHT)
  113. markup.AddChildAttrib("align", "right");
  114. if (align & ALIGN_MIDDLE)
  115. markup.AddChildAttrib("vlign", "middle");
  116. else if (align & ALIGN_BOTTOM)
  117. markup.AddChildAttrib("valign", "bottom");
  118. }
  119. bool ellipsis = GetEllipsis ();
  120. if (ellipsis != table->defaultCell.GetEllipsis())
  121. {
  122. if (ellipsis)
  123. markup.AddChildAttrib ("ellipsis", "true");
  124. else
  125. markup.AddChildAttrib ("ellipsis", "false");
  126. }
  127. bool singleLine = GetSingleLine();
  128. if (singleLine != table->defaultCell.GetSingleLine ())
  129. {
  130. if (overlap)
  131. markup.AddChildAttrib ("singleline", "true");
  132. else
  133. markup.AddChildAttrib ("singleline", "false");
  134. }
  135. bool wordBreak = GetWordbreak();
  136. if (wordBreak != table->defaultCell.GetWordbreak ())
  137. {
  138. if (wordBreak)
  139. markup.AddChildAttrib ("wordbreak", "true");
  140. else
  141. markup.AddChildAttrib ("wordbreak", "false");
  142. }
  143. if (leftMargin != table->defaultCell.leftMargin)
  144. markup.AddChildAttrib ("leftMargin", leftMargin);
  145. if (rightMargin != table->defaultCell.rightMargin)
  146. markup.AddChildAttrib ("rightMargin", rightMargin);
  147. bool overlap = GetOverlap ();
  148. if (overlap != table->defaultCell.GetOverlap ())
  149. {
  150. if (overlap)
  151. markup.AddChildAttrib ("overlap", "true");
  152. else
  153. markup.AddChildAttrib ("overlap", "false");
  154. }
  155. if (label != table->defaultCell.label)
  156. markup.AddChildAttrib ("label", label);
  157. if (text != "")
  158. markup.SetChildData(text);
  159. return 0;
  160. }
  161. int XCell::FromXML (CMarkup& markup)
  162. {
  163. CString value;
  164. *this = table->defaultCell;
  165. colSpan = 1;
  166. rowSpan = 1;
  167. value = markup.GetChildAttrib("colSpan");
  168. if (value != "")
  169. colSpan = atoi (value);
  170. value = markup.GetChildAttrib("rowSpan");
  171. if (value != "")
  172. rowSpan = atoi (value);
  173. value = markup.GetChildAttrib("backColor");
  174. if (value != "")
  175. {
  176. if (*value == '#')
  177. sscanf(value, "#%x", &backColor);
  178. else
  179. backColor = atoi (value);
  180. backColor = RGB (GetBValue(backColor), GetGValue(backColor), GetRValue(backColor));
  181. }
  182. value = markup.GetChildAttrib("textColor");
  183. if (value != "")
  184. {
  185. if (*value == '#')
  186. sscanf(value, "#%x", &textColor);
  187. else
  188. textColor = atoi (value);
  189. textColor = RGB (GetBValue(textColor), GetGValue(textColor), GetRValue(textColor));
  190. }
  191. value = markup.GetChildAttrib("fontWeight");
  192. if (value != "")
  193. textFontSize = atoi (value);
  194. value = markup.GetChildAttrib("fontFace");
  195. if (value != "")
  196. textFontFace = value;
  197. value = markup.GetChildAttrib("backMode");
  198. if (value != "opaque")
  199. backMode = TRANSPARENT;
  200. else
  201. backMode = OPAQUE;
  202. value = markup.GetChildAttrib("align");
  203. if (value == "center")
  204. SetAlignment(ALIGN_CENTER);
  205. else if (value == "right")
  206. SetAlignment(ALIGN_RIGHT);
  207. value = markup.GetChildAttrib("valign");
  208. if (value == "middle")
  209. SetAlignment(ALIGN_MIDDLE);
  210. else if (value == "bottom")
  211. SetAlignment(ALIGN_BOTTOM);
  212. value = markup.GetChildAttrib("ellipsis");
  213. if (value == "true")
  214. SetEllipsis(true);
  215. else
  216. SetEllipsis(false);
  217. value = markup.GetChildAttrib("singleline");
  218. if (value == "true")
  219. SetSingleLine(true);
  220. else
  221. SetSingleLine(false);
  222. value = markup.GetChildAttrib("wordbreak");
  223. if (value == "true")
  224. SetWordbreak(true);
  225. else
  226. SetWordbreak(false);
  227. value = markup.GetChildAttrib("leftMargin");
  228. if (value != "")
  229. leftMargin = atoi (value);
  230. value = markup.GetChildAttrib("rightMargin");
  231. if (value != "")
  232. rightMargin = atoi (value);
  233. value = markup.GetChildAttrib("overlap");
  234. if (value == "true")
  235. overlap = true;
  236. else
  237. overlap = false;
  238. label = markup.GetChildAttrib("label");
  239. text = markup.GetChildData();
  240. if (text != "")
  241. text = markup.GetChildData();
  242. return 0;
  243. }
  244. int XCell::SetSpan(int rows, int cols)
  245. {
  246. rowSpan = rows;
  247. colSpan = cols;
  248. return 0;
  249. }
  250. int XCell::SetText(CString str)
  251. {
  252. text = str;
  253. return 0;
  254. }
  255. CString XCell::GetText()
  256. {
  257. return text;
  258. }
  259. int XCell::SetTextColor(COLORREF color)
  260. {
  261. textColor = color;
  262. return 0;
  263. }
  264. COLORREF XCell::GetTextColor()
  265. {
  266. return textColor;
  267. }
  268. int XCell::SetAlignment (int align)
  269. {
  270. if ( (align & ~ALIGN_MASK) != 0) return -1;
  271. format = (format & ~ALIGN_MASK) | align;
  272. return 0;
  273. }
  274. int XCell::GetAlignment ()
  275. {
  276. return format & ALIGN_MASK;
  277. }
  278. int XCell::SetFormat (int format)
  279. {
  280. this->format = format;
  281. return 0;
  282. }
  283. int XCell::GetFormat ()
  284. {
  285. return format;
  286. }
  287. int XCell::SetSingleLine (bool enable)
  288. {
  289. if (enable)
  290. format |= DT_SINGLELINE;
  291. else
  292. format &= ~DT_SINGLELINE;
  293. return 0;
  294. }
  295. bool XCell::GetSingleLine ()
  296. {
  297. return (format & DT_SINGLELINE) != 0;
  298. }
  299. int XCell::SetWordbreak (bool enable)
  300. {
  301. if (enable)
  302. {
  303. format |= DT_WORDBREAK;
  304. format |= DT_EDITCONTROL;
  305. }
  306. else
  307. {
  308. format &= ~DT_WORDBREAK;
  309. format &= ~DT_EDITCONTROL;
  310. }
  311. return 0;
  312. }
  313. bool XCell::GetWordbreak ()
  314. {
  315. return (format & DT_WORDBREAK) != 0;
  316. }
  317. int XCell::SetEllipsis (bool enable)
  318. {
  319. if (enable)
  320. format |= DT_END_ELLIPSIS;
  321. else
  322. format &= ~DT_END_ELLIPSIS;
  323. return 0;
  324. }
  325. bool XCell::GetEllipsis ()
  326. {
  327. return (format & DT_END_ELLIPSIS) != 0;
  328. }
  329. int XCell::SetLeftMargin (int pixels)
  330. {
  331. leftMargin = pixels;
  332. return 0;
  333. }
  334. int XCell::GetLeftMargin ()
  335. {
  336. return leftMargin;
  337. }
  338. int XCell::SetRightMargin (int pixels)
  339. {
  340. rightMargin = pixels;
  341. return 0;
  342. }
  343. int XCell::GetRightMargin ()
  344. {
  345. return rightMargin;
  346. }
  347. int XCell::SetLabel (CString str)
  348. {
  349. label = str;
  350. return 0;
  351. }
  352. CString XCell::GetLabel ()
  353. {
  354. return label;
  355. }
  356. int XCell::SetOverlap (bool enable)
  357. {
  358. overlap = enable;
  359. return 0;
  360. }
  361. bool XCell::GetOverlap ()
  362. {
  363. return overlap;
  364. }
  365. int XCell::SetTextFont(CFont* font)
  366. {
  367. LOGFONT longfont;
  368. if (!font->GetLogFont(&longfont))
  369. return -1;
  370. if (textFont)
  371. {
  372. delete textFont;
  373. textFont = NULL;
  374. }
  375. textFont = new CFont;
  376. textFont->CreateFontIndirect(&longfont);
  377. return 0;
  378. }
  379. CFont* XCell::GetTextFont()
  380. {
  381. return textFont;
  382. }
  383. int XCell::SetTextFontSize(int size)
  384. {
  385. textFontSize = size;
  386. return 0;
  387. }
  388. int XCell::GetTextFontSize()
  389. {
  390. return textFontSize;
  391. }
  392. int XCell::SetLabelColor(COLORREF color)
  393. {
  394. labelColor = color;
  395. return 0;
  396. }
  397. COLORREF XCell::GetLabelColor()
  398. {
  399. return labelColor;
  400. }
  401. int XCell::SetLabelFont(CFont* font)
  402. {
  403. LOGFONT logfont;
  404. if (!font->GetLogFont(&logfont))
  405. return -1;
  406. if (labelFont)
  407. {
  408. labelFont->DeleteObject();
  409. labelFont = NULL;
  410. }
  411. labelFont = new CFont;
  412. labelFont->CreateFontIndirect(&logfont);
  413. return 0;
  414. }
  415. CFont* XCell::GetLabelFont()
  416. {
  417. return labelFont;
  418. }
  419. int XCell::SetLabelFontSize(int size)
  420. {
  421. labelFontSize = size;
  422. return 0;
  423. }
  424. int XCell::GetLabelFontSize()
  425. {
  426. return labelFontSize;
  427. }
  428. int XCell::SetBackMode(int mode)
  429. {
  430. backMode = mode;
  431. return 0;
  432. }
  433. int XCell::GetBackMode()
  434. {
  435. return backMode;
  436. }
  437. int XCell::SetBackColor(COLORREF color)
  438. {
  439. backColor = color;
  440. return 0;
  441. }
  442. COLORREF XCell::GetBackColor()
  443. {
  444. return backColor;
  445. }
  446. int XCell::SetBorderSyle(int syle)
  447. {
  448. borderStyle = syle;
  449. return 0;
  450. }
  451. int XCell::GetBorderSyle()
  452. {
  453. return borderStyle;
  454. }
  455. int XCell::DrawText(CDC* pDC, RECT rect)
  456. {
  457. if (text.IsEmpty()) return 0;
  458. COLORREF oldTextColor = pDC->SetTextColor (textColor);
  459. int oldBkMode = pDC->SetBkMode (backMode);
  460. CFont* oldFont;
  461. CFont tempFont;
  462. if (textFont)
  463. oldFont = pDC->SelectObject (textFont);
  464. else
  465. {
  466. tempFont.CreateFont (textFontSize,0,0, 0, FW_NORMAL, FALSE, FALSE, 0,
  467. ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  468. DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, textFontFace);
  469. oldFont = pDC->SelectObject (&tempFont);
  470. }
  471. if (leftMargin)
  472. rect.left += leftMargin;
  473. if (rightMargin)
  474. rect.right -= rightMargin;
  475. pDC->DrawText (text, &rect, format);
  476. // pDC->DrawText (text, &rect, DT_VCENTER|DT_SINGLELINE);
  477. pDC->SetTextColor(oldTextColor);
  478. pDC->SetBkMode (oldBkMode);
  479. pDC->SelectObject (oldFont);
  480. tempFont.DeleteObject ();
  481. return 0;
  482. }
  483. int XCell::DrawLabel(CDC* pDC, RECT rect)
  484. {
  485. return 0;
  486. }
  487. int XCell::Draw(CDC* pDC, RECT rect)
  488. {
  489. DrawBorder (pDC, rect);
  490. RECT rect1;
  491. rect1.left = rect.left+1;
  492. rect1.top = rect.top+1;
  493. rect1.right = rect.right;
  494. rect1.bottom = rect.bottom;
  495. DrawBackground (pDC, rect1);
  496. DrawText (pDC, rect1);
  497. return 0;
  498. }
  499. int XCell::DrawBorder(CDC* pDC, RECT rect)
  500. {
  501. CPen linePen (PS_SOLID, 1, borderColor);
  502. CPen* pOldPen = pDC->SelectObject(&linePen);
  503. pDC->MoveTo (rect.left, rect.top);
  504. pDC->LineTo (rect.right, rect.top);
  505. pDC->LineTo (rect.right, rect.bottom);
  506. pDC->LineTo (rect.left, rect.bottom);
  507. pDC->LineTo (rect.left, rect.top);
  508. pDC->SelectObject(pOldPen);
  509. return 0;
  510. }
  511. int XCell::DrawHitBorder (CDC* pDC, RECT rect, COLORREF color)
  512. {
  513. CPen pen (PS_SOLID, 3, color);
  514. CPen* oldPen = pDC->SelectObject(&pen);
  515. pDC->MoveTo (rect.left, rect.top);
  516. pDC->LineTo (rect.right, rect.top);
  517. pDC->LineTo (rect.right, rect.bottom);
  518. pDC->LineTo (rect.left, rect.bottom);
  519. pDC->LineTo (rect.left, rect.top);
  520. pDC->SelectObject(oldPen);
  521. return 0;
  522. }
  523. int XCell::DrawBackground (CDC* pDC, RECT rect)
  524. {
  525. CBrush bkBrush (backColor);
  526. pDC->FillRect (&rect, &bkBrush);
  527. return 0;
  528. }
  529. int XCell::CalcTextRect (CDC* pDC, RECT* rect)
  530. {
  531. CFont* oldFont;
  532. CFont tempFont;
  533. if (textFont)
  534. oldFont = pDC->SelectObject (textFont);
  535. else
  536. {
  537. tempFont.CreateFont (textFontSize,0,0, 0, FW_NORMAL, FALSE, FALSE, 0,
  538. ANSI_CHARSET,OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  539. DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, textFontFace);
  540. oldFont = pDC->SelectObject (&tempFont);
  541. }
  542. if (leftMargin)
  543. rect->left += leftMargin;
  544. pDC->DrawText (text, rect, format | DT_CALCRECT);
  545. // pDC->DrawText ("eee3333ttt tt2ww wwddddffffffffvvvvgg2 33 44", rect, format | DT_CALCRECT);
  546. // pDC->DrawText (text, rect, DT_EDITCONTROL | DT_WORDBREAK | DT_CALCRECT);
  547. pDC->SelectObject (oldFont);
  548. tempFont.DeleteObject ();
  549. return 0;
  550. }