Windows NT 4.0 source code leak
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.

985 lines
30 KiB

4 years ago
  1. /******************************Module*Header*******************************\
  2. * Module Name: sswproc.cxx
  3. *
  4. * Window procedure functions.
  5. *
  6. * Copyright (c) 1996 Microsoft Corporation
  7. \**************************************************************************/
  8. #include <windows.h>
  9. #include <commdlg.h>
  10. #include <scrnsave.h>
  11. #include <GL\gl.h>
  12. #include "tk.h"
  13. #include <math.h>
  14. #include <memory.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <malloc.h>
  20. #include <time.h>
  21. #include "ssintrnl.hxx"
  22. #include "sswproc.hxx"
  23. #include "palette.hxx"
  24. #include "clear.hxx"
  25. // forward declarations of internal functions
  26. static void ss_TimerProc();
  27. LONG SS_ScreenSaverProc(HWND, UINT, WPARAM, LPARAM);
  28. LONG
  29. FullScreenPaletteManageProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  30. static void
  31. ssw_RelayMessageToChildren( PSSW pssw, UINT msg, WPARAM wParam, LPARAM lParam);
  32. static void ssw_RealizePalette( PSSW pssw, BOOL bBackground );
  33. static void ssw_DeletePalette( PSSW pssw );
  34. /**************************************************************************\
  35. * ScreenSaverProc
  36. *
  37. * Processes messages for the top level screen saver window.
  38. *
  39. * Unhandled msgs are sent to DefScreenSaverProc
  40. \**************************************************************************/
  41. LONG
  42. ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  43. {
  44. HDC hdc;
  45. PAINTSTRUCT ps;
  46. static BOOL bInited = FALSE;
  47. static UINT idTimer = 0;
  48. // Draw timer time-out interval
  49. #ifdef SS_DEBUG
  50. static UINT uiTimeOut = 2; // Let it rip !
  51. #else
  52. static UINT uiTimeOut = 16; // Cap at ~60 fps
  53. #endif
  54. static BOOL bSuspend = FALSE;
  55. #ifdef SS_WIN95_TIMER_HACK
  56. static BOOL bIdle = FALSE;
  57. #endif
  58. PSSW pssw;
  59. switch (message)
  60. {
  61. case WM_CREATE:
  62. case WM_ERASEBKGND:
  63. case SS_WM_INITGL:
  64. return SS_ScreenSaverProc( hwnd, message, wParam, lParam);
  65. case WM_ACTIVATE:
  66. if ( LOWORD(wParam) == WA_INACTIVE ) {
  67. SS_DBGMSG( "Main_Proc: WM_ACTIVATE inactive\n" );
  68. gpss->bInForeground = FALSE;
  69. } else {
  70. SS_DBGMSG( "Main_Proc: WM_ACTIVATE active\n" );
  71. gpss->bInForeground = TRUE;
  72. }
  73. // fall thru
  74. case WM_QUERYNEWPALETTE:
  75. case WM_PALETTECHANGED:
  76. case WM_SYSCOLORCHANGE:
  77. case SS_WM_PALETTE:
  78. return( MainPaletteManageProc( hwnd, message, wParam, lParam ) );
  79. case SS_WM_START:
  80. SS_DBGMSG( "Main_Proc: SS_WM_START\n" );
  81. // This is the main GL startup point. The global animation timer
  82. // is started, and SS_WM_START is relayed to the window chain.
  83. //mf: kluge for 'delayed background paint' problem in preview mode - in
  84. // floater type ss's, the main window gets this delayed bg paint thing that
  85. // makes the floater obvious. By relenquishing our time slice here, the
  86. // problem goes away...
  87. #if 1
  88. if( gpss->type == SS_TYPE_PREVIEW ) {
  89. Sleep(0);
  90. }
  91. #endif
  92. // Initialize the animation timer - it should start up once we
  93. // return from here
  94. idTimer = 1;
  95. SetTimer(hwnd, idTimer, uiTimeOut, 0);
  96. #ifdef SS_DEBUG
  97. // Start timer for calculating update rate
  98. gpss->timer.Start();
  99. #endif
  100. if( !bInited )
  101. bInited = TRUE;
  102. // Process SS_WM_START for the window chain
  103. return SS_ScreenSaverProc( hwnd, message, wParam, lParam);
  104. #ifdef SS_WIN95_TIMER_HACK
  105. case SS_WM_IDLE :
  106. if( wParam == SS_IDLE_ON )
  107. bIdle = TRUE;
  108. else if( wParam == SS_IDLE_OFF )
  109. bIdle = FALSE;
  110. break;
  111. #endif
  112. case WM_DESTROY:
  113. if (idTimer) {
  114. KillTimer(hwnd, idTimer);
  115. idTimer = 0;
  116. }
  117. // Destroy any children of the top level window
  118. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  119. PSSW psswChild;
  120. psswChild = pssw->psswChildren;
  121. while( psswChild ) {
  122. if( psswChild->hwnd )
  123. DestroyWindow( psswChild->hwnd );
  124. psswChild = pssw->psswSibling;
  125. }
  126. // Handle any palette stuff
  127. //mf: Before deleting this top level window, we need to use its
  128. // still-valid dc to things like restore SystemPaletteUse mode. Ideally this
  129. // should be done after ~SSW has dumped GL, but ~SSW also releases the DC.
  130. // If this is a problem, we can create a new function SSW::DeleteGL that just
  131. // gets rid of the GL part
  132. if( gpss->pssPal )
  133. ssw_DeletePalette( pssw );
  134. // Dump the main pssw
  135. delete pssw;
  136. // All pssw's have now been deleted - remove global ptr to top of
  137. // window chain.
  138. gpss->psswMain = NULL;
  139. PostQuitMessage(0);
  140. return 0;
  141. case SS_WM_CLOSING:
  142. // mf:This message is sent when the screen saver receives a WM_CLOSE
  143. // msg, *after* any password protection routines.
  144. // For now, only sent in /s mode
  145. SS_DBGMSG( "Main_Proc: SS_WM_CLOSING\n" );
  146. if( gpss->bResSwitch ) {
  147. // pssw->GdiClear();
  148. // mf: untested for child window case
  149. // Restore previous display settings
  150. // Note that this is also checked for in ~SCRNSAVE, in
  151. // case this message is not hit.
  152. ChangeDisplaySettings(NULL, CDS_FULLSCREEN);
  153. gpss->bResSwitch = FALSE;
  154. }
  155. return 0;
  156. case WM_SETFOCUS:
  157. SS_DBGMSG( "Main_Proc: WM_FOCUS\n" );
  158. //mf: this catches some of the win95 passwd dialog problems, where
  159. // we don't get repaint msgs when dialogs end
  160. if( ss_fOnWin95() && ss_fFullScreenMode() ) {
  161. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  162. pssw->Repaint( FALSE );
  163. }
  164. break;
  165. #if DBG
  166. case WM_SHOWWINDOW:
  167. SS_DBGMSG( "Main_Proc: WM_SHOWWINDOW\n" );
  168. break;
  169. #endif
  170. case WM_PAINT:
  171. SS_DBGMSG( "Main_Proc: WM_PAINT\n" );
  172. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  173. pssw->Repaint( TRUE );
  174. #ifdef SS_DO_PAINT
  175. // We do the painting rather than letting the system do it
  176. hdc = BeginPaint(hwnd, &ps);
  177. // This is case where bg brush is NULL and we have to do repaint
  178. // We only do it after bInited, as this will avoid the first
  179. // WM_PAINT for the entire window.
  180. if( bInited )
  181. DrawGdiRect( hdc, gpss->hbrBg, &ps.rcPaint );
  182. EndPaint(hwnd, &ps);
  183. #endif // SS_DO_PAINT
  184. #ifdef SS_DELAYED_START_KLUGE
  185. if( !bInited && SS_DELAY_START(gpss->type) ) {
  186. bInited = TRUE;
  187. // Do initial GL configuration
  188. PostMessage( hwnd, SS_WM_INITGL, 0, 0 );
  189. // Start drawing
  190. PostMessage( hwnd, SS_WM_START, 0, 0 );
  191. }
  192. #endif // SS_DELAYED_START_KLUGE
  193. if( pssw->iSubWindow ) {
  194. // If this window has sub windows, mark the bg for validation,
  195. // since for hardware double buffered schemes, Swapbuffers
  196. // may swap in ugly garbage.
  197. pssw->bValidateBg = TRUE;
  198. }
  199. #ifdef SS_DO_PAINT
  200. return 0; // painting has been handled by us
  201. #endif // SS_DO_PAINT
  202. break;
  203. case WM_SIZE:
  204. // Suspend drawing if minimized
  205. if( wParam == SIZE_MINIMIZED )
  206. bSuspend = TRUE;
  207. else // either SIZE_RESTORED or SIZE_MAXIMIZED
  208. bSuspend = FALSE;
  209. return SS_ScreenSaverProc( hwnd, message, wParam, lParam);
  210. case WM_MOVE:
  211. SS_DBGMSG( "Main_Proc: WM_MOVE\n" );
  212. // See note for WM_PAINT for subWindows
  213. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  214. //mf: kluge for non-floater workaround : see above
  215. if( pssw->iSubWindow )
  216. pssw->bValidateBg = TRUE;
  217. break;
  218. case WM_TIMER:
  219. if( bSuspend )
  220. return 0;
  221. #ifdef SS_WIN95_TIMER_HACK
  222. if( bIdle ) {
  223. // We are in an idle state, and don't want to flood the queue
  224. // with WM_TIMER mesages. So we kill the timer, do our
  225. // drawing, then start another timer.
  226. // Kill current timer
  227. if (idTimer)
  228. KillTimer(hwnd, idTimer);
  229. else
  230. // unlikely, but what the hay
  231. return 0;
  232. }
  233. #endif
  234. ss_TimerProc();
  235. #ifdef SS_WIN95_TIMER_HACK
  236. if( bIdle ) {
  237. // Start another animation timer after we've done drawing
  238. SetTimer(hwnd, idTimer, uiTimeOut, 0);
  239. }
  240. #endif
  241. return 0;
  242. }
  243. return DefScreenSaverProc(hwnd, message, wParam, lParam);
  244. }
  245. /**************************************************************************\
  246. * SS_ScreenSaverProc
  247. *
  248. * Wndproc for child windows, and some messages from top-level window
  249. *
  250. * Unhandled msgs are sent to DefWindowProc
  251. \**************************************************************************/
  252. LONG
  253. SS_ScreenSaverProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  254. {
  255. HDC hdc;
  256. PAINTSTRUCT ps;
  257. int i;
  258. int retVal;
  259. PSSW pssw;
  260. switch (message)
  261. {
  262. case WM_CREATE:
  263. SS_DBGMSG1( "SS_Proc: WM_CREATE for 0x%x\n", hwnd );
  264. pssw = (PSSW) ( ((LPCREATESTRUCT)lParam)->lpCreateParams );
  265. gpss->sswTable.Register( hwnd, pssw );
  266. pssw->size.width = ((LPCREATESTRUCT)lParam)->cx;
  267. pssw->size.height = ((LPCREATESTRUCT)lParam)->cy;
  268. pssw->hwnd = hwnd;
  269. break;
  270. case SS_WM_INITGL:
  271. SS_DBGMSG1( "SS_Proc: SS_WM_INITGL for 0x%x\n", hwnd );
  272. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  273. pssw->InitGL();
  274. break;
  275. #ifdef SS_MULTI_WINDOW_TIMERS
  276. // Enable this section and fill it in if windows start their own animation
  277. // timers. For now, there is just one timer on the main window.
  278. case SS_WM_START:
  279. SS_DBGMSG1( "SS_Proc: SS_WM_START for 0x%x\n", hwnd );
  280. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  281. // Send SS_WM_START to any children of this window
  282. ssw_RelayMessageToChildren( pssw, SS_WM_START, 0, 0 );
  283. // Nothing really to do here yet...
  284. break;
  285. #endif
  286. case SS_WM_PALETTE:
  287. return( MainPaletteManageProc( hwnd, message, wParam, lParam ) );
  288. case WM_DESTROY:
  289. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  290. SS_DBGMSG1( "SS_Proc: WM_DESTROY for 0x%x\n", hwnd );
  291. // Kill off any children of this window first
  292. #if 0
  293. //mf: ??? compiler no like ???
  294. PSSW psswChild = pssw->psswChildren;
  295. #else
  296. PSSW psswChild;
  297. psswChild = pssw->psswChildren;
  298. #endif
  299. while( psswChild ) {
  300. if( psswChild->hwnd )
  301. DestroyWindow( psswChild->hwnd );
  302. else
  303. delete psswChild;
  304. psswChild = pssw->psswSibling;
  305. }
  306. // Delete the pssw - this does all necessary cleanup
  307. delete pssw;
  308. break;
  309. case WM_ERASEBKGND:
  310. SS_DBGMSG1( "SS_Proc: WM_ERASEBKGRND for 0x%x\n", hwnd );
  311. #if 0
  312. // If eventually we want to control bg erasing...
  313. {
  314. BOOL bEraseNow = TRUE; // ! 0 or 1 have same effect !
  315. if( bEraseNow ) {
  316. // If bg for the window is NULL, ? should erase here ?
  317. return TRUE;
  318. }
  319. else
  320. return 0; // window remains marked for erasing
  321. }
  322. #else
  323. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  324. pssw->Repaint( FALSE );
  325. // Don't process this message
  326. return DefWindowProc(hwnd, message, wParam, lParam);
  327. #endif
  328. case WM_PAINT:
  329. // We get this msg every time window moves, since SWP_NOCOPYBITS is
  330. // specified with the window move.
  331. hdc = BeginPaint(hwnd, &ps);
  332. EndPaint(hwnd, &ps);
  333. break;
  334. case WM_SIZE:
  335. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  336. pssw->Resize( LOWORD(lParam), HIWORD(lParam) );
  337. break;
  338. // these msg's are never received by the child window ?
  339. case WM_ACTIVATE:
  340. case WM_QUERYNEWPALETTE:
  341. case WM_PALETTECHANGED:
  342. return( MainPaletteManageProc( hwnd, message, wParam, lParam ) );
  343. case WM_SYSCOMMAND:
  344. case WM_SETCURSOR:
  345. case WM_ACTIVATEAPP:
  346. case WM_MOUSEMOVE:
  347. case WM_LBUTTONDOWN:
  348. case WM_MBUTTONDOWN:
  349. case WM_RBUTTONDOWN:
  350. case WM_KEYDOWN:
  351. case WM_SYSKEYDOWN:
  352. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  353. return DefScreenSaverProc(pssw->psswParent->hwnd, message, wParam, lParam);
  354. default:
  355. return DefWindowProc(hwnd, message, wParam, lParam);
  356. }
  357. return 0;
  358. }
  359. #ifdef SS_DEBUG
  360. /**************************************************************************\
  361. * PrintUpdateRate
  362. *
  363. * Print number of updates per second in the title bar
  364. *
  365. \**************************************************************************/
  366. static void
  367. PrintUpdateRate( double elapsed, long updateCount )
  368. {
  369. char buf[100];
  370. double updateRate;
  371. if( elapsed == 0.0 )
  372. updateRate = 0.0;
  373. else
  374. updateRate = updateCount / elapsed;
  375. sprintf( buf, "Updates per second = %4.1f", updateRate );
  376. SendMessage(gpss->psswMain->hwnd, WM_SETTEXT, 0, (LPARAM)buf);
  377. }
  378. #endif
  379. /**************************************************************************\
  380. * ss_TimerProc
  381. *
  382. * Every time a timer event fires off, update all active windows
  383. *
  384. \**************************************************************************/
  385. static void
  386. ss_TimerProc()
  387. {
  388. static int busy = FALSE;
  389. int i;
  390. #ifdef SS_DEBUG
  391. static long updateCount = 0;
  392. static double updateInterval = 2.0;
  393. SS_TIMER *pTimer = &gpss->timer;
  394. #endif
  395. if (busy)
  396. return;
  397. busy = TRUE;
  398. gpss->psswMain->UpdateWindow();
  399. #ifdef SS_DEBUG
  400. updateCount++;
  401. if( gpss->bDoTiming &&
  402. (( (double) pTimer->ElapsedTime() ) >= updateInterval) )
  403. {
  404. double elapsed = pTimer->Stop();
  405. PrintUpdateRate( elapsed, updateCount );
  406. updateCount = 0;
  407. pTimer->Start();
  408. }
  409. #endif
  410. busy = FALSE;
  411. }
  412. /**************************************************************************\
  413. * RelayMessageToChildren
  414. *
  415. * Pass along the message to any child windows
  416. \**************************************************************************/
  417. static void
  418. ssw_RelayMessageToChildren( PSSW pssw, UINT msg, WPARAM wParam, LPARAM lParam)
  419. {
  420. PSSW psswChild = pssw->psswChildren;
  421. while( psswChild ) {
  422. if( psswChild->hwnd )
  423. SendMessage( psswChild->hwnd, msg, wParam, lParam );
  424. psswChild = psswChild->psswSibling;
  425. }
  426. }
  427. /**************************************************************************\
  428. * UpdateDIBColorTable
  429. *
  430. * Wrapper for SSDIB_UpdateColorTable.
  431. *
  432. * This controls the hPal parameter for SSDIB_UpdateColorTable.
  433. *
  434. \**************************************************************************/
  435. void
  436. ssw_UpdateDIBColorTable( HDC hdcbm, HDC hdcwin )
  437. {
  438. SS_PAL *pssPal = gpss->pssPal;
  439. if( !pssPal )
  440. return;
  441. #if 0
  442. HPALETTE hpal = pssPal->bTakeOver ? pssPal->hPal : NULL;
  443. #else
  444. HPALETTE hpal = pssPal->hPal;
  445. #endif
  446. SSDIB_UpdateColorTable( hdcbm, hdcwin, hpal );
  447. }
  448. /**************************************************************************\
  449. * RealizePalette
  450. *
  451. \**************************************************************************/
  452. static void
  453. ssw_RealizePalette( PSSW pssw, BOOL bBackground )
  454. {
  455. // assumed pssPal valid if get here
  456. SS_PAL *pssPal = gpss->pssPal;
  457. if( !pssw->hrc ) {
  458. // Can assume this window doesn't need to worry about palettes, but
  459. // if any of its children are subWindows, it will have to take care
  460. // of it *for* them.
  461. if( ! pssw->iSubWindow )
  462. return; // no hrc and no subWindow children
  463. }
  464. pssPal->Realize( pssw->hwnd, pssw->hdc, bBackground );
  465. if( pssw->pStretch && pssw->pStretch->ssbm.hdc ) {
  466. SS_BITMAP *pssbm = &pssw->pStretch->ssbm;
  467. ssw_UpdateDIBColorTable( pssbm->hdc, pssw->hdc );
  468. }
  469. }
  470. /**************************************************************************\
  471. * ssw_DeletePalette
  472. *
  473. \**************************************************************************/
  474. static void
  475. ssw_DeletePalette( PSSW pssw )
  476. {
  477. SS_PAL *pssPal = gpss->pssPal;
  478. if( pssPal->bTakeOver ) {
  479. // We took over the system palette - make a note of this
  480. // for any special ss termination conditions.
  481. gpss->flags |= SS_PALETTE_TAKEOVER;
  482. }
  483. pssPal->SetDC( pssw->hdc );
  484. delete pssPal;
  485. gpss->pssPal = NULL;
  486. }
  487. /**************************************************************************\
  488. * PaletteManage Procs
  489. \**************************************************************************/
  490. /* palette related msgs's:
  491. - WM_ACTIVATE:
  492. The WM_ACTIVATE message is sent when a window is being activated or
  493. deactivated. This message is sent first to the window procedure of
  494. the top-level window being deactivated; it is then sent to the
  495. window procedure of the top-level window being activated.
  496. - WM_QUERYNEWPALETTE:
  497. The WM_QUERYNEWPALETTE message informs a window that it is about
  498. to receive the keyboard focus, giving the window the opportunity
  499. to realize its logical palette when it receives the focus.
  500. If the window realizes its logical palette, it must return TRUE;
  501. otherwise, it must return FALSE.
  502. - WM_PALETTECHANGED:
  503. The WM_PALETTECHANGED message is sent to all top-level and overlapped
  504. windows after the window with the keyboard focus has realized its
  505. logical palette, thereby changing the system palette. This message
  506. enables a window that uses a color palette but does not have the
  507. keyboard focus to realize its logical palette and update its client
  508. area.
  509. This message must be sent to all top-level and overlapped windows,
  510. including the one that changed the system palette. If any child
  511. windows use a color palette, this message must be passed on to them
  512. as well.
  513. To avoid creating an infinite loop, a window that receives this
  514. message must not realize its palette, unless it determines that
  515. wParam does not contain its own window handle.
  516. - WM_SYSCOLORCHANGE:
  517. The WM_SYSCOLORCHANGE message is sent to all top-level windows when
  518. a change is made to a system color setting.
  519. - SS_WM_PALETTE:
  520. Internal msg. Uses:
  521. - In fullscreen mode, we send this from Main wndproc to main
  522. window's children on WM_ACTIVATE.
  523. - When this is received in SS_ScreenSaverProc, if fullscreen,
  524. it does:
  525. UnrealizeObject( pssPal->hPal );
  526. RealizePalette( hdc );
  527. otherwise, it is passed to PaletteManageProc, where
  528. Realize is called (for 'floater' windows to realize
  529. their palettes).
  530. - It is also sent by DelayPaletteRealization() when it can't get
  531. the system palette.
  532. */
  533. /**************************************************************************\
  534. * MainPaletteManageProc
  535. *
  536. * Top-level palette management proc.
  537. * Returns immediately if no palette set - otherwise calls through
  538. * paletteManageProc function pointer
  539. *
  540. \**************************************************************************/
  541. LONG
  542. MainPaletteManageProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  543. {
  544. if( !gpss->pssPal )
  545. // No palette management required
  546. return 0;
  547. // else call approppriate palette manage proc
  548. return (*gpss->pssPal->paletteManageProc)(hwnd, message, wParam, lParam);
  549. }
  550. LONG
  551. NullPaletteManageProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  552. {
  553. return 0;
  554. }
  555. /**************************************************************************\
  556. * FullScreenPaletteManageProc
  557. *
  558. * Processes messages relating to palette management in full screen mode.
  559. *
  560. \**************************************************************************/
  561. LONG
  562. FullScreenPaletteManageProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  563. {
  564. SS_PAL *pssPal;
  565. PSSW pssw;
  566. switch (message)
  567. {
  568. case WM_ACTIVATE:
  569. #if SS_DEBUG
  570. if ( LOWORD(wParam) == WA_INACTIVE )
  571. SS_DBGMSG1( "FullScreen_PMProc: WM_ACTIVATE : inactive for 0x%x\n",
  572. hwnd );
  573. else
  574. SS_DBGMSG1( "FullScreen_PMProc: WM_ACTIVATE : active for 0x%x\n",
  575. hwnd );
  576. #endif
  577. // !! This msg is only sent to main top level window
  578. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  579. pssPal = gpss->pssPal;
  580. if ( pssPal->bUseStatic ) {
  581. HDC hdc = pssw->hdc; // hdc *always* valid for top-level pssw
  582. // Note: wParam = 0 when window going *inactive*
  583. SetSystemPaletteUse( hdc, wParam ? SYSPAL_NOSTATIC
  584. : pssPal->uiOldStaticUse);
  585. }
  586. // Send SS_WM_PALETTE msg to main window
  587. SendMessage( hwnd, SS_WM_PALETTE, wParam, 0);
  588. break;
  589. case SS_WM_PALETTE:
  590. SS_DBGMSG1( "FullScreen_PMProc: SS_WM_PALETTE for 0x%x\n", hwnd );
  591. pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  592. ssw_RelayMessageToChildren( pssw, SS_WM_PALETTE, wParam, 0 );
  593. HDC hdc;
  594. if( hdc = pssw->hdc )
  595. {
  596. pssPal = gpss->pssPal;
  597. //mf: this should call thru ssw_RealizePalette for bitmap case ? (for now
  598. // don't need to, since we take over palette...)
  599. // This resets the logical palette, causing remapping of
  600. // logical palette to system palette
  601. // mf: !!! ?? how come no dc with UnrealizeObject ? does that
  602. // mean its done once per app, not per child window ?
  603. // yeah, we should move this up...
  604. UnrealizeObject( pssPal->hPal );
  605. RealizePalette( hdc );
  606. }
  607. break;
  608. }
  609. return 0;
  610. }
  611. /**************************************************************************\
  612. * PaletteManageProc
  613. *
  614. * Processes messages relating to palette management for the general case.
  615. *
  616. * Note: this msg handling strategy is based loosely on the tk, so any changes
  617. * there should be reflected here
  618. \**************************************************************************/
  619. LONG
  620. PaletteManageProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  621. {
  622. // One global palette for all windows
  623. if( !gpss->pssPal )
  624. return 0;
  625. switch (message)
  626. {
  627. case WM_ACTIVATE:
  628. SendMessage( hwnd, SS_WM_PALETTE, gpss->bInBackground(), 0);
  629. // Allow DefWindowProc() to finish the default processing (which
  630. // includes changing the keyboard focus).
  631. break;
  632. case WM_QUERYNEWPALETTE:
  633. SS_DBGMSG1( "Palette_Proc: WM_QUERYNEWPALETTE for 0x%x\n", hwnd );
  634. // We don't actually realize palette here (we do it at WM_ACTIVATE
  635. // time), but we need the system to think that we have so that a
  636. // WM_PALETTECHANGED message is generated.
  637. //mf: why can't we just realize here ? and who wants even more messages to
  638. // be generated !!! :)
  639. // This is the only msg preview mode gets wrt palettes !
  640. if( !ss_fPreviewMode() )
  641. return (1);
  642. // We are in preview mode - realize the palette
  643. SendMessage( hwnd, SS_WM_PALETTE, gpss->bInBackground(), 0);
  644. break;
  645. case WM_PALETTECHANGED:
  646. SS_DBGMSG1( "Palette_Proc: WM_PALETTECHANGED for 0x%x\n", hwnd );
  647. // Respond to this message only if the window that changed the palette
  648. // is not this app's window.
  649. // We are not the foreground window, so realize palette in the
  650. // background. We cannot call Realize to do this because
  651. // we should not do any of the gbUseStaticColors processing while
  652. // in background.
  653. // Actually, we *can* be the fg window, so don't realize if
  654. // we're in foreground
  655. if( (hwnd != (HWND) wParam) && gpss->bInBackground() )
  656. SendMessage( hwnd, SS_WM_PALETTE, TRUE, 0);
  657. break;
  658. case WM_SYSCOLORCHANGE:
  659. // If the system colors have changed and we have a palette
  660. // for an RGB surface then we need to recompute the static
  661. // color mapping because they might have been changed in
  662. // the process of changing the system colors.
  663. SS_DBGMSG1( "Palette_Proc: WM_SYSCOLORCHANGE for 0x%x\n", hwnd );
  664. gpss->pssPal->ReCreateRGBPalette();
  665. SendMessage( hwnd, SS_WM_PALETTE, gpss->bInBackground(), 0);
  666. break;
  667. case SS_WM_PALETTE:
  668. SS_DBGMSG2( "Palette_Proc: SS_WM_PALETTE for 0x%x, bg = %d\n",
  669. hwnd, wParam );
  670. // Realize palette for this window and its children
  671. // wParam = TRUE if realize as bg
  672. PSSW pssw = gpss->sswTable.PsswFromHwnd( hwnd );
  673. ssw_RelayMessageToChildren( pssw, message, wParam, lParam );
  674. ssw_RealizePalette( pssw, wParam );
  675. break;
  676. }
  677. return 0;
  678. }
  679. /******************************Public*Routine******************************\
  680. * GLScreenSaverConfigureDialog
  681. *
  682. * This is a wrapper for ScreenSaverConfigureDialog, which is the main dialog
  683. * proc for all the GL screen savers in config mode.
  684. *
  685. * We call the client's ss_ConfigInit() routine on the first WM_PAINT, since
  686. * the dialog will have focus at this point (can realize palette) and all
  687. * buttons should have been created.
  688. \**************************************************************************/
  689. BOOL
  690. GLScreenSaverConfigureDialog( HWND hDlg, UINT msg, WPARAM wParam,
  691. LPARAM lParam )
  692. {
  693. static BOOL bInited = 0;
  694. switch( msg ) {
  695. case WM_INITDIALOG :
  696. {
  697. SS_DBGMSG( "GLScreenSaverConfigureDialog: WM_INITDIALOG\n" );
  698. // Create wrapper pssw for the dialog box
  699. PSSW pssw;
  700. pssw = new SSW( NULL, // ssw parent
  701. hDlg
  702. );
  703. SS_ASSERT( pssw, "GLScreenSaverConfigureDialog : alloc failure for psswMain\n" );
  704. gpss->psswMain = pssw;
  705. // Load any resource strings common to all the dialogs
  706. BOOL bStringsLoaded = ss_LoadTextureResourceStrings();
  707. // If this doesn't work, things are seriously wrong and we
  708. // shouldn't continue
  709. SS_ASSERT( bStringsLoaded, "GLScreenSaverConfigureDialog : failure loading common resource strings\n" );
  710. }
  711. break;
  712. case WM_PAINT:
  713. if( !bInited ) {
  714. // Call client's ss_ConfigInit()
  715. if( !ss_ConfigInit( hDlg ) ) {
  716. SS_WARNING( "ConfigInit failed\n" );
  717. // Send WM_CLOSE to the dialog - this will enable any
  718. // cleanup code to be called by the client
  719. SendMessage( hDlg, WM_CLOSE, 0, 0l );
  720. }
  721. bInited = TRUE;
  722. }
  723. break;
  724. case WM_ACTIVATE:
  725. case WM_QUERYNEWPALETTE:
  726. case WM_PALETTECHANGED:
  727. case SS_WM_PALETTE:
  728. return( MainPaletteManageProc( hDlg, msg, wParam, lParam ) );
  729. }
  730. return ScreenSaverConfigureDialog( hDlg, msg, wParam, lParam );
  731. }
  732. /******************************Public*Routine******************************\
  733. * SSW_TABLE constructor
  734. *
  735. \**************************************************************************/
  736. SSW_TABLE::SSW_TABLE()
  737. {
  738. nEntries = 0;
  739. }
  740. /******************************Public*Routine******************************\
  741. * Register
  742. *
  743. * Register a HWND/PSSW pair.
  744. \**************************************************************************/
  745. void
  746. SSW_TABLE::Register( HWND hwnd, PSSW pssw )
  747. {
  748. SSW_TABLE_ENTRY *pEntry;
  749. // Check if already in table
  750. if( PsswFromHwnd( hwnd ) )
  751. return;
  752. // put hwnd/pssw pair in the table
  753. pEntry = &sswTable[nEntries];
  754. pEntry->hwnd = hwnd;
  755. pEntry->pssw = pssw;
  756. nEntries++;
  757. }
  758. /******************************Public*Routine******************************\
  759. * PsswFromHwnd
  760. *
  761. * Return PSSW for the HWND
  762. \**************************************************************************/
  763. PSSW
  764. SSW_TABLE::PsswFromHwnd( HWND hwnd )
  765. {
  766. int count = nEntries;
  767. SSW_TABLE_ENTRY *pEntry = sswTable;
  768. while( count-- ) {
  769. if( pEntry->hwnd == hwnd )
  770. return pEntry->pssw;
  771. pEntry++;
  772. }
  773. return NULL;
  774. }
  775. /******************************Public*Routine******************************\
  776. * Remove
  777. *
  778. * Remove HWND/PSSW entry from table
  779. \**************************************************************************/
  780. BOOL
  781. SSW_TABLE::Remove( HWND hwnd )
  782. {
  783. SSW_TABLE_ENTRY *pEntry = sswTable;
  784. // Locate the hwnd/pssw pair
  785. for( int count = 0 ; count < nEntries ; count++, pEntry++ ) {
  786. if( pEntry->hwnd == hwnd )
  787. break;
  788. }
  789. if( count == nEntries )
  790. // couldn't find it in the table
  791. return FALSE;
  792. // Remove entry / shuffle up other entries
  793. for( int i = count; i < nEntries-1; i ++ ) {
  794. sswTable[i] = sswTable[i+1];
  795. }
  796. nEntries--;
  797. return TRUE;
  798. }