UIScrollBar.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. #include "StdAfx.h"
  2. #include "UIScrollBar.h"
  3. namespace DuiLib
  4. {
  5. IMPLEMENT_DUICONTROL(CScrollBarUI)
  6. CScrollBarUI::CScrollBarUI() : m_bHorizontal(false), m_nRange(0), m_nScrollPos(0), m_nLineSize(8),
  7. m_pOwner(NULL), m_nLastScrollPos(0), m_nLastScrollOffset(0), m_nScrollRepeatDelay(0), m_uButton1State(0), \
  8. m_uButton2State(0), m_uThumbState(0), m_bShowButton1(true), m_bShowButton2(true), m_bShow(true)
  9. {
  10. m_cxyFixed.cx = DEFAULT_SCROLLBAR_SIZE;
  11. m_ptLastMouse.x = m_ptLastMouse.y = 0;
  12. ::ZeroMemory(&m_rcThumb, sizeof(m_rcThumb));
  13. ::ZeroMemory(&m_rcButton1, sizeof(m_rcButton1));
  14. ::ZeroMemory(&m_rcButton2, sizeof(m_rcButton2));
  15. }
  16. LPCTSTR CScrollBarUI::GetClass() const
  17. {
  18. return _T("ScrollBarUI");
  19. }
  20. LPVOID CScrollBarUI::GetInterface(LPCTSTR pstrName)
  21. {
  22. if( _tcsicmp(pstrName, DUI_CTR_SCROLLBAR) == 0 ) return static_cast<CScrollBarUI*>(this);
  23. return CControlUI::GetInterface(pstrName);
  24. }
  25. CContainerUI* CScrollBarUI::GetOwner() const
  26. {
  27. return m_pOwner;
  28. }
  29. void CScrollBarUI::SetOwner(CContainerUI* pOwner)
  30. {
  31. m_pOwner = pOwner;
  32. }
  33. void CScrollBarUI::SetVisible(bool bVisible)
  34. {
  35. if( m_bVisible == bVisible ) return;
  36. bool v = IsVisible();
  37. m_bVisible = bVisible;
  38. if( m_bFocused ) m_bFocused = false;
  39. }
  40. void CScrollBarUI::SetEnabled(bool bEnable)
  41. {
  42. CControlUI::SetEnabled(bEnable);
  43. if( !IsEnabled() ) {
  44. m_uButton1State = 0;
  45. m_uButton2State = 0;
  46. m_uThumbState = 0;
  47. }
  48. }
  49. void CScrollBarUI::SetFocus()
  50. {
  51. if( m_pOwner != NULL ) m_pOwner->SetFocus();
  52. else CControlUI::SetFocus();
  53. }
  54. bool CScrollBarUI::IsHorizontal()
  55. {
  56. return m_bHorizontal;
  57. }
  58. void CScrollBarUI::SetHorizontal(bool bHorizontal)
  59. {
  60. if( m_bHorizontal == bHorizontal ) return;
  61. m_bHorizontal = bHorizontal;
  62. if( m_bHorizontal ) {
  63. if( m_cxyFixed.cy == 0 ) {
  64. m_cxyFixed.cx = 0;
  65. m_cxyFixed.cy = DEFAULT_SCROLLBAR_SIZE;
  66. }
  67. }
  68. else {
  69. if( m_cxyFixed.cx == 0 ) {
  70. m_cxyFixed.cx = DEFAULT_SCROLLBAR_SIZE;
  71. m_cxyFixed.cy = 0;
  72. }
  73. }
  74. if( m_pOwner != NULL ) m_pOwner->NeedUpdate(); else NeedParentUpdate();
  75. }
  76. int CScrollBarUI::GetScrollRange() const
  77. {
  78. return m_nRange;
  79. }
  80. void CScrollBarUI::SetScrollRange(int nRange)
  81. {
  82. if( m_nRange == nRange ) return;
  83. m_nRange = nRange;
  84. if( m_nRange < 0 ) m_nRange = 0;
  85. if( m_nScrollPos > m_nRange ) m_nScrollPos = m_nRange;
  86. SetPos(m_rcItem);
  87. }
  88. int CScrollBarUI::GetScrollPos() const
  89. {
  90. return m_nScrollPos;
  91. }
  92. void CScrollBarUI::SetScrollPos(int nPos)
  93. {
  94. if( m_nScrollPos == nPos ) return;
  95. m_nScrollPos = nPos;
  96. if( m_nScrollPos < 0 ) m_nScrollPos = 0;
  97. if( m_nScrollPos > m_nRange ) m_nScrollPos = m_nRange;
  98. SetPos(m_rcItem);
  99. }
  100. int CScrollBarUI::GetLineSize() const
  101. {
  102. return m_nLineSize;
  103. }
  104. void CScrollBarUI::SetLineSize(int nSize)
  105. {
  106. m_nLineSize = nSize;
  107. }
  108. bool CScrollBarUI::GetShowButton1()
  109. {
  110. return m_bShowButton1;
  111. }
  112. void CScrollBarUI::SetShowButton1(bool bShow)
  113. {
  114. m_bShowButton1 = bShow;
  115. SetPos(m_rcItem);
  116. }
  117. LPCTSTR CScrollBarUI::GetButton1NormalImage()
  118. {
  119. return m_sButton1NormalImage;
  120. }
  121. void CScrollBarUI::SetButton1NormalImage(LPCTSTR pStrImage)
  122. {
  123. m_sButton1NormalImage = pStrImage;
  124. Invalidate();
  125. }
  126. LPCTSTR CScrollBarUI::GetButton1HotImage()
  127. {
  128. return m_sButton1HotImage;
  129. }
  130. void CScrollBarUI::SetButton1HotImage(LPCTSTR pStrImage)
  131. {
  132. m_sButton1HotImage = pStrImage;
  133. Invalidate();
  134. }
  135. LPCTSTR CScrollBarUI::GetButton1PushedImage()
  136. {
  137. return m_sButton1PushedImage;
  138. }
  139. void CScrollBarUI::SetButton1PushedImage(LPCTSTR pStrImage)
  140. {
  141. m_sButton1PushedImage = pStrImage;
  142. Invalidate();
  143. }
  144. LPCTSTR CScrollBarUI::GetButton1DisabledImage()
  145. {
  146. return m_sButton1DisabledImage;
  147. }
  148. void CScrollBarUI::SetButton1DisabledImage(LPCTSTR pStrImage)
  149. {
  150. m_sButton1DisabledImage = pStrImage;
  151. Invalidate();
  152. }
  153. bool CScrollBarUI::GetShowButton2()
  154. {
  155. return m_bShowButton2;
  156. }
  157. void CScrollBarUI::SetShowButton2(bool bShow)
  158. {
  159. m_bShowButton2 = bShow;
  160. SetPos(m_rcItem);
  161. }
  162. LPCTSTR CScrollBarUI::GetButton2NormalImage()
  163. {
  164. return m_sButton2NormalImage;
  165. }
  166. void CScrollBarUI::SetButton2NormalImage(LPCTSTR pStrImage)
  167. {
  168. m_sButton2NormalImage = pStrImage;
  169. Invalidate();
  170. }
  171. LPCTSTR CScrollBarUI::GetButton2HotImage()
  172. {
  173. return m_sButton2HotImage;
  174. }
  175. void CScrollBarUI::SetButton2HotImage(LPCTSTR pStrImage)
  176. {
  177. m_sButton2HotImage = pStrImage;
  178. Invalidate();
  179. }
  180. LPCTSTR CScrollBarUI::GetButton2PushedImage()
  181. {
  182. return m_sButton2PushedImage;
  183. }
  184. void CScrollBarUI::SetButton2PushedImage(LPCTSTR pStrImage)
  185. {
  186. m_sButton2PushedImage = pStrImage;
  187. Invalidate();
  188. }
  189. LPCTSTR CScrollBarUI::GetButton2DisabledImage()
  190. {
  191. return m_sButton2DisabledImage;
  192. }
  193. void CScrollBarUI::SetButton2DisabledImage(LPCTSTR pStrImage)
  194. {
  195. m_sButton2DisabledImage = pStrImage;
  196. Invalidate();
  197. }
  198. LPCTSTR CScrollBarUI::GetThumbNormalImage()
  199. {
  200. return m_sThumbNormalImage;
  201. }
  202. void CScrollBarUI::SetThumbNormalImage(LPCTSTR pStrImage)
  203. {
  204. m_sThumbNormalImage = pStrImage;
  205. Invalidate();
  206. }
  207. LPCTSTR CScrollBarUI::GetThumbHotImage()
  208. {
  209. return m_sThumbHotImage;
  210. }
  211. void CScrollBarUI::SetThumbHotImage(LPCTSTR pStrImage)
  212. {
  213. m_sThumbHotImage = pStrImage;
  214. Invalidate();
  215. }
  216. LPCTSTR CScrollBarUI::GetThumbPushedImage()
  217. {
  218. return m_sThumbPushedImage;
  219. }
  220. void CScrollBarUI::SetThumbPushedImage(LPCTSTR pStrImage)
  221. {
  222. m_sThumbPushedImage = pStrImage;
  223. Invalidate();
  224. }
  225. LPCTSTR CScrollBarUI::GetThumbDisabledImage()
  226. {
  227. return m_sThumbDisabledImage;
  228. }
  229. void CScrollBarUI::SetThumbDisabledImage(LPCTSTR pStrImage)
  230. {
  231. m_sThumbDisabledImage = pStrImage;
  232. Invalidate();
  233. }
  234. LPCTSTR CScrollBarUI::GetRailNormalImage()
  235. {
  236. return m_sRailNormalImage;
  237. }
  238. void CScrollBarUI::SetRailNormalImage(LPCTSTR pStrImage)
  239. {
  240. m_sRailNormalImage = pStrImage;
  241. Invalidate();
  242. }
  243. LPCTSTR CScrollBarUI::GetRailHotImage()
  244. {
  245. return m_sRailHotImage;
  246. }
  247. void CScrollBarUI::SetRailHotImage(LPCTSTR pStrImage)
  248. {
  249. m_sRailHotImage = pStrImage;
  250. Invalidate();
  251. }
  252. LPCTSTR CScrollBarUI::GetRailPushedImage()
  253. {
  254. return m_sRailPushedImage;
  255. }
  256. void CScrollBarUI::SetRailPushedImage(LPCTSTR pStrImage)
  257. {
  258. m_sRailPushedImage = pStrImage;
  259. Invalidate();
  260. }
  261. LPCTSTR CScrollBarUI::GetRailDisabledImage()
  262. {
  263. return m_sRailDisabledImage;
  264. }
  265. void CScrollBarUI::SetRailDisabledImage(LPCTSTR pStrImage)
  266. {
  267. m_sRailDisabledImage = pStrImage;
  268. Invalidate();
  269. }
  270. LPCTSTR CScrollBarUI::GetBkNormalImage()
  271. {
  272. return m_sBkNormalImage;
  273. }
  274. void CScrollBarUI::SetBkNormalImage(LPCTSTR pStrImage)
  275. {
  276. m_sBkNormalImage = pStrImage;
  277. Invalidate();
  278. }
  279. LPCTSTR CScrollBarUI::GetBkHotImage()
  280. {
  281. return m_sBkHotImage;
  282. }
  283. void CScrollBarUI::SetBkHotImage(LPCTSTR pStrImage)
  284. {
  285. m_sBkHotImage = pStrImage;
  286. Invalidate();
  287. }
  288. LPCTSTR CScrollBarUI::GetBkPushedImage()
  289. {
  290. return m_sBkPushedImage;
  291. }
  292. void CScrollBarUI::SetBkPushedImage(LPCTSTR pStrImage)
  293. {
  294. m_sBkPushedImage = pStrImage;
  295. Invalidate();
  296. }
  297. LPCTSTR CScrollBarUI::GetBkDisabledImage()
  298. {
  299. return m_sBkDisabledImage;
  300. }
  301. void CScrollBarUI::SetBkDisabledImage(LPCTSTR pStrImage)
  302. {
  303. m_sBkDisabledImage = pStrImage;
  304. Invalidate();
  305. }
  306. bool CScrollBarUI::GetShow()
  307. {
  308. return m_bShow;
  309. }
  310. void CScrollBarUI::SetShow(bool bShow)
  311. {
  312. m_bShow = bShow;
  313. Invalidate();
  314. }
  315. void CScrollBarUI::SetPos(RECT rc, bool bNeedInvalidate)
  316. {
  317. CControlUI::SetPos(rc, bNeedInvalidate);
  318. SIZE cxyFixed = m_cxyFixed;
  319. if (m_pManager != NULL) {
  320. GetManager()->GetDPIObj()->Scale(&cxyFixed);
  321. }
  322. rc = m_rcItem;
  323. if( m_bHorizontal ) {
  324. int cx = rc.right - rc.left;
  325. if( m_bShowButton1 ) cx -= cxyFixed.cy;
  326. if( m_bShowButton2 ) cx -= cxyFixed.cy;
  327. if( cx > cxyFixed.cy ) {
  328. m_rcButton1.left = rc.left;
  329. m_rcButton1.top = rc.top;
  330. if( m_bShowButton1 ) {
  331. m_rcButton1.right = rc.left + cxyFixed.cy;
  332. m_rcButton1.bottom = rc.top + cxyFixed.cy;
  333. }
  334. else {
  335. m_rcButton1.right = m_rcButton1.left;
  336. m_rcButton1.bottom = m_rcButton1.top;
  337. }
  338. m_rcButton2.top = rc.top;
  339. m_rcButton2.right = rc.right;
  340. if( m_bShowButton2 ) {
  341. m_rcButton2.left = rc.right - cxyFixed.cy;
  342. m_rcButton2.bottom = rc.top + cxyFixed.cy;
  343. }
  344. else {
  345. m_rcButton2.left = m_rcButton2.right;
  346. m_rcButton2.bottom = m_rcButton2.top;
  347. }
  348. m_rcThumb.top = rc.top;
  349. m_rcThumb.bottom = rc.top + cxyFixed.cy;
  350. if( m_nRange > 0 ) {
  351. int cxThumb = cx * (rc.right - rc.left) / (m_nRange + rc.right - rc.left);
  352. if( cxThumb < cxyFixed.cy ) cxThumb = cxyFixed.cy;
  353. m_rcThumb.left = m_nScrollPos * (cx - cxThumb) / m_nRange + m_rcButton1.right;
  354. m_rcThumb.right = m_rcThumb.left + cxThumb;
  355. if( m_rcThumb.right > m_rcButton2.left ) {
  356. m_rcThumb.left = m_rcButton2.left - cxThumb;
  357. m_rcThumb.right = m_rcButton2.left;
  358. }
  359. }
  360. else {
  361. m_rcThumb.left = m_rcButton1.right;
  362. m_rcThumb.right = m_rcButton2.left;
  363. }
  364. }
  365. else {
  366. int cxButton = (rc.right - rc.left) / 2;
  367. if( cxButton > cxyFixed.cy ) cxButton = cxyFixed.cy;
  368. m_rcButton1.left = rc.left;
  369. m_rcButton1.top = rc.top;
  370. if( m_bShowButton1 ) {
  371. m_rcButton1.right = rc.left + cxButton;
  372. m_rcButton1.bottom = rc.top + cxyFixed.cy;
  373. }
  374. else {
  375. m_rcButton1.right = m_rcButton1.left;
  376. m_rcButton1.bottom = m_rcButton1.top;
  377. }
  378. m_rcButton2.top = rc.top;
  379. m_rcButton2.right = rc.right;
  380. if( m_bShowButton2 ) {
  381. m_rcButton2.left = rc.right - cxButton;
  382. m_rcButton2.bottom = rc.top + cxyFixed.cy;
  383. }
  384. else {
  385. m_rcButton2.left = m_rcButton2.right;
  386. m_rcButton2.bottom = m_rcButton2.top;
  387. }
  388. ::ZeroMemory(&m_rcThumb, sizeof(m_rcThumb));
  389. }
  390. }
  391. else {
  392. int cy = rc.bottom - rc.top;
  393. if( m_bShowButton1 ) cy -= cxyFixed.cx;
  394. if( m_bShowButton2 ) cy -= cxyFixed.cx;
  395. if( cy > cxyFixed.cx ) {
  396. m_rcButton1.left = rc.left;
  397. m_rcButton1.top = rc.top;
  398. if( m_bShowButton1 ) {
  399. m_rcButton1.right = rc.left + cxyFixed.cx;
  400. m_rcButton1.bottom = rc.top + cxyFixed.cx;
  401. }
  402. else {
  403. m_rcButton1.right = m_rcButton1.left;
  404. m_rcButton1.bottom = m_rcButton1.top;
  405. }
  406. m_rcButton2.left = rc.left;
  407. m_rcButton2.bottom = rc.bottom;
  408. if( m_bShowButton2 ) {
  409. m_rcButton2.top = rc.bottom - cxyFixed.cx;
  410. m_rcButton2.right = rc.left + cxyFixed.cx;
  411. }
  412. else {
  413. m_rcButton2.top = m_rcButton2.bottom;
  414. m_rcButton2.right = m_rcButton2.left;
  415. }
  416. m_rcThumb.left = rc.left;
  417. m_rcThumb.right = rc.left + cxyFixed.cx;
  418. if( m_nRange > 0 ) {
  419. int cyThumb = cy * (rc.bottom - rc.top) / (m_nRange + rc.bottom - rc.top);
  420. if( cyThumb < cxyFixed.cx ) cyThumb = cxyFixed.cx;
  421. m_rcThumb.top = (m_nScrollPos * 1.0f / m_nRange) * (cy - cyThumb) + m_rcButton1.bottom;
  422. m_rcThumb.bottom = m_rcThumb.top + cyThumb;
  423. if( m_rcThumb.bottom > m_rcButton2.top ) {
  424. m_rcThumb.top = m_rcButton2.top - cyThumb;
  425. m_rcThumb.bottom = m_rcButton2.top;
  426. }
  427. }
  428. else {
  429. m_rcThumb.top = m_rcButton1.bottom;
  430. m_rcThumb.bottom = m_rcButton2.top;
  431. }
  432. }
  433. else {
  434. int cyButton = (rc.bottom - rc.top) / 2;
  435. if( cyButton > cxyFixed.cx ) cyButton = cxyFixed.cx;
  436. m_rcButton1.left = rc.left;
  437. m_rcButton1.top = rc.top;
  438. if( m_bShowButton1 ) {
  439. m_rcButton1.right = rc.left + cxyFixed.cx;
  440. m_rcButton1.bottom = rc.top + cyButton;
  441. }
  442. else {
  443. m_rcButton1.right = m_rcButton1.left;
  444. m_rcButton1.bottom = m_rcButton1.top;
  445. }
  446. m_rcButton2.left = rc.left;
  447. m_rcButton2.bottom = rc.bottom;
  448. if( m_bShowButton2 ) {
  449. m_rcButton2.top = rc.bottom - cyButton;
  450. m_rcButton2.right = rc.left + cxyFixed.cx;
  451. }
  452. else {
  453. m_rcButton2.top = m_rcButton2.bottom;
  454. m_rcButton2.right = m_rcButton2.left;
  455. }
  456. ::ZeroMemory(&m_rcThumb, sizeof(m_rcThumb));
  457. }
  458. }
  459. }
  460. void CScrollBarUI::DoEvent(TEventUI& event)
  461. {
  462. if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
  463. if( m_pOwner != NULL ) m_pOwner->DoEvent(event);
  464. else CControlUI::DoEvent(event);
  465. return;
  466. }
  467. if( event.Type == UIEVENT_SETFOCUS )
  468. {
  469. return;
  470. }
  471. if( event.Type == UIEVENT_KILLFOCUS )
  472. {
  473. return;
  474. }
  475. if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
  476. {
  477. if( !IsEnabled() ) return;
  478. m_nLastScrollOffset = 0;
  479. m_nScrollRepeatDelay = 0;
  480. if( ::PtInRect(&m_rcButton1, event.ptMouse) ) {
  481. m_uButton1State |= UISTATE_PUSHED;
  482. if( !m_bHorizontal ) {
  483. if( m_pOwner != NULL ) m_pOwner->LineUp();
  484. else SetScrollPos(m_nScrollPos - m_nLineSize);
  485. }
  486. else {
  487. if( m_pOwner != NULL ) m_pOwner->LineLeft();
  488. else SetScrollPos(m_nScrollPos - m_nLineSize);
  489. }
  490. }
  491. else if( ::PtInRect(&m_rcButton2, event.ptMouse) ) {
  492. m_uButton2State |= UISTATE_PUSHED;
  493. if( !m_bHorizontal ) {
  494. if( m_pOwner != NULL ) m_pOwner->LineDown();
  495. else SetScrollPos(m_nScrollPos + m_nLineSize);
  496. }
  497. else {
  498. if( m_pOwner != NULL ) m_pOwner->LineRight();
  499. else SetScrollPos(m_nScrollPos + m_nLineSize);
  500. }
  501. }
  502. else if( ::PtInRect(&m_rcThumb, event.ptMouse) ) {
  503. m_uThumbState |= UISTATE_CAPTURED | UISTATE_PUSHED;
  504. m_ptLastMouse = event.ptMouse;
  505. m_nLastScrollPos = m_nScrollPos;
  506. m_pManager->SetTimer(this, DEFAULT_TIMERID, 50U);
  507. }
  508. else {
  509. if( !m_bHorizontal ) {
  510. if( event.ptMouse.y < m_rcThumb.top ) {
  511. if( m_pOwner != NULL ) m_pOwner->PageUp();
  512. else SetScrollPos(m_nScrollPos + m_rcItem.top - m_rcItem.bottom);
  513. }
  514. else if ( event.ptMouse.y > m_rcThumb.bottom ){
  515. if( m_pOwner != NULL ) m_pOwner->PageDown();
  516. else SetScrollPos(m_nScrollPos - m_rcItem.top + m_rcItem.bottom);
  517. }
  518. }
  519. else {
  520. if( event.ptMouse.x < m_rcThumb.left ) {
  521. if( m_pOwner != NULL ) m_pOwner->PageLeft();
  522. else SetScrollPos(m_nScrollPos + m_rcItem.left - m_rcItem.right);
  523. }
  524. else if ( event.ptMouse.x > m_rcThumb.right ){
  525. if( m_pOwner != NULL ) m_pOwner->PageRight();
  526. else SetScrollPos(m_nScrollPos - m_rcItem.left + m_rcItem.right);
  527. }
  528. }
  529. }
  530. if( m_pManager != NULL) m_pManager->SendNotify(this, DUI_MSGTYPE_SCROLL);
  531. return;
  532. }
  533. if( event.Type == UIEVENT_BUTTONUP )
  534. {
  535. m_nScrollRepeatDelay = 0;
  536. m_nLastScrollOffset = 0;
  537. m_pManager->KillTimer(this, DEFAULT_TIMERID);
  538. if( (m_uThumbState & UISTATE_CAPTURED) != 0 ) {
  539. m_uThumbState &= ~( UISTATE_CAPTURED | UISTATE_PUSHED );
  540. Invalidate();
  541. }
  542. else if( (m_uButton1State & UISTATE_PUSHED) != 0 ) {
  543. m_uButton1State &= ~UISTATE_PUSHED;
  544. Invalidate();
  545. }
  546. else if( (m_uButton2State & UISTATE_PUSHED) != 0 ) {
  547. m_uButton2State &= ~UISTATE_PUSHED;
  548. Invalidate();
  549. }
  550. return;
  551. }
  552. if( event.Type == UIEVENT_MOUSEMOVE )
  553. {
  554. if( (m_uThumbState & UISTATE_CAPTURED) != 0 ) {
  555. if( !m_bHorizontal ) {
  556. __int64 fMouseRange = (event.ptMouse.y - m_ptLastMouse.y) * m_nRange;
  557. int nBtnSize = 0;
  558. if(GetShowButton1()) nBtnSize += m_cxyFixed.cx;
  559. if(GetShowButton2()) nBtnSize += m_cxyFixed.cx;
  560. int vRange = m_rcItem.bottom - m_rcItem.top - (m_rcThumb.bottom - m_rcThumb.top) - nBtnSize;
  561. if (vRange != 0){
  562. m_nLastScrollOffset = fMouseRange / abs(vRange);
  563. }
  564. }
  565. else {
  566. __int64 fMouseRange = (event.ptMouse.x - m_ptLastMouse.x) * m_nRange;
  567. int nBtnSize = 0;
  568. if(GetShowButton1()) nBtnSize += m_cxyFixed.cy;
  569. if(GetShowButton2()) nBtnSize += m_cxyFixed.cy;
  570. int hRange = m_rcItem.right - m_rcItem.left - m_rcThumb.right + m_rcThumb.left - nBtnSize;
  571. if (hRange != 0) m_nLastScrollOffset = fMouseRange / abs(hRange);
  572. }
  573. }
  574. else {
  575. if( (m_uThumbState & UISTATE_HOT) != 0 ) {
  576. if( !::PtInRect(&m_rcThumb, event.ptMouse) ) {
  577. m_uThumbState &= ~UISTATE_HOT;
  578. Invalidate();
  579. }
  580. }
  581. else {
  582. if( !IsEnabled() ) return;
  583. if( ::PtInRect(&m_rcThumb, event.ptMouse) ) {
  584. m_uThumbState |= UISTATE_HOT;
  585. Invalidate();
  586. }
  587. }
  588. }
  589. return;
  590. }
  591. if( event.Type == UIEVENT_CONTEXTMENU )
  592. {
  593. return;
  594. }
  595. if( event.Type == UIEVENT_TIMER && event.wParam == DEFAULT_TIMERID )
  596. {
  597. ++m_nScrollRepeatDelay;
  598. if( (m_uThumbState & UISTATE_CAPTURED) != 0 ) {
  599. if( !m_bHorizontal ) {
  600. if( m_pOwner != NULL ) m_pOwner->SetScrollPos(CDuiSize(m_pOwner->GetScrollPos().cx, \
  601. m_nLastScrollPos + m_nLastScrollOffset));
  602. else SetScrollPos(m_nLastScrollPos + m_nLastScrollOffset);
  603. }
  604. else {
  605. if( m_pOwner != NULL ) m_pOwner->SetScrollPos(CDuiSize(m_nLastScrollPos + m_nLastScrollOffset, \
  606. m_pOwner->GetScrollPos().cy));
  607. else SetScrollPos(m_nLastScrollPos + m_nLastScrollOffset);
  608. }
  609. Invalidate();
  610. }
  611. else if( (m_uButton1State & UISTATE_PUSHED) != 0 ) {
  612. if( m_nScrollRepeatDelay <= 5 ) return;
  613. if( !m_bHorizontal ) {
  614. if( m_pOwner != NULL ) m_pOwner->LineUp();
  615. else SetScrollPos(m_nScrollPos - m_nLineSize);
  616. }
  617. else {
  618. if( m_pOwner != NULL ) m_pOwner->LineLeft();
  619. else SetScrollPos(m_nScrollPos - m_nLineSize);
  620. }
  621. }
  622. else if( (m_uButton2State & UISTATE_PUSHED) != 0 ) {
  623. if( m_nScrollRepeatDelay <= 5 ) return;
  624. if( !m_bHorizontal ) {
  625. if( m_pOwner != NULL ) m_pOwner->LineDown();
  626. else SetScrollPos(m_nScrollPos + m_nLineSize);
  627. }
  628. else {
  629. if( m_pOwner != NULL ) m_pOwner->LineRight();
  630. else SetScrollPos(m_nScrollPos + m_nLineSize);
  631. }
  632. }
  633. else {
  634. if( m_nScrollRepeatDelay <= 5 ) return;
  635. POINT pt = { 0 };
  636. ::GetCursorPos(&pt);
  637. ::ScreenToClient(m_pManager->GetPaintWindow(), &pt);
  638. if( !m_bHorizontal ) {
  639. if( pt.y < m_rcThumb.top ) {
  640. if( m_pOwner != NULL ) m_pOwner->PageUp();
  641. else SetScrollPos(m_nScrollPos + m_rcItem.top - m_rcItem.bottom);
  642. }
  643. else if ( pt.y > m_rcThumb.bottom ){
  644. if( m_pOwner != NULL ) m_pOwner->PageDown();
  645. else SetScrollPos(m_nScrollPos - m_rcItem.top + m_rcItem.bottom);
  646. }
  647. }
  648. else {
  649. if( pt.x < m_rcThumb.left ) {
  650. if( m_pOwner != NULL ) m_pOwner->PageLeft();
  651. else SetScrollPos(m_nScrollPos + m_rcItem.left - m_rcItem.right);
  652. }
  653. else if ( pt.x > m_rcThumb.right ){
  654. if( m_pOwner != NULL ) m_pOwner->PageRight();
  655. else SetScrollPos(m_nScrollPos - m_rcItem.left + m_rcItem.right);
  656. }
  657. }
  658. }
  659. if( m_pManager != NULL ) m_pManager->SendNotify(this, DUI_MSGTYPE_SCROLL);
  660. return;
  661. }
  662. if( event.Type == UIEVENT_MOUSEENTER )
  663. {
  664. if( IsEnabled() ) {
  665. m_uButton1State |= UISTATE_HOT;
  666. m_uButton2State |= UISTATE_HOT;
  667. if( ::PtInRect(&m_rcThumb, event.ptMouse) ) m_uThumbState |= UISTATE_HOT;
  668. Invalidate();
  669. }
  670. return;
  671. }
  672. if( event.Type == UIEVENT_MOUSELEAVE )
  673. {
  674. if( IsEnabled() ) {
  675. m_uButton1State &= ~UISTATE_HOT;
  676. m_uButton2State &= ~UISTATE_HOT;
  677. m_uThumbState &= ~UISTATE_HOT;
  678. Invalidate();
  679. }
  680. return;
  681. }
  682. if( m_pOwner != NULL ) m_pOwner->DoEvent(event); else CControlUI::DoEvent(event);
  683. }
  684. void CScrollBarUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
  685. {
  686. if( _tcsicmp(pstrName, _T("button1normalimage")) == 0 ) SetButton1NormalImage(pstrValue);
  687. else if( _tcsicmp(pstrName, _T("button1hotimage")) == 0 ) SetButton1HotImage(pstrValue);
  688. else if( _tcsicmp(pstrName, _T("button1pushedimage")) == 0 ) SetButton1PushedImage(pstrValue);
  689. else if( _tcsicmp(pstrName, _T("button1disabledimage")) == 0 ) SetButton1DisabledImage(pstrValue);
  690. else if( _tcsicmp(pstrName, _T("button2normalimage")) == 0 ) SetButton2NormalImage(pstrValue);
  691. else if( _tcsicmp(pstrName, _T("button2hotimage")) == 0 ) SetButton2HotImage(pstrValue);
  692. else if( _tcsicmp(pstrName, _T("button2pushedimage")) == 0 ) SetButton2PushedImage(pstrValue);
  693. else if( _tcsicmp(pstrName, _T("button2disabledimage")) == 0 ) SetButton2DisabledImage(pstrValue);
  694. else if( _tcsicmp(pstrName, _T("thumbnormalimage")) == 0 ) SetThumbNormalImage(pstrValue);
  695. else if( _tcsicmp(pstrName, _T("thumbhotimage")) == 0 ) SetThumbHotImage(pstrValue);
  696. else if( _tcsicmp(pstrName, _T("thumbpushedimage")) == 0 ) SetThumbPushedImage(pstrValue);
  697. else if( _tcsicmp(pstrName, _T("thumbdisabledimage")) == 0 ) SetThumbDisabledImage(pstrValue);
  698. else if( _tcsicmp(pstrName, _T("railnormalimage")) == 0 ) SetRailNormalImage(pstrValue);
  699. else if( _tcsicmp(pstrName, _T("railhotimage")) == 0 ) SetRailHotImage(pstrValue);
  700. else if( _tcsicmp(pstrName, _T("railpushedimage")) == 0 ) SetRailPushedImage(pstrValue);
  701. else if( _tcsicmp(pstrName, _T("raildisabledimage")) == 0 ) SetRailDisabledImage(pstrValue);
  702. else if( _tcsicmp(pstrName, _T("bknormalimage")) == 0 ) SetBkNormalImage(pstrValue);
  703. else if( _tcsicmp(pstrName, _T("bkhotimage")) == 0 ) SetBkHotImage(pstrValue);
  704. else if( _tcsicmp(pstrName, _T("bkpushedimage")) == 0 ) SetBkPushedImage(pstrValue);
  705. else if( _tcsicmp(pstrName, _T("bkdisabledimage")) == 0 ) SetBkDisabledImage(pstrValue);
  706. else if( _tcsicmp(pstrName, _T("hor")) == 0 ) SetHorizontal(_tcsicmp(pstrValue, _T("true")) == 0);
  707. else if( _tcsicmp(pstrName, _T("linesize")) == 0 ) SetLineSize(_ttoi(pstrValue));
  708. else if( _tcsicmp(pstrName, _T("range")) == 0 ) SetScrollRange(_ttoi(pstrValue));
  709. else if( _tcsicmp(pstrName, _T("value")) == 0 ) SetScrollPos(_ttoi(pstrValue));
  710. else if( _tcsicmp(pstrName, _T("showbutton1")) == 0 ) SetShowButton1(_tcsicmp(pstrValue, _T("true")) == 0);
  711. else if( _tcsicmp(pstrName, _T("showbutton2")) == 0 ) SetShowButton2(_tcsicmp(pstrValue, _T("true")) == 0);
  712. else CControlUI::SetAttribute(pstrName, pstrValue);
  713. }
  714. bool CScrollBarUI::DoPaint(HDC hDC, const RECT& rcPaint, CControlUI* pStopControl)
  715. {
  716. if(!GetShow()) return true;
  717. PaintBkColor(hDC);
  718. PaintBkImage(hDC);
  719. PaintBk(hDC);
  720. PaintButton1(hDC);
  721. PaintButton2(hDC);
  722. PaintThumb(hDC);
  723. PaintRail(hDC);
  724. PaintBorder(hDC);
  725. return true;
  726. }
  727. void CScrollBarUI::PaintBk(HDC hDC)
  728. {
  729. if( !IsEnabled() ) m_uThumbState |= UISTATE_DISABLED;
  730. else m_uThumbState &= ~ UISTATE_DISABLED;
  731. if( (m_uThumbState & UISTATE_DISABLED) != 0 ) {
  732. if( !m_sBkDisabledImage.IsEmpty() ) {
  733. if( !DrawImage(hDC, (LPCTSTR)m_sBkDisabledImage) ) {}
  734. else return;
  735. }
  736. }
  737. else if( (m_uThumbState & UISTATE_PUSHED) != 0 ) {
  738. if( !m_sBkPushedImage.IsEmpty() ) {
  739. if( !DrawImage(hDC, (LPCTSTR)m_sBkPushedImage) ) {}
  740. else return;
  741. }
  742. }
  743. else if( (m_uThumbState & UISTATE_HOT) != 0 ) {
  744. if( !m_sBkHotImage.IsEmpty() ) {
  745. if( !DrawImage(hDC, (LPCTSTR)m_sBkHotImage) ) {}
  746. else return;
  747. }
  748. }
  749. if( !m_sBkNormalImage.IsEmpty() ) {
  750. if( !DrawImage(hDC, (LPCTSTR)m_sBkNormalImage) ) {}
  751. else return;
  752. }
  753. }
  754. void CScrollBarUI::PaintButton1(HDC hDC)
  755. {
  756. if( !m_bShowButton1 ) return;
  757. if( !IsEnabled() ) m_uButton1State |= UISTATE_DISABLED;
  758. else m_uButton1State &= ~ UISTATE_DISABLED;
  759. int d1 = MulDiv(m_rcButton1.left - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  760. int d2 = MulDiv(m_rcButton1.top - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  761. int d3 = MulDiv(m_rcButton1.right - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  762. int d4 = MulDiv(m_rcButton1.bottom - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  763. m_sImageModify.Empty();
  764. m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), d1, d2, d3, d4);
  765. if( (m_uButton1State & UISTATE_DISABLED) != 0 ) {
  766. if( !m_sButton1DisabledImage.IsEmpty() ) {
  767. if( !DrawImage(hDC, (LPCTSTR)m_sButton1DisabledImage, (LPCTSTR)m_sImageModify) ) {}
  768. else return;
  769. }
  770. }
  771. else if( (m_uButton1State & UISTATE_PUSHED) != 0 ) {
  772. if( !m_sButton1PushedImage.IsEmpty() ) {
  773. if( !DrawImage(hDC, (LPCTSTR)m_sButton1PushedImage, (LPCTSTR)m_sImageModify) ) {}
  774. else return;
  775. }
  776. }
  777. else if( (m_uButton1State & UISTATE_HOT) != 0 ) {
  778. if( !m_sButton1HotImage.IsEmpty() ) {
  779. if( !DrawImage(hDC, (LPCTSTR)m_sButton1HotImage, (LPCTSTR)m_sImageModify) ) {}
  780. else return;
  781. }
  782. }
  783. if( !m_sButton1NormalImage.IsEmpty() ) {
  784. if( !DrawImage(hDC, (LPCTSTR)m_sButton1NormalImage, (LPCTSTR)m_sImageModify) ) {}
  785. else return;
  786. }
  787. DWORD dwBorderColor = 0xFF85E4FF;
  788. int nBorderSize = 2;
  789. CRenderEngine::DrawRect(hDC, m_rcButton1, nBorderSize, dwBorderColor);
  790. }
  791. void CScrollBarUI::PaintButton2(HDC hDC)
  792. {
  793. if( !m_bShowButton2 ) return;
  794. if( !IsEnabled() ) m_uButton2State |= UISTATE_DISABLED;
  795. else m_uButton2State &= ~ UISTATE_DISABLED;
  796. int d1 = MulDiv(m_rcButton2.left - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  797. int d2 = MulDiv(m_rcButton2.top - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  798. int d3 = MulDiv(m_rcButton2.right - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  799. int d4 = MulDiv(m_rcButton2.bottom - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  800. m_sImageModify.Empty();
  801. m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"),d1 ,d2 ,d3 ,d4 );
  802. if( (m_uButton2State & UISTATE_DISABLED) != 0 ) {
  803. if( !m_sButton2DisabledImage.IsEmpty() ) {
  804. if( !DrawImage(hDC, (LPCTSTR)m_sButton2DisabledImage, (LPCTSTR)m_sImageModify) ) {}
  805. else return;
  806. }
  807. }
  808. else if( (m_uButton2State & UISTATE_PUSHED) != 0 ) {
  809. if( !m_sButton2PushedImage.IsEmpty() ) {
  810. if( !DrawImage(hDC, (LPCTSTR)m_sButton2PushedImage, (LPCTSTR)m_sImageModify) ) {}
  811. else return;
  812. }
  813. }
  814. else if( (m_uButton2State & UISTATE_HOT) != 0 ) {
  815. if( !m_sButton2HotImage.IsEmpty() ) {
  816. if( !DrawImage(hDC, (LPCTSTR)m_sButton2HotImage, (LPCTSTR)m_sImageModify) ) {}
  817. else return;
  818. }
  819. }
  820. if( !m_sButton2NormalImage.IsEmpty() ) {
  821. if( !DrawImage(hDC, (LPCTSTR)m_sButton2NormalImage, (LPCTSTR)m_sImageModify) ) {}
  822. else return;
  823. }
  824. DWORD dwBorderColor = 0xFF85E4FF;
  825. int nBorderSize = 2;
  826. CRenderEngine::DrawRect(hDC, m_rcButton2, nBorderSize, dwBorderColor);
  827. }
  828. void CScrollBarUI::PaintThumb(HDC hDC)
  829. {
  830. if( m_rcThumb.left == 0 && m_rcThumb.top == 0 && m_rcThumb.right == 0 && m_rcThumb.bottom == 0 ) return;
  831. if( !IsEnabled() ) m_uThumbState |= UISTATE_DISABLED;
  832. else m_uThumbState &= ~ UISTATE_DISABLED;
  833. int d1 = MulDiv(m_rcThumb.left - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  834. int d2 = MulDiv(m_rcThumb.top - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  835. int d3 = MulDiv(m_rcThumb.right - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  836. int d4 = MulDiv(m_rcThumb.bottom - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  837. m_sImageModify.Empty();
  838. m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), d1, d2, d3, d4);
  839. if( (m_uThumbState & UISTATE_DISABLED) != 0 ) {
  840. if( !m_sThumbDisabledImage.IsEmpty() ) {
  841. if( !DrawImage(hDC, (LPCTSTR)m_sThumbDisabledImage, (LPCTSTR)m_sImageModify) ) {}
  842. else return;
  843. }
  844. }
  845. else if( (m_uThumbState & UISTATE_PUSHED) != 0 ) {
  846. if( !m_sThumbPushedImage.IsEmpty() ) {
  847. if( !DrawImage(hDC, (LPCTSTR)m_sThumbPushedImage, (LPCTSTR)m_sImageModify) ) {}
  848. else return;
  849. }
  850. }
  851. else if( (m_uThumbState & UISTATE_HOT) != 0 ) {
  852. if( !m_sThumbHotImage.IsEmpty() ) {
  853. if( !DrawImage(hDC, (LPCTSTR)m_sThumbHotImage, (LPCTSTR)m_sImageModify) ) {}
  854. else return;
  855. }
  856. }
  857. if( !m_sThumbNormalImage.IsEmpty() ) {
  858. if( !DrawImage(hDC, (LPCTSTR)m_sThumbNormalImage, (LPCTSTR)m_sImageModify) ) {}
  859. else return;
  860. }
  861. DWORD dwBorderColor = 0xFF85E4FF;
  862. int nBorderSize = 2;
  863. CRenderEngine::DrawRect(hDC, m_rcThumb, nBorderSize, dwBorderColor);
  864. }
  865. void CScrollBarUI::PaintRail(HDC hDC)
  866. {
  867. if( m_rcThumb.left == 0 && m_rcThumb.top == 0 && m_rcThumb.right == 0 && m_rcThumb.bottom == 0 ) return;
  868. if( !IsEnabled() ) m_uThumbState |= UISTATE_DISABLED;
  869. else m_uThumbState &= ~ UISTATE_DISABLED;
  870. m_sImageModify.Empty();
  871. if( !m_bHorizontal ) {
  872. int d1 = MulDiv(m_rcThumb.left - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  873. int d2 = MulDiv((m_rcThumb.top + m_rcThumb.bottom) / 2 - m_rcItem.top - m_cxyFixed.cx / 2, 100, GetManager()->GetDPIObj()->GetScale());
  874. int d3 = MulDiv(m_rcThumb.right - m_rcItem.left, 100, GetManager()->GetDPIObj()->GetScale());
  875. int d4 = MulDiv((m_rcThumb.top + m_rcThumb.bottom) / 2 - m_rcItem.top + m_cxyFixed.cx - m_cxyFixed.cx / 2, 100, GetManager()->GetDPIObj()->GetScale());
  876. m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), d1, d2, d3,d4);
  877. }
  878. else {
  879. int d1 = MulDiv((m_rcThumb.left + m_rcThumb.right) / 2 - m_rcItem.left - m_cxyFixed.cy / 2, 100, GetManager()->GetDPIObj()->GetScale());
  880. int d2 = MulDiv(m_rcThumb.top - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  881. int d3 = MulDiv((m_rcThumb.left + m_rcThumb.right) / 2 - m_rcItem.left + m_cxyFixed.cy - m_cxyFixed.cy / 2, 100, GetManager()->GetDPIObj()->GetScale());
  882. int d4 = MulDiv(m_rcThumb.bottom - m_rcItem.top, 100, GetManager()->GetDPIObj()->GetScale());
  883. m_sImageModify.SmallFormat(_T("dest='%d,%d,%d,%d'"), d1,d2, d3, d4);
  884. }
  885. if( (m_uThumbState & UISTATE_DISABLED) != 0 ) {
  886. if( !m_sRailDisabledImage.IsEmpty() ) {
  887. if( !DrawImage(hDC, (LPCTSTR)m_sRailDisabledImage, (LPCTSTR)m_sImageModify) ) {}
  888. else return;
  889. }
  890. }
  891. else if( (m_uThumbState & UISTATE_PUSHED) != 0 ) {
  892. if( !m_sRailPushedImage.IsEmpty() ) {
  893. if( !DrawImage(hDC, (LPCTSTR)m_sRailPushedImage, (LPCTSTR)m_sImageModify) ) {}
  894. else return;
  895. }
  896. }
  897. else if( (m_uThumbState & UISTATE_HOT) != 0 ) {
  898. if( !m_sRailHotImage.IsEmpty() ) {
  899. if( !DrawImage(hDC, (LPCTSTR)m_sRailHotImage, (LPCTSTR)m_sImageModify) ) {}
  900. else return;
  901. }
  902. }
  903. if( !m_sRailNormalImage.IsEmpty() ) {
  904. if( !DrawImage(hDC, (LPCTSTR)m_sRailNormalImage, (LPCTSTR)m_sImageModify) ) {}
  905. else return;
  906. }
  907. }
  908. }