Problem with cursor position

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Problem with cursor position

      Hi all,
      I am using edit widget for keypad and i have aligned it to the right.
      Problem what i am facing is since it is right aligned cursor will be in the right most pixel, so now if i want to type something i can move the cursor anywhere but it is very difficult to move cursor to the right most place i.e after the last character one some characters are typed.
      Do we have some API where i can set the default position of cursor along with right alignment.like if edit widget is of size 200 then cursor should not come to the right most pixel it should be little left. may be at 215 or something so always there will be some place to touch on right side.

      Thanks
      kusum.
    • Hi,

      Unfortunately, this is not possible with the default API.

      But you can do it with a little bit of work on your own (please refer to the example).

      I have created a window which works as a container for the EDIT widget. This window is slightly larger (to the right) than the EDIT widget. If the user click into this overlapping area the cursor of the EDIT widget gets set to the right most position.

      Since the EDIT widget will become a child of this container window there are some other points which need to be taken care of. For example wouldn't it be possible to receive messages from the EDIT widget from other positions as within the container window. For this reason the container window routes all messages to its parent.

      Here is the code:

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Externals
      5. *
      6. **********************************************************************
      7. */
      8. /*********************************************************************
      9. *
      10. * Types
      11. *
      12. **********************************************************************
      13. */
      14. /*********************************************************************
      15. *
      16. * Defines
      17. *
      18. **********************************************************************
      19. */
      20. #define CLICK_AREA_SIZE 10
      21. #define EFFECT_SIZE 1
      22. #define ID_EDIT_WIN (GUI_ID_USER + 0x00)
      23. /*********************************************************************
      24. *
      25. * Static data
      26. *
      27. **********************************************************************
      28. */
      29. /*********************************************************************
      30. *
      31. * Static code
      32. *
      33. **********************************************************************
      34. */
      35. /*********************************************************************
      36. *
      37. * _cbEditWin
      38. */
      39. static void _cbEditWin(WM_MESSAGE * pMsg) {
      40. WM_PID_STATE_CHANGED_INFO * pInfo;
      41. WM_HWIN hEdit;
      42. GUI_RECT Rect;
      43. int xSize;
      44. int ySize;
      45. int NumChars;
      46. switch (pMsg->MsgId) {
      47. case WM_CREATE:
      48. //
      49. // Calculate the size of EDIT widget, per default the EDIT widget has black frame.
      50. // Since the container window should define the new border of the EDIT, ww have to
      51. // draw the frame manually. For this reasion we have to reduce the size of the EDIT
      52. // widget. Otherwise it would cover the drawn frame and it won'T become visible.
      53. //
      54. // frame size | clickable area
      55. xSize = WM_GetWindowSizeX(pMsg->hWin) - EFFECT_SIZE - CLICK_AREA_SIZE;
      56. ySize = WM_GetWindowSizeY(pMsg->hWin) - EFFECT_SIZE * 2;
      57. hEdit = EDIT_CreateEx(EFFECT_SIZE, EFFECT_SIZE, xSize, ySize, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_EDIT0, 40);
      58. //
      59. // Set text align
      60. //
      61. EDIT_SetTextAlign(hEdit, GUI_TA_RIGHT | GUI_TA_VCENTER);
      62. //
      63. // Remove widget effect, it gets drawn on our own wihtin WM_PAINT of this window.
      64. //
      65. WIDGET_SetEffect(hEdit, &WIDGET_Effect_None);
      66. break;
      67. case WM_GET_ID:
      68. //
      69. // Return an ID, important for getting the EDIT handle.
      70. //
      71. pMsg->Data.v = ID_EDIT_WIN;
      72. break;
      73. case WM_PAINT:
      74. //
      75. // Draw the effect
      76. //
      77. GUI_SetBkColor(GUI_WHITE);
      78. GUI_Clear();
      79. WM_GetClientRect(&Rect);
      80. GUI_SetColor(GUI_BLACK);
      81. GUI_DrawRectEx(&Rect);
      82. break;
      83. case WM_PID_STATE_CHANGED:
      84. //
      85. // On WM_PID_STATE_CHANGE we check if the user has clicked this window set the
      86. // Cursor position to the left most position.
      87. //
      88. pInfo = (WM_PID_STATE_CHANGED_INFO *)pMsg->Data.p;
      89. if (pInfo) {
      90. if (pInfo->State == 1 && pInfo->StatePrev == 0) {
      91. xSize = WM_GetWindowSizeX(pMsg->hWin) - EFFECT_SIZE - CLICK_AREA_SIZE - 1;
      92. //
      93. // Do this only if it is in left area. Otherwise it would also happen
      94. // if the frame to the top and bottom of the widget would get clicked.
      95. //
      96. if (pInfo->x > xSize) {
      97. hEdit = WM_GetDialogItem(pMsg->hWin, GUI_ID_EDIT0);
      98. NumChars = EDIT_GetNumChars(hEdit);
      99. EDIT_SetCursorAtChar(hEdit, NumChars);
      100. }
      101. }
      102. }
      103. break;
      104. case WM_NOTIFY_PARENT:
      105. //
      106. // Route all notification of the EDIT widget (which is the only one who sends
      107. // such messages to this window) to the parent of this window.
      108. //
      109. WM_SendMessage(WM_GetParent(pMsg->hWin), pMsg);
      110. break;
      111. default:
      112. WM_DefaultProc(pMsg);
      113. break;
      114. }
      115. }
      116. /*********************************************************************
      117. *
      118. * _cbBk
      119. */
      120. static void _cbBk(WM_MESSAGE * pMsg) {
      121. WM_HWIN hItem;
      122. int NCode;
      123. int Id;
      124. static char acBuffer[40 + 1];
      125. switch (pMsg->MsgId) {
      126. case WM_PAINT:
      127. GUI_SetBkColor(GUI_BLACK);
      128. GUI_Clear();
      129. GUI_DispStringAt(acBuffer, 10, 40);
      130. break;
      131. case WM_NOTIFY_PARENT:
      132. //
      133. // React normaly to messages of the EDIT widget
      134. //
      135. Id = WM_GetId(pMsg->hWinSrc);
      136. NCode = pMsg->Data.v;
      137. switch (Id) {
      138. case GUI_ID_EDIT0:
      139. switch (NCode) {
      140. case WM_NOTIFICATION_VALUE_CHANGED:
      141. //
      142. // Get the EDIT handle. Here it is important that the container window
      143. // returns and ID. With this ID we can get its handle and further the
      144. // handle of the EDIT widget.
      145. //
      146. hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_WIN);
      147. hItem = WM_GetDialogItem(hItem, GUI_ID_EDIT0);
      148. //
      149. // Get the EDIT widgets content and invalidate this window to display the content.
      150. //
      151. EDIT_GetText(hItem, acBuffer, sizeof(acBuffer));
      152. WM_InvalidateWindow(pMsg->hWin);
      153. break;
      154. }
      155. break;
      156. }
      157. break;
      158. default:
      159. WM_DefaultProc(pMsg);
      160. break;
      161. }
      162. }
      163. /*********************************************************************
      164. *
      165. * Public code
      166. *
      167. **********************************************************************
      168. */
      169. /*********************************************************************
      170. *
      171. * MainTask
      172. */
      173. void MainTask(void) {
      174. GUI_Init();
      175. WM_MULTIBUF_Enable(1);
      176. WM_SetCallback(WM_HBKWIN, _cbBk);
      177. //
      178. // Create a window as some sort of container, it contains the EDIT widget.
      179. // This window gets created instead of the EDIT Widget.
      180. //
      181. WM_CreateWindowAsChild(10, 10, 200, 20, WM_HBKWIN, WM_CF_SHOW, _cbEditWin, 0);
      182. while (1) {
      183. GUI_Delay(100);
      184. }
      185. }
      186. /*************************** End of file ****************************/
      Display All
      Regards,
      Sven
      Files
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.