microui.def 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. DEFINITION MODULE microui;
  2. (* Modula-2 port of microui v2.02 by rxi (MIT license).
  3. Rewrite of the C API in ISO Modula-2 for GNU Modula-2 (gm2 -fiso).
  4. Compilation (from modula2/):
  5. gm2 -fiso -I. -c microuiHelpers.mod
  6. gm2 -fiso -I. -c microui.mod
  7. GM2 notes:
  8. - GNU Modula-2 (GCC 16.0.1) only accepts procedure *types* whose formal
  9. parameter list is a single, unnamed type, e.g.
  10. PROCEDURE(VAR Args) : INTEGER
  11. Named and/or multiple parameters are rejected. The C callbacks taking
  12. several arguments are therefore called through small argument records
  13. (TextWidthArgs, FrameArgs).
  14. - The C `mu_Command` union is represented by a `BaseCommand` header plus
  15. fixed-layout record types; command buffers are accessed through
  16. `CAST(CommandPtr, ...)` to the relevant record type.
  17. *)
  18. FROM SYSTEM IMPORT ADDRESS, BYTE;
  19. CONST
  20. VERSION = '2.02';
  21. COMMANDLIST_SIZE = 262144; (* 256 * 1024 *)
  22. ROOTLIST_SIZE = 32;
  23. CONTAINERSTACK_SIZE = 32;
  24. CLIPSTACK_SIZE = 32;
  25. IDSTACK_SIZE = 32;
  26. LAYOUTSTACK_SIZE = 16;
  27. CONTAINERPOOL_SIZE = 48;
  28. TREENODEPOOL_SIZE = 48;
  29. MAX_WIDTHS = 16;
  30. MAX_FMT = 127;
  31. (* Clip results *)
  32. CLIP_PART = 1;
  33. CLIP_ALL = 2;
  34. (* Command types *)
  35. COMMAND_JUMP = 1;
  36. COMMAND_CLIP = 2;
  37. COMMAND_RECT = 3;
  38. COMMAND_TEXT = 4;
  39. COMMAND_ICON = 5;
  40. COMMAND_MAX = 6;
  41. (* Color IDs *)
  42. COLOR_TEXT = 0;
  43. COLOR_BORDER = 1;
  44. COLOR_WINDOWBG = 2;
  45. COLOR_TITLEBG = 3;
  46. COLOR_TITLETEXT = 4;
  47. COLOR_PANELBG = 5;
  48. COLOR_BUTTON = 6;
  49. COLOR_BUTTONHOVER = 7;
  50. COLOR_BUTTONFOCUS = 8;
  51. COLOR_BASE = 9;
  52. COLOR_BASEHOVER = 10;
  53. COLOR_BASEFOCUS = 11;
  54. COLOR_SCROLLBASE = 12;
  55. COLOR_SCROLLTHUMB = 13;
  56. COLOR_MAX = 14;
  57. (* Icons *)
  58. ICON_CLOSE = 1;
  59. ICON_CHECK = 2;
  60. ICON_COLLAPSED = 3;
  61. ICON_EXPANDED = 4;
  62. ICON_MAX = 5;
  63. (* Result flags *)
  64. RES_ACTIVE = 1;
  65. RES_SUBMIT = 2;
  66. RES_CHANGE = 4;
  67. (* Option flags - one bit each *)
  68. OPT_ALIGNCENTER = 1;
  69. OPT_ALIGNRIGHT = 2;
  70. OPT_NOINTERACT = 4;
  71. OPT_NOFRAME = 8;
  72. OPT_NORESIZE = 16;
  73. OPT_NOSCROLL = 32;
  74. OPT_NOCLOSE = 64;
  75. OPT_NOTITLE = 128;
  76. OPT_HOLDFOCUS = 256;
  77. OPT_AUTOSIZE = 512;
  78. OPT_POPUP = 1024;
  79. OPT_CLOSED = 2048;
  80. OPT_EXPANDED = 4096;
  81. (* Mouse buttons *)
  82. MOUSE_LEFT = 1;
  83. MOUSE_RIGHT = 2;
  84. MOUSE_MIDDLE = 4;
  85. (* Key flags *)
  86. KEY_SHIFT = 1;
  87. KEY_CTRL = 2;
  88. KEY_ALT = 4;
  89. KEY_BACKSPACE = 8;
  90. KEY_RETURN = 16;
  91. (* mu_Real formatting, matching MU_REAL_FMT / MU_SLIDER_FMT *)
  92. REAL_FMT = '%.3g';
  93. SLIDER_FMT = '%.2f';
  94. TYPE
  95. Id = CARDINAL;
  96. Real = SHORTREAL;
  97. Font = ADDRESS;
  98. IntPtr = POINTER TO INTEGER;
  99. RealPtr = POINTER TO Real;
  100. Vec2 = RECORD
  101. x, y : INTEGER;
  102. END;
  103. Rect = RECORD
  104. x, y, w, h : INTEGER;
  105. END;
  106. Color = RECORD
  107. r, g, b, a : BYTE;
  108. END;
  109. PoolItem = RECORD
  110. id : Id;
  111. lastUpdate : INTEGER;
  112. END;
  113. (* --- command records (mirror the C structs, no `str` field in TextCommand) *)
  114. BaseCommand = RECORD
  115. type, size : INTEGER;
  116. END;
  117. JumpCommand = RECORD
  118. base : BaseCommand;
  119. dst : ADDRESS;
  120. END;
  121. ClipCommand = RECORD
  122. base : BaseCommand;
  123. rect : Rect;
  124. END;
  125. RectCommand = RECORD
  126. base : BaseCommand;
  127. rect : Rect;
  128. color : Color;
  129. END;
  130. TextCommand = RECORD
  131. base : BaseCommand;
  132. font : Font;
  133. pos : Vec2;
  134. color : Color;
  135. (* the NUL-terminated string follows at offset SIZE(TextCommand) *)
  136. END;
  137. IconCommand = RECORD
  138. base : BaseCommand;
  139. rect : Rect;
  140. id : INTEGER;
  141. color : Color;
  142. END;
  143. CommandPtr = POINTER TO BaseCommand;
  144. Layout = RECORD
  145. body : Rect;
  146. next : Rect;
  147. position : Vec2;
  148. size : Vec2;
  149. max : Vec2;
  150. widths : ARRAY [0..MAX_WIDTHS - 1] OF INTEGER;
  151. items : INTEGER;
  152. itemIndex : INTEGER;
  153. nextRow : INTEGER;
  154. nextType : INTEGER;
  155. indent : INTEGER;
  156. END;
  157. Container = RECORD
  158. head, tail : CommandPtr;
  159. rect : Rect;
  160. body : Rect;
  161. contentSize : Vec2;
  162. scroll : Vec2;
  163. zindex : INTEGER;
  164. open : INTEGER;
  165. END;
  166. ContainerPtr = POINTER TO Container;
  167. Style = RECORD
  168. font : Font;
  169. size : Vec2;
  170. padding : INTEGER;
  171. spacing : INTEGER;
  172. indent : INTEGER;
  173. titleHeight : INTEGER;
  174. scrollbarSize : INTEGER;
  175. thumbSize : INTEGER;
  176. colors : ARRAY [0..COLOR_MAX - 1] OF Color;
  177. END;
  178. (* --- callbacks ----------------------------------------------------------
  179. The context is carried as ADDRESS to keep the argument records free of a
  180. forward reference to ContextPtr. *)
  181. TextWidthArgs = RECORD
  182. ctx : ADDRESS;
  183. font : Font;
  184. str : ADDRESS;
  185. len : INTEGER;
  186. END;
  187. TextHeightArgs = RECORD
  188. ctx : ADDRESS;
  189. font : Font;
  190. END;
  191. FrameArgs = RECORD
  192. ctx : ADDRESS;
  193. rect : Rect;
  194. colorid : INTEGER;
  195. END;
  196. TextWidthProc = PROCEDURE(VAR TextWidthArgs) : INTEGER;
  197. TextHeightProc = PROCEDURE(VAR TextHeightArgs) : INTEGER;
  198. DrawFrameProc = PROCEDURE(VAR FrameArgs);
  199. ContainerStack = RECORD
  200. idx : INTEGER;
  201. items : ARRAY [0..CONTAINERSTACK_SIZE - 1] OF ContainerPtr;
  202. END;
  203. ClipStack = RECORD
  204. idx : INTEGER;
  205. items : ARRAY [0..CLIPSTACK_SIZE - 1] OF Rect;
  206. END;
  207. IdStack = RECORD
  208. idx : INTEGER;
  209. items : ARRAY [0..IDSTACK_SIZE - 1] OF Id;
  210. END;
  211. LayoutStack = RECORD
  212. idx : INTEGER;
  213. items : ARRAY [0..LAYOUTSTACK_SIZE - 1] OF Layout;
  214. END;
  215. RootStack = RECORD
  216. idx : INTEGER;
  217. items : ARRAY [0..ROOTLIST_SIZE - 1] OF ContainerPtr;
  218. END;
  219. CommandStack = RECORD
  220. idx : INTEGER;
  221. items : ARRAY [0..COMMANDLIST_SIZE - 1] OF CHAR;
  222. END;
  223. Context = RECORD
  224. (* callbacks *)
  225. textWidth : TextWidthProc;
  226. textHeight : TextHeightProc;
  227. drawFrame : DrawFrameProc;
  228. (* core state *)
  229. _style : Style;
  230. style : POINTER TO Style;
  231. hover : Id;
  232. focus : Id;
  233. lastId : Id;
  234. lastRect : Rect;
  235. lastZindex : INTEGER;
  236. updatedFocus : INTEGER;
  237. frame : INTEGER;
  238. hoverRoot : ContainerPtr;
  239. nextHoverRoot : ContainerPtr;
  240. scrollTarget : ContainerPtr;
  241. numberEditBuf : ARRAY [0..MAX_FMT] OF CHAR;
  242. numberEdit : Id;
  243. (* stacks *)
  244. commandList : CommandStack;
  245. rootList : RootStack;
  246. containerStack : ContainerStack;
  247. clipStack : ClipStack;
  248. idStack : IdStack;
  249. layoutStack : LayoutStack;
  250. (* retained state pools *)
  251. containerPool : ARRAY [0..CONTAINERPOOL_SIZE - 1] OF PoolItem;
  252. containers : ARRAY [0..CONTAINERPOOL_SIZE - 1] OF Container;
  253. treenodePool : ARRAY [0..TREENODEPOOL_SIZE - 1] OF PoolItem;
  254. (* input state *)
  255. mousePos : Vec2;
  256. lastMousePos : Vec2;
  257. mouseDelta : Vec2;
  258. scrollDelta : Vec2;
  259. mouseDown : INTEGER;
  260. mousePressed : INTEGER;
  261. keyDown : INTEGER;
  262. keyPressed : INTEGER;
  263. inputText : ARRAY [0..31] OF CHAR;
  264. END;
  265. ContextPtr = POINTER TO Context;
  266. (* ======================================================================== *)
  267. (* Constructors *)
  268. (* ======================================================================== *)
  269. PROCEDURE vec2(x, y : INTEGER) : Vec2;
  270. PROCEDURE rect(x, y, w, h : INTEGER) : Rect;
  271. PROCEDURE color(r, g, b, a : INTEGER) : Color;
  272. (* ======================================================================== *)
  273. (* Core *)
  274. (* ======================================================================== *)
  275. PROCEDURE init(ctx : ContextPtr);
  276. PROCEDURE begin(ctx : ContextPtr);
  277. PROCEDURE end(ctx : ContextPtr);
  278. PROCEDURE setFocus(ctx : ContextPtr; id : Id);
  279. PROCEDURE getId(ctx : ContextPtr; data : ADDRESS; size : INTEGER) : Id;
  280. PROCEDURE pushId(ctx : ContextPtr; data : ADDRESS; size : INTEGER);
  281. PROCEDURE popId(ctx : ContextPtr);
  282. PROCEDURE pushClipRect(ctx : ContextPtr; r : Rect);
  283. PROCEDURE popClipRect(ctx : ContextPtr);
  284. PROCEDURE getClipRect(ctx : ContextPtr) : Rect;
  285. PROCEDURE checkClip(ctx : ContextPtr; r : Rect) : INTEGER;
  286. PROCEDURE getCurrentContainer(ctx : ContextPtr) : ContainerPtr;
  287. PROCEDURE getContainer(ctx : ContextPtr; name : ARRAY OF CHAR) : ContainerPtr;
  288. PROCEDURE bringToFront(ctx : ContextPtr; cnt : ContainerPtr);
  289. (* ======================================================================== *)
  290. (* Pool *)
  291. (* ======================================================================== *)
  292. PROCEDURE poolInit(ctx : ContextPtr; items : ADDRESS; len : INTEGER;
  293. id : Id) : INTEGER;
  294. PROCEDURE poolGet(ctx : ContextPtr; items : ADDRESS; len : INTEGER;
  295. id : Id) : INTEGER;
  296. PROCEDURE poolUpdate(ctx : ContextPtr; items : ADDRESS; idx : INTEGER);
  297. (* ======================================================================== *)
  298. (* Input *)
  299. (* ======================================================================== *)
  300. PROCEDURE inputMousemove(ctx : ContextPtr; x, y : INTEGER);
  301. PROCEDURE inputMousedown(ctx : ContextPtr; x, y, btn : INTEGER);
  302. PROCEDURE inputMouseup(ctx : ContextPtr; x, y, btn : INTEGER);
  303. PROCEDURE inputScroll(ctx : ContextPtr; x, y : INTEGER);
  304. PROCEDURE inputKeydown(ctx : ContextPtr; key : INTEGER);
  305. PROCEDURE inputKeyup(ctx : ContextPtr; key : INTEGER);
  306. PROCEDURE inputText(ctx : ContextPtr; text : ARRAY OF CHAR);
  307. (* ======================================================================== *)
  308. (* Command list *)
  309. (* ======================================================================== *)
  310. PROCEDURE pushCommand(ctx : ContextPtr; type, size : INTEGER) : CommandPtr;
  311. PROCEDURE nextCommand(ctx : ContextPtr; VAR cmd : CommandPtr) : INTEGER;
  312. PROCEDURE setClip(ctx : ContextPtr; r : Rect);
  313. PROCEDURE drawRect(ctx : ContextPtr; r : Rect; color : Color);
  314. PROCEDURE drawBox(ctx : ContextPtr; r : Rect; color : Color);
  315. PROCEDURE drawText(ctx : ContextPtr; font : Font; str : ADDRESS; len : INTEGER;
  316. pos : Vec2; color : Color);
  317. PROCEDURE drawIcon(ctx : ContextPtr; id : INTEGER; r : Rect; color : Color);
  318. (* ======================================================================== *)
  319. (* Layout *)
  320. (* ======================================================================== *)
  321. PROCEDURE layoutRow(ctx : ContextPtr; items : INTEGER;
  322. widths : ADDRESS; height : INTEGER);
  323. PROCEDURE layoutWidth(ctx : ContextPtr; width : INTEGER);
  324. PROCEDURE layoutHeight(ctx : ContextPtr; height : INTEGER);
  325. PROCEDURE layoutBeginColumn(ctx : ContextPtr);
  326. PROCEDURE layoutEndColumn(ctx : ContextPtr);
  327. PROCEDURE layoutSetNext(ctx : ContextPtr; r : Rect; relative : INTEGER);
  328. PROCEDURE layoutNext(ctx : ContextPtr) : Rect;
  329. (* ======================================================================== *)
  330. (* Controls *)
  331. (* ======================================================================== *)
  332. PROCEDURE drawControlFrame(ctx : ContextPtr; id : Id; r : Rect;
  333. colorid, opt : INTEGER);
  334. PROCEDURE drawControlText(ctx : ContextPtr; str : ARRAY OF CHAR;
  335. r : Rect; colorid, opt : INTEGER);
  336. PROCEDURE mouseOver(ctx : ContextPtr; r : Rect) : INTEGER;
  337. PROCEDURE updateControl(ctx : ContextPtr; id : Id; r : Rect; opt : INTEGER);
  338. (* ======================================================================== *)
  339. (* Widgets *)
  340. (* ======================================================================== *)
  341. PROCEDURE text(ctx : ContextPtr; str : ARRAY OF CHAR);
  342. PROCEDURE label(ctx : ContextPtr; str : ARRAY OF CHAR);
  343. PROCEDURE buttonEx(ctx : ContextPtr; label : ARRAY OF CHAR;
  344. icon, opt : INTEGER) : INTEGER;
  345. PROCEDURE checkbox(ctx : ContextPtr; label : ARRAY OF CHAR;
  346. state : IntPtr) : INTEGER;
  347. PROCEDURE textboxRaw(ctx : ContextPtr; buf : ADDRESS; bufsz : INTEGER;
  348. id : Id; r : Rect; opt : INTEGER) : INTEGER;
  349. PROCEDURE textboxEx(ctx : ContextPtr; buf : ADDRESS;
  350. bufsz, opt : INTEGER) : INTEGER;
  351. PROCEDURE sliderEx(ctx : ContextPtr; value : RealPtr;
  352. low, high, step : Real;
  353. fmt : ARRAY OF CHAR; opt : INTEGER) : INTEGER;
  354. PROCEDURE numberEx(ctx : ContextPtr; value : RealPtr;
  355. step : Real; fmt : ARRAY OF CHAR;
  356. opt : INTEGER) : INTEGER;
  357. PROCEDURE headerEx(ctx : ContextPtr; label : ARRAY OF CHAR;
  358. opt : INTEGER) : INTEGER;
  359. PROCEDURE beginTreenodeEx(ctx : ContextPtr; label : ARRAY OF CHAR;
  360. opt : INTEGER) : INTEGER;
  361. PROCEDURE endTreenode(ctx : ContextPtr);
  362. PROCEDURE beginWindowEx(ctx : ContextPtr; title : ARRAY OF CHAR;
  363. r : Rect; opt : INTEGER) : INTEGER;
  364. PROCEDURE endWindow(ctx : ContextPtr);
  365. PROCEDURE openPopup(ctx : ContextPtr; name : ARRAY OF CHAR);
  366. PROCEDURE beginPopup(ctx : ContextPtr; name : ARRAY OF CHAR) : INTEGER;
  367. PROCEDURE endPopup(ctx : ContextPtr);
  368. PROCEDURE beginPanelEx(ctx : ContextPtr; name : ARRAY OF CHAR;
  369. opt : INTEGER);
  370. PROCEDURE endPanel(ctx : ContextPtr);
  371. (* ======================================================================== *)
  372. (* Convenience wrappers (the C macros) *)
  373. (* ======================================================================== *)
  374. PROCEDURE button(ctx : ContextPtr; label : ARRAY OF CHAR) : INTEGER;
  375. PROCEDURE textbox(ctx : ContextPtr; buf : ADDRESS; bufsz : INTEGER) : INTEGER;
  376. PROCEDURE slider(ctx : ContextPtr; value : RealPtr;
  377. low, high : Real) : INTEGER;
  378. PROCEDURE number(ctx : ContextPtr; value : RealPtr;
  379. step : Real) : INTEGER;
  380. PROCEDURE header(ctx : ContextPtr; label : ARRAY OF CHAR) : INTEGER;
  381. PROCEDURE beginTreenode(ctx : ContextPtr; label : ARRAY OF CHAR) : INTEGER;
  382. PROCEDURE beginWindow(ctx : ContextPtr; title : ARRAY OF CHAR;
  383. r : Rect) : INTEGER;
  384. PROCEDURE beginPanel(ctx : ContextPtr; name : ARRAY OF CHAR);
  385. END microui.