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.

435 lines
11 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1992.
  5. //
  6. // File: cballs.cxx
  7. //
  8. // Contents: implementations for CBall
  9. //
  10. // Functions:
  11. // CBall::CBall
  12. // CBall::~CBall
  13. // CBall::QueryInterface
  14. // CBall::CreateBall
  15. // CBall::MoveBall
  16. // CBall::GetBallPos
  17. // CBall::IsOverLapped
  18. // CBall::IsContainedIn
  19. // CBall::Clone
  20. // CBall::Echo
  21. //
  22. // CBallCtrlUnk::CBallCtrlUnk
  23. // CBallCtrlUnk::~CBallCtrlUnk
  24. // CBallCtrlUnk::QueryInterface
  25. // CBallCtrlUnk::AddRef
  26. // CBallCtrlUnk::Release
  27. //
  28. // History: 06-Aug-92 Ricksa Created
  29. //
  30. //--------------------------------------------------------------------------
  31. #include <pch.cxx>
  32. #pragma hdrstop
  33. #include <cballs.hxx> // class definition
  34. #include <icube.h> // ICube definition
  35. #define CUBE_WIDTH 20
  36. //+-------------------------------------------------------------------------
  37. //
  38. // Method: CBall::CBall
  39. //
  40. // Synopsis: Creates the application window
  41. //
  42. // Arguments: [pisb] - ISysBind instance
  43. //
  44. // History: 06-Aug-92 Ricksa Created
  45. //
  46. //--------------------------------------------------------------------------
  47. CBall::CBall(IUnknown *punkOuter)
  48. : _xPos(BALL_UNDEF),
  49. _yPos(BALL_UNDEF),
  50. _punkOuter(punkOuter)
  51. {
  52. CreateBall(0,0);
  53. GlobalRefs(TRUE);
  54. }
  55. //+-------------------------------------------------------------------------
  56. //
  57. // Method: CBall::~CBall
  58. //
  59. // Synopsis: Cleans up object
  60. //
  61. // History: 06-Aug-92 Ricksa Created
  62. //
  63. //--------------------------------------------------------------------------
  64. CBall::~CBall(void)
  65. {
  66. GlobalRefs(FALSE);
  67. // automatic actions are enough
  68. }
  69. //+-------------------------------------------------------------------------
  70. //
  71. // Method: CBall::QueryInterface
  72. //
  73. // Synopsis: Gets called when a WM_COMMAND message received.
  74. //
  75. // Arguments: [ifid] - interface instance requested
  76. // [ppunk] - where to put pointer to interface instance
  77. //
  78. // Returns: S_OK or ERROR_BAD_COMMAND
  79. //
  80. // History: 06-Aug-92 Ricksa Created
  81. //
  82. //--------------------------------------------------------------------------
  83. STDMETHODIMP CBall::QueryInterface(REFIID riid, void **ppunk)
  84. {
  85. return _punkOuter->QueryInterface(riid, ppunk);
  86. }
  87. //+-------------------------------------------------------------------------
  88. //
  89. // Method: CBall::AddRef
  90. //
  91. // History: 06-Aug-92 Ricksa Created
  92. //
  93. //--------------------------------------------------------------------------
  94. STDMETHODIMP_(ULONG) CBall::AddRef(void)
  95. {
  96. return _punkOuter->AddRef();
  97. }
  98. //+-------------------------------------------------------------------------
  99. //
  100. // Method: CBall::Release
  101. //
  102. // History: 06-Aug-92 Ricksa Created
  103. //
  104. //--------------------------------------------------------------------------
  105. STDMETHODIMP_(ULONG) CBall::Release(void)
  106. {
  107. return _punkOuter->Release();
  108. }
  109. //+-------------------------------------------------------------------------
  110. //
  111. // Method: CBall::QueryInternalIface
  112. //
  113. // Synopsis: Gets called when a WM_COMMAND message received.
  114. //
  115. // Arguments: [ifid] - interface instance requested
  116. // [ppunk] - where to put pointer to interface instance
  117. //
  118. // Returns: S_OK or ERROR_BAD_COMMAND
  119. //
  120. // History: 06-Aug-92 Ricksa Created
  121. //
  122. //--------------------------------------------------------------------------
  123. STDMETHODIMP CBall::QueryInternalIface(REFIID riid, void **ppunk)
  124. {
  125. SCODE sc = E_FAIL;
  126. if (IsEqualIID(riid,IID_IUnknown) ||
  127. IsEqualIID(riid,IID_IBalls))
  128. {
  129. // Increase the reference count
  130. *ppunk = (void *)(IBalls *) this;
  131. AddRef();
  132. // Set to success
  133. sc = S_OK;
  134. }
  135. return sc;
  136. }
  137. //+-------------------------------------------------------------------------
  138. //
  139. // Method: CBall::CreateBall
  140. //
  141. // Synopsis: Creates a ball on the screen
  142. //
  143. // Arguments: [xPos] - x position for new ball
  144. // [yPos] - y position for new ball
  145. //
  146. // Returns: S_OK or ERROR_BAD_COMMAND
  147. //
  148. // History: 06-Aug-92 Ricksa Created
  149. //
  150. //--------------------------------------------------------------------------
  151. STDMETHODIMP CBall::CreateBall(ULONG xPos, ULONG yPos)
  152. {
  153. SCODE sc = E_FAIL;
  154. if (xPos != (ULONG) -1)
  155. {
  156. // Update data
  157. _xPos = xPos;
  158. _yPos = yPos;
  159. sc = S_OK;
  160. // Format message for the screen
  161. Display(TEXT("Create Ball xPos = %ld yPos = %ld\n"), xPos, yPos);
  162. }
  163. return sc;
  164. }
  165. //+-------------------------------------------------------------------------
  166. //
  167. // Function: CBall::MoveBall
  168. //
  169. // Synopsis: Move the ball from one position to another
  170. //
  171. // Arguments: [xPos] - new x position for the ball
  172. // [yPos] - new y position for the ball
  173. //
  174. // Returns: S_OK
  175. //
  176. // History: 06-Aug-92 Ricksa Created
  177. //
  178. //--------------------------------------------------------------------------
  179. STDMETHODIMP CBall::MoveBall(ULONG xPos, ULONG yPos)
  180. {
  181. // Set the position
  182. _xPos = xPos;
  183. _yPos = yPos;
  184. // Format message for the screen
  185. Display(TEXT("Move Ball xPos = %ld yPos = %ld\n"), xPos, yPos);
  186. return S_OK;
  187. }
  188. //+-------------------------------------------------------------------------
  189. //
  190. // Function: CBall::GetBallPos
  191. //
  192. // Synopsis: Get the current position of the ball
  193. //
  194. // Arguments: [hWindow] - handle for the window
  195. //
  196. // Returns: S_OK
  197. //
  198. // History: 06-Aug-92 Ricksa Created
  199. //
  200. //--------------------------------------------------------------------------
  201. STDMETHODIMP CBall::GetBallPos(ULONG *pxPos, ULONG *pyPos)
  202. {
  203. *pxPos = _xPos;
  204. *pyPos = _yPos;
  205. // Format message for the screen
  206. Display(TEXT("Get Ball Pos xPos = %ld yPos = %ld\n"), _xPos, _yPos);
  207. return S_OK;
  208. }
  209. //+-------------------------------------------------------------------------
  210. //
  211. // Function: CBall::IsOverLapped
  212. //
  213. // Synopsis: Check if this ball overlaps the given ball.
  214. //
  215. // Arguments: [hWindow] - handle for the window
  216. // [pIBall] - other ball to compare with
  217. //
  218. // Returns: S_OK
  219. //
  220. // History: 06-Aug-92 Ricksa Created
  221. //
  222. //--------------------------------------------------------------------------
  223. STDMETHODIMP CBall::IsOverLapped(IBalls *pIBall)
  224. {
  225. ULONG xPos, yPos;
  226. SCODE sc = pIBall->GetBallPos(&xPos, &yPos);
  227. if (SUCCEEDED(sc))
  228. {
  229. if ((abs(xPos - _xPos) < BALL_DIAMETER) &&
  230. (abs(yPos - _yPos) < BALL_DIAMETER))
  231. {
  232. // Format message for the screen
  233. Display(TEXT("Balls OverLap. xpos = %ld yPos = %ld\n"), xPos, yPos);
  234. }
  235. else
  236. {
  237. // Format message for the screen
  238. Display(TEXT("Balls Dont OverlLap. xPos = %ld yPos = %ld\n"), xPos, yPos);
  239. }
  240. }
  241. return sc;
  242. }
  243. //+-------------------------------------------------------------------------
  244. //
  245. // Function: CBall::IsContainedIn
  246. //
  247. // Synopsis: Check if this ball is contained in the given cube.
  248. //
  249. // Arguments: [hWindow] - handle for the window
  250. // [pICube] - cube to compare with
  251. //
  252. // Returns: S_OK
  253. //
  254. // History: 06-Aug-92 Ricksa Created
  255. //
  256. //--------------------------------------------------------------------------
  257. STDMETHODIMP CBall::IsContainedIn(ICube *pICube)
  258. {
  259. ULONG xPos, yPos;
  260. SCODE sc = pICube->GetCubePos(&xPos, &yPos);
  261. if (SUCCEEDED(sc))
  262. {
  263. if ((abs(xPos - _xPos) < CUBE_WIDTH) &&
  264. (abs(yPos - _yPos) < CUBE_WIDTH))
  265. {
  266. // Format message for the screen
  267. Display(TEXT("Ball Contained in Cube. xPos = %ld yPos = %ld\n"), xPos, yPos);
  268. }
  269. else
  270. {
  271. // Format message for the screen
  272. Display(TEXT("Ball Outside Cube. xPos = %ld yPos = %ld\n"), xPos, yPos);
  273. }
  274. }
  275. return sc;
  276. }
  277. //+-------------------------------------------------------------------------
  278. //
  279. // Function: CBall::Clone
  280. //
  281. // Synopsis: make a copy of this ball
  282. //
  283. // Arguments: [hWindow] - handle for the window
  284. // [ppIBall] - new ball to return
  285. //
  286. // Returns: S_OK
  287. //
  288. // History: 06-Aug-92 Ricksa Created
  289. //
  290. //--------------------------------------------------------------------------
  291. STDMETHODIMP CBall::Clone(IBalls **ppIBall)
  292. {
  293. Display(TEXT("Clone Ball\n"));
  294. CBallCtrlUnk *pNew = new CBallCtrlUnk(NULL);
  295. SCODE sc = pNew->QueryInterface(IID_IBalls, (void **)ppIBall);
  296. pNew->Release();
  297. return sc;
  298. }
  299. //+-------------------------------------------------------------------------
  300. //
  301. // Function: CBall::Echo
  302. //
  303. // Synopsis: returns the same interface passed in. this is just to test
  304. // marshalling in and out interfaces.
  305. //
  306. // Arguments: [hWindow] - handle for the window
  307. // [punkIn] - IUnknown in
  308. // [ppunkOut] - IUnknown out
  309. //
  310. // Returns: S_OK
  311. //
  312. // History: 06-Aug-92 Ricksa Created
  313. //
  314. //--------------------------------------------------------------------------
  315. STDMETHODIMP CBall::Echo(IUnknown *punkIn, IUnknown **ppunkOut)
  316. {
  317. Display(TEXT("Echo Interface\n"));
  318. punkIn->AddRef(); // keep the in parameter...
  319. *ppunkOut = punkIn; // cause we're gonna return it.
  320. return S_OK;
  321. }
  322. #pragma warning(disable:4355) // 'this' used in base member init list
  323. //+-------------------------------------------------------------------------
  324. //
  325. // Method: CBallCtrlUnk::CBallCtrlUnk
  326. //
  327. // Synopsis:
  328. //
  329. // Arguments:
  330. //
  331. // History: 06-Aug-92 Ricksa Created
  332. //
  333. //--------------------------------------------------------------------------
  334. CBallCtrlUnk::CBallCtrlUnk(IUnknown *punkOuter)
  335. : _ball((punkOuter) ? punkOuter : this)
  336. {
  337. ENLIST_TRACKING(CBallCtrlUnk);
  338. }
  339. #pragma warning(default:4355) // 'this' used in base member init list
  340. //+-------------------------------------------------------------------------
  341. //
  342. // Method: CBallCtrlUnk::~CBallCtrlUnk
  343. //
  344. // Synopsis: Cleans up object
  345. //
  346. // History: 06-Aug-92 Ricksa Created
  347. //
  348. //--------------------------------------------------------------------------
  349. CBallCtrlUnk::~CBallCtrlUnk(void)
  350. {
  351. // automatic actions are enough
  352. }
  353. //+-------------------------------------------------------------------------
  354. //
  355. // Method: CBallCtrlUnk::QueryInterface
  356. //
  357. // Synopsis: Gets called when a WM_COMMAND message received.
  358. //
  359. // Arguments: [ifid] - interface instance requested
  360. // [ppunk] - where to put pointer to interface instance
  361. //
  362. // Returns: S_OK or ERROR_BAD_COMMAND
  363. //
  364. // History: 06-Aug-92 Ricksa Created
  365. //
  366. //--------------------------------------------------------------------------
  367. STDMETHODIMP CBallCtrlUnk::QueryInterface(REFIID riid, void **ppunk)
  368. {
  369. SCODE sc = E_FAIL;
  370. if (IsEqualIID(riid,IID_IUnknown))
  371. {
  372. // Increase the reference count
  373. *ppunk = (void *)(IUnknown *) this;
  374. AddRef();
  375. // Set to success
  376. sc = S_OK;
  377. }
  378. else
  379. {
  380. sc = _ball.QueryInternalIface(riid, ppunk);
  381. }
  382. return sc;
  383. }