summaryrefslogtreecommitdiff
path: root/demos/csharp/linux/gtk_viewer/src/MainWindow.cs
blob: 22b6dc7c1c11f387436340ed8a47cc3d1f525d7b (plain)
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/* Copyright (C) 2020-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

using System;
using System.Collections.Generic;
using Gtk;
using GhostMono;
using System.Diagnostics;

namespace gs_mono_example
{
    static class Constants
    {
        public const double SCALE_THUMB = 0.1;
        public const int DEFAULT_GS_RES = 300;
        public const int ZOOM_MAX = 4;
        public const double ZOOM_MIN = .25;
    }

    public enum NotifyType_t
    {
        MESS_STATUS,
        MESS_ERROR
    };

    public enum Page_Content_t
    {
        FULL_RESOLUTION = 0,
        THUMBNAIL,
        OLD_RESOLUTION,
        LOW_RESOLUTION,
        NOTSET,
        BLANK
    };

    public struct pagesizes_t
    {
        public double width;
        public double height;
    }

    public partial class MainWindow : Gtk.Window
    {
        GSMONO m_ghostscript;
        bool m_file_open;
        String m_currfile;
        String m_extension;
        List<TempFile> m_tempfiles;
        String m_origfile;
        int m_currpage;
        gsOutput m_gsoutput;
        public int m_numpages;
        private static Pages m_docPages;
        private static double m_doczoom;
        public List<pagesizes_t> m_page_sizes;
        bool m_init_done;
        bool m_busy_render;
        bool m_firstime;
        bool m_aa;
        bool m_aa_change;
        List<int> m_page_scroll_pos;
        Gtk.ProgressBar m_GtkProgressBar;
        Label m_GtkProgressLabel;
        HBox m_GtkProgressBox;
        Gtk.TreeView m_GtkTreeThumb;
        Gtk.TreeView m_GtkTreeMain;
        Gtk.VBox m_GtkvBoxMain;
        Label m_GtkpageTotal;
        Entry m_GtkpageEntry;
        Gtk.ListStore m_GtkimageStoreThumb;
        Gtk.ListStore m_GtkimageStoreMain;
        Gtk.ScrolledWindow m_GtkthumbScroll;
        Gtk.ScrolledWindow m_GtkmainScroll;
        Gtk.Entry m_GtkzoomEntry;
        Gtk.CheckButton m_GtkaaCheck;
        Gtk.Button m_GtkZoomPlus;
        Gtk.Button m_GtkZoomMinus;
        String m_zoom_txt;
        String m_page_txt;
        bool m_ignore_scroll_change;

        void ShowMessage(NotifyType_t type, string message)
        {
            Gtk.MessageType mess;

            if (type == NotifyType_t.MESS_ERROR)
            {
                mess = MessageType.Error;
            }
            else
            {
                mess = MessageType.Warning;
            }

            /* Dispatch on UI thread */
            Gtk.Application.Invoke(delegate
            {
                MessageDialog md = new MessageDialog(null,
                                   DialogFlags.Modal,
                                   mess,
                                   ButtonsType.Ok,
                                   message);
                md.SetPosition(WindowPosition.Center);
                md.ShowAll();
                md.Run();
                md.Destroy();
            });
        }

        public MainWindow() : base(Gtk.WindowType.Toplevel)
        {
            /* Set up ghostscript calls for progress update */
            m_ghostscript = new GSMONO();
            m_ghostscript.ProgressCallBack += new GSMONO.Progress(gsProgress);
            m_ghostscript.StdIOCallBack += new GSMONO.StdIO(gsIO);
            m_ghostscript.DLLProblemCallBack += new GSMONO.DLLProblem(gsDLL);

            DeleteEvent += delegate { Application.Quit(); };

            m_currpage = 0;
            m_gsoutput = new gsOutput();
            m_gsoutput.Activate();
            m_tempfiles = new List<TempFile>();
            m_thumbnails = new List<DocPage>();
            m_docPages = new Pages();
            m_page_sizes = new List<pagesizes_t>();
            m_page_scroll_pos = new List<int>();
            m_file_open = false;
            m_doczoom = 1.0;
            m_init_done = false;
            m_busy_render = true;
            m_firstime = true;
            m_aa = true;
            m_aa_change = false;
            m_zoom_txt = "100";
            m_page_txt = "1";
            m_ignore_scroll_change = false;

            /* Set up Vbox in main window */
            this.SetDefaultSize(500, 700);
            this.Title = "GhostPDL Mono GTK Demo";
            m_GtkvBoxMain = new VBox(false, 0);
            this.Add(m_GtkvBoxMain);

            /* Add Menu Bar to vBox */
            Gtk.MenuBar menu_bar = new MenuBar();
            Menu filemenu = new Menu();
            MenuItem file = new MenuItem("File");
            file.Submenu = filemenu;

            AccelGroup agr = new AccelGroup();
            AddAccelGroup(agr);

            ImageMenuItem openi = new ImageMenuItem(Stock.Open, agr);
            openi.AddAccelerator("activate", agr, new AccelKey(
                Gdk.Key.o, Gdk.ModifierType.ControlMask, AccelFlags.Visible));
            openi.Activated += OnOpen;
            filemenu.Append(openi);

            ImageMenuItem closei = new ImageMenuItem(Stock.Close, agr);
            closei.AddAccelerator("activate", agr, new AccelKey(
                Gdk.Key.w, Gdk.ModifierType.ControlMask, AccelFlags.Visible));
            closei.Activated += OnClose;
            filemenu.Append(closei);

            MenuItem messagesi = new MenuItem("Show Messages");
            messagesi.Activated += OnShowMessages;
            filemenu.Append(messagesi);

            MenuItem about = new MenuItem("About");
            about.Activated += OnAboutClicked;
            filemenu.Append(about);

            SeparatorMenuItem sep = new SeparatorMenuItem();
            filemenu.Append(sep);

            ImageMenuItem quiti = new ImageMenuItem(Stock.Quit, agr);
            quiti.AddAccelerator("activate", agr, new AccelKey(
                Gdk.Key.q, Gdk.ModifierType.ControlMask, AccelFlags.Visible));
            filemenu.Append(quiti);
            quiti.Activated += OnQuit;

            menu_bar.Append(file);

            m_GtkvBoxMain.PackStart(menu_bar, false, false, 0);

            /* Add a hbox with the page information, zoom control, and aa to vbox */
            HBox pageBox = new HBox(false, 0);
            m_GtkpageEntry = new Entry();
            m_GtkpageEntry.Activated += PageChanged;
            m_GtkpageEntry.WidthChars = 4;
            m_GtkpageTotal = new Label("/0");
            pageBox.PackStart(m_GtkpageEntry, false, false, 0);
            pageBox.PackStart(m_GtkpageTotal, false, false, 0);

            HBox zoomBox = new HBox(false, 0);
            m_GtkZoomPlus = new Button();
            m_GtkZoomPlus.Label = "+";
            m_GtkZoomPlus.Clicked += ZoomIn;
            m_GtkZoomMinus = new Button();
            m_GtkZoomMinus.Label = "–";
            m_GtkZoomMinus.Clicked += ZoomOut;
            m_GtkzoomEntry = new Entry();
            m_GtkzoomEntry.WidthChars = 3;
            m_GtkzoomEntry.Activated += ZoomChanged;
            Label precentLabel = new Label("%");
            zoomBox.PackStart(m_GtkZoomPlus, false, false, 0);
            zoomBox.PackStart(m_GtkZoomMinus, false, false, 0);
            zoomBox.PackStart(m_GtkzoomEntry, false, false, 0);
            zoomBox.PackStart(precentLabel, false, false, 0);

            HBox hBoxControls = new HBox(false, 0);
            m_GtkaaCheck = new CheckButton("Enable Antialias:");
            m_GtkaaCheck.Active = true;
            m_GtkaaCheck.Clicked += AaCheck_Clicked;
            hBoxControls.PackStart(pageBox, false, false, 0);
            hBoxControls.PackStart(zoomBox, false, false, 20);
            hBoxControls.PackStart(m_GtkaaCheck, false, false, 0);

            m_GtkvBoxMain.PackStart(hBoxControls, false, false, 0);

            /* Tree view containing thumbnail and main images */
            HBox hBoxPages = new HBox(false, 0);

            /* Must be scrollable */
            m_GtkthumbScroll = new ScrolledWindow();
            m_GtkthumbScroll.BorderWidth = 5;
            m_GtkthumbScroll.ShadowType = ShadowType.In;

            m_GtkmainScroll = new ScrolledWindow();
            m_GtkmainScroll.BorderWidth = 5;
            m_GtkmainScroll.ShadowType = ShadowType.In;
            m_GtkmainScroll.Vadjustment.ValueChanged += Vadjustment_Changed;

            m_GtkTreeThumb = new Gtk.TreeView();
            m_GtkTreeThumb.HeadersVisible = false;
            m_GtkimageStoreThumb = new Gtk.ListStore(typeof(Gdk.Pixbuf));
            m_GtkTreeThumb.AppendColumn("Thumb", new Gtk.CellRendererPixbuf(), "pixbuf", 0);
            m_GtkTreeThumb.Style.YThickness = 100;
            m_GtkthumbScroll.Add(m_GtkTreeThumb);
            m_GtkTreeThumb.RowActivated += M_GtkTreeThumb_RowActivated;

            m_GtkTreeMain = new Gtk.TreeView();
            m_GtkTreeMain.HeadersVisible = false;
            m_GtkTreeMain.RulesHint = false;

            m_GtkimageStoreMain = new Gtk.ListStore(typeof(Gdk.Pixbuf));
            m_GtkTreeMain.AppendColumn("Main", new Gtk.CellRendererPixbuf(), "pixbuf", 0);
            m_GtkmainScroll.Add(m_GtkTreeMain);

            // Separate with gridlines
            m_GtkTreeMain.EnableGridLines = TreeViewGridLines.Horizontal;

            //To disable selections, set the selection mode to None:
            m_GtkTreeMain.Selection.Mode = SelectionMode.None;

            hBoxPages.PackStart(m_GtkthumbScroll, false, false, 0);
            hBoxPages.PackStart(m_GtkmainScroll, true, true, 0);

            m_GtkTreeThumb.Model = m_GtkimageStoreThumb;
            m_GtkTreeMain.Model = m_GtkimageStoreMain;

            m_GtkvBoxMain.PackStart(hBoxPages, true, true, 0);

            /* For case of opening another file */
            string[] arguments = Environment.GetCommandLineArgs();
            if (arguments.Length > 1)
            {
                m_currfile = arguments[1];
                ProcessFile();
            }
        }

        void Vadjustment_Changed(object sender, EventArgs e)
        {
            if (!m_init_done)
            {
                return;
            }

            if (m_ignore_scroll_change)
            {
                m_ignore_scroll_change = false;
                return;
            }

            Gtk.Adjustment zz = sender as Gtk.Adjustment;
            double val = zz.Value;
            int page = 1;

            for (int k = 0; k < m_numpages; k++)
            {
                if (val <= m_page_scroll_pos[k])
                {
                    m_GtkpageEntry.Text = page.ToString();
                    m_page_txt = m_GtkpageEntry.Text;
                    return;
                }
                page += 1;
            }
            m_GtkpageEntry.Text = m_numpages.ToString();
            m_page_txt = m_GtkpageEntry.Text;
        }

        void OnAboutClicked(object sender, EventArgs args)
        {
            AboutDialog about = new AboutDialog();
            about.ProgramName = "GhostPDL Gtk# Viewer";
            about.Version = "0.1";
            about.Copyright = "(c) Artifex Software";
            about.Comments = @"A demo of GhostPDL API with C# Mono";
            about.Website = "http://www.artifex.com";
            about.Run();
            about.Destroy();
        }

        /* C# Insanity to get the index of the selected item */
        void M_GtkTreeThumb_RowActivated(object o, RowActivatedArgs args)
        {
            TreeModel treeModel;
            TreeSelection my_selected_row = m_GtkTreeThumb.Selection;

            var TreePath = my_selected_row.GetSelectedRows(out treeModel);
            var item = TreePath.GetValue(0);
            var result = item.ToString();
            int index = System.Convert.ToInt32(result);

            ScrollMainTo(index);                  
        }

        void ScrollMainTo(int index)
        {
            m_ignore_scroll_change = true;

            if (index < 0 || index > m_numpages - 1)
                return;

            if (index == 0)
                m_GtkmainScroll.Vadjustment.Value = 0;
            else
                m_GtkmainScroll.Vadjustment.Value = m_page_scroll_pos[index-1];

            m_currpage = index + 1;
            m_GtkpageEntry.Text = m_currpage.ToString();
            m_page_txt = m_GtkpageEntry.Text;
        }

        void AddProgressBar(String text)
        {
            /* Progress bar */
            m_GtkProgressBox = new HBox(false, 0);
            m_GtkProgressBar = new ProgressBar();
            m_GtkProgressBar.Orientation = ProgressBarOrientation.LeftToRight;
            m_GtkProgressBox.PackStart(m_GtkProgressBar, true, true, 0);
            m_GtkProgressLabel = new Label(text);
            m_GtkProgressBox.PackStart(m_GtkProgressLabel, false, false, 0);
            m_GtkvBoxMain.PackStart(m_GtkProgressBox, false, false, 0);
            m_GtkProgressBar.Fraction = 0.0;
            this.ShowAll();
        }

        void RemoveProgressBar()
        {
            m_GtkvBoxMain.Remove(m_GtkProgressBox);
            this.ShowAll();
        }

        void AaCheck_Clicked(object sender, EventArgs e)
        {
            m_aa = !m_aa;
            m_aa_change = true;
            if (m_init_done && !m_busy_render)
                RenderMainAll();
        }

        private void OnQuit(object sender, EventArgs e)
        {
            Application.Quit();
        }

        private void OnShowMessages(object sender, EventArgs e)
        {
            m_gsoutput.Show();
        }

        private void OnClose(object sender, EventArgs e)
        {
            if (m_init_done)
                CleanUp(); 
        }

        private void gsIO(String mess, int len)
        {
            Gtk.TextBuffer buffer = m_gsoutput.m_textView.Buffer;
            Gtk.TextIter ti = buffer.EndIter;

            var part = mess.Substring(0, len);
            buffer.Insert(ref ti, part);
        }

        private void gsDLL(String mess)
        {
            ShowMessage(NotifyType_t.MESS_STATUS, mess);
        }

        private void gsThumbRendered(int width, int height, int raster,
                                    IntPtr data, gsParamState_t state)
        {
            ThumbPageCallback(width, height, raster, state.zoom, state.currpage, data);
        }

        private void gsPageRendered(int width, int height, int raster,
                                    IntPtr data, gsParamState_t state)
        {
            MainPageCallback(width, height, raster, state.zoom, state.currpage, data);
        }

        private void gsProgress(gsThreadCallBack info)
        {
            if (info.Completed)
            {
                switch (info.Params.task)
                {
                    case GS_Task_t.PS_DISTILL:
                        m_GtkProgressBar.Fraction = 1.0;
                        RemoveProgressBar();
                        break;

                    case GS_Task_t.SAVE_RESULT:
                        break;

                    case GS_Task_t.DISPLAY_DEV_THUMBS_NON_PDF:
                    case GS_Task_t.DISPLAY_DEV_THUMBS_PDF:
                        ThumbsDone();
                        break;

                    case GS_Task_t.DISPLAY_DEV_PDF:
                    case GS_Task_t.DISPLAY_DEV_NON_PDF:
                        RenderingDone();
                        break;
                }

                if (info.Params.result == GS_Result_t.gsFAILED)
                {
                    switch (info.Params.task)
                    {
                        case GS_Task_t.CREATE_XPS:
                            ShowMessage(NotifyType_t.MESS_STATUS, "Ghostscript failed to create XPS");
                            break;

                        case GS_Task_t.PS_DISTILL:
                            ShowMessage(NotifyType_t.MESS_STATUS, "Ghostscript failed to distill PS");
                            break;

                        case GS_Task_t.SAVE_RESULT:
                            ShowMessage(NotifyType_t.MESS_STATUS, "Ghostscript failed to convert document");
                            break;

                        default:
                            ShowMessage(NotifyType_t.MESS_STATUS, "Ghostscript failed.");
                            break;
                    }
                    return;
                }
                GSResult(info.Params);
            }
            else
            {
                switch (info.Params.task)
                {
                    case GS_Task_t.CREATE_XPS:
                    case GS_Task_t.PS_DISTILL:
                        m_GtkProgressBar.Fraction = (double)(info.Progress) / 100.0;
                        break;

                    case GS_Task_t.SAVE_RESULT:
                        break;
                }
            }
        }

        /* GS Result*/
        public void GSResult(gsParamState_t gs_result)
        {
            TempFile tempfile = null;

            if (gs_result.outputfile != null)
                 tempfile = new TempFile(gs_result.outputfile);

            if (gs_result.result == GS_Result_t.gsCANCELLED)
            {
                if (tempfile != null)
                {
                    try
                    {
                        tempfile.DeleteFile();
                    }
                    catch
                    {
                        ShowMessage(NotifyType_t.MESS_STATUS, "Problem Deleting Temp File");
                    }
                }
                return;
            }
            if (gs_result.result == GS_Result_t.gsFAILED)
            {
                ShowMessage(NotifyType_t.MESS_STATUS, "GS Failed Conversion");
                if (tempfile != null)
                {
                    try
                    {
                        tempfile.DeleteFile();
                    }
                    catch
                    {
                        ShowMessage(NotifyType_t.MESS_STATUS, "Problem Deleting Temp File");
                    }
                }
                return;
            }

            switch (gs_result.task)
            {
                case GS_Task_t.PS_DISTILL:
                    m_origfile = gs_result.inputfile;

                    Gtk.FileChooserDialog dialog = new Gtk.FileChooserDialog("Save Document",
                                                    (Gtk.Window)this.Toplevel,
                                                     Gtk.FileChooserAction.Save);                                                   
                    dialog.LocalOnly = true;
                    dialog.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
                    dialog.AddButton(Gtk.Stock.Save, Gtk.ResponseType.Yes);
                    dialog.SetFilename(System.IO.Path.GetFileNameWithoutExtension(m_origfile) + ".pdf");             

                    Gtk.FileFilter filter = new Gtk.FileFilter();
                    filter.Name = "doc/pdf";
                    filter.AddMimeType("application/pdf");
                    filter.AddPattern("*.pdf");
                    dialog.Filter = filter;

                    int response = dialog.Run();
                    if (response == (int)Gtk.ResponseType.Yes)
                    {
                        m_currfile = dialog.Filename;
                        try
                        {
                            if (System.IO.File.Exists(m_currfile))
                            {
                                System.IO.File.Delete(m_currfile);
                            }

                            System.IO.File.Exists(tempfile.Filename);
                            System.IO.File.Copy(tempfile.Filename, dialog.Filename);
                        }
                        catch (Exception except)
                        {
                            ShowMessage(NotifyType_t.MESS_ERROR, "Exception Saving Distilled Result:" + except.Message);
                        }
                    }

                    dialog.Destroy();
                    tempfile.DeleteFile();
                    break;
            }                        
        }

        protected void OnDeleteEvent(object sender, DeleteEventArgs a)
        {
            Application.Quit();
            a.RetVal = true;
        }

        protected void OnOpen(object sender, EventArgs e)
        {
            Gtk.FileChooserDialog dialog = new Gtk.FileChooserDialog("Open Document",
                    (Gtk.Window)this.Toplevel,
                    Gtk.FileChooserAction.Open);

            dialog.AddButton(Gtk.Stock.Cancel, Gtk.ResponseType.Cancel);
            dialog.AddButton(Gtk.Stock.Open, Gtk.ResponseType.Accept);

            dialog.DefaultResponse = Gtk.ResponseType.Cancel;
            dialog.LocalOnly = true;

            Gtk.FileFilter filter = new Gtk.FileFilter();
            filter.Name = "doc/pdf";
            filter.AddMimeType("application/pdf");
            filter.AddMimeType("application/ps");
            filter.AddMimeType("application/eps");
            filter.AddMimeType("application/pcl");
            filter.AddMimeType("application/xps");
            filter.AddMimeType("application/oxps");
            filter.AddPattern("*.pdf");
            filter.AddPattern("*.ps");
            filter.AddPattern("*.eps");
            filter.AddPattern("*.bin");
            filter.AddPattern("*.xps");
            filter.AddPattern("*.oxps");
            dialog.Filter = filter;
            int response = dialog.Run();

            if (response != (int)Gtk.ResponseType.Accept)
            {
                dialog.Destroy();
                return;
            }

            if (m_file_open)
            {
                /* launch a new process */
                string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

                try
                {
                    String name = dialog.Filename;
                    Process.Start(path, name);
                }
                catch (Exception except)
                {
                    Console.WriteLine("Problem opening file: " + except.Message);
                }
                dialog.Destroy();
                return;
            }

            m_currfile = dialog.Filename;
            dialog.Destroy();
            ProcessFile();
        }

        public void ProcessFile()
        {
            m_extension = m_currfile.Split('.')[1];
            int result;
            if (!(m_extension.ToUpper() == "PDF" || m_extension.ToUpper() == "pdf"))
            {
                MessageDialog md = new MessageDialog(this,
                    DialogFlags.DestroyWithParent, MessageType.Question,
                    ButtonsType.YesNo, "Would you like to Distill this file ?");

                result = md.Run();
                md.Destroy();
                if ((ResponseType)result == ResponseType.Yes)
                {
                    AddProgressBar("Distilling");
                    if (m_ghostscript.DistillPS(m_currfile, Constants.DEFAULT_GS_RES) == gsStatus.GS_BUSY)
                    {
                        ShowMessage(NotifyType_t.MESS_STATUS, "GS currently busy");
                        return;
                    }
                    return;
                }
            }
            m_file_open = true;
            RenderThumbs();
        }

        private void CleanUp()
        {
            /* Clear out everything */
            if (m_docPages != null && m_docPages.Count > 0)
                m_docPages.Clear();
            if (m_thumbnails != null && m_thumbnails.Count > 0)
                m_thumbnails.Clear();
            if (m_page_sizes != null && m_page_sizes.Count > 0)
                m_page_sizes.Clear();
            if (m_page_scroll_pos != null && m_page_scroll_pos.Count > 0)
                m_page_scroll_pos.Clear();

            m_GtkimageStoreThumb.Clear();
            m_GtkimageStoreMain.Clear();

            m_currpage = 0;
            m_thumbnails = new List<DocPage>();
            m_docPages = new Pages();
            m_page_sizes = new List<pagesizes_t>();
            m_page_scroll_pos = new List<int>();

            m_file_open = false;
            m_doczoom = 1.0;
            m_init_done = false;
            m_busy_render = true;
            m_firstime = true;
            m_aa = true;
            m_aa_change = false;
            m_zoom_txt = "100";
            m_page_txt = "0";
            m_GtkpageTotal.Text = "/ 0";
            m_ignore_scroll_change = false;
            m_currfile = null;
            m_origfile = null;
            m_numpages = -1;
            m_file_open = false;
            CleanUpTempFiles();
            m_tempfiles = new List<TempFile>();
            return;
        }

        private void CleanUpTempFiles()
        {
            for (int k = 0; k < m_tempfiles.Count; k++)
            {
                try
                {
                    m_tempfiles[k].DeleteFile();
                }
                catch
                {
                    ShowMessage(NotifyType_t.MESS_STATUS, "Problem Deleting Temp File");
                }
            }
            m_tempfiles.Clear();
        }
    }
}