2
0

HotKeysCommands.Mod 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. MODULE HotKeysCommands; (** AUTHOR "staubesv"; PURPOSE "Useful hot key commands"; *)
  2. (**
  3. * This modules contains some commands considered being useful for execution via hot keys.
  4. *
  5. * Overview/Usage:
  6. *
  7. * HotKeysCommands.SimulateMouse MouseX|MouseY|MouseButtons|MouseWheel value ~ generates mouse messages
  8. *
  9. * HotKeysCommands.EnterCommand ~ opens a window that queries a command string that is executed on enter
  10. * HotKeysCommands.ClearLog ~ clears the kernel log
  11. *
  12. *)
  13. IMPORT
  14. SYSTEM,
  15. KernelLog, Modules, Commands, Streams, Strings,
  16. Inputs, KernelLogger,
  17. WMWindowManager, WMComponents, WMEditors, WMGraphics;
  18. CONST
  19. (* Command window constants *)
  20. DefaultWidth = 400; DefaultHeight = 40;
  21. DefaultTextColor = WMGraphics.White;
  22. DefaultTextBgColor = 00008080H;
  23. (* Mouse simulator constants *)
  24. MouseX = "MouseX";
  25. MouseY = "MouseY";
  26. MouseButtons = "MouseButtons";
  27. MouseWheel = "MouseWheel";
  28. TYPE
  29. (* Window that queries a command string and executes it when pressing enter. Pressing the escape key closes the window. The
  30. * window is also closed when it looses the focus *)
  31. Window = OBJECT (WMComponents.FormWindow)
  32. VAR
  33. editor : WMEditors.Editor;
  34. PROCEDURE HandleEnter(sender, data : ANY);
  35. VAR commandString : ARRAY 4096 OF CHAR; msg : ARRAY 128 OF CHAR; res : LONGINT;
  36. BEGIN
  37. editor.GetAsString(commandString);
  38. IF commandString # "" THEN
  39. Commands.Call(commandString, {}, res, msg);
  40. IF res # Commands.Ok THEN
  41. KernelLog.String("HotKeysCommands: Failed to execute '"); KernelLog.String(commandString);
  42. KernelLog.String("', res: "); KernelLog.Int(res, 0);
  43. KernelLog.String(" ("); KernelLog.String(msg); KernelLog.String(")");
  44. KernelLog.Ln;
  45. END;
  46. END;
  47. Close;
  48. END HandleEnter;
  49. PROCEDURE HandleEscape(sender, data : ANY);
  50. BEGIN
  51. Close;
  52. END HandleEscape;
  53. PROCEDURE FocusLost*;
  54. BEGIN
  55. Close;
  56. END FocusLost;
  57. PROCEDURE Close*;
  58. BEGIN
  59. Close^; window := NIL;
  60. END Close;
  61. PROCEDURE CreateForm() : WMComponents.VisualComponent;
  62. BEGIN
  63. NEW(editor);
  64. editor.alignment.Set(WMComponents.AlignClient);
  65. editor.onEnter.Add(HandleEnter);
  66. editor.onEscape.Add(HandleEscape);
  67. editor.allowScrollbars.Set(FALSE);
  68. editor.multiLine.Set(FALSE);
  69. editor.tv.textAlignV.Set(WMGraphics.AlignCenter);
  70. editor.tv.defaultTextColor.Set(DefaultTextColor);
  71. editor.tv.defaultTextBgColor.Set(0);
  72. editor.fillColor.Set(DefaultTextBgColor);
  73. RETURN editor;
  74. END CreateForm;
  75. PROCEDURE &New*;
  76. VAR manager : WMWindowManager.WindowManager;
  77. BEGIN
  78. Init(DefaultWidth, DefaultHeight, TRUE);
  79. SetContent(CreateForm());
  80. SetTitle(Strings.NewString("Enter command: "));
  81. WMWindowManager.ExtAddWindow(SELF, 200, 100, {});
  82. manager := WMWindowManager.GetDefaultManager();
  83. manager.SetFocus(SELF);
  84. editor.SetFocus;
  85. END New;
  86. END Window;
  87. VAR
  88. window : Window;
  89. (** Generate mouse message *)
  90. PROCEDURE SimulateMouse*(context : Commands.Context); (** MouseX|MouseY|MouseButtons|MouseWheel value ~ *)
  91. VAR
  92. string : ARRAY 32 OF CHAR; value : LONGINT;
  93. msg : Inputs.MouseMsg;
  94. doHandle : BOOLEAN;
  95. BEGIN
  96. IF context.arg.GetString(string) THEN
  97. IF context.arg.GetInteger(value, FALSE) THEN
  98. doHandle := TRUE;
  99. IF Strings.Match(string, MouseX) THEN msg.dx := value;
  100. ELSIF Strings.Match(string, MouseY) THEN msg.dy := value;
  101. ELSIF Strings.Match(string, MouseWheel) THEN msg.dz := value;
  102. ELSIF Strings.Match(string, MouseButtons) THEN msg.keys := SYSTEM.VAL(SET, value);
  103. ELSE
  104. doHandle := FALSE;
  105. END;
  106. IF doHandle THEN Inputs.mouse.Handle(msg); END;
  107. END;
  108. END;
  109. END SimulateMouse;
  110. (** Opens a window that queries a command string and executes it on enter *)
  111. PROCEDURE EnterCommand*; (** ~ *)
  112. BEGIN {EXCLUSIVE}
  113. IF window = NIL THEN NEW(window); END;
  114. END EnterCommand;
  115. (** Clear kernel log *)
  116. PROCEDURE ClearLog*; (** ~ *)
  117. BEGIN
  118. KernelLogger.kernelLog.AcquireWrite;
  119. KernelLogger.kernelLog.Delete(0, KernelLogger.kernelLog.GetLength());
  120. KernelLogger.kernelLog.ReleaseWrite;
  121. END ClearLog;
  122. PROCEDURE Cleanup;
  123. BEGIN {EXCLUSIVE}
  124. IF window # NIL THEN window.Close; window := NIL; END;
  125. END Cleanup;
  126. BEGIN
  127. Modules.InstallTermHandler(Cleanup);
  128. END HotKeysCommands.
  129. HotKeysCommands.ClearLog ~
  130. HotKeysCommands.EnterCommand ~
  131. HotKeysCommands.SimulateMouse MouseWheel -3 ~
  132. SystemTools.Free HotKeysCommands ~