Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

619 lines
18 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999.
  5. //
  6. // File: brctrl.cxx
  7. //
  8. // Contents:
  9. //
  10. // History: 15 Aug 1996 DLee Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #include <pch.cxx>
  14. #pragma hdrstop
  15. #define TheView (*_pView)
  16. #define TheModel (*_pModel)
  17. // Member functions responding to messages
  18. LRESULT WINAPI BrowseWndProc(
  19. HWND hwnd,
  20. UINT msg,
  21. WPARAM wParam,
  22. LPARAM lParam)
  23. {
  24. LRESULT lr = 0;
  25. CBrowseWindow *pbw = (CBrowseWindow *) GetWindowLongPtr( hwnd, 0 );
  26. switch (msg)
  27. {
  28. case WM_CREATE:
  29. {
  30. App.BrowseLastError() = S_OK;
  31. CREATESTRUCT *pcs = (CREATESTRUCT *) lParam;
  32. MDICREATESTRUCT *pmcs = (MDICREATESTRUCT *) pcs->lpCreateParams;
  33. CQueryResult *pResults = (CQueryResult *) pmcs->lParam;
  34. pbw = new CBrowseWindow();
  35. SetWindowLongPtr( hwnd, 0, (LONG_PTR) pbw);
  36. lr = pbw->TheBrowseController.Create( pResults,
  37. hwnd,
  38. &pbw->TheBrowseModel,
  39. &pbw->TheBrowseView,
  40. App.BrowseFont() );
  41. break;
  42. }
  43. case WM_MDIACTIVATE :
  44. pbw->TheBrowseController.Activate( hwnd, lParam );
  45. break;
  46. case WM_SIZE:
  47. lr = DefMDIChildProc( hwnd, msg, wParam, lParam );
  48. pbw->TheBrowseController.Size( hwnd, lParam );
  49. break;
  50. case WM_VSCROLL:
  51. pbw->TheBrowseController.VScroll(hwnd, wParam, lParam);
  52. break;
  53. case WM_HSCROLL:
  54. pbw->TheBrowseController.HScroll(hwnd, wParam, lParam);
  55. break;
  56. case WM_KEYDOWN:
  57. pbw->TheBrowseController.KeyDown (hwnd, wParam );
  58. lr = DefMDIChildProc(hwnd,msg,wParam,lParam);
  59. break;
  60. case WM_CHAR:
  61. pbw->TheBrowseController.Char (hwnd, wParam);
  62. lr = DefMDIChildProc(hwnd,msg,wParam,lParam);
  63. break;
  64. case WM_PAINT:
  65. pbw->TheBrowseController.Paint(hwnd);
  66. break;
  67. case wmMenuCommand:
  68. pbw->TheBrowseController.Command(hwnd, wParam);
  69. break;
  70. case wmInitMenu:
  71. pbw->TheBrowseController.EnableMenus();
  72. break;
  73. case WM_DESTROY:
  74. SetWindowLongPtr(hwnd,0,0);
  75. delete pbw;
  76. SetProcessWorkingSetSize( GetCurrentProcess(), -1, -1 );
  77. break;
  78. case wmNewFont:
  79. pbw->TheBrowseController.NewFont(hwnd, wParam);
  80. break;
  81. case WM_LBUTTONUP:
  82. pbw->TheBrowseView.ButtonUp( wParam, lParam );
  83. break;
  84. case WM_LBUTTONDOWN:
  85. pbw->TheBrowseView.ButtonDown( wParam, lParam );
  86. break;
  87. case WM_LBUTTONDBLCLK:
  88. pbw->TheBrowseView.DblClk( wParam, lParam );
  89. break;
  90. case WM_MOUSEMOVE :
  91. pbw->TheBrowseView.MouseMove( wParam, lParam );
  92. break;
  93. case WM_MOUSEWHEEL :
  94. lr = pbw->TheBrowseController.MouseWheel( hwnd, wParam, lParam );
  95. break;
  96. case WM_CONTEXTMENU :
  97. pbw->TheBrowseController.ContextMenu( hwnd, wParam, lParam );
  98. break;
  99. case EM_GETSEL:
  100. {
  101. if ( pbw->TheBrowseView.GetSelection().SelectionExists() )
  102. lr = MAKELRESULT( 1, 2 );
  103. else
  104. lr = 0;
  105. break;
  106. }
  107. default:
  108. lr = DefMDIChildProc(hwnd,msg,wParam,lParam);
  109. break;
  110. }
  111. return lr;
  112. } //BrowseWndProc
  113. void Control::ContextMenu(
  114. HWND hwnd,
  115. WPARAM wParam,
  116. LPARAM lParam )
  117. {
  118. POINT pt;
  119. pt.x = LOWORD( lParam );
  120. pt.y = HIWORD( lParam );
  121. GetCursorPos( &pt );
  122. HMENU hMenu = LoadMenu( App.Instance(), L"BrowseContextMenu" );
  123. if ( 0 != hMenu )
  124. {
  125. HMENU hTrackMenu = GetSubMenu( hMenu, 0 );
  126. if ( 0 != hTrackMenu )
  127. {
  128. if ( !TheView.GetSelection().SelectionExists() )
  129. EnableMenuItem( hTrackMenu,
  130. IDM_EDITCOPY,
  131. MF_BYCOMMAND | MF_GRAYED );
  132. // yes, the function returns a BOOL that you switch on
  133. BOOL b = TrackPopupMenuEx( hTrackMenu,
  134. TPM_LEFTALIGN | TPM_RIGHTBUTTON |
  135. TPM_RETURNCMD,
  136. pt.x,
  137. pt.y,
  138. hwnd,
  139. 0 );
  140. switch ( b )
  141. {
  142. case IDM_BROWSE_OPEN :
  143. {
  144. ViewFile( TheModel.Filename(),
  145. fileOpen );
  146. break;
  147. }
  148. case IDM_BROWSE_EDIT :
  149. {
  150. POINT winpt;
  151. winpt.x = 0;
  152. winpt.y = 0;
  153. ClientToScreen( hwnd, &winpt );
  154. ViewFile( TheModel.Filename(),
  155. fileEdit,
  156. TheView.ParaFromY( pt.y - winpt.y ) );
  157. break;
  158. }
  159. case IDM_EDITCOPY :
  160. {
  161. Command( hwnd, b );
  162. break;
  163. }
  164. }
  165. }
  166. DestroyMenu( hMenu );
  167. }
  168. } //ContextMenu
  169. LRESULT Control::Create(
  170. CQueryResult * pResults,
  171. HWND hwnd,
  172. Model * pModel,
  173. View * pView,
  174. HFONT hFont )
  175. {
  176. LRESULT lr = 0;
  177. _iWheelRemainder = 0;
  178. _pModel = pModel;
  179. _pView = pView;
  180. SCODE sc = TheModel.CollectFiles( pResults );
  181. if ( SUCCEEDED( sc ) )
  182. {
  183. TheView.Init( hwnd, _pModel, hFont );
  184. // Go to first query hit (if any)
  185. Position pos;
  186. if ( TheModel.GetPositionCount() != 0 )
  187. pos = TheModel.GetPosition(0);
  188. TheView.SetRange ( TheModel.MaxParaLen(), TheModel.Paras());
  189. RECT rc;
  190. GetClientRect(hwnd,&rc);
  191. TheView.Size( rc.right, rc.bottom );
  192. TheView.SetScroll (pos);
  193. EnableMenus();
  194. }
  195. else
  196. {
  197. // no better way to get the error back
  198. App.BrowseLastError() = sc;
  199. lr = -1; // don't continue creating the window
  200. }
  201. return lr;
  202. } //Create
  203. void Control::Activate( HWND hwnd, LPARAM lParam )
  204. {
  205. if ( hwnd == (HWND) lParam )
  206. {
  207. int apos[3] = { 0, 0, 0 };
  208. int cPos = 2;
  209. HDC hdc = GetDC( hwnd );
  210. if ( 0 == hdc )
  211. return;
  212. SIZE size;
  213. WCHAR awcLines[100];
  214. CResString strLines( IDS_BRSTAT_CLINES );
  215. wsprintf( awcLines, strLines.Get(), TheModel.ParaCount() );
  216. GetTextExtentPoint( hdc, awcLines, wcslen( awcLines ), &size );
  217. apos[0] = 2 * size.cx;
  218. WCHAR awcHits[100];
  219. CResString strHits( IDS_BRSTAT_CHITS );
  220. wsprintf( awcHits, strHits.Get(), TheModel.HitCount(), TheModel.HitCount() );
  221. GetTextExtentPoint( hdc, awcHits, wcslen( awcHits ), &size );
  222. apos[1] = 2 * size.cx + apos[0];
  223. ReleaseDC( hwnd, hdc );
  224. SendMessage( App.StatusBarWindow(), SB_SETPARTS, cPos, (LPARAM) apos );
  225. SendMessage( App.StatusBarWindow(), SB_SETTEXT, 0, (LPARAM) awcLines );
  226. static UINT aDisable[] = { IDM_SEARCH,
  227. IDM_SEARCHCLASSDEF,
  228. IDM_SEARCHFUNCDEF,
  229. IDM_BROWSE,
  230. IDM_DISPLAY_PROPS };
  231. UpdateButtons( aDisable, 5, FALSE );
  232. EnableMenus();
  233. }
  234. } //Activate
  235. void Control::NewFont ( HWND hwnd, WPARAM wParam)
  236. {
  237. TheView.FontChange ( hwnd, (HFONT) wParam );
  238. InvalidateRect(hwnd, NULL, TRUE);
  239. } //NewFont
  240. void Control::SetScrollBars ( HWND hwnd )
  241. {
  242. SetScrollPos ( hwnd, SB_VERT, TheView.VScrollPara(), TRUE );
  243. SetScrollPos ( hwnd, SB_HORZ, TheView.HScrollPos(), TRUE );
  244. } //SetScrollBars
  245. // Go to next query hit when 'n' pressed
  246. void Control::Char ( HWND hwnd, WPARAM wparam )
  247. {
  248. Position pos;
  249. HCURSOR hCursor;
  250. BOOL success;
  251. switch ( wparam )
  252. {
  253. case 'n':
  254. {
  255. if ( !TheModel.NextHit() )
  256. break;
  257. pos = TheModel.GetPosition(0);
  258. TheView.SetScroll( pos );
  259. SetScrollBars (hwnd);
  260. EnableMenus();
  261. InvalidateRect(hwnd, NULL, TRUE);
  262. break;
  263. }
  264. case 'p':
  265. {
  266. if ( !TheModel.PrevHit() )
  267. break;
  268. pos = TheModel.GetPosition(0);
  269. TheView.SetScroll( pos );
  270. SetScrollBars(hwnd);
  271. EnableMenus();
  272. InvalidateRect(hwnd, NULL, TRUE);
  273. break;
  274. }
  275. case 'N':
  276. {
  277. hCursor = SetCursor (LoadCursor(0, IDC_WAIT));
  278. ShowCursor (TRUE);
  279. success = S_OK == TheModel.NextDoc();
  280. ShowCursor(FALSE);
  281. SetCursor (hCursor);
  282. if ( success )
  283. {
  284. if ( TheModel.GetPositionCount() != 0 )
  285. {
  286. pos = TheModel.GetPosition(0);
  287. }
  288. TheView.SetRange ( TheModel.MaxParaLen(), TheModel.Paras());
  289. TheView.SetScrollMax();
  290. TheView.SetScroll (pos);
  291. UpdateScroll(hwnd);
  292. InvalidateRect(hwnd, NULL, TRUE);
  293. EnableMenus();
  294. SetWindowText( hwnd, TheModel.Filename() );
  295. }
  296. break;
  297. }
  298. case 'P':
  299. {
  300. hCursor = SetCursor (LoadCursor(0, IDC_WAIT));
  301. ShowCursor (TRUE);
  302. success = TheModel.PrevDoc();
  303. ShowCursor(FALSE);
  304. SetCursor (hCursor);
  305. if ( success )
  306. {
  307. if ( TheModel.GetPositionCount() != 0 )
  308. pos = TheModel.GetPosition(0);
  309. TheView.SetRange ( TheModel.MaxParaLen(), TheModel.Paras());
  310. TheView.SetScrollMax();
  311. TheView.SetScroll (pos);
  312. UpdateScroll(hwnd);
  313. InvalidateRect(hwnd, NULL, TRUE);
  314. EnableMenus();
  315. SetWindowText( hwnd, TheModel.Filename() );
  316. }
  317. break;
  318. }
  319. }
  320. } //Char
  321. void Control::EnableMenus()
  322. {
  323. UINT ui = IDM_NEXT_HIT;
  324. UpdateButtons( &ui, 1, ! TheModel.isLastHit() );
  325. ui = IDM_PREVIOUS_HIT;
  326. UpdateButtons( &ui, 1, ! TheModel.isFirstHit() );
  327. HMENU hmenu = GetMenu( App.AppWindow() );
  328. EnableMenuItem(hmenu,IDM_NEXT_HIT,MF_BYCOMMAND |
  329. (TheModel.isLastHit() ? MF_GRAYED | MF_DISABLED :
  330. MF_ENABLED) );
  331. EnableMenuItem(hmenu,IDM_PREVIOUS_HIT,MF_BYCOMMAND |
  332. (TheModel.isFirstHit() ? MF_GRAYED | MF_DISABLED :
  333. MF_ENABLED) );
  334. EnableMenuItem( hmenu, IDM_NEWSEARCH, MF_ENABLED );
  335. int cHits = TheModel.HitCount();
  336. WCHAR awcHits[100];
  337. if ( 0 == cHits )
  338. {
  339. awcHits[0] = 0;
  340. }
  341. else
  342. {
  343. CResString strHits( IDS_BRSTAT_CHITS );
  344. wsprintf( awcHits,
  345. strHits.Get(),
  346. TheModel.CurrentHit() + 1,
  347. cHits );
  348. }
  349. SendMessage( App.StatusBarWindow(), SB_SETTEXT, 1, (LPARAM) awcHits );
  350. } //EnableMenus
  351. void Control::Size ( HWND hwnd, LPARAM lParam )
  352. {
  353. TheView.Size ( LOWORD(lParam), HIWORD(lParam) );
  354. TheView.SetScrollMax();
  355. UpdateScroll( hwnd );
  356. // in case we have to scroll to close up the gap
  357. // at the bottom of the window
  358. int delta = TheView.IncVScrollPos( 0 );
  359. if (delta != 0)
  360. {
  361. MyScrollWindow (hwnd, 0, -delta * TheView.LineHeight(), 0, 0 );
  362. SetScrollPos (hwnd, SB_VERT, TheView.VScrollPara(), TRUE );
  363. UpdateWindow(hwnd);
  364. }
  365. } //Size
  366. void Control::UpdateScroll( HWND hwnd)
  367. {
  368. TheView.SetRange ( TheModel.MaxParaLen(), TheModel.Paras());
  369. SetScrollRange(hwnd, SB_VERT, 0, TheView.VScrollMax(), FALSE );
  370. SetScrollRange(hwnd, SB_HORZ, 0, TheView.HScrollMax(), FALSE );
  371. SetScrollBars (hwnd);
  372. // proportional scroll box
  373. SCROLLINFO si;
  374. si.cbSize = sizeof(si);
  375. si.fMask = SIF_PAGE;
  376. si.nPage = TheView.VisibleLines();
  377. SetScrollInfo( hwnd, SB_VERT, &si, TRUE );
  378. } //UpdateScroll
  379. LRESULT Control::MouseWheel( HWND hwnd, WPARAM wParam, LPARAM lParam )
  380. {
  381. // forward what we don't process
  382. if ( wParam & ( MK_SHIFT | MK_CONTROL ) )
  383. return DefMDIChildProc( hwnd, WM_MOUSEWHEEL, wParam, lParam );
  384. // add the current scroll to the remainder from last time
  385. int iDelta = (int) (short) HIWORD( wParam );
  386. iDelta += _iWheelRemainder;
  387. // if there isn't enough to process this time, just return
  388. if ( abs( iDelta ) < WHEEL_DELTA )
  389. {
  390. _iWheelRemainder = iDelta;
  391. return 0;
  392. }
  393. // compute the remainder and amount to scroll
  394. _iWheelRemainder = ( iDelta % WHEEL_DELTA );
  395. iDelta /= WHEEL_DELTA;
  396. BOOL fDown;
  397. if ( iDelta < 0 )
  398. {
  399. fDown = TRUE;
  400. iDelta = -iDelta;
  401. }
  402. else
  403. fDown = FALSE;
  404. // get the # of lines to scroll per WHEEL_DELTA
  405. int cLines;
  406. SystemParametersInfo( SPI_GETWHEELSCROLLLINES, 0, &cLines, 0 );
  407. if ( 0 == cLines )
  408. return 0;
  409. int cVisibleLines = TheView.VisibleLines();
  410. // if scrolling a page, do so. don't scroll more than one page
  411. if ( WHEEL_PAGESCROLL == cLines )
  412. iDelta = __max( 1, (cVisibleLines - 1) );
  413. else
  414. {
  415. iDelta *= cLines;
  416. if ( iDelta >= cVisibleLines )
  417. iDelta = __max( 1, (cVisibleLines - 1) );
  418. }
  419. // phew. do the scroll
  420. if ( 0 != iDelta )
  421. {
  422. int delta = TheView.IncVScrollPos( fDown ? iDelta : -iDelta );
  423. MyScrollWindow( hwnd, 0, -delta * TheView.LineHeight(), 0, 0, FALSE );
  424. SetScrollPos( hwnd, SB_VERT, TheView.VScrollPara(), TRUE );
  425. UpdateWindow( hwnd );
  426. }
  427. return iDelta;
  428. } //MouseWheel
  429. void Control::VScroll( HWND hwnd, WPARAM wParam, LPARAM lParam )
  430. {
  431. int nVScrollInc;
  432. int delta = 0;
  433. switch (LOWORD(wParam))
  434. {
  435. case SB_TOP:
  436. TheView.Home();
  437. InvalidateRect (hwnd, 0, TRUE);
  438. delta = 1;
  439. break;
  440. case SB_BOTTOM:
  441. TheView.End();
  442. InvalidateRect (hwnd, 0, TRUE);
  443. delta = 1;
  444. break;
  445. case SB_LINEUP:
  446. delta = TheView.IncVScrollPos( -1 );
  447. MyScrollWindow (hwnd, 0, -delta * TheView.LineHeight(), 0, 0, FALSE );
  448. break;
  449. case SB_LINEDOWN:
  450. nVScrollInc = 1;
  451. delta = TheView.IncVScrollPos( 1 );
  452. MyScrollWindow (hwnd, 0, -delta * TheView.LineHeight(), 0, 0, FALSE );
  453. break;
  454. case SB_PAGEUP:
  455. nVScrollInc = - max ( 1, TheView.VisibleLines() - 1);
  456. delta = TheView.IncVScrollPos( nVScrollInc );
  457. MyScrollWindow (hwnd, 0, -delta * TheView.LineHeight(), 0, 0, FALSE );
  458. break;
  459. case SB_PAGEDOWN:
  460. nVScrollInc = max ( 1, TheView.VisibleLines() - 1);
  461. delta = TheView.IncVScrollPos( nVScrollInc );
  462. MyScrollWindow (hwnd, 0, -delta * TheView.LineHeight(), 0, 0, FALSE );
  463. break;
  464. case SB_THUMBTRACK:
  465. delta = TheView.JumpToPara ( HIWORD(wParam) );
  466. MyScrollWindow (hwnd, 0, -delta * TheView.LineHeight(), 0, 0, FALSE );
  467. break;
  468. default:
  469. break;
  470. }
  471. if ( delta != 0 )
  472. {
  473. SetScrollPos (hwnd, SB_VERT, TheView.VScrollPara(), TRUE );
  474. UpdateWindow(hwnd);
  475. }
  476. } //VScroll
  477. void Control::HScroll( HWND hwnd, WPARAM wParam, LPARAM lParam )
  478. {
  479. }
  480. void Control::KeyDown ( HWND hwnd, WPARAM wparam )
  481. {
  482. switch(wparam)
  483. {
  484. case VK_HOME:
  485. SendMessage( hwnd, WM_VSCROLL, SB_TOP, 0L );
  486. break;
  487. case VK_END:
  488. SendMessage( hwnd, WM_VSCROLL, SB_BOTTOM, 0L );
  489. break;
  490. case VK_PRIOR:
  491. SendMessage( hwnd, WM_VSCROLL, SB_PAGEUP, 0L );
  492. break;
  493. case VK_NEXT:
  494. SendMessage( hwnd, WM_VSCROLL, SB_PAGEDOWN, 0L );
  495. break;
  496. case VK_UP:
  497. SendMessage( hwnd, WM_VSCROLL, SB_LINEUP, 0L );
  498. break;
  499. case VK_DOWN:
  500. SendMessage( hwnd, WM_VSCROLL, SB_LINEDOWN, 0L );
  501. break;
  502. case VK_LEFT:
  503. SendMessage( hwnd, WM_HSCROLL, SB_PAGEUP, 0L );
  504. break;
  505. case VK_RIGHT:
  506. SendMessage( hwnd, WM_HSCROLL, SB_PAGEDOWN, 0L );
  507. break;
  508. }
  509. } //KeyDown
  510. // Menu commands processing
  511. void Control::Command ( HWND hwnd, WPARAM wParam )
  512. {
  513. switch ( wParam )
  514. {
  515. case IDM_NEXT_HIT:
  516. SendMessage ( hwnd, WM_CHAR, 'n', 0L );
  517. break;
  518. case IDM_PREVIOUS_HIT:
  519. SendMessage ( hwnd, WM_CHAR, 'p', 0L );
  520. break;
  521. case IDM_NEWSEARCH:
  522. // close the browser window
  523. PostMessage( hwnd, WM_CLOSE, 0, 0 );
  524. break;
  525. case IDM_EDITCOPY :
  526. TheView.EditCopy( hwnd, wParam );
  527. }
  528. } //Command