Text Editor.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // A simple text editor example
  2. #define USE_TI89 // Compile for TI-89
  3. #define USE_TI92PLUS // Compile for TI-92 Plus
  4. #define USE_V200 // Compile for V200
  5. #define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
  6. #define MIN_AMS 100 // Compile for AMS 1.00 or higher
  7. #define SAVE_SCREEN // Save/Restore LCD Contents
  8. #include <tigcclib.h> // Include All Header Files
  9. TEXT_EDIT te;
  10. // Typical event handler for text editing
  11. CALLBACK void EventHandler(EVENT *ev)
  12. {
  13. if (ev->Type == CM_KEYPRESS && ev->extra.Key.Code == KEY_ESC)
  14. ER_throw (1);
  15. if (!TE_handleEvent (&te, ev))
  16. EV_defaultHandler (ev);
  17. }
  18. // Main Function
  19. void _main(void)
  20. {
  21. char *base_addr;
  22. SYM_ENTRY *sym = SymFindPtr (SYMSTR ("mytext"), 0);
  23. if (!sym) return; // Exit if file not found...
  24. // First, you need to remove the garbage data at the begining of
  25. // the text variable, because the text editor expects raw data:
  26. base_addr = HeapDeref (sym->handle);
  27. memmove (base_addr, base_addr + 4, peek_w(base_addr));
  28. // Now, do the editing. This is straightforward...
  29. WinClr (DeskTop);
  30. TE_open (&te, DeskTop, MakeWinRect (0, 16, 159, 92), sym->handle, 1, 0, 7);
  31. CU_start ();
  32. EV_captureEvents (EventHandler);
  33. TRY
  34. EV_eventLoop ();
  35. ONERR
  36. EV_captureEvents (NULL);
  37. ENDTRY
  38. // Finally, you must transform raw editor data into the proper
  39. // format of the text variable. This is not so straightforward:
  40. base_addr = HeapDeref (HeapRealloc (sym->handle, te.CurSize + 10));
  41. memmove (base_addr + 4, base_addr, te.CurSize);
  42. poke_w (base_addr, te.CurSize + 4);
  43. poke_w (base_addr + 2, te.CursorOffset);
  44. poke (base_addr + te.CurSize + 4, 0);
  45. poke (base_addr + te.CurSize + 5, TEXT_TAG);
  46. }