gemwin.pas 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. {
  2. Copyright (c) 2017 Karoly Balogh
  3. Simple, resizable and movable GEM Window
  4. Example program for Free Pascal's Atari TOS bindings
  5. This example program is in the Public Domain under the terms of
  6. Unlicense: http://unlicense.org/
  7. **********************************************************************}
  8. {$APPTYPE GUI}
  9. {$MODESWITCH OUT+}
  10. {$WARN 3124 OFF}
  11. {$WARN 4055 OFF}
  12. program gemwin;
  13. uses
  14. aes, vdi;
  15. var
  16. win_h: smallint;
  17. win_name: PAnsiChar;
  18. win_info: PAnsiChar;
  19. vdi_h: smallint;
  20. const
  21. WIN_KIND = NAME or INFO or CLOSER or MOVER or SIZER or FULLER;
  22. function open_vwk: smallint;
  23. var
  24. work_in: array[0..16] of smallint;
  25. work_out: array[0..64] of smallint;
  26. dummy, i: smallint;
  27. handle: smallint;
  28. begin
  29. handle:=graf_handle(@dummy,@dummy,@dummy,@dummy);
  30. for i:=0 to 9 do work_in[i]:=1;
  31. work_in[10]:=2;
  32. v_opnvwk(@work_in, @handle, @work_out);
  33. open_vwk:=handle;
  34. end;
  35. function wind_get_grect(wh, what: smallint; rect: PGRECT): boolean;
  36. begin
  37. wind_get_grect:=wind_get(wh, what, @rect^.x, @rect^.y, @rect^.w, @rect^.h)<>0;
  38. end;
  39. function open_win: smallint;
  40. var
  41. handle: smallint;
  42. dim: TGRECT;
  43. begin
  44. handle:=wind_create(WIN_KIND, 0, 0, 0, 0);
  45. win_name:='FPC GEM Window';
  46. wind_set(handle, WF_NAME, hi(ptruint(win_name)), lo(ptruint(win_name)), 0, 0);
  47. win_info:='Move me and resize me...';
  48. wind_set(handle, WF_INFO, hi(ptruint(win_info)), lo(ptruint(win_info)), 0, 0);
  49. wind_get_grect(0, WF_WORKXYWH, @dim);
  50. dim.x:=dim.x + (dim.w div 20);
  51. dim.y:=dim.y + (dim.h div 20);
  52. dim.w:=dim.w - (dim.w div 20) * 2;
  53. dim.h:=dim.h - (dim.h div 20) * 2;
  54. wind_open(handle, dim.x, dim.y, dim.w, dim.h);
  55. open_win:=handle;
  56. end;
  57. procedure wind_set_grect(wh: smallint; rect: PGRECT);
  58. var
  59. fsrect: TGRECT;
  60. begin
  61. if rect = nil then
  62. begin
  63. wind_get_grect(0, WF_WORKXYWH, @fsrect);
  64. rect:=@fsrect;
  65. end;
  66. wind_set(wh,WF_CURRXYWH,rect^.x,rect^.y,rect^.w,rect^.h);
  67. end;
  68. function min(a, b: smallint): smallint;
  69. begin
  70. if a < b then
  71. min:=a
  72. else
  73. min:=b;
  74. end;
  75. function max(a, b: smallint): smallint;
  76. begin
  77. if a > b then
  78. max:=a
  79. else
  80. max:=b;
  81. end;
  82. function rc_intersect(p1: PGRECT; p2: PGRECT): boolean;
  83. var
  84. tx, ty, tw, th: smallint;
  85. begin
  86. tw:=min(p2^.x+p2^.w, p1^.x+p1^.w);
  87. th:=min(p2^.y+p2^.h, p1^.y+p1^.h);
  88. tx:=max(p2^.x, p1^.x);
  89. ty:=max(p2^.y, p1^.y);
  90. p2^.x:=tx;
  91. p2^.y:=ty;
  92. p2^.w:=tw-tx;
  93. p2^.h:=th-ty;
  94. rc_intersect:=(tw > tx) and (th > ty);
  95. end;
  96. procedure wind_redraw(wh: smallint; rect: PGRECT);
  97. var
  98. xyarray: array[0..3] of smallint;
  99. wrect: TGRECT;
  100. begin
  101. wind_update(BEG_UPDATE);
  102. v_hide_c(vdi_h);
  103. wind_get_grect(wh,WF_FIRSTXYWH,@wrect);
  104. while (wrect.w<>0) and (wrect.h<>0) do
  105. begin
  106. if rc_intersect(rect,@wrect) then
  107. begin
  108. xyarray[0]:=wrect.x;
  109. xyarray[1]:=wrect.y;
  110. xyarray[2]:=wrect.x+wrect.w-1;
  111. xyarray[3]:=wrect.y+wrect.h-1;
  112. vs_clip(vdi_h, 1, @xyarray);
  113. vsf_color(vdi_h,WHITE);
  114. v_bar(vdi_h,@xyarray);
  115. end;
  116. wind_get_grect(wh,WF_NEXTXYWH,@wrect);
  117. end;
  118. v_show_c(vdi_h,0);
  119. wind_update(END_UPDATE);
  120. end;
  121. procedure event_loop;
  122. var
  123. msg_buf: array[0..7] of smallint;
  124. begin
  125. graf_mouse(ARROW, nil);
  126. repeat
  127. evnt_mesag(@msg_buf);
  128. case msg_buf[0] of
  129. WM_CLOSED:
  130. break;
  131. WM_REDRAW:
  132. wind_redraw(win_h,PGRECT(@msg_buf[4]));
  133. WM_MOVED,
  134. WM_SIZED:
  135. wind_set_grect(win_h,PGRECT(@msg_buf[4]));
  136. WM_FULLED:
  137. wind_set_grect(win_h,nil);
  138. WM_TOPPED,WM_NEWTOP:
  139. wind_set(win_h,WF_TOP,0,0,0,0);
  140. end;
  141. until false;
  142. end;
  143. begin
  144. appl_init;
  145. vdi_h:=open_vwk;
  146. win_h:=open_win;
  147. event_loop;
  148. wind_close(win_h);
  149. wind_delete(win_h);
  150. v_clsvwk(vdi_h);
  151. appl_exit;
  152. end.