/[EmuXPortal]/branches/mono/EmuXPortal/Form1.cs
ViewVC logotype

Annotation of /branches/mono/EmuXPortal/Form1.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 355 - (hide annotations) (download)
Tue Dec 19 20:15:02 2017 UTC (2 years, 1 month ago) by william
File size: 48241 byte(s)
reintegrate branch /branches/mono-cache-support
1 william 266 #define HAVE_X11_BORDERSTYLE_ERROR // indicates that there is an error with Control.set_InternalBorderStyle resulting in an X11 Error BadWindow (invalid Window parameter)
2    
3     //#define DISABLE_CURSOR_HIDE // when this is present, the cursor will not be hidden
4 william 183 //#define DISABLE_PROGRESS_PERCENTAGE_MESSASGE // when this is present, no progress percent message will be shown on any progressbar
5 william 272
6 william 241 #define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
7     //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
8 william 355
9 william 181 using System;
10 william 4 using System.Collections.Generic;
11     using System.ComponentModel;
12     using System.Data;
13     using System.Drawing;
14     using System.Linq;
15     using System.Text;
16     using System.Windows.Forms;
17 william 12 using EmuXPortal.Api;
18 william 23 using System.Diagnostics;
19 william 60 using System.Reflection;
20     using System.Threading;
21 william 183 using Utilities.TransparentControls;
22 william 238 using Enterprise.Logging;
23     using System.IO;
24 william 272 using Enterprise.CrossPlatform;
25 william 4
26     namespace EmuXPortal
27 william 355 {
28 william 4 public partial class Form1 : Form
29     {
30 william 355 static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR = System.Drawing.Color.Lime;
31     static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR_ERROR = System.Drawing.Color.Yellow;
32     static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR_LAUNCH = System.Drawing.Color.Blue;
33     static readonly System.Drawing.Color DEFAULT_TEXTFORECOLOR_MISSING = System.Drawing.Color.Red;
34    
35    
36     static bool bUserAbort =false;
37 william 255 static readonly Color SELECTED_CONTROL_BACKCOLOR = Color.SteelBlue;
38 william 183 const float PROGRESS_BAR_FONT_SIZE = 24;
39     private delegate Font Delegate_GetFormFont();
40     private delegate Font Delegate_ResizeFont(Font font, float size);
41 william 324 IEmuConfig CurrentSelectedRom = null;
42     PlatformControl CurrentPlatformControl = null;
43     int SavedPlatformIndex= -1;
44     GameControl CurrentGameControl = null;
45 william 327 private CustomProgressBar progress_bar;
46 william 355 private IPlatformCache cache = null;
47 william 243
48 william 355 static bool OnAbort()
49     {
50     return bUserAbort;
51     }
52 william 243 #region unhandled exception support
53     static void Application_Unhandled_ThreadException(object sender, ThreadExceptionEventArgs e)
54     {
55     UnhandledExceptionEventArgs uea = new UnhandledExceptionEventArgs(e.Exception, false);
56     UnhandledExceptionEventHandler(sender, uea);
57     }
58     static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs args)
59     {
60     Exception ex = (args.ExceptionObject as Exception);
61     if (sender == null)
62     {
63     gLog.Error.WriteLine("Caught an unhandled exception from an unkown source");
64     }
65     else
66     {
67     gLog.Error.WriteLine("Caught an unhandled exception from type: {0}", sender.GetType().Name);
68     }
69    
70     if (ex == null)
71     {
72     gLog.Error.WriteLine("The exception object was null -- it probably is not derived from System.Exception");
73     }
74     else
75     {
76     ex = ex.GetBaseException();
77     gLog.Error.WriteLine("{0}:", ex.GetType().Name);
78     gLog.Verbose.Error.WriteLine(ex.ToString());
79     }
80    
81     }
82     #endregion
83 william 4 public Form1()
84     {
85 william 243 InitializeComponent();
86 william 327 this.progress_bar = new CustomProgressBar();
87 william 266 this.KeyPreview = true;
88 william 243
89 william 238 #region logging support
90     string log_path = Application.StartupPath;
91     string log_filename = string.Format("{0}.log", typeof(Form1).Assembly.GetName().Name);
92 william 283 FileInfo log_file = new FileInfo(OSInfo.FormatPath(string.Format(@"{0}\{1}", log_path, log_filename)));
93 william 238
94 william 283 gLog.CreateLog(log_file.Name, true, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
95 william 238 #if DEBUG
96     LogLevel gLevel = gLog.LogLevel;
97     #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
98     gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
99     #else
100     gLevel |= LogLevel.kLogLevel_VerboseDebug;
101     #endif
102     gLevel |= LogLevel.kLogLevel_Debug;
103     gLog.SetLogLevel(gLevel);
104     #else
105     LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
106     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
107     #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
108     gLevel &= ~LogLevel.kLogLevel_Debug;
109     #else
110     gLevel |= LogLevel.kLogLevel_Debug;
111     #endif
112     gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
113     gLog.SetLogLevel(gLevel);
114     #endif
115     #endregion
116    
117 william 243 #region unhandled exception support
118 william 266 // AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
119     // Application.ThreadException += Application_Unhandled_ThreadException;
120 william 243 #endregion
121    
122 william 17 platform_flow.Dock = DockStyle.Fill;
123     rom_flow.Dock = DockStyle.Fill;
124 william 355
125     // this.PreviewKeyDown += Form1_PreviewKeyDown;
126     // this.KeyPreview = true;
127     //progress_bar.PreviewKeyDown += Progress_bar_PreviewKeyDown;
128     this.KeyDown += Form1_KeyDown;
129    
130 william 238 }
131 william 243
132 william 355 void Form1_KeyDown (object sender, KeyEventArgs e)
133     {
134     if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape) {
135     bAbortRomCopyToLocalCache = true; // this will cancel any copy operations
136     }
137     }
138    
139 william 243
140    
141    
142 william 238 #region logging support
143     void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
144     void OnLogFlush(string logmessage)
145     {
146     //if (this.IsDisposed) { return; }
147     ////UpdateStatus(logmessage);
148     ////UpdateLogOutput(logmessage);
149     //Application.DoEvents();
150     }
151     #endregion
152 william 184
153 william 355 private bool bCachedLoaded = false;
154     private bool OnCacheLoaded() {
155     return bCachedLoaded;
156     }
157    
158 william 17 private void Form1_Load(object sender, EventArgs e)
159     {
160 william 355
161     SplashLoader loader = new SplashLoader(new AbortEvent(OnCacheLoaded));
162     loader.Show();
163     cache = PlatformCache.Create (new AbortEvent (OnAbort));
164     Config.LoadConfig(cache);
165 william 238 Config.InitializePresentationForm(this);
166 william 355 bCachedLoaded = true;
167    
168 william 17 }
169 william 181 private void Form1_Shown(object sender, EventArgs e)
170     {
171     platform_flow.Visible = true;
172     #if !DISABLE_CURSOR_HIDE
173     Cursor.Hide();
174     #else
175     Cursor.Show();
176     #endif
177 william 355
178 william 181 }
179 william 355 private bool bAbortRomCopyToLocalCache = false;
180     private bool OnAbortRomCopyToLocalCache ()
181     {
182     return bAbortRomCopyToLocalCache;
183     }
184 william 334
185 william 355 private void DeleteRomFromLocalCache (string rom_file, string rom_image)
186     {
187     progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
188     #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
189     progress_bar.ShowPercentageLabel = false;
190     #endif
191     progress_bar.ProgressColor = Color.LimeGreen;
192     progress_bar.Dock = DockStyle.Top;
193     if (this.InvokeRequired) {
194     this.Invoke ((MethodInvoker)delegate() {
195     this.Controls.Add (progress_bar);
196     });
197     } else {
198     this.Controls.Add (progress_bar);
199     }
200     progress_bar.Invoke (new MethodInvoker (delegate {
201     progress_bar.Margin = new System.Windows.Forms.Padding (0);
202     progress_bar.Size = new Size (GetFormWidth () - 25, 100);
203     }));
204     Application.DoEvents ();
205     if (!string.IsNullOrEmpty (rom_image)) {
206     if (rom_image.ToLower ().Contains (Config.RomPath.ToLower ())) {
207     gLog.Warn.WriteLine("Not deleteing rom image in path: {0}", rom_image);
208     return;
209     }
210     progress_bar.Message = string.Format("Deleting local rom image: {0}", EmuXPortal.Api.Win32.CompactPath(rom_image));
211     File.Delete(rom_image);
212     progress_bar.Value = 50;
213    
214     }
215     if (!string.IsNullOrEmpty (rom_file)) {
216     if (rom_image.ToLower ().Contains (Config.RomPath.ToLower ())) {
217     gLog.Warn.WriteLine("Not deleteing rom file in path: {0}", rom_file);
218     return;
219     }
220     progress_bar.Message = string.Format("Deleting local rom file: {0}", EmuXPortal.Api.Win32.CompactPath(rom_file));
221     File.Delete(rom_file);
222     progress_bar.Value = 100;
223     }
224     if (this.InvokeRequired) {
225     this.Invoke ((MethodInvoker)delegate() {
226     this.Controls.Remove (progress_bar);
227     });
228     } else {
229     this.Controls.Remove (progress_bar);
230     }
231     Application.DoEvents ();
232     }
233     private void CopyRomToLocalCache (string src_rom_file, string dst_rom_file, string src_rom_image, string dst_rom_image)
234     {
235     bAbortRomCopyToLocalCache = false; // reset the copy abort flag
236     progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
237     #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
238     progress_bar.ShowPercentageLabel = false;
239     #endif
240     progress_bar.ProgressColor = Color.LimeGreen;
241     progress_bar.Dock = DockStyle.Top;
242     if (this.InvokeRequired) {
243     this.Invoke ((MethodInvoker)delegate() {
244     this.Controls.Add (progress_bar);
245     });
246     } else {
247     this.Controls.Add (progress_bar);
248     }
249     progress_bar.Invoke (new MethodInvoker (delegate {
250     progress_bar.Margin = new System.Windows.Forms.Padding (0);
251     progress_bar.Size = new Size (GetFormWidth () - 25, 100);
252     }));
253     Application.DoEvents ();
254     FileProgressCopier cp = null;
255     if (!string.IsNullOrEmpty (src_rom_image) && !string.IsNullOrEmpty (dst_rom_image)) {
256     bAbortRomCopyToLocalCache = false;
257     cp = new FileProgressCopier (src_rom_image, dst_rom_image, new CopyProgressEvent (OnFileCopyProgress), new AbortEvent (OnAbortRomCopyToLocalCache));
258     cp.Start ();
259     while (!cp.CopyFinished) {
260     Application.DoEvents();
261     }
262     }
263     if (!string.IsNullOrEmpty (src_rom_file) && !string.IsNullOrEmpty (dst_rom_file)) {
264     bAbortRomCopyToLocalCache = false;
265     cp = new FileProgressCopier (src_rom_file, dst_rom_file, new CopyProgressEvent (OnFileCopyProgress), new AbortEvent (OnAbortRomCopyToLocalCache));
266     cp.Start ();
267     while (!cp.CopyFinished) {
268     Application.DoEvents();
269     }
270     }
271     if (this.InvokeRequired) {
272     this.Invoke ((MethodInvoker)delegate() {
273     this.Controls.Remove (progress_bar);
274     });
275     } else {
276     this.Controls.Remove (progress_bar);
277     }
278     Application.DoEvents ();
279     }
280     public void OnFileCopyProgress (object sender, int progress, string message)
281     {
282     //gLog.Debug.WriteLine("{0} [{1}%]", message, progress);
283    
284     if (progress_bar.InvokeRequired) {
285     progress_bar.Invoke (new MethodInvoker (delegate {
286     progress_bar.Message = message;
287     progress_bar.Value = progress;
288     progress_bar.Update ();
289     Application.DoEvents();
290     }));
291     } else {
292     progress_bar.Message = message;
293     progress_bar.Value = progress;
294     progress_bar.Update ();
295     Application.DoEvents();
296     }
297     }
298 william 334 void ReleasePlatformControlStreams ()
299     {
300     foreach (var c in platform_flow.Controls) {
301     if (c.GetType () == typeof(PlatformControl)) {
302     PlatformControl pc = (c as PlatformControl);
303     if (pc != null) {
304     pc.ReleasePlatformStream();
305     }
306     }
307     }
308     }
309     void ReleaseGameControlStreams() {
310     foreach (var c in rom_flow.Controls) {
311     if (c.GetType () == typeof(GameControl)) {
312     GameControl gc = (c as GameControl);
313     if (gc != null) {
314     gc.ReleaseGameStream();
315     }
316     }
317     }
318     }
319 william 253 void platform_ctrl_LostFocus(object sender, EventArgs e)
320     {
321     PlatformControl c = sender as PlatformControl;
322 william 266 if (c != null) {
323     #if !HAVE_X11_BORDERSTYLE_ERROR
324     c.BorderStyle = BorderStyle.None;
325     #endif
326     c.BackColor = this.BackColor;
327     }
328 william 253 }
329 william 13
330 william 253 void platform_ctrl_GotFocus(object sender, EventArgs e)
331 william 266 {
332 william 253 PlatformControl c = sender as PlatformControl;
333 william 266 if (c != null) {
334     #if !HAVE_X11_BORDERSTYLE_ERROR
335     c.BorderStyle = BorderStyle.FixedSingle;
336     #endif
337     c.BackColor = SELECTED_CONTROL_BACKCOLOR;
338     CurrentPlatformControl = c;
339     }
340 william 253 }
341 william 355
342     void game_ctrl_PreviewKeyDown (object sender, PreviewKeyDownEventArgs e)
343     {
344 william 286 //gLog.Verbose.Debug.WriteLine ("game_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
345 william 355 GameControl c = sender as GameControl;
346 william 17
347 william 355 int changeAmount = 0;
348     int currentPosition = 0;
349     IRomConfig config = null;
350     if (e.KeyCode == Keys.C && (e.Modifiers & Keys.Control) == Keys.Control) {
351     config = (c.Tag as IRomConfig);
352     if (config != null) {
353     string platform_short = config.EmuConfig.PlatformNameShort;
354     string rom_file = config.RomFile;
355     string rom_image = config.RomImage;
356 william 324
357 william 355 string rom_filename = "";
358     string rom_imagefilename = "";
359     FileInfo fi = null;
360     try {
361     fi = new FileInfo (rom_file);
362     rom_filename = fi.Name;
363     } finally {
364     fi = null;
365     }
366     try {
367     fi = new FileInfo (rom_image);
368     rom_imagefilename = fi.Name;
369     } finally {
370     fi = null;
371     }
372    
373     string local_path = Config.LocalRomPath.TrimEnd (new char[]{ '/' });
374     string local_rom_path = string.Format ("{0}/{1}", local_path, platform_short);
375     string local_romfile = OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_filename));
376     string local_romimage = OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_imagefilename));
377    
378     // create the local rom path for this platform
379     DirectoryInfo di = new DirectoryInfo (local_rom_path);
380     if (!di.Exists) {
381     di.Create ();
382     }
383     this.Enabled = false;
384     CopyRomToLocalCache (rom_file, local_romfile, rom_image, local_romimage);
385     this.Enabled = true;
386     }
387     }
388     if (e.KeyCode == Keys.D && (e.Modifiers & Keys.Control) == Keys.Control) {
389     config = (c.Tag as IRomConfig);
390     if (config != null) {
391     string platform_short = config.EmuConfig.PlatformNameShort;
392     string rom_file = config.RomFile;
393     string rom_image = config.RomImage;
394    
395     string rom_filename = "";
396     string rom_imagefilename = "";
397     FileInfo fi = null;
398     try {
399     fi = new FileInfo (rom_file);
400     rom_filename = fi.Name;
401     } finally {
402     fi = null;
403     }
404     try {
405     fi = new FileInfo (rom_image);
406     rom_imagefilename = fi.Name;
407     } finally {
408     fi = null;
409     }
410     string local_path = Config.LocalRomPath.TrimEnd (new char[]{ '/' });
411     string local_rom_path = string.Format ("{0}/{1}", local_path, platform_short);
412     string local_romfile = OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_filename));
413     string local_romimage = OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_imagefilename));
414     this.Enabled = false;
415     DeleteRomFromLocalCache (local_romfile, local_romimage);
416     this.Enabled = true;
417     }
418     }
419     if (e.KeyCode == Keys.F && (e.Modifiers & Keys.Control) == Keys.Control) {
420     config = c.Tag as IRomConfig;
421     if (config == null) {
422     gLog.Error.WriteLine ("Unable to add/remove from/to favorites (config) is null");
423     } else {
424     var isFavorite = RomFavorite.IsFavorite (config);
425     if (isFavorite) {
426     // add to favorites
427     //gLog.Debug.WriteLine("Removing from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
428     if (!RomFavorite.RemoveFavorite (config)) {
429     gLog.Error.WriteLine ("Failed to remove from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
430     } else {
431     gLog.Info.WriteLine ("Removed from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
432     if (config.EmuConfig.PlatformNameShort == "Favorites") {
433     var parent = c.Parent;
434     if (parent != null) {
435     parent.Controls.Remove (c);
436     if (parent.Controls.Count > 0) {
437     var next_ctrl = parent.Controls [0];
438     if (next_ctrl != null) {
439     next_ctrl.Select ();
440     }
441     }
442     }
443     }
444     }
445     } else {
446     // add to favorites
447     //gLog.Debug.WriteLine("Adding to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
448     if (!RomFavorite.AddFavorite (config)) {
449     gLog.Error.WriteLine ("Failed to add to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
450     } else {
451     gLog.Info.WriteLine ("Added to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
452     }
453     }
454     //gLog.Debug.WriteLine("Updateing favorites");
455     if (!RomFavorite.UpdateFavorites (cache)) {
456     gLog.Error.WriteLine ("Failed to update favorites");
457     } else {
458     gLog.Info.WriteLine ("Updated favorites");
459     }
460     }
461     return; // stop processing other keys
462    
463     }
464 william 273 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
465 william 272 if (OSInfo.OSIsUnix) {
466     rom_flow.SuspendLayout ();
467     var ctl = game_ctrl_get_previous_control (true);
468     rom_flow.ScrollControlIntoView (ctl);
469     rom_flow.ResumeLayout (false);
470     }
471     }
472 william 273 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
473 william 272 if (OSInfo.OSIsUnix) {
474     rom_flow.SuspendLayout ();
475     var ctl = game_ctrl_get_next_control (true);
476     rom_flow.ScrollControlIntoView (ctl);
477     rom_flow.ResumeLayout (false);
478     }
479     }
480 william 355 if (e.KeyCode == Keys.Home) {
481     rom_flow.SuspendLayout ();
482     rom_flow.Controls [0].Select ();
483     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
484     rom_flow.ResumeLayout (false);
485     }
486     if (e.KeyCode == Keys.End) {
487     rom_flow.SuspendLayout ();
488     rom_flow.Controls [rom_flow.Controls.Count - 1].Select ();
489     rom_flow.ScrollControlIntoView (rom_flow.Controls [rom_flow.Controls.Count - 1]);
490     rom_flow.ResumeLayout (false);
491     }
492     if (e.KeyCode == Keys.PageUp) {
493     rom_flow.SuspendLayout ();
494     changeAmount = rom_flow.VerticalScroll.LargeChange;
495     currentPosition = rom_flow.VerticalScroll.Value;
496     if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum) {
497     try {
498     rom_flow.VerticalScroll.Value -= changeAmount;
499     } catch {
500     rom_flow.Controls [0].Select ();
501     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
502     rom_flow.PerformLayout ();
503     return;
504     }
505     } else {
506     rom_flow.Controls [0].Select ();
507     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
508     }
509     GameControl s = game_ctrl_get_last_visible ();
510     s.Select ();
511     rom_flow.ScrollControlIntoView (s);
512     rom_flow.ResumeLayout (false);
513     }
514     if (e.KeyCode == Keys.PageDown) {
515     rom_flow.SuspendLayout ();
516     changeAmount = rom_flow.VerticalScroll.LargeChange;
517     currentPosition = rom_flow.VerticalScroll.Value;
518     if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum) {
519     try {
520     rom_flow.VerticalScroll.Value += changeAmount;
521     } catch {
522     rom_flow.Controls [0].Select ();
523     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
524     rom_flow.PerformLayout ();
525     return;
526     }
527     } else {
528     rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
529     }
530     GameControl s = game_ctrl_get_last_visible ();
531     s.Select ();
532     rom_flow.ScrollControlIntoView (s);
533     rom_flow.ResumeLayout (false);
534     }
535 william 168
536 william 355 if (e.KeyCode == Keys.Enter) {
537     config = c.Tag as IRomConfig;
538     if (config.RomExists) {
539     c.TextForeColor = DEFAULT_TEXTFORECOLOR_LAUNCH;
540     string gamename = c.GameName;
541     c.GameName = string.Format ("{0} (Loading...)", gamename);
542     bool game_launched = true;
543     using (Process p = new Process ()) {
544     string binary = "";
545     string arguments = "";
546     if (!string.IsNullOrEmpty (config.EmuConfig.EmuBinaryPath)) {
547     binary = config.EmuConfig.EmuBinaryPath;
548     arguments = EmuConfigLoader.GetEMUOptions (config);
549     }
550     if (!string.IsNullOrEmpty (config.EmuConfig.GameExe)) {
551     binary = config.EmuConfig.GameExe;
552     arguments = config.EmuConfig.GameExeArgs;
553     }
554     if (string.IsNullOrEmpty (binary)) {
555     gLog.Warn.WriteLine ("Cannot launch this rom file -- missing arguments");
556     game_launched = false;
557     } else {
558     p.StartInfo.FileName = binary;
559     p.StartInfo.Arguments = arguments == null ? "" : arguments;
560     p.Start ();
561     // wait for exit of game
562     p.WaitForExit ();
563     c.TextForeColor = DEFAULT_TEXTFORECOLOR;
564     c.GameName = gamename;
565     }
566     }
567     if (!game_launched) {
568     c.TextForeColor = DEFAULT_TEXTFORECOLOR_ERROR;
569     c.GameName = string.Format ("{0} (Error While Loading...)", gamename);
570     }
571     } else {
572     c.TextForeColor = Color.Red;
573     }
574 william 253 }
575 william 273 if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape)
576 william 355 {
577    
578     bAbortRomCopyToLocalCache = true; // this will cancel any copy operations
579 william 334 ReleaseGameControlStreams();
580 william 274 SavedPlatformIndex = platform_flow.Controls.IndexOf (CurrentPlatformControl);
581 william 253 rom_flow.Visible = false;
582     platform_flow.Visible = true;
583     }
584 william 168
585 william 253 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
586     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
587     {
588     rom_flow.SuspendLayout();
589     char t = (char)e.KeyCode;
590     GameControl ctrl = (rom_flow.GetNextControl(CurrentGameControl, true) as GameControl);
591     if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
592     bool found = false;
593     GameControl pc = CurrentGameControl;
594     bool wrapped = false;
595     bool not_found = true;
596     while (!found)
597     {
598     if (wrapped)
599     {
600     foreach (Control ctl in rom_flow.Controls)
601     {
602     GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
603     }
604     if (not_found) { found = true; }
605     }
606     ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
607     if (ctrl == null)
608     {
609     ctrl = rom_flow.Controls[0] as GameControl;
610     wrapped = true;
611     }
612     if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
613     {
614     rom_flow.ScrollControlIntoView(ctrl);
615     ctrl.Select();
616     found = true;
617     }
618     pc = ctrl;
619     }
620     rom_flow.ResumeLayout(false);
621     }
622     }
623     void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
624     {
625 william 286 //gLog.Verbose.Debug.WriteLine ("platform_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
626 william 253 PlatformControl c = sender as PlatformControl;
627     int changeAmount = 0;
628     int currentPosition = 0;
629 william 273 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
630 william 272 if (OSInfo.OSIsUnix) {
631     platform_flow.SuspendLayout ();
632     var ctl = platform_ctrl_get_previous_control (true);
633     platform_flow.ScrollControlIntoView (ctl);
634     platform_flow.ResumeLayout (false);
635     }
636     }
637 william 273 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
638 william 272 if (OSInfo.OSIsUnix) {
639     platform_flow.SuspendLayout ();
640     var ctl = platform_ctrl_get_next_control (true);
641     platform_flow.ScrollControlIntoView (ctl);
642     platform_flow.ResumeLayout (false);
643     }
644     }
645 william 253 if (e.KeyCode == Keys.Home)
646     {
647     platform_flow.SuspendLayout();
648     platform_flow.Controls[0].Select();
649     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
650     platform_flow.ResumeLayout(false);
651     }
652     if (e.KeyCode == Keys.End)
653     {
654     platform_flow.SuspendLayout();
655     platform_flow.Controls[platform_flow.Controls.Count -1].Select();
656     platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
657     platform_flow.ResumeLayout(false);
658     }
659     if (e.KeyCode == Keys.PageUp)
660     {
661     platform_flow.SuspendLayout();
662     changeAmount = platform_flow.VerticalScroll.LargeChange;
663     currentPosition = platform_flow.VerticalScroll.Value;
664     if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
665     {
666     platform_flow.VerticalScroll.Value -= changeAmount;
667     }
668     else
669     {
670     platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
671     }
672     PlatformControl s = platform_ctrl_get_last_visible();
673     s.Select();
674     platform_flow.ScrollControlIntoView(s);
675     platform_flow.ResumeLayout(false);
676     }
677     if (e.KeyCode == Keys.PageDown)
678     {
679     platform_flow.SuspendLayout();
680     changeAmount = platform_flow.VerticalScroll.LargeChange;
681     currentPosition = platform_flow.VerticalScroll.Value;
682     if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
683     {
684     try
685     {
686     platform_flow.VerticalScroll.Value += changeAmount;
687     }
688     catch
689     {
690     platform_flow.Controls[0].Select();
691     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
692     rom_flow.PerformLayout();
693     return;
694     }
695     }
696     else
697     {
698     platform_flow.Controls[0].Select();
699     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
700     }
701     PlatformControl s = platform_ctrl_get_last_visible();
702     s.Select();
703     platform_flow.ScrollControlIntoView(s);
704     platform_flow.ResumeLayout(false);
705     }
706     if (e.KeyCode == Keys.Enter)
707     {
708 william 334 ReleasePlatformControlStreams();
709     // load this platform
710 william 253 platform_flow.Visible = false;
711 william 334 CurrentSelectedRom = c.Tag as IEmuConfig;
712 william 253 rom_flow.Visible = true;
713     rom_flow.BringToFront();
714     }
715 william 273 if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape)
716 william 253 {
717 william 355 bAbortRomCopyToLocalCache = true;
718 william 334 ReleasePlatformControlStreams();
719 william 253 this.Close();
720     }
721     if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
722     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
723     {
724     platform_flow.SuspendLayout();
725     char t = (char)e.KeyCode;
726     PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl);
727     if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); }
728     bool found = false;
729     PlatformControl pc = CurrentPlatformControl;
730     bool wrapped = false;
731     bool not_found = true;
732     while (!found)
733     {
734     if (wrapped)
735     {
736     foreach (Control ctl in platform_flow.Controls)
737     {
738     PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
739     }
740     if (not_found) { found = true; }
741     }
742     ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl);
743     if (ctrl == null)
744     {
745     ctrl = platform_flow.Controls[0] as PlatformControl;
746     wrapped = true;
747     }
748     if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower()))
749     {
750     platform_flow.ScrollControlIntoView(ctrl);
751     ctrl.Select();
752     found = true;
753     }
754     pc = ctrl;
755     }
756     platform_flow.ResumeLayout(false);
757     }
758     }
759 william 17
760     private void platform_flow_VisibleChanged(object sender, EventArgs e)
761     {
762     if (!platform_flow.Visible) return;
763     platform_flow.Controls.Clear();
764     platform_flow.BringToFront();
765 william 37 Stopwatch t = new Stopwatch();
766     t.Start();
767 william 60 platformWorker.RunWorkerAsync(t);
768 william 17 }
769    
770     private void rom_flow_VisibleChanged(object sender, EventArgs e)
771     {
772     if (!rom_flow.Visible) return;
773     rom_flow.Controls.Clear();
774     rom_flow.BringToFront();
775 william 37 Stopwatch t = new Stopwatch();
776     t.Start();
777 william 60 gameWorker.RunWorkerAsync(t);
778 william 17 }
779    
780 william 253 void game_ctrl_LostFocus(object sender, EventArgs e)
781     {
782     GameControl c = sender as GameControl;
783 william 266 #if !HAVE_X11_BORDERSTYLE_ERROR
784 william 253 c.BorderStyle = BorderStyle.None;
785 william 266 #endif
786 william 255 c.BackColor = this.BackColor;
787 william 253 }
788 william 17
789 william 253 void game_ctrl_GotFocus(object sender, EventArgs e)
790     {
791     GameControl c = sender as GameControl;
792 william 266 #if !HAVE_X11_BORDERSTYLE_ERROR
793 william 253 c.BorderStyle = BorderStyle.FixedSingle;
794 william 266 #endif
795 william 255 c.BackColor = SELECTED_CONTROL_BACKCOLOR;
796 william 253 CurrentGameControl = c;
797     }
798 william 17
799 william 253 private GameControl game_ctrl_get_last_visible()
800     {
801     GameControl s = new GameControl();
802     foreach (GameControl c in rom_flow.Controls)
803     {
804     if (c.Bounds.IntersectsWith(rom_flow.Bounds))
805     s = c;
806     }
807     return s;
808     }
809     private PlatformControl platform_ctrl_get_last_visible()
810     {
811     PlatformControl s = new PlatformControl();
812     foreach (PlatformControl c in platform_flow.Controls)
813     {
814     if (c.Bounds.IntersectsWith(platform_flow.Bounds))
815     s = c;
816     }
817     return s;
818     }
819 william 272 private PlatformControl platform_ctrl_get_next_control(bool update_control)
820     {
821     PlatformControl s = new PlatformControl();
822     int index = platform_flow.Controls.IndexOf (CurrentPlatformControl);
823     index = index + 1;
824     if (index < 0) {
825     index = platform_flow.Controls.Count-1;
826     }
827     if (index > platform_flow.Controls.Count-1) {
828     index = 0;
829     }
830     s = platform_flow.Controls [index] as PlatformControl;
831     if (update_control) {
832     CurrentPlatformControl = s;
833     s.Select ();
834     }
835     return s;
836     }
837     private PlatformControl platform_ctrl_get_previous_control(bool update_control)
838     {
839     PlatformControl s = null;
840     int index = platform_flow.Controls.IndexOf (CurrentPlatformControl);
841     index = index - 1;
842     if (index < 0) {
843     index = platform_flow.Controls.Count-1;
844     }
845     if (index > platform_flow.Controls.Count-1) {
846     index = 0;
847     }
848     s = platform_flow.Controls [index] as PlatformControl;
849     if (update_control) {
850     CurrentPlatformControl = s;
851     s.Select ();
852     }
853     return s;
854     }
855     private GameControl game_ctrl_get_next_control(bool update_control)
856     {
857     GameControl s = new GameControl();
858     int index = rom_flow.Controls.IndexOf (CurrentGameControl);
859     index = index + 1;
860     if (index < 0) {
861     index = rom_flow.Controls.Count-1;
862     }
863     if (index > rom_flow.Controls.Count-1) {
864     index = 0;
865     }
866     s = rom_flow.Controls [index] as GameControl;
867     if (update_control) {
868     CurrentGameControl = s;
869     s.Select ();
870     }
871     return s;
872     }
873     private GameControl game_ctrl_get_previous_control(bool update_control)
874     {
875     GameControl s = new GameControl();
876     int index = rom_flow.Controls.IndexOf (CurrentGameControl);
877     index = index - 1;
878     if (index < 0) {
879     index = rom_flow.Controls.Count-1;
880     }
881     if (index > rom_flow.Controls.Count-1) {
882     index = 0;
883     }
884     s = rom_flow.Controls [index] as GameControl;
885     if (update_control) {
886     CurrentGameControl = s;
887     s.Select ();
888     }
889     return s;
890     }
891 william 168
892 william 60
893     #region Background Workers
894    
895     private int GetFormWidth()
896     {
897     if (this.InvokeRequired)
898     {
899     return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
900     }
901     else
902     {
903     return this.Width;
904     }
905     }
906 william 183 private Font GetFormFont()
907     {
908     if (this.InvokeRequired)
909     {
910     return (this.Invoke(new Delegate_GetFormFont(GetFormFont)) as Font);
911     }
912     else
913     {
914     return this.Font;
915     }
916     }
917    
918     private Font ResizeFont(Font font, float size)
919     {
920     if (this.InvokeRequired)
921     {
922     return (this.Invoke(new Delegate_ResizeFont(ResizeFont), new object[] { font, size })as Font);
923     }
924     else
925     {
926     return new Font(font.FontFamily, size);
927     }
928     }
929    
930 william 113 //private void AddPlatformControl(Control c)
931     //{
932     // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
933     // else
934     // {
935     // platform_flow.Controls.Add(c);
936     // }
937     //}
938     //private void UpdatePlatformControls()
939     //{
940     // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
941     // //else { this.Update(); }
942     //}
943     //private void AddGameControl(Control c)
944     //{
945     // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
946     // else
947     // {
948     // rom_flow.Controls.Add(c);
949     // }
950     //}
951     //private void UpdateGameControls()
952     //{
953     // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
954     // //else { this.Update(); }
955     //}
956 william 60 #region gameWorker
957 william 324 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
958 william 137 private object gameimage_lock = new object();
959 william 327 private void worker_progress_event (object event_source, ProgressEventArgs event_args)
960     {
961     //gLog.Debug.WriteLine ("worker_progress_event called!");
962     object user_object = event_args.GetUserObject ();
963     int progress = event_args.GetProgress();
964     string message = event_args.GetUserMessage() == null ? "" : event_args.GetUserMessage();
965     if (user_object != null) {
966     IRomConfig irc = (user_object as IRomConfig);
967     if (irc != null) {
968     gLog.Verbose.Debug.WriteLine ("Read Rom[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, irc);
969     } else {
970     IEmuConfig iec = (user_object as IEmuConfig);
971     if (iec != null) {
972     gLog.Verbose.Debug.WriteLine ("Read Platform[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, iec);
973     }
974     }
975     }
976     if (progress_bar.InvokeRequired) {
977     progress_bar.Invoke (new MethodInvoker (delegate {
978     progress_bar.Message = message;
979     progress_bar.Value = progress;
980     progress_bar.Update();
981     }));
982     } else {
983     progress_bar.Message = message;
984     progress_bar.Value = progress;
985     progress_bar.Update();
986     }
987     }
988     private void gameWorker_DoWork (object sender, DoWorkEventArgs e)
989     {
990     Stopwatch t = e.Argument as Stopwatch;
991     progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
992 william 183 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
993 william 327 progress_bar.ShowPercentageLabel = false;
994 william 183 #endif
995 william 327 progress_bar.ProgressColor = Color.LimeGreen;
996     progress_bar.Dock = DockStyle.Top;
997 william 183
998 william 327 if (this.InvokeRequired) {
999     this.Invoke ((MethodInvoker)delegate() {
1000     this.Controls.Add (progress_bar);
1001     });
1002     } else {
1003     this.Controls.Add (progress_bar);
1004     }
1005     progress_bar.Invoke (new MethodInvoker (delegate {
1006     progress_bar.Margin = new System.Windows.Forms.Padding (0);
1007     progress_bar.Size = new Size (GetFormWidth () - 25, 100);
1008     }));
1009    
1010     HashSet<IRomConfig> roms = new HashSet<IRomConfig> ();
1011 william 355 using (RomParser parser = new RomParser (cache, CurrentSelectedRom, new ProgressEvent (worker_progress_event))) {
1012 william 327 foreach (var rom in parser.Roms) {
1013 william 355 roms.Add (rom);
1014 william 327 }
1015     }
1016    
1017     double count = 0;
1018     double total_count = roms.Count;
1019     if (total_count > 0) {
1020     progress_bar.Invoke (new MethodInvoker (delegate {
1021     progress_bar.Message = string.Format ("Please Wait Adding {0} {1} Roms...", total_count, CurrentSelectedRom.PlatformNameLong);
1022     }));
1023     }
1024    
1025    
1026 william 355 foreach (IRomConfig config in roms) {
1027     progress_bar.Invoke (new MethodInvoker (delegate {
1028     progress_bar.Value = (int)(100.0 * (count / total_count));
1029     }));
1030     GameControl game_ctrl = new GameControl ();
1031     game_ctrl.Font = GetFormFont ();
1032     game_ctrl.Dock = DockStyle.Top;
1033     game_ctrl.Width = this.Width - 10;
1034     game_ctrl.Tag = config;
1035     try {
1036     lock (gameimage_lock) {
1037 william 327 //gLog.Debug.WriteLine ("handling rom: {0}", config.ToString ());
1038 william 355 if (String.IsNullOrEmpty (config.RomImage)) {
1039     game_ctrl.UpdateGameImage ((Image)DefaultGameImage.Clone ());
1040     } else {
1041     string path = config.isCachedLocally ? config.GetLocalRomImageOverride() : config.GetFullRomImage ();
1042     if (File.Exists (path)) {
1043     game_ctrl.UpdateGameImage (path);
1044     } else {
1045     game_ctrl.UpdateGameImage ((Image)DefaultGameImage.Clone ());
1046     }
1047 william 327 }
1048 william 355 }
1049     } catch (Exception ex) {
1050 william 327 gLog.Error.WriteLine (ex.Message);
1051 william 355 gLog.Verbose.Error.WriteLine (ex.ToString ());
1052     throw ex;
1053     }
1054     if (CurrentSelectedRom.PlatformNameShort == "Favorites") {
1055     //game_ctrl.GameName = config.RomTitle;
1056     var favorite = RomFavorite.GetFavoriteInfoFromRom (config);
1057     game_ctrl.GameName = favorite.RomConfig.RomExists ? favorite.RomConfig.RomTitle : string.Format ("{0} - (unavailable)", favorite.RomConfig.RomTitle);
1058     //RomFavorite.GetRomTitleFromConfig(config);
1059     if (!favorite.RomConfig.RomExists) {
1060     game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR_MISSING;
1061     } else {
1062     game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR;
1063     }
1064 william 178 }
1065     else
1066     {
1067 william 355 game_ctrl.GameName = config.RomExists ? config.RomTitle : string.Format("{0} - (unavailable)", config.RomTitle);
1068     if (!config.RomExists) {
1069     game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR_MISSING;
1070     } else {
1071     game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR;
1072     }
1073 william 183 }
1074 william 253 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
1075     game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
1076     game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
1077 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
1078     else { rom_flow.Controls.Add(game_ctrl); }
1079 william 60 count++;
1080     }
1081 william 327 progress_bar.Invoke(new MethodInvoker(delegate { progress_bar.Value = 0; progress_bar.Update(); }));
1082     e.Result = t;
1083 william 60 }
1084     private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
1085     private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1086     {
1087 william 327 Stopwatch t = e.Result as Stopwatch;
1088     if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() {
1089     progress_bar.Message = "";
1090     this.Controls.Remove(progress_bar); });
1091     this.Refresh();
1092     } else {
1093     progress_bar.Message = "";
1094     this.Controls.Remove(progress_bar);
1095     this.Refresh();
1096    
1097     }
1098 william 198 if (rom_flow.Controls.Count == 0)
1099 william 197 {
1100     GameControl game_ctrl = new GameControl();
1101 william 253 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
1102     game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
1103     game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
1104 william 197 game_ctrl.Font = GetFormFont();
1105     game_ctrl.Dock = DockStyle.Top;
1106     game_ctrl.Width = this.Width - 10;
1107 william 273 IEmuConfig config = CurrentPlatformControl.Tag as IEmuConfig;
1108     if (config.PlatformNameLong == "Favorites") {
1109     game_ctrl.GameName = "You haven't favorited any games, select a game and then press CTRL+F to favorite it";
1110     } else {
1111     game_ctrl.GameName = string.Format ("No {0} games were found", config.PlatformNameShort);
1112     }
1113 william 197 rom_flow.Controls.Add(game_ctrl);
1114     }
1115 william 253 rom_flow.Controls[0].Select();
1116 william 60 t.Stop();
1117 william 327 gLog.Profiler.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
1118 william 60 }
1119     #endregion
1120     #region platformWorker
1121 william 110 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
1122 william 324 private object platformimage_lock = new object();
1123 william 327 private void platformWorker_DoWork (object sender, DoWorkEventArgs e)
1124     {
1125     Stopwatch t = e.Argument as Stopwatch;
1126     progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
1127 william 183 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
1128 william 327 progress_bar.ShowPercentageLabel = false;
1129 william 183 #endif
1130 william 327 progress_bar.ProgressColor = Color.LimeGreen;
1131     progress_bar.Dock = DockStyle.Top;
1132 william 270
1133 william 327 if (this.InvokeRequired) {
1134     this.Invoke ((MethodInvoker)delegate() {
1135     this.Controls.Add (progress_bar);
1136     });
1137     } else {
1138     this.Controls.Add (progress_bar);
1139     }
1140     progress_bar.Invoke (new MethodInvoker (delegate {
1141     progress_bar.Margin = new System.Windows.Forms.Padding (0);
1142     progress_bar.Size = new Size (GetFormWidth () - 25, 100);
1143     }));
1144    
1145     HashSet<IEmuConfig> platforms = new HashSet<IEmuConfig>();
1146 william 355 using (PlatformParser parser = new PlatformParser (cache, Config.LocalRomPath, Config.RomPath, new ProgressEvent (worker_progress_event))) {
1147 william 327 foreach (var platform in parser.Platforms) {
1148     platforms.Add(platform);
1149     }
1150     }
1151    
1152    
1153     double count = 0;
1154     double total_count = platforms.Count;
1155     if (total_count > 0) {
1156     progress_bar.Invoke (new MethodInvoker (delegate {
1157     progress_bar.Message = string.Format ("Please Wait Loading {0} Rom Platforms...", total_count);
1158     }));
1159     }
1160     foreach (IEmuConfig config in platforms) {
1161     progress_bar.Invoke (new MethodInvoker (delegate {
1162     progress_bar.Value = (int)(100.0 * (count / total_count));
1163     }));
1164 william 183
1165 william 327 PlatformControl platform_ctrl = new PlatformControl ();
1166     platform_ctrl.Font = GetFormFont ();
1167     platform_ctrl.Dock = DockStyle.Top;
1168     platform_ctrl.Width = this.Width - 10;
1169     platform_ctrl.Tag = config;
1170     try {
1171     lock (platformimage_lock) {
1172     //gLog.Debug.WriteLine ("handling platform: {0}", config.ToString ());
1173     if(String.IsNullOrEmpty(config.PlatformImage)) {
1174 william 334 platform_ctrl.UpdatePlatformImage((Image)DefaultPlatformImage.Clone ());
1175 william 327 }
1176     else {
1177     string path = config.GetFullPlatformImage();
1178 william 355 if(File.Exists(path)) {
1179     platform_ctrl.UpdatePlatformImage(path);
1180     }
1181     else {
1182     platform_ctrl.UpdatePlatformImage((Image)DefaultPlatformImage.Clone ());
1183     }
1184 william 327 }
1185     }
1186     } catch (Exception ex) {
1187     gLog.Error.WriteLine (ex.Message);
1188     gLog.Verbose.Error.WriteLine (ex.ToString());
1189 william 137 throw ex;
1190     }
1191 william 60 platform_ctrl.PlatformName = config.ToString();
1192 william 253 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
1193     platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
1194     platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
1195 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
1196 william 183 else { platform_flow.Controls.Add(platform_ctrl); }
1197 william 60 count++;
1198     }
1199 william 327 progress_bar.Invoke(new MethodInvoker(delegate {
1200     progress_bar.Value = 0; progress_bar.Update();
1201     }));
1202     e.Result = t;
1203 william 60 }
1204     private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
1205     private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
1206     {
1207 william 327 gLog.Debug.WriteLine("platformWorker_RunWorkerCompleted called");
1208     if (this.InvokeRequired) {
1209     this.Invoke((MethodInvoker)delegate() {
1210     progress_bar.Message = "";
1211     this.Controls.Remove(progress_bar);
1212     });
1213     }
1214     else {
1215     progress_bar.Message = "";
1216     this.Controls.Remove(progress_bar);
1217     }
1218 william 60 Stopwatch t = e.Result as Stopwatch;
1219 william 198 if (platform_flow.Controls.Count == 0)
1220 william 197 {
1221     PlatformControl platform_ctrl = new PlatformControl();
1222 william 253 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
1223     platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
1224     platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
1225 william 197 platform_ctrl.Font = GetFormFont();
1226     platform_ctrl.Dock = DockStyle.Top;
1227     platform_ctrl.Width = this.Width - 10;
1228     platform_ctrl.PlatformName = string.Format("You don't have any roms in your rompath: '{0}'",Config.RomPath);
1229     platform_flow.Controls.Add(platform_ctrl);
1230 william 274 }
1231     if (SavedPlatformIndex != -1) {
1232     var platform_control = platform_flow.Controls [SavedPlatformIndex] as PlatformControl;
1233     platform_flow.SuspendLayout ();
1234     CurrentPlatformControl = platform_control;
1235     platform_flow.ScrollControlIntoView (CurrentPlatformControl);
1236     CurrentPlatformControl.Select ();
1237     platform_flow.ResumeLayout ();
1238     SavedPlatformIndex = -1;
1239     } else {
1240     platform_flow.Controls[0].Select();
1241     }
1242 william 327 t.Stop();
1243     gLog.Profiler.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
1244 william 60 }
1245     #endregion
1246    
1247     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
1248     {
1249     Cursor.Show();
1250     }
1251     #endregion
1252 william 4 }
1253     }

  ViewVC Help
Powered by ViewVC 1.1.22