ODWin.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* OpenDoors Online Software Programming Toolkit
  2. * (C) Copyright 1991 - 1999 by Brian Pirie.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. *
  19. * File: ODWin.c
  20. *
  21. * Description: Implements the od_window_...() functions for creating
  22. * and removing text-mode windows.
  23. *
  24. * Revisions: Date Ver Who Change
  25. * ---------------------------------------------------------------
  26. * Oct 13, 1994 6.00 BP New file header format.
  27. * Nov 01, 1994 6.00 BP Include stdlib.h for malloc prototype.
  28. * Dec 09, 1994 6.00 BP Standardized coding style.
  29. * Dec 12, 1994 6.00 BP Set od_error on window remove failure.
  30. * Aug 19, 1995 6.00 BP 32-bit portability.
  31. * Nov 16, 1995 6.00 BP Removed oddoor.h, added odcore.h.
  32. * Dec 12, 1995 6.00 BP Added entry, exit and kernel macros.
  33. * Dec 30, 1995 6.00 BP Added ODCALL for calling convention.
  34. * Feb 19, 1996 6.00 BP Changed version number to 6.00.
  35. * Mar 03, 1996 6.10 BP Begin version 6.10.
  36. * Aug 10, 2003 6.23 SH *nix support
  37. */
  38. #define BUILDING_OPENDOORS
  39. #include <string.h>
  40. #include <stdlib.h>
  41. #include "OpenDoor.h"
  42. #include "ODGen.h"
  43. #include "ODCore.h"
  44. #include "ODKrnl.h"
  45. /* ----------------------------------------------------------------------------
  46. * od_window_create()
  47. *
  48. * Creates a window on the screen, storing information on the original screen
  49. * contents "under" the window in order to restore the screen after the window
  50. * is removed. A window that is created with this function must be destroyed
  51. * by od_window_remove() in order to free up memory that is allocated by this
  52. * function.
  53. *
  54. * Parameters: nLeft - 1-based column number of left edge of window.
  55. *
  56. * nTop - 1-based row number of top edge of window.
  57. *
  58. * nRight - 1-based column number of right edge of window.
  59. *
  60. * nBottom - 1-based row number of bottom edge of window.
  61. *
  62. * pszTitle - Pointer to a string containing title for window.
  63. * If this string is empty, no title is displayed.
  64. *
  65. * btBoarderCol - Colour of window boarder.
  66. *
  67. * btTitleCol - Colour of window title.
  68. *
  69. * btInsideCol - Colour of rest of window.
  70. *
  71. * nReserved - Should always be 0 for this version.
  72. *
  73. * Return: Pointer to window description buffer (which must later be
  74. * passed to od_window_remove(), or NULL on failure.
  75. */
  76. ODAPIDEF void * ODCALL od_window_create(INT nLeft, INT nTop, INT nRight,
  77. INT nBottom, char *pszTitle, BYTE btBorderCol, BYTE btTitleCol,
  78. BYTE btInsideCol, INT nReserved)
  79. {
  80. BYTE btLine;
  81. BYTE btBetweenSize;
  82. void *pBuffer;
  83. BYTE btTitleSize;
  84. BYTE btRemaining;
  85. /* Log function entry if running in trace mode. */
  86. TRACE(TRACE_API, "od_window_create()");
  87. /* Ensure that OpenDoors has been initialized */
  88. if(!bODInitialized) od_init();
  89. OD_API_ENTRY();
  90. nReserved &= 0x00;
  91. btBetweenSize = (nRight - nLeft) - 1;
  92. /* Setup od_box_chars appropriately. */
  93. if(od_control.od_box_chars[BOX_BOTTOM]==0)
  94. {
  95. od_control.od_box_chars[BOX_BOTTOM] = od_control.od_box_chars[BOX_TOP];
  96. }
  97. if(od_control.od_box_chars[BOX_RIGHT]==0)
  98. {
  99. od_control.od_box_chars[BOX_RIGHT] = od_control.od_box_chars[BOX_LEFT];
  100. }
  101. /* Ensure that the current display mode can support the capabilities */
  102. /* required to display and remove windows. */
  103. if(!(od_control.user_ansi || od_control.user_avatar))
  104. {
  105. od_control.od_error = ERR_NOGRAPHICS;
  106. OD_API_EXIT();
  107. return(NULL);
  108. }
  109. /* Validate parameters. */
  110. if(nLeft < 1 || nTop < 1 || nRight > 80 || nBottom > 25 || nRight-nLeft < 2
  111. || nBottom-nTop < 2)
  112. {
  113. od_control.od_error = ERR_PARAMETER;
  114. OD_API_EXIT();
  115. return(NULL);
  116. }
  117. /* Allocate a buffer large enough to hold all window information. */
  118. if((pBuffer = malloc((nRight - nLeft + 1) * 2 + (nBottom - nTop + 1) * 160
  119. + 4)) == NULL)
  120. {
  121. od_control.od_error = ERR_MEMORY;
  122. OD_API_EXIT();
  123. return(NULL);
  124. }
  125. /* Store current contents of screen where window will be drawn. */
  126. if(!od_gettext(nLeft, nTop, nRight, nBottom, (char *)pBuffer+4))
  127. {
  128. free(pBuffer);
  129. /* Note: od_control.od_error code has been set by od_gettext(). */
  130. OD_API_EXIT();
  131. return(NULL);
  132. }
  133. /* Store window information in buffer. */
  134. ((char *)pBuffer)[0]=nLeft;
  135. ((char *)pBuffer)[1]=nTop;
  136. ((char *)pBuffer)[2]=nRight;
  137. ((char *)pBuffer)[3]=nBottom;
  138. /* Determine number of characters of title to display. */
  139. if(pszTitle==NULL)
  140. {
  141. btTitleSize = 0;
  142. }
  143. else
  144. {
  145. if((btTitleSize = strlen(pszTitle)) > (btBetweenSize - 4))
  146. {
  147. btTitleSize = btBetweenSize - 4;
  148. }
  149. }
  150. /* Move to position of window's top corner, prepare to begin drawing the */
  151. /* window. */
  152. od_set_cursor(nTop,nLeft);
  153. od_set_attrib(btBorderCol);
  154. /* Display corner character. */
  155. od_putch(od_control.od_box_chars[BOX_UPPERLEFT]);
  156. /* If there is no title, display top line all in one piece. */
  157. if(btTitleSize == 0)
  158. {
  159. /* Display top line. */
  160. od_repeat(od_control.od_box_chars[BOX_TOP],btBetweenSize);
  161. }
  162. else
  163. {
  164. /* If there is a title, display the top line with a title centered in */
  165. /* it. */
  166. od_repeat(od_control.od_box_chars[BOX_TOP],btRemaining =
  167. ((btBetweenSize - btTitleSize - 2) / 2));
  168. od_set_attrib(btTitleCol);
  169. od_putch(' ');
  170. od_disp(pszTitle,btTitleSize,TRUE);
  171. od_putch(' ');
  172. od_set_attrib(btBorderCol);
  173. od_repeat(od_control.od_box_chars[BOX_TOP],
  174. (BYTE)(btBetweenSize - btRemaining - btTitleSize - 2));
  175. }
  176. /* Display top right corner character. */
  177. od_putch(od_control.od_box_chars[BOX_UPPERRIGHT]);
  178. /* If AVATAR mode is available. */
  179. if(od_control.user_avatar)
  180. {
  181. /* Display first left verticle line. */
  182. od_set_cursor(nTop + 1, nLeft);
  183. od_putch(od_control.od_box_chars[BOX_LEFT]);
  184. /* Fill in center of window with AVATAR clear area control sequence. */
  185. od_emulate(22);
  186. od_emulate(12);
  187. od_emulate(btInsideCol);
  188. od_emulate((BYTE)((nBottom - nTop) - 1));
  189. od_emulate(btBetweenSize);
  190. od_set_attrib(btBorderCol);
  191. od_set_cursor(nTop + 1 , nRight);
  192. /* Display first right verticle line. */
  193. od_putch(od_control.od_box_chars[BOX_RIGHT]);
  194. /* Display remaining verticle lines. */
  195. for(btLine=nTop+2;btLine<nBottom;++btLine)
  196. {
  197. /* Move to line start and display left line character. */
  198. od_set_cursor(btLine,nLeft);
  199. od_putch(od_control.od_box_chars[BOX_LEFT]);
  200. /* Move to line start and display right line character. */
  201. od_set_cursor(btLine,nRight);
  202. od_putch(od_control.od_box_chars[BOX_RIGHT]);
  203. }
  204. }
  205. /* If AVATAR mode is not available. */
  206. else
  207. {
  208. /* Loop through middle lines of window. */
  209. for(btLine=nTop+1;btLine<nBottom;++btLine)
  210. {
  211. /* Move to line start and display left line character. */
  212. od_set_cursor(btLine,nLeft);
  213. od_putch(od_control.od_box_chars[BOX_LEFT]);
  214. /* Set window colour. */
  215. od_set_attrib(btInsideCol);
  216. /* display blank area between left and right vertical lines. */
  217. od_repeat(' ',btBetweenSize);
  218. /* Set border colour. */
  219. od_set_attrib(btBorderCol);
  220. /* Display right line. */
  221. od_putch(od_control.od_box_chars[BOX_RIGHT]);
  222. }
  223. }
  224. /* Display bottom border of window */
  225. od_set_cursor(nBottom,nLeft);
  226. od_putch(od_control.od_box_chars[BOX_LOWERLEFT]);
  227. od_repeat(od_control.od_box_chars[BOX_BOTTOM],btBetweenSize);
  228. od_putch(od_control.od_box_chars[BOX_LOWERRIGHT]);
  229. /* Return a pointer to the window information buffer. */
  230. OD_API_EXIT();
  231. return(pBuffer);
  232. }
  233. /* ----------------------------------------------------------------------------
  234. * od_window_remove()
  235. *
  236. * Removes window from the screen, restoring the screen contents that where
  237. * in the window area when the window was first created.
  238. *
  239. * Parameters: pWinInfo - Pointer to buffer returned by od_window_create().
  240. * This buffer is deallocated before od_window_remove()
  241. * returns.
  242. *
  243. * Return: TRUE on success, or FALSE on failure.
  244. */
  245. ODAPIDEF BOOL ODCALL od_window_remove(void *pWinInfo)
  246. {
  247. /* Log function entry if running in trace mode */
  248. TRACE(TRACE_API, "od_window_remove()");
  249. /* Ensure that OpenDoors has been initialized */
  250. if(!bODInitialized) od_init();
  251. OD_API_ENTRY();
  252. if(pWinInfo == NULL)
  253. {
  254. /* Set error code and return with failure. */
  255. od_control.od_error = ERR_PARAMETER;
  256. OD_API_EXIT();
  257. return(FALSE);
  258. }
  259. if(!od_puttext(((char *)pWinInfo)[0], ((char *)pWinInfo)[1], ((char *)pWinInfo)[2], ((char *)pWinInfo)[3], (char *)pWinInfo + 4))
  260. {
  261. /* Deallocate memory assigned to window information structure. */
  262. free(pWinInfo);
  263. /* Note: od_control.od_error code has been set by od_puttext(). */
  264. /* Return with failure. */
  265. OD_API_EXIT();
  266. return(FALSE);
  267. }
  268. /* Deallocate memory assigned to window information structure. */
  269. free(pWinInfo);
  270. /* Return with success. */
  271. OD_API_EXIT();
  272. return(TRUE);
  273. }