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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 393 - (hide annotations) (download)
Mon May 6 02:08:15 2019 UTC (9 months, 2 weeks ago) by william
File size: 43932 byte(s)
add support when caching roms locally (ie copying them) also copy the emulator config file
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 william 393 private void CopyRomToLocalCache (string src_emu_rom_config, string dst_emu_rom_config, 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 william 393
266     if (!string.IsNullOrEmpty (src_emu_rom_config) && !string.IsNullOrEmpty (dst_emu_rom_config)) {
267     bAbortRomCopyToLocalCache = false;
268     cp = new FileProgressCopier (src_emu_rom_config, dst_emu_rom_config, new CopyProgressEvent (OnFileCopyProgress), new AbortEvent (OnAbortRomCopyToLocalCache));
269     cp.Start ();
270     while (!cp.CopyFinished) {
271     Application.DoEvents ();
272     }
273     }
274    
275     // check if local rom emu.config is present
276    
277    
278 william 355 if (this.InvokeRequired) {
279 william 377 this.Invoke ((MethodInvoker)delegate () {
280 william 355 this.Controls.Remove (progress_bar);
281     });
282     } else {
283     this.Controls.Remove (progress_bar);
284     }
285 william 377 Application.DoEvents ();
286     }
287 william 355 public void OnFileCopyProgress (object sender, int progress, string message)
288 william 377 {
289     //gLog.Debug.WriteLine("{0} [{1}%]", message, progress);
290    
291     if (progress_bar.InvokeRequired) {
292     progress_bar.Invoke (new MethodInvoker (delegate {
293     progress_bar.Message = message;
294     progress_bar.Value = progress;
295     progress_bar.Update ();
296     Application.DoEvents ();
297     }));
298     } else {
299     progress_bar.Message = message;
300     progress_bar.Value = progress;
301     progress_bar.Update ();
302     Application.DoEvents ();
303     }
304     }
305     void platform_ctrl_LostFocus (object sender, EventArgs e)
306     {
307     PlatformControl c = sender as PlatformControl;
308     if (c != null) {
309     #if !HAVE_X11_BORDERSTYLE_ERROR
310 william 266 c.BorderStyle = BorderStyle.None;
311 william 377 #endif
312 william 266 c.BackColor = this.BackColor;
313 william 377 }
314     }
315    
316     void platform_ctrl_GotFocus (object sender, EventArgs e)
317     {
318     PlatformControl c = sender as PlatformControl;
319     if (c != null) {
320     #if !HAVE_X11_BORDERSTYLE_ERROR
321 william 266 c.BorderStyle = BorderStyle.FixedSingle;
322 william 377 #endif
323     c.BackColor = SELECTED_CONTROL_BACKCOLOR;
324 william 266 CurrentPlatformControl = c;
325 william 377 }
326     }
327    
328     void game_ctrl_PreviewKeyDown (object sender, PreviewKeyDownEventArgs e)
329     {
330     //gLog.Verbose.Debug.WriteLine ("game_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
331 william 355 GameControl c = sender as GameControl;
332 william 17
333 william 355 int changeAmount = 0;
334     int currentPosition = 0;
335     IRomConfig config = null;
336 william 377 if (e.KeyCode == Keys.C && (e.Modifiers & Keys.Control) == Keys.Control) {
337 william 355 config = (c.Tag as IRomConfig);
338     if (config != null) {
339     string platform_short = config.EmuConfig.PlatformNameShort;
340     string rom_file = config.RomFile;
341     string rom_image = config.RomImage;
342 william 324
343 william 355 string rom_filename = "";
344     string rom_imagefilename = "";
345     FileInfo fi = null;
346     try {
347 william 377 if (!string.IsNullOrEmpty (rom_file)) {
348     fi = new FileInfo (rom_file);
349     rom_filename = fi.Name;
350     }
351 william 355 } finally {
352     fi = null;
353     }
354     try {
355 william 377 if (!string.IsNullOrEmpty (rom_image)) {
356     fi = new FileInfo (rom_image);
357     rom_imagefilename = fi.Name;
358     }
359 william 355 } finally {
360     fi = null;
361     }
362    
363 william 377 string local_path = Config.LocalRomPath.TrimEnd (new char [] { '/' });
364 william 355 string local_rom_path = string.Format ("{0}/{1}", local_path, platform_short);
365 william 377 string local_romfile = string.IsNullOrEmpty(rom_filename) ? "" : OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_filename));
366     string local_romimage = string.IsNullOrEmpty(rom_imagefilename) ? "" : OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_imagefilename));
367 william 355
368 william 393 string emu_config = config.EmuConfig.ConfigPath;
369    
370     string local_emu_config = string.IsNullOrEmpty(emu_config) ? "" : OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, config.EmuConfig.ConfigFile));
371    
372 william 377 if (string.IsNullOrEmpty (local_romfile)) {
373     gLog.Error.WriteLine ("Local rom file could not be found. [src rom={0}]", rom_file);
374     return;
375     }
376    
377     // create the local rom path for this platform
378 william 355 DirectoryInfo di = new DirectoryInfo (local_rom_path);
379     if (!di.Exists) {
380     di.Create ();
381     }
382     this.Enabled = false;
383 william 393 CopyRomToLocalCache (emu_config, local_emu_config, rom_file, local_romfile, rom_image, local_romimage);
384 william 355 this.Enabled = true;
385     }
386     }
387 william 377 if (e.KeyCode == Keys.D && (e.Modifiers & Keys.Control) == Keys.Control) {
388 william 355 config = (c.Tag as IRomConfig);
389     if (config != null) {
390     string platform_short = config.EmuConfig.PlatformNameShort;
391     string rom_file = config.RomFile;
392     string rom_image = config.RomImage;
393    
394     string rom_filename = "";
395     string rom_imagefilename = "";
396     FileInfo fi = null;
397     try {
398     fi = new FileInfo (rom_file);
399     rom_filename = fi.Name;
400     } finally {
401     fi = null;
402     }
403     try {
404     fi = new FileInfo (rom_image);
405     rom_imagefilename = fi.Name;
406     } finally {
407     fi = null;
408     }
409 william 377 string local_path = Config.LocalRomPath.TrimEnd (new char [] { '/' });
410 william 355 string local_rom_path = string.Format ("{0}/{1}", local_path, platform_short);
411     string local_romfile = OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_filename));
412     string local_romimage = OSInfo.FormatPath (string.Format ("{0}/{1}", local_rom_path, rom_imagefilename));
413     this.Enabled = false;
414     DeleteRomFromLocalCache (local_romfile, local_romimage);
415     this.Enabled = true;
416     }
417     }
418     if (e.KeyCode == Keys.F && (e.Modifiers & Keys.Control) == Keys.Control) {
419     config = c.Tag as IRomConfig;
420     if (config == null) {
421     gLog.Error.WriteLine ("Unable to add/remove from/to favorites (config) is null");
422     } else {
423     var isFavorite = RomFavorite.IsFavorite (config);
424 william 385 if (isFavorite) {
425     IEmuConfig cpc = CurrentPlatformControl.Tag as IEmuConfig;
426     string platform_long = cpc.PlatformNameLong;
427     if (platform_long == FAVORITES_PLATFORM) {
428     // add to favorites
429     //gLog.Debug.WriteLine("Removing from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
430     if (!RomFavorite.RemoveFavorite (config)) {
431     gLog.Error.WriteLine ("Failed to remove from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
432     } else {
433     gLog.Info.WriteLine ("Removed from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
434 william 355 var parent = c.Parent;
435     if (parent != null) {
436     parent.Controls.Remove (c);
437     if (parent.Controls.Count > 0) {
438     var next_ctrl = parent.Controls [0];
439     if (next_ctrl != null) {
440     next_ctrl.Select ();
441     }
442     }
443 william 385 }
444 william 377 }
445 william 385 } else {
446     gLog.Info.WriteLine ("Already Favorited: {0} [{1}]", config.RomTitle, config.RomFile);
447     return;
448 william 355 }
449 william 377 } else {
450     // add to favorites
451     //gLog.Debug.WriteLine("Adding to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
452 william 355 if (!RomFavorite.AddFavorite (config)) {
453     gLog.Error.WriteLine ("Failed to add to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
454     } else {
455     gLog.Info.WriteLine ("Added to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
456     }
457 william 377 }
458     //gLog.Debug.WriteLine("Updateing favorites");
459 william 355 if (!RomFavorite.UpdateFavorites (cache)) {
460     gLog.Error.WriteLine ("Failed to update favorites");
461     } else {
462     gLog.Info.WriteLine ("Updated favorites");
463     }
464     }
465 william 377 return; // stop processing other keys
466    
467 william 355 }
468 william 273 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
469 william 272 if (OSInfo.OSIsUnix) {
470     rom_flow.SuspendLayout ();
471     var ctl = game_ctrl_get_previous_control (true);
472     rom_flow.ScrollControlIntoView (ctl);
473     rom_flow.ResumeLayout (false);
474     }
475     }
476 william 273 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
477 william 272 if (OSInfo.OSIsUnix) {
478     rom_flow.SuspendLayout ();
479     var ctl = game_ctrl_get_next_control (true);
480     rom_flow.ScrollControlIntoView (ctl);
481     rom_flow.ResumeLayout (false);
482     }
483 william 377 }
484 william 355 if (e.KeyCode == Keys.Home) {
485     rom_flow.SuspendLayout ();
486     rom_flow.Controls [0].Select ();
487     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
488     rom_flow.ResumeLayout (false);
489     }
490     if (e.KeyCode == Keys.End) {
491     rom_flow.SuspendLayout ();
492     rom_flow.Controls [rom_flow.Controls.Count - 1].Select ();
493     rom_flow.ScrollControlIntoView (rom_flow.Controls [rom_flow.Controls.Count - 1]);
494     rom_flow.ResumeLayout (false);
495     }
496     if (e.KeyCode == Keys.PageUp) {
497     rom_flow.SuspendLayout ();
498     changeAmount = rom_flow.VerticalScroll.LargeChange;
499     currentPosition = rom_flow.VerticalScroll.Value;
500     if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum) {
501     try {
502     rom_flow.VerticalScroll.Value -= changeAmount;
503     } catch {
504     rom_flow.Controls [0].Select ();
505     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
506     rom_flow.PerformLayout ();
507     return;
508     }
509     } else {
510     rom_flow.Controls [0].Select ();
511     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
512     }
513     GameControl s = game_ctrl_get_last_visible ();
514     s.Select ();
515     rom_flow.ScrollControlIntoView (s);
516     rom_flow.ResumeLayout (false);
517     }
518     if (e.KeyCode == Keys.PageDown) {
519     rom_flow.SuspendLayout ();
520     changeAmount = rom_flow.VerticalScroll.LargeChange;
521     currentPosition = rom_flow.VerticalScroll.Value;
522     if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum) {
523     try {
524     rom_flow.VerticalScroll.Value += changeAmount;
525     } catch {
526     rom_flow.Controls [0].Select ();
527     rom_flow.ScrollControlIntoView (rom_flow.Controls [0]);
528     rom_flow.PerformLayout ();
529     return;
530     }
531     } else {
532     rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
533     }
534     GameControl s = game_ctrl_get_last_visible ();
535     s.Select ();
536     rom_flow.ScrollControlIntoView (s);
537     rom_flow.ResumeLayout (false);
538     }
539 william 168
540 william 377 if (e.KeyCode == Keys.Enter) {
541 william 355 config = c.Tag as IRomConfig;
542     if (config.RomExists) {
543     c.TextForeColor = DEFAULT_TEXTFORECOLOR_LAUNCH;
544     string gamename = c.GameName;
545     c.GameName = string.Format ("{0} (Loading...)", gamename);
546     bool game_launched = true;
547     using (Process p = new Process ()) {
548     string binary = "";
549     string arguments = "";
550     if (!string.IsNullOrEmpty (config.EmuConfig.EmuBinaryPath)) {
551     binary = config.EmuConfig.EmuBinaryPath;
552     arguments = EmuConfigLoader.GetEMUOptions (config);
553     }
554     if (!string.IsNullOrEmpty (config.EmuConfig.GameExe)) {
555     binary = config.EmuConfig.GameExe;
556     arguments = config.EmuConfig.GameExeArgs;
557     }
558     if (string.IsNullOrEmpty (binary)) {
559     gLog.Warn.WriteLine ("Cannot launch this rom file -- missing arguments");
560     game_launched = false;
561     } else {
562     p.StartInfo.FileName = binary;
563     p.StartInfo.Arguments = arguments == null ? "" : arguments;
564 william 391 gLog.Verbose.Debug.WriteLine ("Executing: {0} {1}", p.StartInfo.FileName, p.StartInfo.Arguments);
565 william 377 p.Start ();
566     // wait for exit of game
567 william 355 p.WaitForExit ();
568     c.TextForeColor = DEFAULT_TEXTFORECOLOR;
569     c.GameName = gamename;
570     }
571     }
572     if (!game_launched) {
573     c.TextForeColor = DEFAULT_TEXTFORECOLOR_ERROR;
574     c.GameName = string.Format ("{0} (Error While Loading...)", gamename);
575     }
576     } else {
577     c.TextForeColor = Color.Red;
578 william 377 }
579     }
580     if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape) {
581    
582 william 383 bAbortRomCopyToLocalCache = true; // this will cancel any copy operations
583 william 377 SavedPlatformIndex = platform_flow.Controls.IndexOf (CurrentPlatformControl);
584     rom_flow.Visible = false;
585     platform_flow.Visible = true;
586     }
587    
588     if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
589     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)) {
590     rom_flow.SuspendLayout ();
591     char t = (char)e.KeyCode;
592     GameControl ctrl = (rom_flow.GetNextControl (CurrentGameControl, true) as GameControl);
593     if (ctrl == null) { ctrl = (rom_flow.GetNextControl (rom_flow.Controls [0], true) as GameControl); }
594     bool found = false;
595     GameControl pc = CurrentGameControl;
596     bool wrapped = false;
597     bool not_found = true;
598     while (!found) {
599     if (wrapped) {
600     foreach (Control ctl in rom_flow.Controls) {
601     GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower ().StartsWith (t.ToString ().ToLower ())) { not_found = false; }
602     }
603     if (not_found) { found = true; }
604     }
605     ctrl = (rom_flow.GetNextControl (pc, true) as GameControl);
606     if (ctrl == null) {
607     ctrl = rom_flow.Controls [0] as GameControl;
608     wrapped = true;
609     }
610     if (ctrl.GameName.ToLower ().StartsWith (t.ToString ().ToLower ())) {
611     rom_flow.ScrollControlIntoView (ctrl);
612     ctrl.Select ();
613     found = true;
614     }
615     pc = ctrl;
616     }
617     rom_flow.ResumeLayout (false);
618     }
619     }
620     void platform_ctrl_PreviewKeyDown (object sender, PreviewKeyDownEventArgs e)
621     {
622     //gLog.Verbose.Debug.WriteLine ("platform_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
623     PlatformControl c = sender as PlatformControl;
624     int changeAmount = 0;
625     int currentPosition = 0;
626 william 273 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
627 william 272 if (OSInfo.OSIsUnix) {
628     platform_flow.SuspendLayout ();
629     var ctl = platform_ctrl_get_previous_control (true);
630     platform_flow.ScrollControlIntoView (ctl);
631     platform_flow.ResumeLayout (false);
632     }
633     }
634 william 273 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
635 william 272 if (OSInfo.OSIsUnix) {
636     platform_flow.SuspendLayout ();
637     var ctl = platform_ctrl_get_next_control (true);
638     platform_flow.ScrollControlIntoView (ctl);
639     platform_flow.ResumeLayout (false);
640     }
641 william 377 }
642     if (e.KeyCode == Keys.Home) {
643     platform_flow.SuspendLayout ();
644     platform_flow.Controls [0].Select ();
645     platform_flow.ScrollControlIntoView (platform_flow.Controls [0]);
646     platform_flow.ResumeLayout (false);
647     }
648     if (e.KeyCode == Keys.End) {
649     platform_flow.SuspendLayout ();
650     platform_flow.Controls [platform_flow.Controls.Count - 1].Select ();
651     platform_flow.ScrollControlIntoView (platform_flow.Controls [platform_flow.Controls.Count - 1]);
652     platform_flow.ResumeLayout (false);
653     }
654     if (e.KeyCode == Keys.PageUp) {
655     platform_flow.SuspendLayout ();
656     changeAmount = platform_flow.VerticalScroll.LargeChange;
657     currentPosition = platform_flow.VerticalScroll.Value;
658     if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum) {
659     platform_flow.VerticalScroll.Value -= changeAmount;
660     } else {
661     platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
662     }
663     PlatformControl s = platform_ctrl_get_last_visible ();
664     s.Select ();
665     platform_flow.ScrollControlIntoView (s);
666     platform_flow.ResumeLayout (false);
667     }
668     if (e.KeyCode == Keys.PageDown) {
669     platform_flow.SuspendLayout ();
670     changeAmount = platform_flow.VerticalScroll.LargeChange;
671     currentPosition = platform_flow.VerticalScroll.Value;
672     if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum) {
673     try {
674     platform_flow.VerticalScroll.Value += changeAmount;
675     } catch {
676     platform_flow.Controls [0].Select ();
677     platform_flow.ScrollControlIntoView (platform_flow.Controls [0]);
678     rom_flow.PerformLayout ();
679     return;
680     }
681     } else {
682     platform_flow.Controls [0].Select ();
683     platform_flow.ScrollControlIntoView (platform_flow.Controls [0]);
684     }
685     PlatformControl s = platform_ctrl_get_last_visible ();
686     s.Select ();
687     platform_flow.ScrollControlIntoView (s);
688     platform_flow.ResumeLayout (false);
689     }
690 william 383 if (e.KeyCode == Keys.Enter) {
691 william 377 // load this platform
692     platform_flow.Visible = false;
693     CurrentSelectedRom = c.Tag as IEmuConfig;
694     rom_flow.Visible = true;
695     rom_flow.BringToFront ();
696 william 272 }
697 william 377 if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape) {
698 william 383 bAbortRomCopyToLocalCache = true;
699 william 377 this.Close ();
700     }
701     if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
702     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)) {
703     platform_flow.SuspendLayout ();
704     char t = (char)e.KeyCode;
705     PlatformControl ctrl = (platform_flow.GetNextControl (CurrentPlatformControl, true) as PlatformControl);
706     if (ctrl == null) { ctrl = (platform_flow.GetNextControl (platform_flow.Controls [0], true) as PlatformControl); }
707     bool found = false;
708     PlatformControl pc = CurrentPlatformControl;
709     bool wrapped = false;
710     bool not_found = true;
711     while (!found) {
712     if (wrapped) {
713     foreach (Control ctl in platform_flow.Controls) {
714     PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower ().StartsWith (t.ToString ().ToLower ())) { not_found = false; }
715     }
716     if (not_found) { found = true; }
717     }
718     ctrl = (platform_flow.GetNextControl (pc, true) as PlatformControl);
719     if (ctrl == null) {
720     ctrl = platform_flow.Controls [0] as PlatformControl;
721     wrapped = true;
722     }
723     if (ctrl.PlatformName.ToLower ().StartsWith (t.ToString ().ToLower ())) {
724     platform_flow.ScrollControlIntoView (ctrl);
725     ctrl.Select ();
726     found = true;
727     }
728     pc = ctrl;
729     }
730     platform_flow.ResumeLayout (false);
731     }
732     }
733    
734     private void platform_flow_VisibleChanged (object sender, EventArgs e)
735     {
736     if (!platform_flow.Visible) return;
737     platform_flow.Controls.Clear ();
738     platform_flow.BringToFront ();
739     Stopwatch t = new Stopwatch ();
740     t.Start ();
741     platformWorker.RunWorkerAsync (t);
742     }
743    
744     private void rom_flow_VisibleChanged (object sender, EventArgs e)
745     {
746     if (!rom_flow.Visible) return;
747     rom_flow.Controls.Clear ();
748     rom_flow.BringToFront ();
749     Stopwatch t = new Stopwatch ();
750     t.Start ();
751     gameWorker.RunWorkerAsync (t);
752     }
753    
754     void game_ctrl_LostFocus (object sender, EventArgs e)
755     {
756     GameControl c = sender as GameControl;
757     #if !HAVE_X11_BORDERSTYLE_ERROR
758 william 253 c.BorderStyle = BorderStyle.None;
759 william 377 #endif
760     c.BackColor = this.BackColor;
761     }
762    
763     void game_ctrl_GotFocus (object sender, EventArgs e)
764     {
765     GameControl c = sender as GameControl;
766     #if !HAVE_X11_BORDERSTYLE_ERROR
767 william 253 c.BorderStyle = BorderStyle.FixedSingle;
768 william 377 #endif
769     c.BackColor = SELECTED_CONTROL_BACKCOLOR;
770     CurrentGameControl = c;
771     }
772    
773     private GameControl game_ctrl_get_last_visible ()
774     {
775     GameControl s = new GameControl ();
776     foreach (GameControl c in rom_flow.Controls) {
777     if (c.Bounds.IntersectsWith (rom_flow.Bounds))
778     s = c;
779     }
780     return s;
781     }
782     private PlatformControl platform_ctrl_get_last_visible ()
783     {
784     PlatformControl s = new PlatformControl ();
785     foreach (PlatformControl c in platform_flow.Controls) {
786     if (c.Bounds.IntersectsWith (platform_flow.Bounds))
787     s = c;
788     }
789     return s;
790     }
791     private PlatformControl platform_ctrl_get_next_control (bool update_control)
792 william 272 {
793 william 377 PlatformControl s = new PlatformControl ();
794 william 272 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 PlatformControl platform_ctrl_get_previous_control (bool update_control)
810 william 272 {
811     PlatformControl s = null;
812     int index = platform_flow.Controls.IndexOf (CurrentPlatformControl);
813     index = index - 1;
814     if (index < 0) {
815 william 377 index = platform_flow.Controls.Count - 1;
816 william 272 }
817 william 377 if (index > platform_flow.Controls.Count - 1) {
818 william 272 index = 0;
819     }
820     s = platform_flow.Controls [index] as PlatformControl;
821     if (update_control) {
822     CurrentPlatformControl = s;
823     s.Select ();
824     }
825     return s;
826     }
827 william 377 private GameControl game_ctrl_get_next_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     }
845 william 377 private GameControl game_ctrl_get_previous_control (bool update_control)
846 william 272 {
847 william 377 GameControl s = new GameControl ();
848 william 272 int index = rom_flow.Controls.IndexOf (CurrentGameControl);
849     index = index - 1;
850     if (index < 0) {
851 william 377 index = rom_flow.Controls.Count - 1;
852 william 272 }
853 william 377 if (index > rom_flow.Controls.Count - 1) {
854 william 272 index = 0;
855     }
856     s = rom_flow.Controls [index] as GameControl;
857     if (update_control) {
858     CurrentGameControl = s;
859     s.Select ();
860     }
861     return s;
862 william 377 }
863    
864    
865     #region Background Workers
866    
867     private int GetFormWidth ()
868     {
869     if (this.InvokeRequired) {
870     return Convert.ToInt32 (this.Invoke ((MethodInvoker)delegate () { GetFormWidth (); }));
871     } else {
872     return this.Width;
873     }
874     }
875     private Font GetFormFont ()
876     {
877     if (this.InvokeRequired) {
878     return (this.Invoke (new Delegate_GetFormFont (GetFormFont)) as Font);
879     } else {
880     return this.Font;
881     }
882     }
883    
884     private Font ResizeFont (Font font, float size)
885     {
886     if (this.InvokeRequired) {
887     return (this.Invoke (new Delegate_ResizeFont (ResizeFont), new object [] { font, size }) as Font);
888     } else {
889     return new Font (font.FontFamily, size);
890     }
891     }
892    
893     //private void AddPlatformControl(Control c)
894     //{
895     // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
896     // else
897     // {
898     // platform_flow.Controls.Add(c);
899     // }
900     //}
901     //private void UpdatePlatformControls()
902     //{
903     // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
904     // //else { this.Update(); }
905     //}
906     //private void AddGameControl(Control c)
907     //{
908     // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
909     // else
910     // {
911     // rom_flow.Controls.Add(c);
912     // }
913     //}
914     //private void UpdateGameControls()
915     //{
916     // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
917     // //else { this.Update(); }
918     //}
919     #region gameWorker
920     private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
921     private object gameimage_lock = new object ();
922 william 327 private void worker_progress_event (object event_source, ProgressEventArgs event_args)
923 william 377 {
924     //gLog.Debug.WriteLine ("worker_progress_event called!");
925 william 327 object user_object = event_args.GetUserObject ();
926 william 377 int progress = event_args.GetProgress ();
927     string message = event_args.GetUserMessage () == null ? "" : event_args.GetUserMessage ();
928     if (user_object != null) {
929 william 327 IRomConfig irc = (user_object as IRomConfig);
930     if (irc != null) {
931     gLog.Verbose.Debug.WriteLine ("Read Rom[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, irc);
932     } else {
933     IEmuConfig iec = (user_object as IEmuConfig);
934     if (iec != null) {
935     gLog.Verbose.Debug.WriteLine ("Read Platform[{0}] ({1}%): '{2}'", event_args.GetCount (), progress, iec);
936     }
937     }
938     }
939     if (progress_bar.InvokeRequired) {
940     progress_bar.Invoke (new MethodInvoker (delegate {
941     progress_bar.Message = message;
942     progress_bar.Value = progress;
943 william 377 progress_bar.Update ();
944 william 327 }));
945     } else {
946     progress_bar.Message = message;
947     progress_bar.Value = progress;
948 william 377 progress_bar.Update ();
949 william 327 }
950 william 377 }
951     private void gameWorker_DoWork (object sender, DoWorkEventArgs e)
952 william 327 {
953     Stopwatch t = e.Argument as Stopwatch;
954 william 377 progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
955 william 183 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
956 william 327 progress_bar.ShowPercentageLabel = false;
957 william 183 #endif
958 william 327 progress_bar.ProgressColor = Color.LimeGreen;
959     progress_bar.Dock = DockStyle.Top;
960 william 183
961 william 327 if (this.InvokeRequired) {
962 william 377 this.Invoke ((MethodInvoker)delegate () {
963 william 327 this.Controls.Add (progress_bar);
964     });
965     } else {
966     this.Controls.Add (progress_bar);
967     }
968     progress_bar.Invoke (new MethodInvoker (delegate {
969     progress_bar.Margin = new System.Windows.Forms.Padding (0);
970     progress_bar.Size = new Size (GetFormWidth () - 25, 100);
971     }));
972    
973     HashSet<IRomConfig> roms = new HashSet<IRomConfig> ();
974 william 377 using (RomParser parser = new RomParser (cache, CurrentSelectedRom, new ProgressEvent (worker_progress_event))) {
975 william 327 foreach (var rom in parser.Roms) {
976 william 355 roms.Add (rom);
977 william 327 }
978     }
979    
980     double count = 0;
981     double total_count = roms.Count;
982     if (total_count > 0) {
983     progress_bar.Invoke (new MethodInvoker (delegate {
984     progress_bar.Message = string.Format ("Please Wait Adding {0} {1} Roms...", total_count, CurrentSelectedRom.PlatformNameLong);
985     }));
986     }
987    
988    
989 william 355 foreach (IRomConfig config in roms) {
990     progress_bar.Invoke (new MethodInvoker (delegate {
991     progress_bar.Value = (int)(100.0 * (count / total_count));
992     }));
993     GameControl game_ctrl = new GameControl ();
994     game_ctrl.Font = GetFormFont ();
995     game_ctrl.Dock = DockStyle.Top;
996     game_ctrl.Width = this.Width - 10;
997     game_ctrl.Tag = config;
998     try {
999 william 377 lock (gameimage_lock) {
1000     //gLog.Debug.WriteLine ("handling rom: {0}", config.ToString ());
1001     if (String.IsNullOrEmpty (config.RomImage)) {
1002     game_ctrl.UpdateGameImage ((Image)DefaultGameImage.Clone ());
1003     } else {
1004 william 355 string path = config.isCachedLocally ? config.GetLocalRomImageOverride() : config.GetFullRomImage ();
1005     if (File.Exists (path)) {
1006     game_ctrl.UpdateGameImage (path);
1007     } else {
1008     game_ctrl.UpdateGameImage ((Image)DefaultGameImage.Clone ());
1009 william 377 }
1010 william 327 }
1011 william 355 }
1012     } catch (Exception ex) {
1013 william 327 gLog.Error.WriteLine (ex.Message);
1014 william 355 gLog.Verbose.Error.WriteLine (ex.ToString ());
1015     throw ex;
1016     }
1017 william 385 if (CurrentSelectedRom.PlatformNameShort == FAVORITES_PLATFORM) {
1018 william 377 //game_ctrl.GameName = config.RomTitle;
1019 william 355 var favorite = RomFavorite.GetFavoriteInfoFromRom (config);
1020 william 377 game_ctrl.GameName = favorite.RomConfig.RomExists ? favorite.RomConfig.RomTitle : string.Format ("{0} - (unavailable)", favorite.RomConfig.RomTitle);
1021     //RomFavorite.GetRomTitleFromConfig(config);
1022 william 355 if (!favorite.RomConfig.RomExists) {
1023 william 377 game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR_MISSING;
1024 william 355 } else {
1025 william 377 game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR;
1026     }
1027     } else {
1028     game_ctrl.GameName = config.RomExists ? config.RomTitle : string.Format ("{0} - (unavailable)", config.RomTitle);
1029 william 355 if (!config.RomExists) {
1030 william 377 game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR_MISSING;
1031 william 355 } else {
1032 william 377 game_ctrl.TextForeColor = DEFAULT_TEXTFORECOLOR;
1033     }
1034     }
1035     game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler (game_ctrl_PreviewKeyDown);
1036     game_ctrl.GotFocus += new EventHandler (game_ctrl_GotFocus);
1037     game_ctrl.LostFocus += new EventHandler (game_ctrl_LostFocus);
1038     if (rom_flow.InvokeRequired) { rom_flow.Invoke ((MethodInvoker)delegate () { rom_flow.Controls.Add (game_ctrl); }); } else { rom_flow.Controls.Add (game_ctrl); }
1039     count++;
1040     }
1041     progress_bar.Invoke (new MethodInvoker (delegate { progress_bar.Value = 0; progress_bar.Update (); }));
1042     e.Result = t;
1043     }
1044     private void gameWorker_ProgressChanged (object sender, ProgressChangedEventArgs e) { }
1045     private void gameWorker_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)
1046     {
1047 william 327 Stopwatch t = e.Result as Stopwatch;
1048 william 377 if (this.InvokeRequired) {
1049     this.Invoke ((MethodInvoker)delegate () {
1050     progress_bar.Message = "";
1051     this.Controls.Remove (progress_bar);
1052     });
1053     this.Refresh ();
1054     } else {
1055 william 327 progress_bar.Message = "";
1056 william 377 this.Controls.Remove (progress_bar);
1057     this.Refresh ();
1058 william 327
1059 william 377 }
1060     if (rom_flow.Controls.Count == 0) {
1061     GameControl game_ctrl = new GameControl ();
1062     game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler (game_ctrl_PreviewKeyDown);
1063     game_ctrl.GotFocus += new EventHandler (game_ctrl_GotFocus);
1064     game_ctrl.LostFocus += new EventHandler (game_ctrl_LostFocus);
1065     game_ctrl.Font = GetFormFont ();
1066     game_ctrl.Dock = DockStyle.Top;
1067     game_ctrl.Width = this.Width - 10;
1068 william 273 IEmuConfig config = CurrentPlatformControl.Tag as IEmuConfig;
1069 william 385 if (config.PlatformNameLong == FAVORITES_PLATFORM) {
1070 william 273 game_ctrl.GameName = "You haven't favorited any games, select a game and then press CTRL+F to favorite it";
1071     } else {
1072     game_ctrl.GameName = string.Format ("No {0} games were found", config.PlatformNameShort);
1073 william 377 }
1074     rom_flow.Controls.Add (game_ctrl);
1075     }
1076     rom_flow.Controls [0].Select ();
1077     t.Stop ();
1078     gLog.Profiler.WriteLine ("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
1079     }
1080     #endregion
1081     #region platformWorker
1082     private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
1083     private object platformimage_lock = new object ();
1084     private void platformWorker_DoWork (object sender, DoWorkEventArgs e)
1085 william 327 {
1086     Stopwatch t = e.Argument as Stopwatch;
1087 william 377 progress_bar.Font = ResizeFont (GetFormFont (), PROGRESS_BAR_FONT_SIZE);
1088 william 183 #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
1089 william 327 progress_bar.ShowPercentageLabel = false;
1090 william 183 #endif
1091 william 327 progress_bar.ProgressColor = Color.LimeGreen;
1092 william 377 progress_bar.Dock = DockStyle.Top;
1093    
1094     if (this.InvokeRequired) {
1095     this.Invoke ((MethodInvoker)delegate () {
1096     this.Controls.Add (progress_bar);
1097     });
1098 william 327 } else {
1099     this.Controls.Add (progress_bar);
1100     }
1101     progress_bar.Invoke (new MethodInvoker (delegate {
1102     progress_bar.Margin = new System.Windows.Forms.Padding (0);
1103     progress_bar.Size = new Size (GetFormWidth () - 25, 100);
1104     }));
1105    
1106 william 377 HashSet<IEmuConfig> platforms = new HashSet<IEmuConfig> ();
1107 william 355 using (PlatformParser parser = new PlatformParser (cache, Config.LocalRomPath, Config.RomPath, new ProgressEvent (worker_progress_event))) {
1108 william 327 foreach (var platform in parser.Platforms) {
1109 william 377 platforms.Add (platform);
1110 william 327 }
1111     }
1112    
1113    
1114     double count = 0;
1115     double total_count = platforms.Count;
1116     if (total_count > 0) {
1117     progress_bar.Invoke (new MethodInvoker (delegate {
1118     progress_bar.Message = string.Format ("Please Wait Loading {0} Rom Platforms...", total_count);
1119     }));
1120     }
1121     foreach (IEmuConfig config in platforms) {
1122     progress_bar.Invoke (new MethodInvoker (delegate {
1123     progress_bar.Value = (int)(100.0 * (count / total_count));
1124 william 377 }));
1125    
1126 william 327 PlatformControl platform_ctrl = new PlatformControl ();
1127     platform_ctrl.Font = GetFormFont ();
1128     platform_ctrl.Dock = DockStyle.Top;
1129     platform_ctrl.Width = this.Width - 10;
1130     platform_ctrl.Tag = config;
1131     try {
1132 william 377 lock (platformimage_lock) {
1133     //gLog.Debug.WriteLine ("handling platform: {0}", config.ToString ());
1134     if (String.IsNullOrEmpty (config.PlatformImage)) {
1135     platform_ctrl.UpdatePlatformImage ((Image)DefaultPlatformImage.Clone ());
1136     } else {
1137     string path = config.GetFullPlatformImage ();
1138     if (File.Exists (path)) {
1139     platform_ctrl.UpdatePlatformImage (path);
1140     } else {
1141     platform_ctrl.UpdatePlatformImage ((Image)DefaultPlatformImage.Clone ());
1142 william 355 }
1143 william 327 }
1144     }
1145     } catch (Exception ex) {
1146     gLog.Error.WriteLine (ex.Message);
1147 william 377 gLog.Verbose.Error.WriteLine (ex.ToString ());
1148     throw ex;
1149     }
1150     platform_ctrl.PlatformName = config.ToString ();
1151     platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler (platform_ctrl_PreviewKeyDown);
1152     platform_ctrl.GotFocus += new EventHandler (platform_ctrl_GotFocus);
1153     platform_ctrl.LostFocus += new EventHandler (platform_ctrl_LostFocus);
1154     if (platform_flow.InvokeRequired) { platform_flow.Invoke ((MethodInvoker)delegate () { platform_flow.Controls.Add (platform_ctrl); }); } else { platform_flow.Controls.Add (platform_ctrl); }
1155     count++;
1156     }
1157     progress_bar.Invoke (new MethodInvoker (delegate {
1158     progress_bar.Value = 0; progress_bar.Update ();
1159     }));
1160     e.Result = t;
1161     }
1162     private void platformWorker_ProgressChanged (object sender, ProgressChangedEventArgs e) { }
1163     private void platformWorker_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)
1164     {
1165     gLog.Debug.WriteLine ("platformWorker_RunWorkerCompleted called");
1166     if (this.InvokeRequired) {
1167     this.Invoke ((MethodInvoker)delegate () {
1168 william 327 progress_bar.Message = "";
1169 william 377 this.Controls.Remove (progress_bar);
1170     });
1171     } else {
1172 william 327 progress_bar.Message = "";
1173 william 377 this.Controls.Remove (progress_bar);
1174     }
1175     Stopwatch t = e.Result as Stopwatch;
1176     if (platform_flow.Controls.Count == 0) {
1177     PlatformControl platform_ctrl = new PlatformControl ();
1178     platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler (platform_ctrl_PreviewKeyDown);
1179     platform_ctrl.GotFocus += new EventHandler (platform_ctrl_GotFocus);
1180     platform_ctrl.LostFocus += new EventHandler (platform_ctrl_LostFocus);
1181     platform_ctrl.Font = GetFormFont ();
1182     platform_ctrl.Dock = DockStyle.Top;
1183     platform_ctrl.Width = this.Width - 10;
1184     platform_ctrl.PlatformName = string.Format ("You don't have any roms in your rompath: '{0}'", Config.RomPath);
1185     platform_flow.Controls.Add (platform_ctrl);
1186     }
1187 william 274 if (SavedPlatformIndex != -1) {
1188     var platform_control = platform_flow.Controls [SavedPlatformIndex] as PlatformControl;
1189     platform_flow.SuspendLayout ();
1190     CurrentPlatformControl = platform_control;
1191     platform_flow.ScrollControlIntoView (CurrentPlatformControl);
1192     CurrentPlatformControl.Select ();
1193     platform_flow.ResumeLayout ();
1194     SavedPlatformIndex = -1;
1195     } else {
1196 william 377 platform_flow.Controls [0].Select ();
1197 william 274 }
1198 william 377 t.Stop ();
1199     gLog.Profiler.WriteLine ("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
1200     }
1201     #endregion
1202    
1203     private void Form1_FormClosed (object sender, FormClosedEventArgs e)
1204     {
1205     Cursor.Show ();
1206     }
1207     #endregion
1208     }
1209 william 4 }

  ViewVC Help
Powered by ViewVC 1.1.22