window.pas 4.3 KB

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