Chapter 5 Example ADL Programs

5.4 A Video Viewer Class

In this example, we create the VCR class using the wrapped class MMmovie.

5.4.1 ADL Implementation of the VCR class

The ADL code for the VCR class is given below. Detailed comments on specific lines are below.

1 class VCR : XFlayout

2 {

3

4 handle Screen = NULL;

5 handle Movie = NULL;

6 handle PlayButton = NULL;

7 handle RewindButton = NULL;

8 handle PauseButton = NULL;

9 handle FastForwardButton = NULL;

10 handle StopButton = NULL;

11

12 string MovieTitle = "";

13 boolean Paused = FALSE;

14

15 upon Create : handle VisualParent, list SizePosition, string InitialMovie

16 init { {'Create, VisualParent} => XFlayout }

17 { integer Xpos;

18 integer Ypos;

19 integer Width;

20 integer Height;

21

22 Xpos = first(SizePosition);

23 Ypos = at(2, SizePosition);

24 Width = at(3, SizePosition);

25 Height = at(4, SizePosition);

26

27 x = Xpos;

28 y = Ypos;

29 width = Width;

30 height = height;

31

32 MovieTitle = InitialMovie;

33

34 } // End of Create Method

35

36 upon Construct

37 {

38 MovieTitle = "";

39 } // End of Construct Method

40

41 on Init

42 {

43 integer ButtonWidth;

44

45 ButtonWidth = width/5;

46

47 Screen = new {'Create, self} => XFvisual {

48 visible = TRUE;

49 x = 0;

50 y = 0;

51 };

52 Screen->width = width;

53 Screen->height = height - 20;

54

55 PlayButton = new {'Create, self} => XFbutton {

56 visible = TRUE;

57 label = ">";

58 fontRequest = {'Helvetica, 14, {'bold}, 'roman};

59 };

60 PlayButton->x = 0;

61 PlayButton->y = height - 20;

62 PlayButton->width = ButtonWidth;

63 PlayButton->height = 20;

64 PlayButton->Pressed = {'Play, self};

65

66 RewindButton = new {'Create, self} => XFbutton {

67 visible = TRUE;

68 label = "<<";

69 fontRequest = {'Helvetica, 14, {'bold}, 'roman};

70 };

71 RewindButton->x = ButtonWidth;

72 RewindButton->y = height - 20;

73 RewindButton->width = ButtonWidth;

74 RewindButton->height = 20;

75 RewindButton->Pressed = {'Rewind, self};

76

77 PauseButton = new {'Create, self} => XFbutton {

78 visible = TRUE;

79 label = "||";

80 fontRequest = {'Helvetica, 14, {'bold}, 'roman};

81 };

82 PauseButton->x = 2*ButtonWidth;

83 PauseButton->y = height - 20;

84 PauseButton->width = ButtonWidth;

85 PauseButton->height = 20;

86 PauseButton->Pressed = {'Pause, self};

87

88 FastForwardButton = new {'Create, self} => XFbutton {

89 visible = TRUE;

90 label = ">>";

91 fontRequest = {'Helvetica, 14, {'bold}, 'roman};

92 };

93 FastForwardButton->x = 3*ButtonWidth;

94 FastForwardButton->y = height - 20;

95 FastForwardButton->width = ButtonWidth;

96 FastForwardButton->height = 20;

97 FastForwardButton->Pressed = {'FastForward, self};

98

99 StopButton = new {'Create, self} => XFbutton {

100 visible = TRUE;

101 label = "[]";

102 fontRequest = {'Helvetica, 14, {'bold}, 'roman};

103 };

104 StopButton->x = 4*ButtonWidth;

105 StopButton->y = height - 20;

106 StopButton->width = ButtonWidth;

107 StopButton->height = 20;

108 StopButton->Pressed = {'Stop, self};

109

110 {'SetMovie, MovieTitle} => self;

111

112 } // End of Init Method

113

114 on SetMovie : string NewMovieTitle

115 {

116 if (isValid(Movie)) {

117 delete Movie;

118 Movie = NULL;

119 }

120

121 if (NewMovieTitle != "") {

122 Movie=new{'Construct,{'MEavi,{'MAfile,NewMovieTitle}}}=> MMmovie;

123 {'RegisterOn, Screen} => Movie;

124 }

125

126 MovieTitle = NewMovieTitle;

127 'UpdateButtons => self;

128

129 } // End of SetMovie Method

130

131 on UpdateButtons

132 {

133 if (isValid(Movie)) {

134 PlayButton->disabled = FALSE;

135 RewindButton->disabled = FALSE;

136 PauseButton->disabled = FALSE;

137 FastForwardButton->disabled = FALSE;

138 StopButton->disabled = FALSE;

139 }

140 else {

141 PlayButton->disabled = TRUE;

142 RewindButton->disabled = TRUE;

143 PauseButton->disabled = TRUE;

144 FastForwardButton->disabled = TRUE;

145 StopButton->disabled = TRUE;

146 }

147

148 } // End of UpdateButtons Method

149

150 on Show

151 {

152 visible = TRUE;

153 } // End of Show Method

154

155 on Hide

156 {

157 visible = FALSE;

158 } // End of Hide Method

159

160 on Play

161 {

162 integer a, b;

163 list l;

164 interval i;

165 if (isValid(Movie)) {

166 if (! Paused ) {

167 'Present => Movie;

168 }

169 else {

170 'Resume => Movie;

171 }

172 Paused = FALSE;

173 }

174 } // End of Play Method

175

176 on Rewind

177 {

178 integer a, b;

179 list l;

180 interval i;

181

182 if (isValid(Movie)) {

183

184 if (((Movie->position) - 10) > Movie->startPosition) {

185 a = (Movie->position) - 10;

186 }

187 else {

188 a = Movie->startPosition;

189 }

190

191 if (! Paused) {

192 b = Movie->endPosition;

193 }

194 else {

195 b = a;

196 }

197

198 l = { {TRUE, a}, {TRUE, b}};

199 i = toInterval(l);

200

201 {'PlayInterval, i} => Movie;

202

203 }

204

205 } // End of Rewind Method

206

207 on Pause

208 {

209 integer a;

210 list l;

211 interval i;

212 if (isValid(Movie)) {

213 if (! Paused) {

214 'Pause => Movie;

215 Paused = TRUE;

216 }

217 else {

218 'Resume => Movie;

219 Paused = FALSE;

220 }}

221 } // End of Pause Method

222

223 on FastForward

224 {

225 integer a, b;

226 list l;

227 interval i;

228

229 if (isValid(Movie)) {

230

231 if (((Movie->position) + 10) < Movie->endPosition) {

232 a = (Movie->position) + 10;

233 }

234 else {

235 a = Movie->endPosition;

236 }

237

238 if (! Paused) {

239 b = Movie->endPosition;

240 }

241 else {

242 b = a;

243 }

244

245 l = { {TRUE, a}, {TRUE, b}};

246 i = toInterval(l);

247

248 {'PlayInterval, i} => Movie;

249

250 }

251

252 } // End of FastForward Method

253

254 on Stop

255 {

256 integer a;

257 list l;

258 interval i;

259

260 if (isValid(Movie)) {

261

262 'Stop => Movie;

263 Paused = FALSE;

264

265 a = Movie->startPosition;

266 l = { {TRUE, a}, {TRUE, a}};

267 i = toInterval(l);

268

269 {'PlayInterval, i} => Movie;

270

271 }

272

273 } // End of Stop Method

274

275 }; // End of VCR Class Definition

276

277

278 anonymous : XFtop

279

280 {

281 VCR myVcr{x = 10; y = 10; width = 400; height = 300;};

282

283 upon Construct

284 {

285 {'SetMovie, "NAME.avi"} => myVcr;

286 'Show => myVcr;

287

288 } // End of Construct Method

289 } mytop{width = 410; height = 310;};

290 */

Line 1 declares VCR class as a subclass of XFlayout. In other words, the VCR class inherits behavior from the XFlayout class.

Lines 4-13 declare internal variables used by the class such as button handles, the title of the current movie and a boolean value that tracks whether the current movie is paused or not.

Line 15 declares the "Create" method as a constructor using the "upon" key word. This method will take three arguments including a visual parent, a list containing the size and position of the VCR and an initial movie title.

Line 16 initializes the base class of the VCR by sending the visual parent to the XFlayout inherited class.

Lines 17-20 declares internal variables to be used within the method. These variables do not exist outside of this method and are used for readability.

Lines 22-25 assigns each variable the appropriate value from the list which describes the coordinates and its width and height of the VCR.

Line 32 saves the filename for the initial movie to an internal class variable for later use.

Line 34 ends the declaration of the "Create" method.

Line 36 declares the "Construct" method as a constructor using the "upon" key word. This method takes no arguments and can only be used when the "visual parent" of the VCR can be found by asking the windowing system. Use this method when you statically create a VCR (see application example below).

Line 38 ensures that the "MovieTitle" variable is properly set to the default value.

Line 39 ends the declaration of the "Construct" method.

Line 41 begins the declaration of the "Init" method. This method is used to initialize internal variables, to create and position buttons and to initialize the movie to be played, if any.

Line 43 declares an internal method variable used to calculate and store the width of the buttons.

Line 45 calculates the width of all of the buttons based on the width of the VCR.

Line 47 creates a new instance of the "XFvisual" class and stores the new handle in the variable "Screen".

Lines 48-50 initialize some of the attributes of the newly created XFvisual, such as coordinates and visibility.

Line 52 sets the width of the "Screen" to be the same width of the VCR.

Line 53 sets the height of the "Screen" to be as large as possible but still leave room for the VCR buttons.

Line 55-59 creates a new instance of the "XFbutton" class and stores the new handle in a variable. Some of the attributes of the new button are set including the "label", visibility and font to be used.

Lines 60-63 these lines set the other attributes of the newly created button, namely the coordinates and width and height of the button.

Line 64 this sets the both the method name and object called when the button is pressed. In this case, the method "Play" will be sent to the "self" i.e. the current instance of the VCR class. This completes the declaration of the "Play" button for the VCR class.

Lines 66-108 these lines follow the same format and pattern as Lines 55-64, creating the "Rewind", "Pause", "Fast Forward" and "Stop" buttons of the VCR class. Pay particular attention to the "label" attribute of each button and Lines 75, 86, 97 and 108 which set the appearance of the button and the response of the button when pressed, respectively.

Line 110 calls the method "SetMovie" with the argument "MovieTitle" within the VCR class. This call activates the internal method responsible for loading the initial movie and preparing it for viewing.

Line 112 ends the declaration of the "Init" method.

Line 114 begins the declaration of the "SetMovie" method. The "SetMovie" method takes one argument in the form of a string which contains either a movie filename or an empty string. This method is responsible for preparing a movie to be viewed within the VCR class.

Line 116 this line checks to see if a current, valid movie exists.

Line 117 if a valid movie is currently loaded, delete the "old" movie.

Line 118 then, after deletion, reinitialize the "Movie" handle to NULL. This allows error checking should the new movie title be the empty string.

Line 121 this line checks to see if the "NewMovieTitle" is the empty string.

Line 122 if not, a new instance of MMmovie is created using the "NewMovieTitle" and the handle is stored in the internal variable "Movie".

Line 123 registers the new movie for display on the "Screen" of the VCR.

Line 126 stores the new movie title in the internal variable "MovieTitle".

Line 127 this line calls the method "UpdataButtons" within the VCR class. This method is responsible for enabling and disabling the VCR control buttons.

Line 129 ends the declaration of the "SetMovie" method.

Line 131 begins the declaration of the "UpdateButtons" method. This method enables or disables the VCR control buttons depending on whether a valid movie is loaded or not.

Line 133 checks to see of the handle stored in the internal variable "Movie" is valid.

Lines 134-138 if it is valid, all of the buttons are enabled so the user may use them to control viewing.

Lines 140-145 otherwise, the buttons are disabled so they may not be used.

Line 148 ends the declaration of the "UpdateButtons" method.

Line 150 begins the declaration of the "Show" method. This method displays the VCR (but does not start a movie playing).

Line 152 sets the VCR class visibility attribute to TRUE.

Line 153 ends the declaration of the "Show" method.

Line 155 begins the declaration of the "Hide" method. This method hides the VCR (but does not stop a movie from playing).

Line 157 sets the VCR class visibility attribute to FALSE.

Line 158 ends the declaration of the "Hide" method.

Line 160 begins the declaration of the "Play" method. This method is responsible for playing a valid movie or unpausing a valid, currently paused move.

Line 163 this line checks to ensure that a valid movie is loaded.

Line 164 if there is a valid movie loaded, this line checks to see if the movie is paused.

Line 165 if the movie is not paused, it is "Present"-ed which begins playing the movie on the "Screen".

Lines 167-168 if the movie is paused, it is "Resume"-ed which unpaused and continues the movie.

Line 171 sets the "Paused" state variable equal to FALSE.

Line 172 this line ends the "if" statement ensuring a valid movie is loaded.

Line 174 ends the declaration of the "Play" method.

Line 176 begins the declaration of the "Rewind" method.

Lines 178-180 declare variables to be used within the "Rewind" method. These variables will be used to declare segments of the movie to be displayed.

Line 182 checks to ensure the current movie is valid.

Line 184 if the current movie is valid, this line checks to see if the movie can be "rewound" 10 frames without moving before the "starting position" of the movie.

Line 185 if the current position can be moved back 10 frames, the variable "a" is set to the current position minus 10.

Lines 187-188 if the current position can not be moved back 10 frames, the variable "a" is set to the starting position of the movie.

Line 191 this line checks to see if the movie is currently paused.

Line 192 if the movie is not paused, the variable "b" is set to the "ending position" of the current movie.

Lines 194-195 if the movie is currently paused, the variable "b" is set equal to the variable "a".

Line 198 this line sets the variable "l" equal to a list that is formatted to be converted into an interval of the movie that can be displayed. The format says that the variable "a" is the first value of the interval and will be included within the bounds of the interval and the variable "b" is the second value of the interval and will also be included within the bounds of the interval.

Line 199 converts the list variable "l" into an interval and stores the value in the variable "i".

Line 201 displays the interval of the movie defined by "i".

Line 203 ends the "if" statement ensuring a valid movie is loaded.

Line 205 ends the declaration of the "Rewind" method.

Line 207 begins the declaration of the "Pause" method.

Line 209 checks to make sure a valid movie is currently loaded.

Line 210 if there is a valid movie loaded, this line checks to see if the movie is currently paused.

Line 211 if the movie is not currently paused, pause the movie and set the variable "Paused" equal to TRUE.

Lines 214-216 if the movie is currently paused, unpause the movie and set the variable "Paused" equal to FALSE.

Line 219 this line ends the "if" statement ensuring a valid movie is loaded.

Line 221 ends the declaration of the "Pause" method.

Line 223 begins the declaration of the "FastForward" method.

Lines 225-227 declare variables to be used within the "FastForward" method. These variables will be used to declare segments of the movie to be displayed.

Line 229 checks to ensure the current movie is valid.

Line 231 if the current movie is valid, this line checks to see if the movie can be "fast forwarded" 10 frames without moving beyond the "ending position" of the movie.

Line 232 if the current position can be moved forward 10 frames, the variable "a" is set to the current position plus 10.

Lines 234-235 if the current position can not be moved forward 10 frames, the variable "a" is set to the ending position of the movie.

Line 238 this line checks to see if the movie is currently paused.

Line 239 if the movie is not paused, the variable "b" is set to the "ending position" of the current movie.

Lines 241-242 if the movie is currently paused, the variable "b" is set equal to the variable "a".

Line 245 sets the variable "l" equal to a list that is formatted to be converted into an interval of the movie that can be displayed. The format says that the variable "a" is the first value of the interval and will be included within the bounds of the interval and the variable "b" is the second value of the interval and will also be included within the bounds of the interval.

Line 246 converts the list variable "l" into an interval and stores the value in the variable "i".

Line 248 displays the interval of the movie defined by "i".

Line 250 ends the "if" statement ensuring a valid movie is loaded.

Line 252 ends the declaration of the "FastForward" method.

Line 254 begins the declaration of the "Stop" method.

Lines 256-258 declare variables to be used within the "Stop" method. These variables will be used to move the movie back to the beginning.

Line 260 this line checks to see if the current movie is valid.

Line 262 if the movie is valid, stop the movie.

Line 263 resets the "Paused" variable to FALSE. A stopped movie is no longer paused.

Line 265 sets the variable "a" to the "starting position" of the movie.

Line 266 sets the variable "l" equal to a list that is formatted to be converted into an interval of the movie that can be displayed. The format says that the variable "a" is the first value of the interval and will be included within the bounds of the interval and the variable "a" is also the second value of the interval and will also be included within the bounds of the interval. This interval defines a point in the movie to be displayed, in this case, the beginning of the movie.

Line 267 converts the list variable "l" into an interval and stores the value in the variable "i".

Line 269 displays the interval of the movie defined by "i" effectively showing the first frame of the movie and stopping.

Line 271 ends the "if" statement ensuring a valid movie is loaded.

Line 273 ends the declaration of the "Stop" method.

Line 275 ends the declaration of the VCR class.

Line 277 begins a comment which includes a small, sample application which uses the VCR class. In order to use the sample application, the programmer will need to remove the "comment" markers.

Line 278 declares an "anonymous" subclass of the class XFtop. These are often used as the main window for applications.

Line 281 statically declares the creation of a VCR object and sets the values for x, y, width and height. In performing this type of object creation, the "Construct" method of the object is called. The name of the VCR object within the "anonymous" class is "myVcr".

Line 283 begins the declaration of the "Construct" method for the "anonymous" class.

Line 285 calls the "SetMovie" method of the VCR object "myVcr" with the argument "NAME.avi". The programmer should change "NAME.avi" to an appropriate, sample .avi file.

Line 286 calls the "Show" method of the VCR object "myVcr" effectively making the VCR object visible and usable by the user.

Line 288 ends the declaration of the "Construct" method for the "anonymous" class.

Line 289 completes the declaration of the "anonymous" class and creates an instance of the "anonymous" class named "mytop" along with setting the initial width and height of the "anonymous" class.

Line 290 this marks the end of the "comment" markers surrounding the sample application.

5.4.1 - ADL Implementation of the VCR class

AM2 Documentation - 19 NOV 1996

Generated with Harlequin WebMaker