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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 385 - (hide annotations) (download)
Thu May 2 05:15:37 2019 UTC (9 months, 2 weeks ago) by william
File size: 43052 byte(s)
+ another update to how favorite roms are handled:
  ** when chosing a rom on a platform (other than Favorites)
     pressing CTRL+F will favorite that rom (as it always has).

     The difference is that pressing CTRL+F after favoriting the
     rom will not unfavorite it unless it is done on the favorites list.

     In other words, to remove a favorited rom, it must be done
     from the Favorites platform (from the root/main menu).

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

  ViewVC Help
Powered by ViewVC 1.1.22