Debian Codegraph @

contrib/kylix/mngview/Main.pas

raw
1 unit Main;
2
3 interface
4
5 uses
6 Qt, QExtCtrls, QDialogs, QMenus, QTypes, QGraphics, QControls, QForms,
7 SysUtils, Classes, QStdCtrls, IdGlobal,
8 libmng;
9
10 {****************************************************************************}
11 {* For conditions of distribution and use, *}
12 {* see copyright notice in libmng.pas *}
13 {****************************************************************************}
14 {* *}
15 {* project : libmng *}
16 {* file : main.pas copyright (c) 2000-2002 G.Juyn *}
17 {* version : 1.0.5 *}
18 {* *}
19 {* purpose : Main form for mngview application *}
20 {* *}
21 {* author : G.Juyn *}
22 {* web : http://www.3-t.com *}
23 {* email : mailto:[email protected] *}
24 {* *}
25 {* comment : this is the heart of the mngview applciation *}
26 {* *}
27 {* changes : 1.0.5 - 09/21/2002 - G.Juyn *}
28 {* - modified for Kylix use *}
29 {* *}
30 {****************************************************************************}
31
32 type
33 TMainForm = class(TForm)
34
35 OFMainMenu: TMainMenu;
36 OFMenuFile: TMenuItem;
37 OFMenuFileOpen: TMenuItem;
38 OFMenuFileN1: TMenuItem;
39 OFMenuFileExit: TMenuItem;
40
41 OFTimer: TTimer;
42
43 OFOpenDialog: TOpenDialog;
44 OFPanel: TPanel;
45 OFImage: TImage;
46
47 procedure FormCreate(Sender: TObject);
48 procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
49 procedure FormShow(Sender: TObject);
50 procedure FormResize(Sender: TObject);
51 procedure FormKeyDown(Sender: TObject; var Key: Word;
52 Shift: TShiftState);
53 procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
54 Y: Integer);
55
56 procedure OFImageMouseMove(Sender: TObject; Shift: TShiftState; X,
57 Y: Integer);
58 procedure OFImageMouseDown(Sender: TObject; Button: TMouseButton;
59 Shift: TShiftState; X, Y: Integer);
60 procedure OFImageMouseUp(Sender: TObject; Button: TMouseButton;
61 Shift: TShiftState; X, Y: Integer);
62
63 procedure OFTimerTimer(Sender: TObject);
64
65 procedure OFMenuFileOpenClick(Sender: TObject);
66 procedure OFMenuFileExitClick(Sender: TObject);
67
68 private
69 { Private declarations }
70
71 SFFileName : string; { filename of the input stream }
72 OFFile : TFileStream; { input stream }
73 IFHandle : mng_handle; { the libray handle }
74 OFBitmap : TBitmap; { drawing canvas }
75 BFCancelled : boolean; { <esc> or app-exit }
76 BFHasMouse : boolean; { mouse is/was over image }
77
78 procedure MNGerror (SHMsg : string);
79
80 public
81 { Public declarations }
82
83 end;
84
85 var
86 MainForm: TMainForm;
87
88 {****************************************************************************}
89
90 implementation
91
92 {$R *.dfm}
93
94 {****************************************************************************}
95
96 {$F+}
97 function Memalloc (iLen : mng_uint32) : mng_ptr; cdecl;
98 {$F-}
99 begin
100 getmem (Result, iLen); { get memory from the heap }
101 fillchar (Result^, iLen, 0); { and initialize it }
102 end;
103
104 {****************************************************************************}
105
106 {$F+}
107 procedure Memfree (iPtr : mng_ptr;
108 iLen : mng_size_t); cdecl;
109 {$F-}
110 begin
111 freemem (iPtr, iLen); { free the memory }
112 end;
113
114 {****************************************************************************}
115
116 {$F+}
117 function Openstream (hHandle : mng_handle) : mng_bool; cdecl;
118 {$F-}
119
120 var OHForm : TMainForm;
121
122 begin { get a fix on our form }
123 OHForm := TMainForm (mng_get_userdata (hHandle));
124
125 with OHFORM do
126 begin
127 if OFFile <> nil then { free previous stream (if any) }
128 OFFile.Free;
129 { open a new stream }
130 OFFile := TFileStream.Create (SFFileName, fmOpenRead or fmShareDenyWrite);
131 end;
132
133 Result := MNG_TRUE;
134 end;
135
136 {****************************************************************************}
137
138 {$F+}
139 function Closestream (hHandle : mng_handle) : mng_bool; cdecl;
140 {$F-}
141
142 var OHForm : TMainForm;
143
144 begin { get a fix on our form }
145 OHForm := TMainForm (mng_get_userdata (hHandle));
146
147 with OHFORM do
148 begin
149 OFFile.Free; { cleanup the stream }
150 OFFile := nil; { don't use it again ! }
151 end;
152
153 Result := MNG_TRUE;
154 end;
155
156 {****************************************************************************}
157
158 {$F+}
159 function Readdata ( hHandle : mng_handle;
160 pBuf : mng_ptr;
161 iBuflen : mng_uint32;
162 var pRead : mng_uint32) : mng_bool; cdecl;
163 {$F-}
164
165 var OHForm : TMainForm;
166
167 begin
168 { get a fix on our form }
169 OHForm := TMainForm (mng_get_userdata (hHandle));
170
171 with OHForm do
172 begin { are we at EOF ? }
173 if OFFile.Position >= OFFile.Size then
174 begin
175 pRead := 0; { indicate so }
176 end
177 else
178 begin
179 { read the requested data }
180 pRead := OFFile.Read (pBuf^, iBuflen);
181 end;
182 end;
183
184 Result := MNG_TRUE;
185 end;
186
187 {****************************************************************************}
188
189 {$F+}
190 function ProcessHeader (hHandle : mng_handle;
191 iWidth : mng_uint32;
192 iHeight : mng_uint32) : mng_bool; cdecl;
193 {$F-}
194
195 var OHForm : TMainForm;
196
197 begin { get a fix on our form }
198 OHForm := TMainForm (mng_get_userdata (hHandle));
199
200 with OHForm do
201 begin
202 OFBitmap.Width := iWidth; { store the new dimensions }
203 OFBitmap.Height := iHeight;
204 OFBitmap.PixelFormat := pf32bit;
205
206 OFImage.Left := 0; { adjust the visible component }
207 OFImage.Top := 0;
208 OFImage.Width := iWidth;
209 OFImage.Height := iHeight;
210
211 FormResize (OHForm); { force re-centering the image}
212 { clear the canvas & draw an outline }
213 OFBitmap.Canvas.Brush.Color := clGray;
214 OFBitmap.Canvas.Brush.Style := bsSolid;
215 OFBitmap.Canvas.FillRect (OFBitmap.Canvas.ClipRect);
216
217 OFImage.Picture.Assign (OFBitmap); { make sure it gets out there }
218 { tell the library we want funny windows-bgr}
219 if mng_set_canvasstyle (hHandle, MNG_CANVAS_BGRX8) <> 0 then
220 MNGerror ('libmng reported an error setting the canvas style');
221
222 end;
223
224 Result := MNG_TRUE;
225 end;
226
227 {****************************************************************************}
228
229 {$F+}
230 function GetCanvasLine (hHandle : mng_handle;
231 iLinenr : mng_uint32) : mng_ptr; cdecl;
232 {$F-}
233
234 var OHForm : TMainForm;
235
236 begin { get a fix on our form }
237 OHForm := TMainForm (mng_get_userdata (hHandle));
238 { easy with these bitmap objects ! }
239 Result := TBitmap(OHForm.OFImage.Picture.Graphic).ScanLine [iLinenr];
240 end;
241
242 {****************************************************************************}
243
244 {$F+}
245 function ImageRefresh (hHandle : mng_handle;
246 iX : mng_uint32;
247 iY : mng_uint32;
248 iWidth : mng_uint32;
249 iHeight : mng_uint32) : mng_bool; cdecl;
250 {$F-}
251
252 var OHForm : TMainForm;
253
254 begin { get a fix on our form }
255 OHForm := TMainForm (mng_get_userdata (hHandle));
256 { force redraw }
257 OHForm.OFImage.Refresh;
258
259 Result := MNG_TRUE;
260 end;
261
262
263 {****************************************************************************}
264
265 {$F+}
266 function MyGetTickCount (hHandle : mng_handle) : mng_uint32; cdecl;
267 {$F-}
268 begin
269 Result := GetTickCount; { the system knows that }
270 end;
271
272 {****************************************************************************}
273
274 {$F+}
275 function SetTimer (hHandle : mng_handle;
276 iMsecs : mng_uint32) : mng_bool; cdecl;
277 {$F-}
278
279 var OHForm : TMainForm;
280
281 begin { get a fix on our form }
282 OHForm := TMainForm (mng_get_userdata (hHandle));
283 OHForm.OFTimer.Interval := iMsecs; { and set the timer }
284 OHForm.OFTimer.Enabled := true;
285
286 Result := MNG_TRUE;
287 end;
288
289 {****************************************************************************}
290
291 procedure TMainForm.FormCreate(Sender: TObject);
292
293 var IHRed, IHGreen, IHBlue : word;
294
295 begin { initialize }
296 OFBitmap := TBitmap.Create;
297 BFHasMouse := false;
298 OFFile := nil;
299
300 OFOpenDialog.Initialdir := '';
301 { now initialize the library }
302 IFHandle := mng_initialize (mng_ptr(self), Memalloc, Memfree, nil);
303
304 if IFHandle = NIL then
305 begin
306 MNGerror ('libmng initialization error' + #13#10 +
307 'Program aborted');
308 Application.Terminate;
309 end;
310 { no need to store chunk-info ! }
311 mng_set_storechunks (IFHandle, MNG_FALSE);
312 { do not use suspension-buffer }
313 mng_set_suspensionmode (IFHandle, MNG_FALSE);
314 { set all the callbacks }
315 if (mng_setcb_openstream (IFHandle, Openstream ) <> MNG_NOERROR) or
316 (mng_setcb_closestream (IFHandle, Closestream ) <> MNG_NOERROR) or
317 (mng_setcb_readdata (IFHandle, Readdata ) <> MNG_NOERROR) or
318 (mng_setcb_processheader (IFHandle, ProcessHeader ) <> MNG_NOERROR) or
319 (mng_setcb_getcanvasline (IFHandle, GetCanvasLine ) <> MNG_NOERROR) or
320 (mng_setcb_refresh (IFHandle, ImageRefresh ) <> MNG_NOERROR) or
321 (mng_setcb_gettickcount (IFHandle, MyGetTickCount ) <> MNG_NOERROR) or
322 (mng_setcb_settimer (IFHandle, SetTimer ) <> MNG_NOERROR) then
323 begin
324 MNGerror ('libmng reported an error setting a callback function!' + #13#10 +
325 'Program aborted');
326 Application.Terminate;
327 end;
328
329 IHRed := (Color ) and $FF; { supply our own bg-color }
330 IHGreen := (Color shr 8) and $FF;
331 IHBlue := (Color shr 16) and $FF;
332
333 IHRed := (IHRed shl 8) + IHRed;
334 IHGreen := (IHGreen shl 8) + IHGreen;
335 IHBlue := (IHBlue shl 8) + IHBlue;
336
337 if mng_set_bgcolor (IFHandle, IHRed, IHGreen, IHBlue) <> MNG_NOERROR then
338 MNGerror ('libmng reported an error setting the background color!');
339
340 end;
341
342 {****************************************************************************}
343
344 procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
345 begin
346 BFCancelled := true;
347
348 if OFTimer.Enabled then { if we're still animating then stop it }
349 begin
350 OFTimer.Enabled := false;
351
352 Application.ProcessMessages;
353
354 if mng_reset (IFHandle) <> MNG_NOERROR then
355 MNGerror ('libmng reported an error during reset!');
356
357 end;
358
359 mng_cleanup (IFHandle);
360 end;
361
362 {****************************************************************************}
363
364 procedure TMainForm.FormShow(Sender: TObject);
365 begin
366 FormResize (self);
367 end;
368
369 {****************************************************************************}
370
371 procedure TMainForm.FormResize(Sender: TObject);
372 begin { center the image in the window }
373 if ClientWidth < OFImage.Width then
374 OFImage.Left := 0
375 else
376 OFImage.Left := (ClientWidth - OFImage.Width ) div 2;
377
378 if ClientHeight < OFImage.Height then
379 OFImage.Top := 0
380 else
381 OFImage.Top := (ClientHeight - OFImage.Height) div 2;
382
383 end;
384
385 {****************************************************************************}
386
387 procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
388 Shift: TShiftState);
389 begin
390 if Key = Key_Escape then { pressing <esc> will freeze an animation }
391 begin
392 if OFTimer.Enabled then
393 if mng_display_freeze (IFHandle) <> MNG_NOERROR then
394 MNGerror ('libmng reported an error during display_freeze!');
395
396 OFTimer.Enabled := false; { don't let that timer go off then ! }
397 BFCancelled := true;
398 end;
399 end;
400
401 {****************************************************************************}
402
403 procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
404 Y: Integer);
405 begin
406 if mng_status_dynamic (IFHandle) then
407 begin
408 if BFHasMouse then { if we had the mouse, it's left ! }
409 begin
410 if mng_trapevent (IFHandle, 3, 0, 0) <> MNG_NOERROR then
411 MNGerror ('libmng reported an error during trapevent!');
412
413 BFHasMouse := false;
414 end;
415 end;
416 end;
417
418 {****************************************************************************}
419
420 procedure TMainForm.OFImageMouseMove(Sender: TObject; Shift: TShiftState;
421 X, Y: Integer);
422 begin
423 if mng_status_dynamic (IFHandle) then
424 begin
425 if BFHasMouse then { did we have the mouse already ? }
426 begin
427 if mng_trapevent (IFHandle, 2, X, Y) <> MNG_NOERROR then
428 MNGerror ('libmng reported an error during trapevent!');
429 end
430 else
431 begin { if not, it has entered ! }
432 if mng_trapevent (IFHandle, 1, X, Y) <> MNG_NOERROR then
433 MNGerror ('libmng reported an error during trapevent!');
434
435 BFHasMouse := true;
436 end;
437 end;
438 end;
439
440 {****************************************************************************}
441
442 procedure TMainForm.OFImageMouseDown(Sender: TObject; Button: TMouseButton;
443 Shift: TShiftState; X, Y: Integer);
444 begin
445 if mng_status_dynamic (IFHandle) then
446 if mng_trapevent (IFHandle, 4, X, Y) <> MNG_NOERROR then
447 MNGerror ('libmng reported an error during trapevent!');
448 end;
449
450 {****************************************************************************}
451
452 procedure TMainForm.OFImageMouseUp(Sender: TObject; Button: TMouseButton;
453 Shift: TShiftState; X, Y: Integer);
454 begin
455 if mng_status_dynamic (IFHandle) then
456 if mng_trapevent (IFHandle, 5, X, Y) <> MNG_NOERROR then
457 MNGerror ('libmng reported an error during trapevent!');
458 end;
459
460 {****************************************************************************}
461
462 procedure TMainForm.OFTimerTimer(Sender: TObject);
463
464 var IHRslt : mng_retcode;
465
466 begin
467 OFTimer.Enabled := false; { only once ! }
468
469 if not BFCancelled then
470 begin { and inform the library }
471 IHRslt := mng_display_resume (IFHandle);
472
473 if (IHRslt <> MNG_NOERROR) and (IHRslt <> MNG_NEEDTIMERWAIT) then
474 MNGerror ('libmng reported an error during display_resume!');
475
476 end;
477 end;
478
479 {****************************************************************************}
480
481 procedure TMainForm.OFMenuFileOpenClick(Sender: TObject);
482
483 var IHRslt : mng_retcode;
484
485 begin
486 OFOpenDialog.InitialDir := '';
487 OFOpenDialog.FileName := SFFileName;
488
489 if OFOpenDialog.Execute then { get the filename }
490 begin
491 if OFTimer.Enabled then { if the lib was active; stop it }
492 begin
493 OFTimer.Enabled := false;
494
495 Application.ProcessMessages; { process any timer requests (for safety) }
496 { now freeze the animation }
497 if mng_reset (IFHandle) <> MNG_NOERROR then
498 MNGerror ('libmng reported an error during reset!');
499 end;
500 { save interesting fields }
501 SFFileName := OFOpenDialog.FileName;
502 BFCancelled := false;
503
504 OFImage.Picture.Graphic := nil; { clear the output-canvas }
505 OFImage.Refresh;
506 { always reset (just in case) }
507 if mng_reset (IFHandle) <> MNG_NOERROR then
508 MNGerror ('libmng reported an error during reset!')
509 else
510 begin { and let the lib do it's job ! }
511 IHRslt := mng_readdisplay (IFHandle);
512
513 if (IHRslt <> MNG_NOERROR) and (IHRSLT <> MNG_NEEDTIMERWAIT) then
514 MNGerror ('libmng reported an error reading the input file!');
515
516 end;
517 end;
518 end;
519
520 {****************************************************************************}
521
522 procedure TMainForm.OFMenuFileExitClick(Sender: TObject);
523 begin
524 if mng_cleanup (IFHandle) <> MNG_NOERROR then
525 MNGerror ('libmng cleanup error');
526
527 Close;
528 end;
529
530 {****************************************************************************}
531
532 procedure TMainForm.MNGerror;
533
534 var iErrorcode : mng_uint32;
535 iSeverity : mng_uint8;
536 iChunkname : mng_chunkid;
537 iChunkseq : mng_uint32;
538 iExtra1 : mng_int32;
539 iExtra2 : mng_int32;
540 zErrortext : mng_pchar;
541
542 begin { get extended info }
543 iErrorcode := mng_getlasterror (IFHandle, iSeverity, iChunkname, iChunkseq,
544 iExtra1, iExtra2, zErrortext);
545
546 MessageDlg (SHMsg + #13#10#13#10 + strpas (zErrortext) + #13#10#13#10 +
547 Format ('Error = %d; Severity = %d; Chunknr = %d; Extra1 = %d',
548 [iErrorcode, iSeverity, iChunkseq, iExtra1]),
549 mtError, [mbOK], 0);
550 Application.Terminate;
551 end;
552
553 {****************************************************************************}
554
555 end.