barstat.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_CORE3_SEG
  12. #pragma code_seg(AFX_CORE3_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #define new DEBUG_NEW
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CStatusBar creation, etc
  21. #define SBPF_UPDATE 0x0001 // pending update of text
  22. struct AFX_STATUSPANE
  23. {
  24. UINT nID; // IDC of indicator: 0 => normal text area
  25. int cxText; // width of string area in pixels
  26. // on both sides there is a 3 pixel gap and
  27. // a one pixel border, making a pane 6 pixels wider
  28. UINT nStyle; // style flags (SBPS_*)
  29. UINT nFlags; // state flags (SBPF_*)
  30. CString strText; // text in the pane
  31. };
  32. inline AFX_STATUSPANE* CStatusBar::_GetPanePtr(int nIndex) const
  33. {
  34. ASSERT((nIndex >= 0 && nIndex < m_nCount) || m_nCount == 0);
  35. return ((AFX_STATUSPANE*)m_pData) + nIndex;
  36. }
  37. #ifdef AFX_INIT_SEG
  38. #pragma code_seg(AFX_INIT_SEG)
  39. #endif
  40. #define CX_PANE_BORDER 6 // 3 pixels on each side of each pane
  41. CStatusBar::CStatusBar()
  42. {
  43. // setup default border/margin depending on type of system
  44. m_cyTopBorder = 2;
  45. if (afxData.bWin4)
  46. {
  47. m_cxLeftBorder = 0;
  48. m_cxRightBorder = 0;
  49. m_cyBottomBorder = 0;
  50. }
  51. else
  52. {
  53. m_cxLeftBorder = 2;
  54. m_cxRightBorder = 2;
  55. m_cyBottomBorder = 1;
  56. }
  57. // minimum height set with SB_SETMINHEIGHT is cached
  58. m_nMinHeight = 0;
  59. }
  60. CStatusBar::~CStatusBar()
  61. {
  62. AllocElements(0, 0); // destroys existing elements
  63. }
  64. BOOL CStatusBar::Create(CWnd* pParentWnd, DWORD dwStyle, UINT nID)
  65. {
  66. return CreateEx(pParentWnd, 0, dwStyle, nID);
  67. }
  68. BOOL CStatusBar::CreateEx(CWnd* pParentWnd, DWORD dwCtrlStyle, DWORD dwStyle, UINT nID)
  69. {
  70. ASSERT_VALID(pParentWnd); // must have a parent
  71. // save the style (some of these style bits are MFC specific)
  72. m_dwStyle = (dwStyle & CBRS_ALL);
  73. // translate MFC style bits to windows style bits
  74. dwStyle &= ~CBRS_ALL;
  75. dwStyle |= CCS_NOPARENTALIGN|CCS_NOMOVEY|CCS_NODIVIDER|CCS_NORESIZE;
  76. if (pParentWnd->GetStyle() & WS_THICKFRAME)
  77. dwStyle |= SBARS_SIZEGRIP;
  78. dwStyle |= dwCtrlStyle;
  79. // initialize common controls
  80. VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTL_BAR_REG));
  81. // create the HWND
  82. CRect rect; rect.SetRectEmpty();
  83. return CWnd::Create(STATUSCLASSNAME, NULL, dwStyle, rect, pParentWnd, nID);
  84. }
  85. BOOL CStatusBar::PreCreateWindow(CREATESTRUCT& cs)
  86. {
  87. // in Win4, status bars do not have a border at all, since it is
  88. // provided by the client area.
  89. if (afxData.bWin4 &&
  90. (m_dwStyle & (CBRS_ALIGN_ANY|CBRS_BORDER_ANY)) == CBRS_BOTTOM)
  91. {
  92. m_dwStyle &= ~(CBRS_BORDER_ANY|CBRS_BORDER_3D);
  93. }
  94. return CControlBar::PreCreateWindow(cs);
  95. }
  96. BOOL CStatusBar::SetIndicators(const UINT* lpIDArray, int nIDCount)
  97. {
  98. ASSERT_VALID(this);
  99. ASSERT(nIDCount >= 1); // must be at least one of them
  100. ASSERT(lpIDArray == NULL ||
  101. AfxIsValidAddress(lpIDArray, sizeof(UINT) * nIDCount, FALSE));
  102. ASSERT(::IsWindow(m_hWnd));
  103. // first allocate array for panes and copy initial data
  104. if (!AllocElements(nIDCount, sizeof(AFX_STATUSPANE)))
  105. return FALSE;
  106. ASSERT(nIDCount == m_nCount);
  107. // copy initial data from indicator array
  108. BOOL bResult = TRUE;
  109. if (lpIDArray != NULL)
  110. {
  111. HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
  112. CClientDC dcScreen(NULL);
  113. HGDIOBJ hOldFont = NULL;
  114. if (hFont != NULL)
  115. hOldFont = dcScreen.SelectObject(hFont);
  116. AFX_STATUSPANE* pSBP = _GetPanePtr(0);
  117. for (int i = 0; i < nIDCount; i++)
  118. {
  119. pSBP->nID = *lpIDArray++;
  120. pSBP->nFlags |= SBPF_UPDATE;
  121. if (pSBP->nID != 0)
  122. {
  123. if (!pSBP->strText.LoadString(pSBP->nID))
  124. {
  125. TRACE1("Warning: failed to load indicator string 0x%04X.\n",
  126. pSBP->nID);
  127. bResult = FALSE;
  128. break;
  129. }
  130. pSBP->cxText = dcScreen.GetTextExtent(pSBP->strText).cx;
  131. ASSERT(pSBP->cxText >= 0);
  132. if (!SetPaneText(i, pSBP->strText, FALSE))
  133. {
  134. bResult = FALSE;
  135. break;
  136. }
  137. }
  138. else
  139. {
  140. // no indicator (must access via index)
  141. // default to 1/4 the screen width (first pane is stretchy)
  142. pSBP->cxText = ::GetSystemMetrics(SM_CXSCREEN)/4;
  143. if (i == 0)
  144. pSBP->nStyle |= (SBPS_STRETCH | SBPS_NOBORDERS);
  145. }
  146. ++pSBP;
  147. }
  148. if (hOldFont != NULL)
  149. dcScreen.SelectObject(hOldFont);
  150. }
  151. UpdateAllPanes(TRUE, TRUE);
  152. return bResult;
  153. }
  154. BOOL CStatusBar::AllocElements(int nElements, int cbElement)
  155. {
  156. // destruct old elements
  157. AFX_STATUSPANE* pSBP = _GetPanePtr(0);
  158. for (int i = 0; i < m_nCount; i++)
  159. {
  160. pSBP->strText.~CString();
  161. ++pSBP;
  162. }
  163. // allocate new elements
  164. if (!CControlBar::AllocElements(nElements, cbElement))
  165. return FALSE;
  166. // construct new elements
  167. pSBP = _GetPanePtr(0);
  168. for (i = 0; i < m_nCount; i++)
  169. {
  170. memcpy(&pSBP->strText, &afxEmptyString, sizeof(CString));
  171. ++pSBP;
  172. }
  173. return TRUE;
  174. }
  175. void CStatusBar::CalcInsideRect(CRect& rect, BOOL bHorz) const
  176. {
  177. ASSERT_VALID(this);
  178. ASSERT(::IsWindow(m_hWnd));
  179. ASSERT(bHorz); // vertical status bar not supported
  180. // subtract standard CControlBar borders
  181. CControlBar::CalcInsideRect(rect, bHorz);
  182. // subtract size grip if present
  183. if ((GetStyle() & SBARS_SIZEGRIP) && !::IsZoomed(::GetParent(m_hWnd)))
  184. {
  185. // get border metrics from common control
  186. int rgBorders[3];
  187. CStatusBar* pBar = (CStatusBar*)this;
  188. pBar->DefWindowProc(SB_GETBORDERS, 0, (LPARAM)&rgBorders);
  189. // size grip uses a border + size of scrollbar + cx border
  190. rect.right -= rgBorders[0] + ::GetSystemMetrics(SM_CXVSCROLL) +
  191. ::GetSystemMetrics(SM_CXBORDER) * 2;
  192. }
  193. }
  194. void CStatusBar::UpdateAllPanes(BOOL bUpdateRects, BOOL bUpdateText)
  195. {
  196. ASSERT_VALID(this);
  197. ASSERT(::IsWindow(m_hWnd));
  198. // update the status pane locations
  199. if (bUpdateRects)
  200. {
  201. // get border information and client work area
  202. CRect rect; GetWindowRect(rect);
  203. rect.OffsetRect(-rect.left, -rect.top);
  204. CalcInsideRect(rect, TRUE);
  205. int rgBorders[3];
  206. VERIFY((BOOL)DefWindowProc(SB_GETBORDERS, 0, (LPARAM)&rgBorders));
  207. // determine extra space for stretchy pane
  208. int cxExtra = rect.Width() + rgBorders[2];
  209. int nStretchyCount = 0;
  210. AFX_STATUSPANE* pSBP = _GetPanePtr(0);
  211. for (int i = 0; i < m_nCount; i++)
  212. {
  213. if (pSBP->nStyle & SBPS_STRETCH)
  214. ++nStretchyCount;
  215. cxExtra -= (pSBP->cxText+CX_PANE_BORDER + rgBorders[2]);
  216. ++pSBP;
  217. }
  218. // determine right edge of each pane
  219. int* rgRights = (int*)_alloca(m_nCount * sizeof(int));
  220. int right = rgBorders[0];
  221. pSBP = _GetPanePtr(0);
  222. for (i = 0; i < m_nCount; i++)
  223. {
  224. // determine size of the pane
  225. ASSERT(pSBP->cxText >= 0);
  226. right += pSBP->cxText+CX_PANE_BORDER;
  227. if ((pSBP->nStyle & SBPS_STRETCH) && cxExtra > 0)
  228. {
  229. ASSERT(nStretchyCount != 0);
  230. int cxAddExtra = cxExtra / nStretchyCount;
  231. right += cxAddExtra;
  232. --nStretchyCount;
  233. cxExtra -= cxAddExtra;
  234. }
  235. rgRights[i] = right;
  236. // next pane
  237. ++pSBP;
  238. right += rgBorders[2];
  239. }
  240. // set new right edges for all panes
  241. DefWindowProc(SB_SETPARTS, m_nCount, (LPARAM)rgRights);
  242. }
  243. // update text in the status panes if specified
  244. if (bUpdateText)
  245. {
  246. AFX_STATUSPANE* pSBP = _GetPanePtr(0);
  247. for (int i = 0; i < m_nCount; i++)
  248. {
  249. if (pSBP->nFlags & SBPF_UPDATE)
  250. SetPaneText(i, pSBP->strText);
  251. ++pSBP;
  252. }
  253. }
  254. }
  255. #ifdef AFX_CORE3_SEG
  256. #pragma code_seg(AFX_CORE3_SEG)
  257. #endif
  258. /////////////////////////////////////////////////////////////////////////////
  259. // CStatusBar attribute access
  260. int CStatusBar::CommandToIndex(UINT nIDFind) const
  261. {
  262. ASSERT_VALID(this);
  263. if (m_nCount <= 0)
  264. return -1;
  265. AFX_STATUSPANE* pSBP = _GetPanePtr(0);
  266. for (int i = 0; i < m_nCount; i++, pSBP++)
  267. if (pSBP->nID == nIDFind)
  268. return i;
  269. return -1;
  270. }
  271. UINT CStatusBar::GetItemID(int nIndex) const
  272. {
  273. ASSERT_VALID(this);
  274. return _GetPanePtr(nIndex)->nID;
  275. }
  276. void CStatusBar::GetItemRect(int nIndex, LPRECT lpRect) const
  277. {
  278. ASSERT_VALID(this);
  279. ASSERT(::IsWindow(m_hWnd));
  280. CStatusBar* pBar = (CStatusBar*)this;
  281. if (!pBar->DefWindowProc(SB_GETRECT, nIndex, (LPARAM)lpRect))
  282. ::SetRectEmpty(lpRect);
  283. }
  284. UINT CStatusBar::GetPaneStyle(int nIndex) const
  285. {
  286. return _GetPanePtr(nIndex)->nStyle;
  287. }
  288. void CStatusBar::SetPaneStyle(int nIndex, UINT nStyle)
  289. {
  290. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  291. if (pSBP->nStyle != nStyle)
  292. {
  293. // if the pane is changing SBPS_STRETCH, then...
  294. if ((pSBP->nStyle ^ nStyle) & SBPS_STRETCH)
  295. {
  296. // ... we need to re-layout the panes
  297. pSBP->nStyle = nStyle;
  298. UpdateAllPanes(TRUE, FALSE);
  299. }
  300. // use SetPaneText, since it updates the style and text
  301. pSBP->nStyle = nStyle;
  302. pSBP->nFlags |= SBPF_UPDATE;
  303. SetPaneText(nIndex, pSBP->strText);
  304. }
  305. }
  306. void CStatusBar::GetPaneInfo(int nIndex, UINT& nID, UINT& nStyle,
  307. int& cxWidth) const
  308. {
  309. ASSERT_VALID(this);
  310. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  311. nID = pSBP->nID;
  312. nStyle = pSBP->nStyle;
  313. cxWidth = pSBP->cxText;
  314. }
  315. void CStatusBar::SetPaneInfo(int nIndex, UINT nID, UINT nStyle, int cxWidth)
  316. {
  317. ASSERT_VALID(this);
  318. BOOL bChanged = FALSE;
  319. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  320. pSBP->nID = nID;
  321. if (pSBP->nStyle != nStyle)
  322. {
  323. if ((pSBP->nStyle ^ nStyle) & SBPS_STRETCH)
  324. bChanged = TRUE;
  325. else
  326. {
  327. pSBP->nStyle = nStyle;
  328. pSBP->nFlags |= SBPF_UPDATE;
  329. SetPaneText(nIndex, pSBP->strText);
  330. }
  331. pSBP->nStyle = nStyle;
  332. }
  333. if (cxWidth != pSBP->cxText)
  334. {
  335. // change width of one pane -> invalidate the entire status bar
  336. pSBP->cxText = cxWidth;
  337. bChanged = TRUE;
  338. }
  339. if (bChanged)
  340. UpdateAllPanes(TRUE, FALSE);
  341. }
  342. void CStatusBar::GetPaneText(int nIndex, CString& s) const
  343. {
  344. ASSERT_VALID(this);
  345. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  346. s = pSBP->strText;
  347. }
  348. CString CStatusBar::GetPaneText(int nIndex) const
  349. {
  350. ASSERT_VALID(this);
  351. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  352. return pSBP->strText;
  353. }
  354. BOOL CStatusBar::SetPaneText(int nIndex, LPCTSTR lpszNewText, BOOL bUpdate)
  355. {
  356. ASSERT_VALID(this);
  357. ASSERT(::IsWindow(m_hWnd));
  358. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  359. if (!(pSBP->nFlags & SBPF_UPDATE) &&
  360. ((lpszNewText == NULL && pSBP->strText.IsEmpty()) ||
  361. (lpszNewText != NULL && pSBP->strText.Compare(lpszNewText) == 0)))
  362. {
  363. // nothing to change
  364. return TRUE;
  365. }
  366. TRY
  367. {
  368. if (lpszNewText != NULL)
  369. pSBP->strText = lpszNewText;
  370. else
  371. pSBP->strText.Empty();
  372. }
  373. CATCH_ALL(e)
  374. {
  375. // Note: DELETE_EXCEPTION(e) not required
  376. return FALSE;
  377. }
  378. END_CATCH_ALL
  379. if (!bUpdate)
  380. {
  381. // can't update now, wait until later
  382. pSBP->nFlags |= SBPF_UPDATE;
  383. return TRUE;
  384. }
  385. pSBP->nFlags &= ~SBPF_UPDATE;
  386. DefWindowProc(SB_SETTEXT, ((WORD)pSBP->nStyle)|nIndex,
  387. (pSBP->nStyle & SBPS_DISABLED) ? NULL :
  388. (LPARAM)(LPCTSTR)pSBP->strText);
  389. return TRUE;
  390. }
  391. /////////////////////////////////////////////////////////////////////////////
  392. // CStatusBar implementation
  393. CSize CStatusBar::CalcFixedLayout(BOOL, BOOL bHorz)
  394. {
  395. ASSERT_VALID(this);
  396. ASSERT(::IsWindow(m_hWnd));
  397. // determinme size of font being used by the status bar
  398. TEXTMETRIC tm;
  399. {
  400. CClientDC dc(NULL);
  401. HFONT hFont = (HFONT)SendMessage(WM_GETFONT);
  402. HGDIOBJ hOldFont = NULL;
  403. if (hFont != NULL)
  404. hOldFont = dc.SelectObject(hFont);
  405. VERIFY(dc.GetTextMetrics(&tm));
  406. if (hOldFont != NULL)
  407. dc.SelectObject(hOldFont);
  408. }
  409. // get border information
  410. CRect rect; rect.SetRectEmpty();
  411. CalcInsideRect(rect, bHorz);
  412. int rgBorders[3];
  413. DefWindowProc(SB_GETBORDERS, 0, (LPARAM)&rgBorders);
  414. // determine size, including borders
  415. CSize size;
  416. size.cx = 32767;
  417. size.cy = tm.tmHeight - tm.tmInternalLeading - 1
  418. + rgBorders[1] * 2 + ::GetSystemMetrics(SM_CYBORDER) * 2
  419. - rect.Height();
  420. if (size.cy < m_nMinHeight)
  421. size.cy = m_nMinHeight;
  422. return size;
  423. }
  424. /////////////////////////////////////////////////////////////////////////////
  425. // CStatusBar message handlers
  426. BEGIN_MESSAGE_MAP(CStatusBar, CControlBar)
  427. //{{AFX_MSG_MAP(CStatusBar)
  428. ON_WM_NCHITTEST()
  429. ON_WM_NCPAINT()
  430. ON_WM_PAINT()
  431. ON_WM_NCCALCSIZE()
  432. ON_WM_SIZE()
  433. ON_WM_WINDOWPOSCHANGING()
  434. ON_MESSAGE(WM_SETTEXT, OnSetText)
  435. ON_MESSAGE(WM_GETTEXT, OnGetText)
  436. ON_MESSAGE(WM_GETTEXTLENGTH, OnGetTextLength)
  437. ON_MESSAGE(SB_SETMINHEIGHT, OnSetMinHeight)
  438. //}}AFX_MSG_MAP
  439. END_MESSAGE_MAP()
  440. UINT CStatusBar::OnNcHitTest(CPoint)
  441. {
  442. UINT nResult = (UINT)Default();
  443. if (nResult == HTBOTTOMRIGHT)
  444. return HTBOTTOMRIGHT;
  445. else
  446. return HTCLIENT;
  447. }
  448. void CStatusBar::OnNcCalcSize(BOOL /*bCalcValidRects*/, NCCALCSIZE_PARAMS* lpncsp)
  449. {
  450. // calculate border space (will add to top/bottom, subtract from right/bottom)
  451. CRect rect; rect.SetRectEmpty();
  452. CControlBar::CalcInsideRect(rect, TRUE);
  453. ASSERT(rect.top >= 2);
  454. // adjust non-client area for border space
  455. lpncsp->rgrc[0].left += rect.left;
  456. lpncsp->rgrc[0].top += rect.top - 2;
  457. lpncsp->rgrc[0].right += rect.right;
  458. lpncsp->rgrc[0].bottom += rect.bottom;
  459. }
  460. void CStatusBar::OnBarStyleChange(DWORD dwOldStyle, DWORD dwNewStyle)
  461. {
  462. if (m_hWnd != NULL &&
  463. ((dwOldStyle & CBRS_BORDER_ANY) != (dwNewStyle & CBRS_BORDER_ANY)))
  464. {
  465. // recalc non-client area when border styles change
  466. SetWindowPos(NULL, 0, 0, 0, 0,
  467. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_DRAWFRAME);
  468. }
  469. }
  470. void CStatusBar::OnNcPaint()
  471. {
  472. EraseNonClient();
  473. }
  474. // Derived class is responsible for implementing all of these handlers
  475. // for owner/self draw controls.
  476. void CStatusBar::DrawItem(LPDRAWITEMSTRUCT)
  477. {
  478. ASSERT(FALSE);
  479. }
  480. BOOL CStatusBar::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  481. {
  482. if (message != WM_DRAWITEM)
  483. return CWnd::OnChildNotify(message, wParam, lParam, pResult);
  484. ASSERT(pResult == NULL);
  485. UNUSED(pResult); // unused in release builds
  486. DrawItem((LPDRAWITEMSTRUCT)lParam);
  487. return TRUE;
  488. }
  489. void CStatusBar::OnPaint()
  490. {
  491. UpdateAllPanes(FALSE, TRUE);
  492. Default();
  493. }
  494. void CStatusBar::OnSize(UINT nType, int cx, int cy)
  495. {
  496. ASSERT_VALID(this);
  497. ASSERT(::IsWindow(m_hWnd));
  498. CControlBar::OnSize(nType, cx, cy);
  499. // need to adjust pane right edges (because of stretchy pane)
  500. UpdateAllPanes(TRUE, FALSE);
  501. }
  502. void CStatusBar::OnWindowPosChanging(LPWINDOWPOS lpWndPos)
  503. {
  504. // not necessary to invalidate the borders
  505. DWORD dwStyle = m_dwStyle;
  506. m_dwStyle &= ~(CBRS_BORDER_ANY);
  507. CControlBar::OnWindowPosChanging(lpWndPos);
  508. m_dwStyle = dwStyle;
  509. }
  510. LRESULT CStatusBar::OnSetText(WPARAM, LPARAM lParam)
  511. {
  512. ASSERT_VALID(this);
  513. ASSERT(::IsWindow(m_hWnd));
  514. int nIndex = CommandToIndex(0);
  515. if (nIndex < 0)
  516. return -1;
  517. return SetPaneText(nIndex, (LPCTSTR)lParam) ? 0 : -1;
  518. }
  519. LRESULT CStatusBar::OnGetText(WPARAM wParam, LPARAM lParam)
  520. {
  521. ASSERT_VALID(this);
  522. ASSERT(::IsWindow(m_hWnd));
  523. int nMaxLen = (int)wParam;
  524. if (nMaxLen == 0)
  525. return 0; // nothing copied
  526. LPTSTR lpszDest = (LPTSTR)lParam;
  527. int nLen = 0;
  528. int nIndex = CommandToIndex(0); // use pane with ID zero
  529. if (nIndex >= 0)
  530. {
  531. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  532. nLen = pSBP->strText.GetLength();
  533. if (nLen > nMaxLen)
  534. nLen = nMaxLen - 1; // number of characters to copy (less term.)
  535. memcpy(lpszDest, (LPCTSTR)pSBP->strText, nLen*sizeof(TCHAR));
  536. }
  537. lpszDest[nLen] = '\0';
  538. return nLen+1; // number of bytes copied
  539. }
  540. LRESULT CStatusBar::OnGetTextLength(WPARAM, LPARAM)
  541. {
  542. ASSERT_VALID(this);
  543. ASSERT(::IsWindow(m_hWnd));
  544. int nLen = 0;
  545. int nIndex = CommandToIndex(0); // use pane with ID zero
  546. if (nIndex >= 0)
  547. {
  548. AFX_STATUSPANE* pSBP = _GetPanePtr(nIndex);
  549. nLen = pSBP->strText.GetLength();
  550. }
  551. return nLen;
  552. }
  553. LRESULT CStatusBar::OnSetMinHeight(WPARAM wParam, LPARAM)
  554. {
  555. LRESULT lResult = Default();
  556. m_nMinHeight = wParam;
  557. return lResult;
  558. }
  559. /////////////////////////////////////////////////////////////////////////////
  560. // CStatusBar idle update through CStatusCmdUI class
  561. class CStatusCmdUI : public CCmdUI // class private to this file!
  562. {
  563. public: // re-implementations only
  564. virtual void Enable(BOOL bOn);
  565. virtual void SetCheck(int nCheck);
  566. virtual void SetText(LPCTSTR lpszText);
  567. };
  568. void CStatusCmdUI::Enable(BOOL bOn)
  569. {
  570. m_bEnableChanged = TRUE;
  571. CStatusBar* pStatusBar = (CStatusBar*)m_pOther;
  572. ASSERT(pStatusBar != NULL);
  573. ASSERT_KINDOF(CStatusBar, pStatusBar);
  574. ASSERT(m_nIndex < m_nIndexMax);
  575. UINT nNewStyle = pStatusBar->GetPaneStyle(m_nIndex) & ~SBPS_DISABLED;
  576. if (!bOn)
  577. nNewStyle |= SBPS_DISABLED;
  578. pStatusBar->SetPaneStyle(m_nIndex, nNewStyle);
  579. }
  580. void CStatusCmdUI::SetCheck(int nCheck) // "checking" will pop out the text
  581. {
  582. CStatusBar* pStatusBar = (CStatusBar*)m_pOther;
  583. ASSERT(pStatusBar != NULL);
  584. ASSERT_KINDOF(CStatusBar, pStatusBar);
  585. ASSERT(m_nIndex < m_nIndexMax);
  586. UINT nNewStyle = pStatusBar->GetPaneStyle(m_nIndex) & ~SBPS_POPOUT;
  587. if (nCheck != 0)
  588. nNewStyle |= SBPS_POPOUT;
  589. pStatusBar->SetPaneStyle(m_nIndex, nNewStyle);
  590. }
  591. void CStatusCmdUI::SetText(LPCTSTR lpszText)
  592. {
  593. CStatusBar* pStatusBar = (CStatusBar*)m_pOther;
  594. ASSERT(pStatusBar != NULL);
  595. ASSERT_KINDOF(CStatusBar, pStatusBar);
  596. ASSERT(m_nIndex < m_nIndexMax);
  597. pStatusBar->SetPaneText(m_nIndex, lpszText);
  598. }
  599. void CStatusBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
  600. {
  601. CStatusCmdUI state;
  602. state.m_pOther = this;
  603. state.m_nIndexMax = (UINT)m_nCount;
  604. for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
  605. state.m_nIndex++)
  606. {
  607. state.m_nID = _GetPanePtr(state.m_nIndex)->nID;
  608. // allow the statusbar itself to have update handlers
  609. if (CWnd::OnCmdMsg(state.m_nID, CN_UPDATE_COMMAND_UI, &state, NULL))
  610. continue;
  611. // allow target (owner) to handle the remaining updates
  612. state.DoUpdate(pTarget, FALSE);
  613. }
  614. // update the dialog controls added to the status bar
  615. UpdateDialogControls(pTarget, bDisableIfNoHndler);
  616. }
  617. /////////////////////////////////////////////////////////////////////////////
  618. // CStatusBar diagnostics
  619. #ifdef _DEBUG
  620. void CStatusBar::AssertValid() const
  621. {
  622. CControlBar::AssertValid();
  623. }
  624. void CStatusBar::Dump(CDumpContext& dc) const
  625. {
  626. CControlBar::Dump(dc);
  627. if (dc.GetDepth() > 0)
  628. {
  629. for (int i = 0; i < m_nCount; i++)
  630. {
  631. dc << "\nstatus pane[" << i << "] = {";
  632. dc << "\n\tnID = " << _GetPanePtr(i)->nID;
  633. dc << "\n\tnStyle = " << _GetPanePtr(i)->nStyle;
  634. dc << "\n\tcxText = " << _GetPanePtr(i)->cxText;
  635. dc << "\n\tstrText = " << _GetPanePtr(i)->strText;
  636. dc << "\n\t}";
  637. }
  638. }
  639. dc << "\n";
  640. }
  641. #endif //_DEBUG
  642. #ifdef AFX_INIT_SEG
  643. #pragma code_seg(AFX_INIT_SEG)
  644. #endif
  645. IMPLEMENT_DYNAMIC(CStatusBar, CControlBar)
  646. /////////////////////////////////////////////////////////////////////////////