File: Main.pas

package info (click to toggle)
libmng 2.0.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,092 kB
  • sloc: ansic: 78,373; sh: 11,434; cpp: 2,624; pascal: 2,185; makefile: 186
file content (692 lines) | stat: -rw-r--r-- 24,959 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, StdCtrls, ExtCtrls, Buttons,
  libmng;

{****************************************************************************}
{*              For conditions of distribution and use,                     *}
{*                 see copyright notice in libmng.pas                       *}
{****************************************************************************}
{*                                                                          *}
{*  project   : libmng                                                      *}
{*  file      : main.pas                  copyright (c) 2000-2002 G.Juyn    *}
{*  version   : 1.0.5                                                       *}
{*                                                                          *}
{*  purpose   : Main form for mngview application                           *}
{*                                                                          *}
{*  author    : G.Juyn                                                      *}
{*  web       : http://www.3-t.com                                          *}
{*  email     : mailto:info@3-t.com                                         *}
{*                                                                          *}
{*  comment   : this is the heart of the mngview applciation                *}
{*                                                                          *}
{*  changes   : 0.5.1 - 05/02/2000 - G.Juyn                                 *}
{*              - added this version block                                  *}
{*              - made the initialization part more robust                  *}
{*                eg. program aborts on initialization errors               *}
{*              - B002(105797) - added check for existence of default sRGB  *}
{*                profile (now included in distribution)                    *}
{*              - added mng_cleanup to program exit                         *}
{*              0.5.1 - 05/08/2000 - G.Juyn                                 *}
{*              - changed to stdcall convention                             *}
{*              0.5.1 - 05/11/2000 - G.Juyn                                 *}
{*              - changed callback function declarations                    *}
{*                                                                          *}
{*              0.5.3 - 06/16/2000 - G.Juyn                                 *}
{*              - removed processmessages call from refresh callback        *}
{*              0.5.3 - 06/17/2000 - G.Juyn                                 *}
{*              - switched "storechunks" off                                *}
{*              0.5.3 - 06/26/2000 - G.Juyn                                 *}
{*              - changed definition of userdata to mng_ptr                 *}
{*              0.5.3 - 06/28/2000 - G.Juyn                                 *}
{*              - changed the default icon to something more appropriate    *}
{*              - changed definition of memory alloc size to mng_size_t     *}
{*              0.5.3 - 06/29/2000 - G.Juyn                                 *}
{*              - changed order of refresh parameters                       *}
{*                                                                          *}
{*              0.9.0 - 06/30/2000 - G.Juyn                                 *}
{*              - changed refresh parameters to 'x,y,width,height'          *}
{*                                                                          *}
{*              0.9.1 - 07/08/2000 - G.Juyn                                 *}
{*              - fixed to use returncode constants                         *}
{*              - changed to accomodate MNG_NEEDTIMERWAIT returncode        *}
{*              0.9.1 - 07/10/2000 - G.Juyn                                 *}
{*              - changed to use suspension-mode                            *}
{*                                                                          *}
{*              0.9.3 - 09/11/2000 - G.Juyn                                 *}
{*              - removed some tesst-stuff                                  *}
{*                                                                          *}
{*              1.0.1 - 05/02/2000 - G.Juyn                                 *}
{*              - removed loading default sRGB profile (auto in libmng)     *}
{*                                                                          *}
{*              1.0.5 - 09/16/2002 - G.Juyn                                 *}
{*              - added dynamic MNG features                                *}
{*              1.0.5 - 11/27/2002 - G.Juyn                                 *}
{*              - fixed freeze during read-cycle                            *}
{*                                                                          *}
{****************************************************************************}

type
  TMainForm = class(TForm)

    OFMainMenu: TMainMenu;
    OFMenuFile: TMenuItem;
    OFMenuFileOpen: TMenuItem;
    OFMenuFileProfile: TMenuItem;
    OFMenuFileN1: TMenuItem;
    OFMenuFileExit: TMenuItem;

    OFMenuOptions: TMenuItem;
    OFMenuOptionsModemSpeed: TMenuItem;
    OFMenuOptionsModem28k8: TMenuItem;
    OFMenuOptionsModem33k6: TMenuItem;
    OFMenuOptionsModem56k: TMenuItem;
    OFMenuOptionsModemISDN64: TMenuItem;
    OFMenuOptionsModemISDN128: TMenuItem;
    OFMenuOptionsModemCable512: TMenuItem;
    OFMenuOptionsModemUnlimited: TMenuItem;

    OFTimer: TTimer;

    OFOpenDialog: TOpenDialog;
    OFOpenDialogProfile: TOpenDialog;

    OFImage: TImage;

    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormShow(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);

    procedure OFImageMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure OFImageMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure OFImageMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);

    procedure OFTimerTimer(Sender: TObject);

    procedure OFMenuFileOpenClick(Sender: TObject);
    procedure OFMenuFileProfileClick(Sender: TObject);
    procedure OFMenuFileExitClick(Sender: TObject);

    procedure OFMenuOptionsModemSpeedClick(Sender: TObject);
    procedure OFMenuOptionsModemXClick(Sender: TObject);

  private
    { Private declarations }

    SFFileName    : string;            { filename of the input stream }
    OFFile        : TFileStream;       { input stream }
    IFHandle      : mng_handle;        { the libray handle }
    OFBitmap      : TBitmap;           { drawing canvas }
    BFCancelled   : boolean;           { <esc> or app-exit }
    BFHasMouse    : boolean;           { mouse is/was over image }

    IFTicks       : cardinal;          { used to fake slow connections }
    IFBytes       : cardinal;
    IFBytesPerSec : integer;

    procedure MNGerror (SHMsg : string);

  public
    { Public declarations }

  end;

var
  MainForm: TMainForm;

{****************************************************************************}

implementation

{$R *.DFM}

{****************************************************************************}

{$F+}
function  Memalloc (iLen : mng_uint32) : mng_ptr; stdcall;
{$F-}
begin
  getmem   (Result, iLen);             { get memory from the heap }
  fillchar (Result^, iLen, 0);         { and initialize it }
end;

{****************************************************************************}

{$F+}
procedure Memfree (iPtr : mng_ptr;
                   iLen : mng_size_t); stdcall;
{$F-}
begin
  freemem (iPtr, iLen);                { free the memory }
end;

{****************************************************************************}

{$F+}
function Openstream (hHandle : mng_handle) : mng_bool; stdcall;
{$F-}

var OHForm : TMainForm;

begin                                  { get a fix on our form }
  OHForm := TMainForm (mng_get_userdata (hHandle));

  with OHFORM do
  begin
    if OFFile <> nil then              { free previous stream (if any) }
      OFFile.Free;
                                       { open a new stream }
    OFFile := TFileStream.Create (SFFileName, fmOpenRead or fmShareDenyWrite);
  end;

  Result := MNG_TRUE;  
end;

{****************************************************************************}

{$F+}
function Closestream (hHandle : mng_handle) : mng_bool; stdcall;
{$F-}

var OHForm : TMainForm;

begin                                  { get a fix on our form }
  OHForm := TMainForm (mng_get_userdata (hHandle));

  with OHFORM do
  begin
    OFFile.Free;                       { cleanup the stream }
    OFFile := nil;                     { don't use it again ! }
  end;

  Result := MNG_TRUE;
end;

{****************************************************************************}

{$F+}
function Readdata (    hHandle : mng_handle;
                       pBuf    : mng_ptr;
                       iBuflen : mng_uint32;
                   var pRead   : mng_uint32) : mng_bool; stdcall;
{$F-}

var OHForm        : TMainForm;
    IHTicks       : cardinal;
    IHByte1       : cardinal;
    IHByte2       : cardinal;
    IHBytesPerSec : cardinal;

begin
                                       { get a fix on our form }
  OHForm := TMainForm (mng_get_userdata (hHandle));

  with OHForm do
  begin                                { are we at EOF ? }
    if OFFile.Position >= OFFile.Size then
    begin
      pRead := 0;                      { indicate so }
    end
    else
    begin
      IHBytesPerSec := IFBytesPerSec;  { fake a slow connection }

      if IHBytesPerSec > 0 then
      begin
        IHTicks       := Windows.GetTickCount;
        IHByte1       := round (((IHTicks - IFTicks) / 1000) * IHBytesPerSec);
        IHByte2       := (IFBytes + iBuflen);

        if ((IHByte2 - IHByte1) div IHBytesPerSec) > 10 then
          Windows.Sleep ((IHByte2 - IHByte1) div IHBytesPerSec);

      end;
                                       { read the requested data }
      pRead   := OFFile.Read (pBuf^, iBuflen);
      IFBytes := IFBytes + pRead;
    end;
  end;

  Result := MNG_TRUE;  
end;

{****************************************************************************}

{$F+}
function ProcessHeader (hHandle : mng_handle;
                        iWidth  : mng_uint32;
                        iHeight : mng_uint32) : mng_bool; stdcall;
{$F-}

var OHForm : TMainForm;

begin                                  { get a fix on our form }
  OHForm := TMainForm (mng_get_userdata (hHandle));

  with OHForm do
  begin
    OFBitmap.Width  := iWidth;         { store the new dimensions }
    OFBitmap.Height := iHeight;
    OFImage.Left    := 0;              { adjust the visible component }
    OFImage.Top     := 0;
    OFImage.Width   := iWidth;
    OFImage.Height  := iHeight;

    FormResize (OHForm);               { force re-centering the image}
                                       { clear the canvas & draw an outline }
    OFBitmap.Canvas.Brush.Color := clGray;
    OFBitmap.Canvas.Brush.Style := bsSolid;
    OFBitmap.Canvas.FillRect  (OFBitmap.Canvas.ClipRect);
    OFBitmap.Canvas.Brush.Color := clRed;
    OFBitmap.Canvas.Brush.Style := bsSolid;
    OFBitmap.Canvas.Pen.Color   := clRed;
    OFBitmap.Canvas.Pen.Style   := psSolid;
    OFBitmap.Canvas.FrameRect (OFBitmap.Canvas.ClipRect);

    OFImage.Picture.Assign (OFBitmap); { make sure it gets out there }
                                       { tell the library we want funny windows-bgr}
    if mng_set_canvasstyle (hHandle, MNG_CANVAS_BGRX8) <> 0 then
      MNGerror ('libmng reported an error setting the canvas style');

  end;

  Result := MNG_TRUE;
end;

{****************************************************************************}

{$F+}
function GetCanvasLine (hHandle : mng_handle;
                        iLinenr : mng_uint32) : mng_ptr; stdcall;
{$F-}

var OHForm : TMainForm;

begin                                  { get a fix on our form }
  OHForm := TMainForm (mng_get_userdata (hHandle));
                                       { easy with these bitmap objects ! }
  Result := OHForm.OFBitmap.ScanLine [iLinenr];
end;

{****************************************************************************}

{$F+}
function ImageRefresh (hHandle : mng_handle;
                       iX      : mng_uint32;
                       iY      : mng_uint32;
                       iWidth  : mng_uint32;
                       iHeight : mng_uint32) : mng_bool; stdcall;
{$F-}

var OHForm : TMainForm;

begin                                  { get a fix on our form }
  OHForm := TMainForm (mng_get_userdata (hHandle));
                                       { force redraw }
  OHForm.OFImage.Picture.Assign (OHForm.OFBitmap);

  Result := MNG_TRUE;
end;


{****************************************************************************}

{$F+}
function GetTickCount (hHandle : mng_handle) : mng_uint32; stdcall;
{$F-}
begin
  Result := Windows.GetTickCount;      { windows knows that }
end;

{****************************************************************************}

{$F+}
function SetTimer (hHandle : mng_handle;
                   iMsecs  : mng_uint32) : mng_bool; stdcall;
{$F-}

var OHForm : TMainForm;

begin                                  { get a fix on our form }
  OHForm := TMainForm (mng_get_userdata (hHandle));
  OHForm.OFTimer.Interval := iMsecs;   { and set the timer }
  OHForm.OFTimer.Enabled  := true;

  Result := MNG_TRUE;
end;

{****************************************************************************}

procedure TMainForm.FormCreate(Sender: TObject);

var IHRed, IHGreen, IHBlue : word;

begin                                  { initialize }
  OFBitmap                := TBitmap.Create;
  IFBytesPerSec           := 10000000;
  BFHasMouse              := false;
  OFFile                  := nil;

  OFOpenDialog.Initialdir := '';
  OFBitmap.HandleType     := bmDIB;    { make it a 24-bit DIB }
  OFBitmap.PixelFormat    := pf32bit;
                                       { now initialize the library }
  IFHandle := mng_initialize (mng_ptr(self), Memalloc, Memfree, nil);

  if IFHandle = NIL then
  begin
    MNGerror ('libmng initialization error' + #13#10 +
              'Program aborted');
    Windows.Postmessage (handle, WM_Close, 0, 0);
    Exit;
  end;
                                       { no need to store chunk-info ! }
  mng_set_storechunks    (IFHandle, MNG_FALSE);
                                       { do not use suspension-buffer }
  mng_set_suspensionmode (IFHandle, MNG_FALSE);
                                       { set all the callbacks }
  if (mng_setcb_openstream    (IFHandle, Openstream   ) <> MNG_NOERROR) or
     (mng_setcb_closestream   (IFHandle, Closestream  ) <> MNG_NOERROR) or
     (mng_setcb_readdata      (IFHandle, Readdata     ) <> MNG_NOERROR) or
     (mng_setcb_processheader (IFHandle, ProcessHeader) <> MNG_NOERROR) or
     (mng_setcb_getcanvasline (IFHandle, GetCanvasLine) <> MNG_NOERROR) or
     (mng_setcb_refresh       (IFHandle, ImageRefresh ) <> MNG_NOERROR) or
     (mng_setcb_gettickcount  (IFHandle, GetTickCount ) <> MNG_NOERROR) or
     (mng_setcb_settimer      (IFHandle, SetTimer     ) <> MNG_NOERROR) then
  begin
    MNGerror ('libmng reported an error setting a callback function!' + #13#10 +
              'Program aborted');
    Windows.Postmessage (handle, WM_Close, 0, 0);
    Exit;
  end;

  IHRed   := (Color       ) and $FF;   { supply our own bg-color }
  IHGreen := (Color shr  8) and $FF;
  IHBlue  := (Color shr 16) and $FF;

  IHRed   := (IHRed   shl 8) + IHRed;
  IHGreen := (IHGreen shl 8) + IHGreen;
  IHBlue  := (IHBlue  shl 8) + IHBlue;

  if mng_set_bgcolor (IFHandle, IHRed, IHGreen, IHBlue) <> MNG_NOERROR then
    MNGerror ('libmng reported an error setting the background color!');

end;

{****************************************************************************}

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  OFTimer.Enabled := false;
  BFCancelled     := true;
                                       { if we're still animating then stop it }
  if mng_status_running (IFHandle) and not mng_status_reading (IFHandle) then
    if mng_display_freeze (IFHandle) <> MNG_NOERROR then
      MNGerror ('libmng reported an error during display_freeze!');

  mng_cleanup (IFHandle);
end;

{****************************************************************************}

procedure TMainForm.FormShow(Sender: TObject);
begin
  FormResize (self);
end;

{****************************************************************************}

procedure TMainForm.FormResize(Sender: TObject);
begin                                  { center the image in the window }
  if ClientWidth  < OFImage.Width  then
    OFImage.Left := 0
  else
    OFImage.Left := (ClientWidth  - OFImage.Width ) div 2;

  if ClientHeight < OFImage.Height then
    OFImage.Top  := 0
  else
    OFImage.Top  := (ClientHeight - OFImage.Height) div 2;

end;

{****************************************************************************}

procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = vk_Escape then              { pressing <esc> will freeze an animation }
  begin
    OFTimer.Enabled := false;          { don't let that timer go off then ! }
    BFCancelled     := true;

    if mng_status_running (IFHandle) and not mng_status_reading (IFHandle) then
      if mng_display_freeze (IFHandle) <> MNG_NOERROR then
        MNGerror ('libmng reported an error during display_freeze!');
  end;
end;

{****************************************************************************}

procedure TMainForm.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if mng_status_dynamic (IFHandle) then
  begin
    if BFHasMouse then                 { if we had the mouse, it's left ! }
    begin
      if mng_trapevent (IFHandle, 3, 0, 0) <> MNG_NOERROR then
        MNGerror ('libmng reported an error during trapevent!');

      BFHasMouse := false;
    end;
  end;
end;

{****************************************************************************}

procedure TMainForm.OFImageMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  if mng_status_dynamic (IFHandle) then
  begin
    if BFHasMouse then                 { did we have the mouse already ? }
    begin
      if mng_trapevent (IFHandle, 2, X, Y) <> MNG_NOERROR then
        MNGerror ('libmng reported an error during trapevent!');
    end
    else
    begin                              { if not, it has entered ! }
      if mng_trapevent (IFHandle, 1, X, Y) <> MNG_NOERROR then
        MNGerror ('libmng reported an error during trapevent!');

      BFHasMouse := true;
    end;
  end;
end;

{****************************************************************************}

procedure TMainForm.OFImageMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if mng_status_dynamic (IFHandle) then
    if mng_trapevent (IFHandle, 4, X, Y) <> MNG_NOERROR then
      MNGerror ('libmng reported an error during trapevent!');
end;

{****************************************************************************}

procedure TMainForm.OFImageMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if mng_status_dynamic (IFHandle) then
    if mng_trapevent (IFHandle, 5, X, Y) <> MNG_NOERROR then
      MNGerror ('libmng reported an error during trapevent!');
end;

{****************************************************************************}

procedure TMainForm.OFTimerTimer(Sender: TObject);

var IHRslt : mng_retcode;

begin
  OFTimer.Enabled := false;            { only once ! }

  if not BFCancelled then
  begin                                { and inform the library }
    IHRslt := mng_display_resume (IFHandle);

    if (IHRslt <> MNG_NOERROR) and (IHRslt <> MNG_NEEDTIMERWAIT) then
      MNGerror ('libmng reported an error during display_resume!');

  end;
end;

{****************************************************************************}

procedure TMainForm.OFMenuFileOpenClick(Sender: TObject);

var IHRslt : mng_retcode;

begin
  OFOpenDialog.InitialDir := '';
  OFOpenDialog.FileName   := SFFileName;

  if OFOpenDialog.Execute then         { get the filename }
  begin
    if OFTimer.Enabled then            { if the lib was active; stop it }
    begin
      OFTimer.Enabled := false;

      Application.ProcessMessages;     { process any timer requests (for safety) }
                                       { now freeze the animation }
      if mng_display_freeze (IFHandle) <> MNG_NOERROR then
        MNGerror ('libmng reported an error during display_freeze!');
    end;
                                       { save interesting fields }
    SFFileName      := OFOpenDialog.FileName;
    IFTicks         := Windows.GetTickCount;
    IFBytes         := 0;
    BFCancelled     := false;
                                       { always reset (just in case) }
    if mng_reset (IFHandle) <> MNG_NOERROR then
      MNGerror ('libmng reported an error during reset!')
    else
    begin                              { and let the lib do it's job ! }
      IHRslt := mng_readdisplay (IFHandle);

      if (IHRslt <> MNG_NOERROR) and (IHRSLT <> MNG_NEEDTIMERWAIT) then
        MNGerror ('libmng reported an error reading the input file!');

    end;
  end;
end;

{****************************************************************************}

procedure TMainForm.OFMenuFileProfileClick(Sender: TObject);

var SHProfileDir : array [0 .. MAX_PATH + 20] of char;

begin
  GetSystemDirectory (@SHProfileDir, MAX_PATH);
  strcat (@SHProfileDir, '\Color');

  OFOpenDialogProfile.InitialDir := strpas (@SHProfileDir);

  if OFOpenDialogProfile.Execute then
    if mng_set_outputprofile (IFHandle, pchar (OFOpenDialogProfile.FileName)) <> 0 then
      MNGerror ('libmng reported an error setting the output-profile!');

end;

{****************************************************************************}

procedure TMainForm.OFMenuFileExitClick(Sender: TObject);
begin
  if mng_cleanup (IFHandle) <> MNG_NOERROR then
    MNGerror ('libmng cleanup error');

  Close;
end;

{****************************************************************************}

procedure TMainForm.OFMenuOptionsModemSpeedClick(Sender: TObject);
begin
  OFMenuOptionsModem28k8.Checked      := false;
  OFMenuOptionsModem33k6.Checked      := false;
  OFMenuOptionsModem56k.Checked       := false;
  OFMenuOptionsModemISDN64.Checked    := false;
  OFMenuOptionsModemISDN128.Checked   := false;
  OFMenuOptionsModemCable512.Checked  := false;
  OFMenuOptionsModemUnlimited.Checked := false;

  if IFBytesPerSec = OFMenuOptionsModem28k8.Tag      div 10 then
    OFMenuOptionsModem28k8.Checked      := true
  else
  if IFBytesPerSec = OFMenuOptionsModem33k6.Tag      div 10 then
    OFMenuOptionsModem33k6.Checked      := true
  else
  if IFBytesPerSec = OFMenuOptionsModem56k.Tag       div 10 then
    OFMenuOptionsModem56k.Checked       := true
  else
  if IFBytesPerSec = OFMenuOptionsModemISDN64.Tag    div 10 then
    OFMenuOptionsModemISDN64.Checked    := true
  else
  if IFBytesPerSec = OFMenuOptionsModemISDN128.Tag   div 10 then
    OFMenuOptionsModemISDN128.Checked   := true
  else
  if IFBytesPerSec = OFMenuOptionsModemUnlimited.Tag div 10 then
    OFMenuOptionsModemCable512.Checked  := true
  else
    OFMenuOptionsModemUnlimited.Checked := true;

end;

{****************************************************************************}

procedure TMainForm.OFMenuOptionsModemXClick(Sender: TObject);
begin
  IFBytesPerSec := TMenuItem (Sender).Tag div 10;
end;

{****************************************************************************}

procedure TMainForm.MNGerror;

var iErrorcode : mng_uint32;
    iSeverity  : mng_uint8;
    iChunkname : mng_chunkid;
    iChunkseq  : mng_uint32;
    iExtra1    : mng_int32;
    iExtra2    : mng_int32;
    zErrortext : mng_pchar;

begin                                  { get extended info }
  iErrorcode := mng_getlasterror (IFHandle, iSeverity, iChunkname, iChunkseq,
                                            iExtra1, iExtra2, zErrortext);

  MessageDlg (SHMsg + #13#10#13#10 + strpas (zErrortext) + #13#10#13#10 +
              Format ('Error = %d; Severity = %d; Chunknr = %d; Extra1 = %d',
                      [iErrorcode, iSeverity, iChunkseq, iExtra1]),
              mtError, [mbOK], 0);
end;

{****************************************************************************}

end.