TOOLTIP.CPP 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. // ToolTip.cpp
  15. #if (0)//PG
  16. #include "function.h"
  17. #include "ToolTip.h"
  18. #include "IconList.h"
  19. //#include "WolDebug.h"
  20. bool SaveSurfaceRect( int xRect, int yRect, int wRect, int hRect, char* pBits, WindowNumberType window );
  21. bool RestoreSurfaceRect( int xRect, int yRect, int wRect, int hRect, const char* pBits, WindowNumberType window );
  22. //***********************************************************************************************
  23. ToolTipClass::ToolTipClass( GadgetClass* pGadget, const char* szText, int xShow, int yShow,
  24. bool bRightAlign /* = false */, bool bIconList /*= false */ )
  25. : pGadget( pGadget ), xShow( xShow ), yShow( yShow ), next( NULL ), bShowing( false ), bIconList( bIconList ),
  26. bRightAlign( bRightAlign )
  27. {
  28. if( szText )
  29. {
  30. if( strlen( szText ) > TOOLTIPTEXT_MAX_LEN )
  31. strcpy( szTip, "Tooltip too long!" );
  32. else
  33. strcpy( szTip, szText );
  34. }
  35. else
  36. *szTip = 0;
  37. Set_Font( TypeFontPtr );
  38. Fancy_Text_Print( TXT_NONE, 0, 0, TBLACK, TBLACK, TPF_TYPE ); // Required before String_Pixel_Width() call, for god's sake.
  39. wShow = String_Pixel_Width( szTip ) + 2;
  40. hShow = 11;
  41. if( !bIconList )
  42. {
  43. pSaveRect = new char[ wShow * hShow ]; // Else it is reallocated on every draw.
  44. if( bRightAlign )
  45. this->xShow -= wShow;
  46. }
  47. else
  48. pSaveRect = NULL;
  49. // bIconList is true if tooltips appear for individual line items in an iconlist.
  50. // szText in this case is ignored.
  51. // yShow is the y position of the top row's tooltip - other rows will be offset from here.
  52. }
  53. //***********************************************************************************************
  54. ToolTipClass* ToolTipClass::GetToolTipHit()
  55. {
  56. // Returns 'this' if the mouse is over gadget bound to tooltip.
  57. // Otherwise calls the same function in the next tooltip in the list of which *this is a part.
  58. if( bGadgetHit() )
  59. return this;
  60. else if( next )
  61. return next->GetToolTipHit();
  62. else
  63. return NULL;
  64. }
  65. //***********************************************************************************************
  66. bool ToolTipClass::bGadgetHit()
  67. {
  68. // Returns true if the mouse is currently over the gadget to which *this is bound.
  69. int x = Get_Mouse_X();
  70. int y = Get_Mouse_Y();
  71. return ( x > pGadget->X && x < pGadget->X + pGadget->Width && y > pGadget->Y && y < pGadget->Y + pGadget->Height );
  72. }
  73. //***********************************************************************************************
  74. void ToolTipClass::Move( int xShow, int yShow )
  75. {
  76. bool bRestoreShow = false;
  77. if( bShowing )
  78. {
  79. bRestoreShow = true;
  80. Unshow();
  81. }
  82. this->xShow = xShow;
  83. if( !bIconList )
  84. {
  85. if( bRightAlign )
  86. this->xShow -= wShow;
  87. }
  88. this->yShow = yShow;
  89. if( bRestoreShow )
  90. Show();
  91. }
  92. //***********************************************************************************************
  93. void ToolTipClass::Show()
  94. {
  95. if( !bShowing )
  96. {
  97. Set_Font( TypeFontPtr );
  98. int xShowUse = xShow, yShowUse, wShowUse;
  99. const char* szTipUse;
  100. if( !bIconList )
  101. {
  102. yShowUse = yShow;
  103. wShowUse = wShow;
  104. szTipUse = szTip;
  105. }
  106. else
  107. {
  108. IconListClass* pIconList = (IconListClass*)pGadget;
  109. iLastIconListIndex = pIconList->IndexUnderMouse();
  110. if( iLastIconListIndex < 0 )
  111. {
  112. // Nothing to show.
  113. bShowing = true;
  114. return;
  115. }
  116. yShowUse = pIconList->OffsetToIndex( iLastIconListIndex, yShow );
  117. szTipUse = pIconList->Get_Item_Help( iLastIconListIndex );
  118. if( !szTipUse || *szTipUse == 0 )
  119. {
  120. // Nothing to show.
  121. bShowing = true;
  122. bLastShowNoText = true;
  123. return;
  124. }
  125. Fancy_Text_Print( TXT_NONE, 0, 0, TBLACK, TBLACK, TPF_TYPE ); // Required before String_Pixel_Width() call, for god's sake.
  126. wShowUse = String_Pixel_Width( szTipUse ) + 2;
  127. if( bRightAlign )
  128. xShowUse -= wShowUse;
  129. delete [] pSaveRect;
  130. pSaveRect = new char[ wShowUse * hShow ];
  131. bLastShowNoText = false;
  132. xLastShow = xShowUse;
  133. yLastShow = yShowUse;
  134. wLastShow = wShowUse;
  135. }
  136. // Save rect about to be corrupted.
  137. Hide_Mouse();
  138. SaveSurfaceRect( xShowUse, yShowUse, wShowUse, hShow, pSaveRect, WINDOW_MAIN );
  139. // Draw text.
  140. //Simple_Text_Print( szTipUse, xShowUse, yShowUse, GadgetClass::Get_Color_Scheme(), ColorRemaps[ PCOLOR_BROWN ].Color, TPF_TYPE ); //TPF_DROPSHADOW );
  141. Simple_Text_Print( szTipUse, xShowUse, yShowUse, GadgetClass::Get_Color_Scheme(), BLACK, TPF_TYPE ); //TPF_DROPSHADOW );
  142. // Draw bounding rect.
  143. // LogicPage->Draw_Rect( xShowUse, yShowUse, xShowUse + wShowUse - 1, yShowUse + hShow - 1, ColorRemaps[ PCOLOR_GOLD ].Color );
  144. Draw_Box( xShowUse, yShowUse, wShowUse, hShow, BOXSTYLE_BOX, false );
  145. Show_Mouse();
  146. bShowing = true;
  147. }
  148. }
  149. //***********************************************************************************************
  150. void ToolTipClass::Unshow()
  151. {
  152. if( bShowing )
  153. {
  154. int xShowUse, yShowUse, wShowUse;
  155. if( !bIconList )
  156. {
  157. xShowUse = xShow;
  158. wShowUse = wShow;
  159. yShowUse = yShow;
  160. }
  161. else
  162. {
  163. if( iLastIconListIndex == -1 || bLastShowNoText )
  164. {
  165. // Nothing to restore.
  166. bShowing = false;
  167. return;
  168. }
  169. // (Can't rely on iconlist being the same as when Show() occurred.)
  170. // IconListClass* pIconList = (IconListClass*)pGadget;
  171. // yShowUse = pIconList->OffsetToIndex( iLastIconListIndex, yShow );
  172. // const char* szTipUsed = pIconList->Get_Item_Help( iLastIconListIndex );
  173. // if( !szTipUsed || *szTipUsed == 0 )
  174. // {
  175. // // Nothing to restore.
  176. // bShowing = false;
  177. // return;
  178. // }
  179. // Fancy_Text_Print( TXT_NONE, 0, 0, TBLACK, TBLACK, TPF_TYPE ); // Required before String_Pixel_Width() call, for god's sake.
  180. // wShowUse = String_Pixel_Width( szTipUsed ) + 2;
  181. // if( bRightAlign )
  182. // xShowUse -= wShowUse;
  183. xShowUse = xLastShow;
  184. yShowUse = yLastShow;
  185. wShowUse = wLastShow;
  186. }
  187. Hide_Mouse();
  188. RestoreSurfaceRect( xShowUse, yShowUse, wShowUse, hShow, pSaveRect, WINDOW_MAIN );
  189. Show_Mouse();
  190. bShowing = false;
  191. }
  192. }
  193. //***********************************************************************************************
  194. bool ToolTipClass::bOverDifferentLine() const
  195. {
  196. // bIconList must be true if this is being used.
  197. // Returns true if the iconlist line that the mouse is over is different than the last time Show() was called.
  198. return ( ((IconListClass*)pGadget)->IndexUnderMouse() != iLastIconListIndex );
  199. }
  200. //***********************************************************************************************
  201. bool SaveSurfaceRect( int xRect, int yRect, int wRect, int hRect, char* pBits, WindowNumberType window )
  202. {
  203. // Saves a rect of the LogicPage DirectDraw surface to pBits.
  204. // if( wRect * hRect > iBufferSize )
  205. // {
  206. // debugprint( "SaveSurfaceRect failed.\n" );
  207. // return false;
  208. // }
  209. GraphicViewPortClass draw_window( LogicPage->Get_Graphic_Buffer(),
  210. WindowList[window][WINDOWX] + LogicPage->Get_XPos(),
  211. WindowList[window][WINDOWY] + LogicPage->Get_YPos(),
  212. WindowList[window][WINDOWWIDTH],
  213. WindowList[window][WINDOWHEIGHT] );
  214. if( draw_window.Lock() )
  215. {
  216. int iPitchSurf = draw_window.Get_Pitch() + draw_window.Get_Width(); // Meaning of "Pitch" in this class seems to mean the eol skip.
  217. const char* pLineSurf = (char*)draw_window.Get_Offset() + xRect + yRect * iPitchSurf;
  218. char* pLineSave = pBits;
  219. // ajw - Should copy DWORDs here instead for speed.
  220. for( int y = 0; y != hRect; y++ )
  221. {
  222. const char* pSurf = pLineSurf;
  223. char* pSave = pLineSave;
  224. for( int x = 0; x != wRect; x++ )
  225. *pSave++ = *pSurf++;
  226. pLineSurf += iPitchSurf;
  227. pLineSave += wRect;
  228. }
  229. draw_window.Unlock();
  230. return true;
  231. }
  232. else
  233. {
  234. // debugprint( "SaveSurfaceRect Could not lock surface.\n" );
  235. return false;
  236. }
  237. }
  238. //***********************************************************************************************
  239. bool RestoreSurfaceRect( int xRect, int yRect, int wRect, int hRect, const char* pBits, WindowNumberType window )
  240. {
  241. // Copies a saved rect of bits back to the LogicPage DD surface.
  242. GraphicViewPortClass draw_window( LogicPage->Get_Graphic_Buffer(),
  243. WindowList[window][WINDOWX] + LogicPage->Get_XPos(),
  244. WindowList[window][WINDOWY] + LogicPage->Get_YPos(),
  245. WindowList[window][WINDOWWIDTH],
  246. WindowList[window][WINDOWHEIGHT] );
  247. if( draw_window.Lock() )
  248. {
  249. int iPitchSurf = draw_window.Get_Pitch() + draw_window.Get_Width(); // Meaning of "Pitch" in this class seems to mean the eol skip.
  250. char* pLineSurf = (char*)draw_window.Get_Offset() + xRect + yRect * iPitchSurf;
  251. const char* pLineSave = pBits;
  252. // ajw - Should copy DWORDs here instead for speed.
  253. for( int y = 0; y != hRect; y++ )
  254. {
  255. char* pSurf = pLineSurf;
  256. const char* pSave = pLineSave;
  257. for( int x = 0; x != wRect; x++ )
  258. *pSurf++ = *pSave++;
  259. pLineSurf += iPitchSurf;
  260. pLineSave += wRect;
  261. }
  262. draw_window.Unlock();
  263. return true;
  264. }
  265. else
  266. {
  267. // debugprint( "RestoreSurfaceRect Could not lock surface.\n" );
  268. return false;
  269. }
  270. }
  271. #endif