window.pas 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. {
  2. Opening a window with optional custom screen, and
  3. basic event-driven drawing into the window
  4. Free Pascal for MorphOS example
  5. Copyright (C) 2004 by Karoly Balogh
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. { * 2004.12.10 * }
  13. program window;
  14. uses exec, intuition, graphics, utility;
  15. { * Enable this to run on custom screen. If it's not * }
  16. { * enabled, window will open on default pubscreen * }
  17. { DEFINE CUSTOMSCREEN}
  18. var
  19. {$IFDEF CUSTOMSCREEN}
  20. myScreen : PScreen;
  21. {$ENDIF}
  22. myWindow : PWindow;
  23. IMesg : PIntuiMessage;
  24. Quit : Boolean;
  25. LeftButton: Boolean;
  26. msg_Class : Cardinal;
  27. msg_Code : Word;
  28. X_Pos : Integer;
  29. Y_Pos : Integer;
  30. const
  31. ERRMSG_NOINTUI = 'Unable to open intuition.library V50!';
  32. ERRMSG_NOGFX = 'Unable to open graphics.library V50!';
  33. ERRMSG_NOSCREEN= 'Can''t open screen!';
  34. ERRMSG_NOWINDOW= 'Can''t open window!';
  35. procedure ShutDown(exitString: String; code: LongInt);
  36. begin
  37. if assigned(myWindow) then CloseWindow(myWindow);
  38. {$IFDEF CUSTOMSCREEN}
  39. if assigned(myScreen) then CloseScreen(myScreen);
  40. {$ENDIF}
  41. { * We're using library functions built into units, so it's * }
  42. { * not needed to close libs here. If you're using additional * }
  43. { * libs, add code to close them here. * }
  44. if exitString<>'' then writeln(exitString);
  45. Halt(code);
  46. end;
  47. procedure Init;
  48. begin
  49. { * Opening the needed libs with builtin functions. * }
  50. if Not InitIntuitionLibrary then ShutDown(ERRMSG_NOINTUI,20);
  51. if Not InitGraphicsLibrary then ShutDown(ERRMSG_NOGFX,20);
  52. {$IFDEF CUSTOMSCREEN}
  53. myScreen:=NIL;
  54. { * Opening our custom screen * }
  55. myScreen:=OpenScreenTags(NIL,[SA_Width,640,SA_Height,480,
  56. SA_Depth,24,
  57. SA_Title,DWord(PChar('Free Pascal Rules!')),
  58. TAG_DONE]);
  59. if myScreen=NIL then ShutDown(ERRMSG_NOSCREEN,20);
  60. {$ENDIF}
  61. myWindow:=NIL;
  62. { * We open our window here. * }
  63. myWindow:=OpenWindowTags(NIL,[WA_Left,0,WA_Top,0,
  64. WA_Width,400,WA_Height,300,
  65. WA_Title,DWord(PChar('Free Pascal Test')),
  66. WA_IDCMP,(IDCMP_CLOSEWINDOW or IDCMP_MOUSEBUTTONS or
  67. IDCMP_MOUSEMOVE),
  68. WA_Flags,(WFLG_SIMPLE_REFRESH or WFLG_NOCAREREFRESH or
  69. WFLG_ACTIVATE or WFLG_REPORTMOUSE or
  70. WFLG_CLOSEGADGET or WFLG_SIZEGADGET or
  71. WFLG_SIZEBBOTTOM or WFLG_GIMMEZEROZERO or
  72. WFLG_DRAGBAR),
  73. {$IFDEF CUSTOMSCREEN}
  74. WA_CustomScreen,DWord(myScreen),
  75. {$ENDIF}
  76. TAG_END]);
  77. if myWindow=NIL then ShutDown(ERRMSG_NOWINDOW,20);
  78. SetAPen(myWindow^.RPort,1);
  79. end;
  80. begin
  81. Init;
  82. Quit:=False;
  83. LeftButton:=False;
  84. repeat
  85. WaitPort(myWindow^.UserPort);
  86. IMesg:=PIntuiMessage(GetMsg(myWindow^.UserPort));
  87. if IMesg<>NIL then Begin
  88. { * It's recommended to copy contents of incoming * }
  89. { * message, and reply it as soon as possible. * }
  90. msg_Class:=IMesg^.IClass;
  91. msg_Code :=IMesg^.Code;
  92. X_Pos :=IMesg^.MouseX - myWindow^.BorderLeft;
  93. Y_Pos :=IMesg^.MouseY - myWindow^.BorderTop;
  94. ReplyMsg(Pointer(IMesg));
  95. { * Handle different kind of messages here. * }
  96. case msg_Class of
  97. IDCMP_CLOSEWINDOW:
  98. quit:=True;
  99. IDCMP_MOUSEBUTTONS:
  100. case msg_Code of
  101. SELECTDOWN:
  102. LeftButton:=True;
  103. SELECTUP:
  104. LeftButton:=False;
  105. end;
  106. IDCMP_MOUSEMOVE:
  107. begin
  108. { * Draw when left button is pressed, and mouse is moved. * }
  109. if LeftButton then WritePixel(myWindow^.RPort,X_Pos,Y_Pos);
  110. end;
  111. end;
  112. end;
  113. until Quit;
  114. ShutDown('',0);
  115. end.