Misc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /** @file
  2. Implementation of various string and line routines
  3. Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved. <BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "HexEditor.h"
  7. extern BOOLEAN HEditorMouseAction;
  8. /**
  9. Free a line and it's internal buffer.
  10. @param[in] Src The line to be freed.
  11. **/
  12. VOID
  13. HLineFree (
  14. IN HEFI_EDITOR_LINE *Src
  15. )
  16. {
  17. if (Src == NULL) {
  18. return ;
  19. }
  20. SHELL_FREE_NON_NULL (Src);
  21. }
  22. /**
  23. Advance to the next Count lines.
  24. @param[in] Count The line number to advance.
  25. @retval NULL An error occurred.
  26. @return A pointer to the line after advance.
  27. **/
  28. HEFI_EDITOR_LINE *
  29. HLineAdvance (
  30. IN UINTN Count
  31. )
  32. {
  33. UINTN Index;
  34. HEFI_EDITOR_LINE *Line;
  35. Line = HMainEditor.BufferImage->CurrentLine;
  36. if (Line == NULL) {
  37. return NULL;
  38. }
  39. for (Index = 0; Index < Count; Index++) {
  40. //
  41. // if already last line
  42. //
  43. if (Line->Link.ForwardLink == HMainEditor.BufferImage->ListHead) {
  44. return NULL;
  45. }
  46. Line = CR (Line->Link.ForwardLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  47. }
  48. return Line;
  49. }
  50. /**
  51. Retreat to the previous Count lines.
  52. @param[in] Count The line number to retreat.
  53. @retval NULL An error occurred.
  54. @return A pointer to the line after retreat.
  55. **/
  56. HEFI_EDITOR_LINE *
  57. HLineRetreat (
  58. IN UINTN Count
  59. )
  60. {
  61. UINTN Index;
  62. HEFI_EDITOR_LINE *Line;
  63. Line = HMainEditor.BufferImage->CurrentLine;
  64. if (Line == NULL) {
  65. return NULL;
  66. }
  67. for (Index = 0; Index < Count; Index++) {
  68. //
  69. // already the first line
  70. //
  71. if (Line->Link.BackLink == HMainEditor.BufferImage->ListHead) {
  72. return NULL;
  73. }
  74. Line = CR (Line->Link.BackLink, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  75. }
  76. return Line;
  77. }
  78. /**
  79. Advance/Retreat lines.
  80. @param[in] Count The line number to advance/retreat.
  81. >0 : advance
  82. <0: retreat
  83. @retval NULL An error occurred.
  84. @return A pointer to the line after move.
  85. **/
  86. HEFI_EDITOR_LINE *
  87. HMoveLine (
  88. IN INTN Count
  89. )
  90. {
  91. HEFI_EDITOR_LINE *Line;
  92. UINTN AbsCount;
  93. //
  94. // difference with MoveCurrentLine
  95. // just return Line
  96. // do not set currentline to Line
  97. //
  98. if (Count <= 0) {
  99. AbsCount = (UINTN)ABS(Count);
  100. Line = HLineRetreat (AbsCount);
  101. } else {
  102. Line = HLineAdvance ((UINTN)Count);
  103. }
  104. return Line;
  105. }
  106. /**
  107. Advance/Retreat lines and set CurrentLine in BufferImage to it.
  108. @param[in] Count The line number to advance/retreat.
  109. >0 : advance
  110. <0: retreat
  111. @retval NULL An error occurred.
  112. @return A pointer to the line after move.
  113. **/
  114. HEFI_EDITOR_LINE *
  115. HMoveCurrentLine (
  116. IN INTN Count
  117. )
  118. {
  119. HEFI_EDITOR_LINE *Line;
  120. UINTN AbsCount;
  121. //
  122. // <0: retreat
  123. // >0: advance
  124. //
  125. if (Count <= 0) {
  126. AbsCount = (UINTN)ABS(Count);
  127. Line = HLineRetreat (AbsCount);
  128. } else {
  129. Line = HLineAdvance ((UINTN)Count);
  130. }
  131. if (Line == NULL) {
  132. return NULL;
  133. }
  134. HMainEditor.BufferImage->CurrentLine = Line;
  135. return Line;
  136. }
  137. /**
  138. Free all the lines in HBufferImage.
  139. Fields affected:
  140. Lines
  141. CurrentLine
  142. NumLines
  143. ListHead
  144. @param[in] ListHead The list head.
  145. @param[in] Lines The lines.
  146. @retval EFI_SUCCESS The operation was successful.
  147. **/
  148. EFI_STATUS
  149. HFreeLines (
  150. IN LIST_ENTRY *ListHead,
  151. IN HEFI_EDITOR_LINE *Lines
  152. )
  153. {
  154. LIST_ENTRY *Link;
  155. HEFI_EDITOR_LINE *Line;
  156. //
  157. // release all the lines
  158. //
  159. if (Lines != NULL) {
  160. Line = Lines;
  161. Link = &(Line->Link);
  162. do {
  163. Line = CR (Link, HEFI_EDITOR_LINE, Link, EFI_EDITOR_LINE_LIST);
  164. Link = Link->ForwardLink;
  165. HLineFree (Line);
  166. } while (Link != ListHead);
  167. }
  168. ListHead->ForwardLink = ListHead;
  169. ListHead->BackLink = ListHead;
  170. return EFI_SUCCESS;
  171. }
  172. /**
  173. Get the X information for the mouse.
  174. @param[in] GuidX The change.
  175. @return the new information.
  176. **/
  177. INT32
  178. HGetTextX (
  179. IN INT32 GuidX
  180. )
  181. {
  182. INT32 Gap;
  183. HMainEditor.MouseAccumulatorX += GuidX;
  184. Gap = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
  185. HMainEditor.MouseAccumulatorX = (HMainEditor.MouseAccumulatorX * (INT32) HMainEditor.ScreenSize.Column) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionX);
  186. HMainEditor.MouseAccumulatorX = HMainEditor.MouseAccumulatorX / (INT32) HMainEditor.ScreenSize.Column;
  187. return Gap;
  188. }
  189. /**
  190. Get the Y information for the mouse.
  191. @param[in] GuidY The change.
  192. @return the new information.
  193. **/
  194. INT32
  195. HGetTextY (
  196. IN INT32 GuidY
  197. )
  198. {
  199. INT32 Gap;
  200. HMainEditor.MouseAccumulatorY += GuidY;
  201. Gap = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) / (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
  202. HMainEditor.MouseAccumulatorY = (HMainEditor.MouseAccumulatorY * (INT32) HMainEditor.ScreenSize.Row) % (INT32) (50 * (INT32) HMainEditor.MouseInterface->Mode->ResolutionY);
  203. HMainEditor.MouseAccumulatorY = HMainEditor.MouseAccumulatorY / (INT32) HMainEditor.ScreenSize.Row;
  204. return Gap;
  205. }