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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 286 - (hide annotations) (download)
Thu Aug 18 05:26:09 2016 UTC (3 years, 6 months ago) by william
File size: 37711 byte(s)
remove logging spam
1 william 266 #define HAVE_X11_BORDERSTYLE_ERROR // indicates that there is an error with Control.set_InternalBorderStyle resulting in an X11 Error BadWindow (invalid Window parameter)
2    
3     //#define DISABLE_CURSOR_HIDE // when this is present, the cursor will not be hidden
4 william 183 //#define DISABLE_PROGRESS_PERCENTAGE_MESSASGE // when this is present, no progress percent message will be shown on any progressbar
5 william 272
6 william 241 #define DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG // when defined will turn off kLogLevel_Debug messages, in release mode
7     //#define DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG // when defined will turn off kLogLevel_VerboseDebug message, in debug mode
8 william 181 using System;
9 william 4 using System.Collections.Generic;
10     using System.ComponentModel;
11     using System.Data;
12     using System.Drawing;
13     using System.Linq;
14     using System.Text;
15     using System.Windows.Forms;
16 william 12 using EmuXPortal.Api;
17 william 23 using System.Diagnostics;
18 william 60 using System.Reflection;
19     using System.Threading;
20 william 183 using Utilities.TransparentControls;
21 william 238 using Enterprise.Logging;
22     using System.IO;
23 william 272 using Enterprise.CrossPlatform;
24 william 4
25     namespace EmuXPortal
26     {
27     public partial class Form1 : Form
28     {
29 william 255 static readonly Color SELECTED_CONTROL_BACKCOLOR = Color.SteelBlue;
30 william 183 const float PROGRESS_BAR_FONT_SIZE = 24;
31     private delegate Font Delegate_GetFormFont();
32     private delegate Font Delegate_ResizeFont(Font font, float size);
33 william 17 IEmuConfig CurrentSelectedRom = null;
34 william 31 PlatformControl CurrentPlatformControl = null;
35 william 274 int SavedPlatformIndex= -1;
36 william 31 GameControl CurrentGameControl = null;
37 william 243
38     #region unhandled exception support
39     static void Application_Unhandled_ThreadException(object sender, ThreadExceptionEventArgs e)
40     {
41     UnhandledExceptionEventArgs uea = new UnhandledExceptionEventArgs(e.Exception, false);
42     UnhandledExceptionEventHandler(sender, uea);
43     }
44     static void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs args)
45     {
46     Exception ex = (args.ExceptionObject as Exception);
47     if (sender == null)
48     {
49     gLog.Error.WriteLine("Caught an unhandled exception from an unkown source");
50     }
51     else
52     {
53     gLog.Error.WriteLine("Caught an unhandled exception from type: {0}", sender.GetType().Name);
54     }
55    
56     if (ex == null)
57     {
58     gLog.Error.WriteLine("The exception object was null -- it probably is not derived from System.Exception");
59     }
60     else
61     {
62     ex = ex.GetBaseException();
63     gLog.Error.WriteLine("{0}:", ex.GetType().Name);
64     gLog.Verbose.Error.WriteLine(ex.ToString());
65     }
66    
67     }
68     #endregion
69 william 4 public Form1()
70     {
71 william 243 InitializeComponent();
72    
73 william 266 this.KeyPreview = true;
74 william 243
75 william 238 #region logging support
76     string log_path = Application.StartupPath;
77     string log_filename = string.Format("{0}.log", typeof(Form1).Assembly.GetName().Name);
78 william 283 FileInfo log_file = new FileInfo(OSInfo.FormatPath(string.Format(@"{0}\{1}", log_path, log_filename)));
79 william 238
80 william 283 gLog.CreateLog(log_file.Name, true, LogLevel.kLogLevel_All_NoProgress, new EventHandler<LoggerOnFlushEventArgs>(Log_OnFlush));
81 william 238 #if DEBUG
82     LogLevel gLevel = gLog.LogLevel;
83     #if DISABLE_DEBUG_MODE_KLOGLEVEL_VERBOSE_DEBUG
84     gLevel &= ~LogLevel.kLogLevel_VerboseDebug;
85     #else
86     gLevel |= LogLevel.kLogLevel_VerboseDebug;
87     #endif
88     gLevel |= LogLevel.kLogLevel_Debug;
89     gLog.SetLogLevel(gLevel);
90     #else
91     LogLevel gLevel = LogLevel.kLogLevel_Default; // set the default log level: Info, Warn, Error, Debug
92     // it is OK for kLogLevel_Debug to be set in Release mode ... must of the chatty messages are from kLogLevel_VerboseDebug
93     #if DISABLE_RELEASE_MODE_KLOGLEVEL_DEBUG
94     gLevel &= ~LogLevel.kLogLevel_Debug;
95     #else
96     gLevel |= LogLevel.kLogLevel_Debug;
97     #endif
98     gLevel &= ~LogLevel.kLogLevel_VerboseDebug; // ensure this is not set, ever in release mode
99     gLog.SetLogLevel(gLevel);
100     #endif
101     #endregion
102    
103 william 243 #region unhandled exception support
104 william 266 // AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionEventHandler);
105     // Application.ThreadException += Application_Unhandled_ThreadException;
106 william 243 #endregion
107    
108 william 17 platform_flow.Dock = DockStyle.Fill;
109     rom_flow.Dock = DockStyle.Fill;
110 william 238 }
111 william 243
112    
113    
114    
115 william 238 #region logging support
116     void Log_OnFlush(object sender, LoggerOnFlushEventArgs e) { OnLogFlush(e.Buffer); }
117     void OnLogFlush(string logmessage)
118     {
119     //if (this.IsDisposed) { return; }
120     ////UpdateStatus(logmessage);
121     ////UpdateLogOutput(logmessage);
122     //Application.DoEvents();
123     }
124     #endregion
125 william 184
126 william 17 private void Form1_Load(object sender, EventArgs e)
127     {
128 william 238 Config.LoadConfig();
129     Config.InitializePresentationForm(this);
130 william 17 }
131 william 181 private void Form1_Shown(object sender, EventArgs e)
132     {
133     platform_flow.Visible = true;
134     #if !DISABLE_CURSOR_HIDE
135     Cursor.Hide();
136     #else
137     Cursor.Show();
138     #endif
139     }
140 william 253 void platform_ctrl_LostFocus(object sender, EventArgs e)
141     {
142     PlatformControl c = sender as PlatformControl;
143 william 266 if (c != null) {
144     #if !HAVE_X11_BORDERSTYLE_ERROR
145     c.BorderStyle = BorderStyle.None;
146     #endif
147     c.BackColor = this.BackColor;
148     }
149 william 253 }
150 william 13
151 william 253 void platform_ctrl_GotFocus(object sender, EventArgs e)
152 william 266 {
153 william 253 PlatformControl c = sender as PlatformControl;
154 william 266 if (c != null) {
155     #if !HAVE_X11_BORDERSTYLE_ERROR
156     c.BorderStyle = BorderStyle.FixedSingle;
157     #endif
158     c.BackColor = SELECTED_CONTROL_BACKCOLOR;
159     CurrentPlatformControl = c;
160     }
161 william 253 }
162     void game_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
163     {
164 william 286 //gLog.Verbose.Debug.WriteLine ("game_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
165 william 253 GameControl c = sender as GameControl;
166 william 17
167 william 253 int changeAmount = 0;
168     int currentPosition = 0;
169     if (e.KeyCode == Keys.F && (e.Modifiers & Keys.Control) == Keys.Control)
170     {
171     IRomConfig config = c.Tag as IRomConfig;
172     if (config == null)
173     {
174     gLog.Error.WriteLine("Unable to add/remove from/to favorites (config is null): {0} [{1}]", config.RomTitle, config.RomFile);
175     }
176     else
177     {
178     var isFavorite = RomFavorite.IsFavorite(config);
179     if (isFavorite)
180     {
181     // add to favorites
182 william 286 //gLog.Debug.WriteLine("Removing from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
183 william 253 if (!RomFavorite.RemoveFavorite(config))
184     {
185     gLog.Error.WriteLine("Failed to remove from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
186     }
187     else
188     {
189     gLog.Info.WriteLine("Removed from favorites: {0} [{1}]", config.RomTitle, config.RomFile);
190     if (config.Config.PlatformNameShort == "Favorites")
191     {
192     var parent = c.Parent;
193     if (parent != null)
194     {
195     parent.Controls.Remove(c);
196     if (parent.Controls.Count > 0)
197     {
198     var next_ctrl = parent.Controls[0];
199     if (next_ctrl != null)
200     {
201     next_ctrl.Select();
202     }
203     }
204     }
205     }
206     }
207     }
208     else
209     {
210     // add to favorites
211 william 286 //gLog.Debug.WriteLine("Adding to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
212 william 253 if (!RomFavorite.AddFavorite(config))
213     {
214     gLog.Error.WriteLine("Failed to add to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
215     }
216     else
217     {
218     gLog.Info.WriteLine("Added to favorites: {0} [{1}]", config.RomTitle, config.RomFile);
219     }
220     }
221 william 286 //gLog.Debug.WriteLine("Updateing favorites");
222 william 253 if (!RomFavorite.UpdateFavorites())
223     {
224     gLog.Error.WriteLine("Failed to update favorites");
225     }
226     else
227     {
228     gLog.Info.WriteLine("Updated favorites");
229     }
230     }
231     return; // stop processing other keys
232 william 168
233 william 253 }
234 william 273 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
235 william 272 if (OSInfo.OSIsUnix) {
236     rom_flow.SuspendLayout ();
237     var ctl = game_ctrl_get_previous_control (true);
238     rom_flow.ScrollControlIntoView (ctl);
239     rom_flow.ResumeLayout (false);
240     }
241     }
242 william 273 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
243 william 272 if (OSInfo.OSIsUnix) {
244     rom_flow.SuspendLayout ();
245     var ctl = game_ctrl_get_next_control (true);
246     rom_flow.ScrollControlIntoView (ctl);
247     rom_flow.ResumeLayout (false);
248     }
249     }
250 william 253 if (e.KeyCode == Keys.Home)
251     {
252     rom_flow.SuspendLayout();
253     rom_flow.Controls[0].Select();
254     rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
255     rom_flow.ResumeLayout(false);
256     }
257     if (e.KeyCode == Keys.End)
258     {
259     rom_flow.SuspendLayout();
260     rom_flow.Controls[rom_flow.Controls.Count - 1].Select();
261     rom_flow.ScrollControlIntoView(rom_flow.Controls[rom_flow.Controls.Count - 1]);
262     rom_flow.ResumeLayout(false);
263     }
264     if (e.KeyCode == Keys.PageUp)
265     {
266     rom_flow.SuspendLayout();
267     changeAmount = rom_flow.VerticalScroll.LargeChange;
268     currentPosition = rom_flow.VerticalScroll.Value;
269     if ((currentPosition - changeAmount) > rom_flow.VerticalScroll.Minimum)
270     {
271     try
272     {
273     rom_flow.VerticalScroll.Value -= changeAmount;
274     }
275     catch
276     {
277     rom_flow.Controls[0].Select();
278     rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
279     rom_flow.PerformLayout();
280     return;
281     }
282     }
283     else
284     {
285     rom_flow.Controls[0].Select();
286     rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
287     }
288     GameControl s = game_ctrl_get_last_visible();
289     s.Select();
290     rom_flow.ScrollControlIntoView(s);
291     rom_flow.ResumeLayout(false);
292     }
293     if (e.KeyCode == Keys.PageDown)
294     {
295     rom_flow.SuspendLayout();
296     changeAmount = rom_flow.VerticalScroll.LargeChange;
297     currentPosition = rom_flow.VerticalScroll.Value;
298     if ((currentPosition - changeAmount) < rom_flow.VerticalScroll.Maximum)
299     {
300     try
301     {
302     rom_flow.VerticalScroll.Value += changeAmount;
303     }
304     catch
305     {
306     rom_flow.Controls[0].Select();
307     rom_flow.ScrollControlIntoView(rom_flow.Controls[0]);
308     rom_flow.PerformLayout();
309     return;
310     }
311     }
312     else
313     {
314     rom_flow.VerticalScroll.Value = rom_flow.VerticalScroll.Maximum;
315     }
316     GameControl s = game_ctrl_get_last_visible();
317     s.Select();
318     rom_flow.ScrollControlIntoView(s);
319     rom_flow.ResumeLayout(false);
320     }
321 william 168
322 william 253 if (e.KeyCode == Keys.Enter)
323     {
324     IRomConfig config = c.Tag as IRomConfig;
325 william 168
326 william 253 Process p = new Process();
327 william 168
328 william 253 p.StartInfo.FileName = config.Config.GameExe == "" ? config.Config.EmuPath : config.Config.GameExe;
329     p.StartInfo.Arguments = config.Config.GameExeArgs == "" ? EmuConfigLoader.GetEMUOptions(config) : config.Config.GameExeArgs;
330 william 275
331 william 286 //gLog.Verbose.Debug.WriteLine ("Emulator: {0}", p.StartInfo.FileName);
332     //gLog.Verbose.Debug.WriteLine ("Args: {0}", p.StartInfo.Arguments);
333 william 253 p.Start();
334 william 168
335 william 253 // minimize EmuXPortal
336     this.WindowState = FormWindowState.Minimized;
337     // wait for exit of game
338     p.WaitForExit();
339     // maximize EmuXPortal
340     this.WindowState = FormWindowState.Maximized;
341     }
342 william 273 if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape)
343 william 274 {
344     SavedPlatformIndex = platform_flow.Controls.IndexOf (CurrentPlatformControl);
345 william 253 rom_flow.Visible = false;
346     platform_flow.Visible = true;
347     }
348 william 168
349 william 253 if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
350     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
351     {
352     rom_flow.SuspendLayout();
353     char t = (char)e.KeyCode;
354     GameControl ctrl = (rom_flow.GetNextControl(CurrentGameControl, true) as GameControl);
355     if (ctrl == null) { ctrl = (rom_flow.GetNextControl(rom_flow.Controls[0], true) as GameControl); }
356     bool found = false;
357     GameControl pc = CurrentGameControl;
358     bool wrapped = false;
359     bool not_found = true;
360     while (!found)
361     {
362     if (wrapped)
363     {
364     foreach (Control ctl in rom_flow.Controls)
365     {
366     GameControl p_ctl = ctl as GameControl; if (p_ctl.GameName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
367     }
368     if (not_found) { found = true; }
369     }
370     ctrl = (rom_flow.GetNextControl(pc, true) as GameControl);
371     if (ctrl == null)
372     {
373     ctrl = rom_flow.Controls[0] as GameControl;
374     wrapped = true;
375     }
376     if (ctrl.GameName.ToLower().StartsWith(t.ToString().ToLower()))
377     {
378     rom_flow.ScrollControlIntoView(ctrl);
379     ctrl.Select();
380     found = true;
381     }
382     pc = ctrl;
383     }
384     rom_flow.ResumeLayout(false);
385     }
386     }
387     void platform_ctrl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
388     {
389 william 286 //gLog.Verbose.Debug.WriteLine ("platform_ctrl_PreviewKeyDown() fired -- keycode [{0}]", e.KeyCode);
390 william 253 PlatformControl c = sender as PlatformControl;
391     int changeAmount = 0;
392     int currentPosition = 0;
393 william 273 if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Left) {
394 william 272 if (OSInfo.OSIsUnix) {
395     platform_flow.SuspendLayout ();
396     var ctl = platform_ctrl_get_previous_control (true);
397     platform_flow.ScrollControlIntoView (ctl);
398     platform_flow.ResumeLayout (false);
399     }
400     }
401 william 273 if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Right) {
402 william 272 if (OSInfo.OSIsUnix) {
403     platform_flow.SuspendLayout ();
404     var ctl = platform_ctrl_get_next_control (true);
405     platform_flow.ScrollControlIntoView (ctl);
406     platform_flow.ResumeLayout (false);
407     }
408     }
409 william 253 if (e.KeyCode == Keys.Home)
410     {
411     platform_flow.SuspendLayout();
412     platform_flow.Controls[0].Select();
413     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
414     platform_flow.ResumeLayout(false);
415     }
416     if (e.KeyCode == Keys.End)
417     {
418     platform_flow.SuspendLayout();
419     platform_flow.Controls[platform_flow.Controls.Count -1].Select();
420     platform_flow.ScrollControlIntoView(platform_flow.Controls[platform_flow.Controls.Count - 1]);
421     platform_flow.ResumeLayout(false);
422     }
423     if (e.KeyCode == Keys.PageUp)
424     {
425     platform_flow.SuspendLayout();
426     changeAmount = platform_flow.VerticalScroll.LargeChange;
427     currentPosition = platform_flow.VerticalScroll.Value;
428     if ((currentPosition - changeAmount) > platform_flow.VerticalScroll.Minimum)
429     {
430     platform_flow.VerticalScroll.Value -= changeAmount;
431     }
432     else
433     {
434     platform_flow.VerticalScroll.Value = platform_flow.VerticalScroll.Minimum;
435     }
436     PlatformControl s = platform_ctrl_get_last_visible();
437     s.Select();
438     platform_flow.ScrollControlIntoView(s);
439     platform_flow.ResumeLayout(false);
440     }
441     if (e.KeyCode == Keys.PageDown)
442     {
443     platform_flow.SuspendLayout();
444     changeAmount = platform_flow.VerticalScroll.LargeChange;
445     currentPosition = platform_flow.VerticalScroll.Value;
446     if ((currentPosition - changeAmount) < platform_flow.VerticalScroll.Maximum)
447     {
448     try
449     {
450     platform_flow.VerticalScroll.Value += changeAmount;
451     }
452     catch
453     {
454     platform_flow.Controls[0].Select();
455     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
456     rom_flow.PerformLayout();
457     return;
458     }
459     }
460     else
461     {
462     platform_flow.Controls[0].Select();
463     platform_flow.ScrollControlIntoView(platform_flow.Controls[0]);
464     }
465     PlatformControl s = platform_ctrl_get_last_visible();
466     s.Select();
467     platform_flow.ScrollControlIntoView(s);
468     platform_flow.ResumeLayout(false);
469     }
470     if (e.KeyCode == Keys.Enter)
471     {
472     // load this platform
473     platform_flow.Visible = false;
474     CurrentSelectedRom = c.Tag as IEmuConfig;
475     rom_flow.Visible = true;
476     rom_flow.BringToFront();
477     }
478 william 273 if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Escape)
479 william 253 {
480     this.Close();
481     }
482     if ((e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z) ||
483     (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9))
484     {
485     platform_flow.SuspendLayout();
486     char t = (char)e.KeyCode;
487     PlatformControl ctrl = (platform_flow.GetNextControl(CurrentPlatformControl, true) as PlatformControl);
488     if (ctrl == null) { ctrl = (platform_flow.GetNextControl(platform_flow.Controls[0], true) as PlatformControl); }
489     bool found = false;
490     PlatformControl pc = CurrentPlatformControl;
491     bool wrapped = false;
492     bool not_found = true;
493     while (!found)
494     {
495     if (wrapped)
496     {
497     foreach (Control ctl in platform_flow.Controls)
498     {
499     PlatformControl p_ctl = ctl as PlatformControl; if (p_ctl.PlatformName.ToLower().StartsWith(t.ToString().ToLower())) { not_found = false; }
500     }
501     if (not_found) { found = true; }
502     }
503     ctrl = (platform_flow.GetNextControl(pc, true) as PlatformControl);
504     if (ctrl == null)
505     {
506     ctrl = platform_flow.Controls[0] as PlatformControl;
507     wrapped = true;
508     }
509     if (ctrl.PlatformName.ToLower().StartsWith(t.ToString().ToLower()))
510     {
511     platform_flow.ScrollControlIntoView(ctrl);
512     ctrl.Select();
513     found = true;
514     }
515     pc = ctrl;
516     }
517     platform_flow.ResumeLayout(false);
518     }
519     }
520 william 17
521     private void platform_flow_VisibleChanged(object sender, EventArgs e)
522     {
523     if (!platform_flow.Visible) return;
524     platform_flow.Controls.Clear();
525     platform_flow.BringToFront();
526 william 37 Stopwatch t = new Stopwatch();
527     t.Start();
528 william 60 platformWorker.RunWorkerAsync(t);
529 william 17 }
530    
531     private void rom_flow_VisibleChanged(object sender, EventArgs e)
532     {
533     if (!rom_flow.Visible) return;
534     rom_flow.Controls.Clear();
535     rom_flow.BringToFront();
536 william 37 Stopwatch t = new Stopwatch();
537     t.Start();
538 william 60 gameWorker.RunWorkerAsync(t);
539 william 17 }
540    
541 william 253 void game_ctrl_LostFocus(object sender, EventArgs e)
542     {
543     GameControl c = sender as GameControl;
544 william 266 #if !HAVE_X11_BORDERSTYLE_ERROR
545 william 253 c.BorderStyle = BorderStyle.None;
546 william 266 #endif
547 william 255 c.BackColor = this.BackColor;
548 william 253 }
549 william 17
550 william 253 void game_ctrl_GotFocus(object sender, EventArgs e)
551     {
552     GameControl c = sender as GameControl;
553 william 266 #if !HAVE_X11_BORDERSTYLE_ERROR
554 william 253 c.BorderStyle = BorderStyle.FixedSingle;
555 william 266 #endif
556 william 255 c.BackColor = SELECTED_CONTROL_BACKCOLOR;
557 william 253 CurrentGameControl = c;
558     }
559 william 17
560 william 253 private GameControl game_ctrl_get_last_visible()
561     {
562     GameControl s = new GameControl();
563     foreach (GameControl c in rom_flow.Controls)
564     {
565     if (c.Bounds.IntersectsWith(rom_flow.Bounds))
566     s = c;
567     }
568     return s;
569     }
570     private PlatformControl platform_ctrl_get_last_visible()
571     {
572     PlatformControl s = new PlatformControl();
573     foreach (PlatformControl c in platform_flow.Controls)
574     {
575     if (c.Bounds.IntersectsWith(platform_flow.Bounds))
576     s = c;
577     }
578     return s;
579     }
580 william 272 private PlatformControl platform_ctrl_get_next_control(bool update_control)
581     {
582     PlatformControl s = new PlatformControl();
583     int index = platform_flow.Controls.IndexOf (CurrentPlatformControl);
584     index = index + 1;
585     if (index < 0) {
586     index = platform_flow.Controls.Count-1;
587     }
588     if (index > platform_flow.Controls.Count-1) {
589     index = 0;
590     }
591     s = platform_flow.Controls [index] as PlatformControl;
592     if (update_control) {
593     CurrentPlatformControl = s;
594     s.Select ();
595     }
596     return s;
597     }
598     private PlatformControl platform_ctrl_get_previous_control(bool update_control)
599     {
600     PlatformControl s = null;
601     int index = platform_flow.Controls.IndexOf (CurrentPlatformControl);
602     index = index - 1;
603     if (index < 0) {
604     index = platform_flow.Controls.Count-1;
605     }
606     if (index > platform_flow.Controls.Count-1) {
607     index = 0;
608     }
609     s = platform_flow.Controls [index] as PlatformControl;
610     if (update_control) {
611     CurrentPlatformControl = s;
612     s.Select ();
613     }
614     return s;
615     }
616     private GameControl game_ctrl_get_next_control(bool update_control)
617     {
618     GameControl s = new GameControl();
619     int index = rom_flow.Controls.IndexOf (CurrentGameControl);
620     index = index + 1;
621     if (index < 0) {
622     index = rom_flow.Controls.Count-1;
623     }
624     if (index > rom_flow.Controls.Count-1) {
625     index = 0;
626     }
627     s = rom_flow.Controls [index] as GameControl;
628     if (update_control) {
629     CurrentGameControl = s;
630     s.Select ();
631     }
632     return s;
633     }
634     private GameControl game_ctrl_get_previous_control(bool update_control)
635     {
636     GameControl s = new GameControl();
637     int index = rom_flow.Controls.IndexOf (CurrentGameControl);
638     index = index - 1;
639     if (index < 0) {
640     index = rom_flow.Controls.Count-1;
641     }
642     if (index > rom_flow.Controls.Count-1) {
643     index = 0;
644     }
645     s = rom_flow.Controls [index] as GameControl;
646     if (update_control) {
647     CurrentGameControl = s;
648     s.Select ();
649     }
650     return s;
651     }
652 william 168
653 william 60
654     #region Background Workers
655    
656     private int GetFormWidth()
657     {
658     if (this.InvokeRequired)
659     {
660     return Convert.ToInt32(this.Invoke((MethodInvoker)delegate() { GetFormWidth(); }));
661     }
662     else
663     {
664     return this.Width;
665     }
666     }
667 william 183 private Font GetFormFont()
668     {
669     if (this.InvokeRequired)
670     {
671     return (this.Invoke(new Delegate_GetFormFont(GetFormFont)) as Font);
672     }
673     else
674     {
675     return this.Font;
676     }
677     }
678    
679     private Font ResizeFont(Font font, float size)
680     {
681     if (this.InvokeRequired)
682     {
683     return (this.Invoke(new Delegate_ResizeFont(ResizeFont), new object[] { font, size })as Font);
684     }
685     else
686     {
687     return new Font(font.FontFamily, size);
688     }
689     }
690    
691 william 113 //private void AddPlatformControl(Control c)
692     //{
693     // if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { AddPlatformControl(c); }); }
694     // else
695     // {
696     // platform_flow.Controls.Add(c);
697     // }
698     //}
699     //private void UpdatePlatformControls()
700     //{
701     // //if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { UpdatePlatformControls(); }); }
702     // //else { this.Update(); }
703     //}
704     //private void AddGameControl(Control c)
705     //{
706     // if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { AddGameControl(c); }); }
707     // else
708     // {
709     // rom_flow.Controls.Add(c);
710     // }
711     //}
712     //private void UpdateGameControls()
713     //{
714     // //if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { UpdateGameControls(); }); }
715     // //else { this.Update(); }
716     //}
717 william 60 #region gameWorker
718 william 110 private static Image DefaultGameImage = Properties.Resources.DefaultGameImage;
719 william 137 private object gameimage_lock = new object();
720 william 60 private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
721     {
722     Stopwatch t = e.Argument as Stopwatch;
723 william 238 RomParser parser = new RomParser(CurrentSelectedRom);
724 william 60
725 william 183 CustomProgressBar bar = new CustomProgressBar();
726     bar.Font = ResizeFont(GetFormFont(), PROGRESS_BAR_FONT_SIZE);
727     #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
728     bar.ShowPercentageLabel = false;
729     #endif
730     bar.ProgressColor = Color.LimeGreen;
731     bar.Dock = DockStyle.Top;
732    
733     if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Add(bar); }); }
734     else { this.Controls.Add(bar); }
735     bar.Invoke(new MethodInvoker(delegate { bar.Margin = new System.Windows.Forms.Padding(0); bar.Size = new Size(GetFormWidth() - 25, 100); }));
736 william 60
737     double count = 0;
738     double total_count = parser.Roms.Count;
739     foreach (IRomConfig config in parser.Roms)
740     {
741 william 183 bar.Invoke(new MethodInvoker(delegate { bar.Value = (int)(100.0 * (count / total_count)); }));
742 william 60 GameControl game_ctrl = new GameControl();
743 william 183 game_ctrl.Font = GetFormFont();
744 william 60 game_ctrl.Dock = DockStyle.Top;
745     game_ctrl.Width = this.Width - 10;
746     game_ctrl.Tag = config;
747 william 137 try
748     {
749     lock (gameimage_lock)
750     {
751     game_ctrl.GameImage = config.RomImage == null ? (Image)DefaultGameImage.Clone() : (Image)config.RomImage.Clone();
752     config.ReleaseRomImageResource();
753     }
754     }
755     catch (Exception ex)
756     {
757     throw ex;
758 william 178 }
759     if (CurrentSelectedRom.PlatformNameShort == "Favorites")
760     {
761     //game_ctrl.GameName = config.RomTitle;
762     game_ctrl.GameName = RomFavorite.GetRomTitleFromConfig(config);
763     }
764     else
765     {
766     game_ctrl.GameName = config.RomTitle;
767 william 183 }
768 william 253 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
769     game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
770     game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
771 william 113 if (rom_flow.InvokeRequired) { rom_flow.Invoke((MethodInvoker)delegate() { rom_flow.Controls.Add(game_ctrl); }); }
772     else { rom_flow.Controls.Add(game_ctrl); }
773 william 60 count++;
774     }
775 william 183 bar.Invoke(new MethodInvoker(delegate { bar.Value = 1; bar.Update(); }));
776     if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Remove(bar); }); } else { this.Controls.Remove(bar); }
777 william 270 e.Result = t;
778 william 105 parser.Dispose();
779 william 60 }
780     private void gameWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
781     private void gameWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
782     {
783 william 197 Stopwatch t = e.Result as Stopwatch;
784 william 198 if (rom_flow.Controls.Count == 0)
785 william 197 {
786     GameControl game_ctrl = new GameControl();
787 william 253 game_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(game_ctrl_PreviewKeyDown);
788     game_ctrl.GotFocus += new EventHandler(game_ctrl_GotFocus);
789     game_ctrl.LostFocus += new EventHandler(game_ctrl_LostFocus);
790 william 197 game_ctrl.Font = GetFormFont();
791     game_ctrl.Dock = DockStyle.Top;
792     game_ctrl.Width = this.Width - 10;
793 william 273 IEmuConfig config = CurrentPlatformControl.Tag as IEmuConfig;
794     if (config.PlatformNameLong == "Favorites") {
795     game_ctrl.GameName = "You haven't favorited any games, select a game and then press CTRL+F to favorite it";
796     } else {
797     game_ctrl.GameName = string.Format ("No {0} games were found", config.PlatformNameShort);
798     }
799 william 197 rom_flow.Controls.Add(game_ctrl);
800     }
801 william 253 rom_flow.Controls[0].Select();
802 william 60 t.Stop();
803 william 286 //gLog.Profiler.WriteLine("RomParser took: {0}s to parse roms", (int)t.Elapsed.TotalSeconds);
804 william 60 }
805     #endregion
806     #region platformWorker
807 william 110 private static Image DefaultPlatformImage = Properties.Resources.DefaultPlatformImage;
808 william 137 private object platformimage_lock = new object();
809 william 60 private void platformWorker_DoWork(object sender, DoWorkEventArgs e)
810     {
811     Stopwatch t = e.Argument as Stopwatch;
812 william 238 PlatformParser parser = new PlatformParser(Config.RomPath);
813 william 60 double count = 0;
814     double total_count = parser.Platforms.Count;
815 william 183 CustomProgressBar bar = new CustomProgressBar();
816     bar.Font = ResizeFont(GetFormFont(), PROGRESS_BAR_FONT_SIZE);
817     #if DISABLE_PROGRESS_PERCENTAGE_MESSASGE
818     bar.ShowPercentageLabel = false;
819     #endif
820     bar.ProgressColor = Color.LimeGreen;
821     bar.Dock = DockStyle.Top;
822 william 270
823 william 183 if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Add(bar); }); }
824     else { this.Controls.Add(bar); }
825     bar.Invoke(new MethodInvoker(delegate { bar.Margin = new System.Windows.Forms.Padding(0); bar.Size = new Size(GetFormWidth() - 25, 100); }));
826 william 60 foreach (IEmuConfig config in parser.Platforms)
827     {
828 william 183 bar.Invoke(new MethodInvoker(delegate { bar.Value = (int)(100.0 * (count / total_count)); }));
829    
830 william 60 PlatformControl platform_ctrl = new PlatformControl();
831 william 183 platform_ctrl.Font = GetFormFont();
832 william 60 platform_ctrl.Dock = DockStyle.Top;
833     platform_ctrl.Width = this.Width - 10;
834     platform_ctrl.Tag = config;
835 william 137 try
836     {
837     lock (platformimage_lock)
838     {
839 william 138 platform_ctrl.PlatformImage = config.PlatformImage == null ? (Image)DefaultPlatformImage.Clone() : (Image)config.PlatformImage.Clone();
840 william 137 config.ReleasePlatformImageResource();
841     }
842     }
843     catch (Exception ex)
844     {
845     throw ex;
846     }
847 william 60 platform_ctrl.PlatformName = config.ToString();
848 william 253 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
849     platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
850     platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
851 william 113 if (platform_flow.InvokeRequired) { platform_flow.Invoke((MethodInvoker)delegate() { platform_flow.Controls.Add(platform_ctrl); }); }
852 william 183 else { platform_flow.Controls.Add(platform_ctrl); }
853 william 60 count++;
854     }
855 william 183 bar.Invoke(new MethodInvoker(delegate { bar.Value = 1; bar.Update(); }));
856     if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate() { this.Controls.Remove(bar); }); } else { this.Controls.Remove(bar); }
857 william 270 e.Result = t;
858 william 105 parser.Dispose();
859 william 60 }
860     private void platformWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { }
861     private void platformWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
862     {
863     Stopwatch t = e.Result as Stopwatch;
864 william 198 if (platform_flow.Controls.Count == 0)
865 william 197 {
866     PlatformControl platform_ctrl = new PlatformControl();
867 william 253 platform_ctrl.PreviewKeyDown += new PreviewKeyDownEventHandler(platform_ctrl_PreviewKeyDown);
868     platform_ctrl.GotFocus += new EventHandler(platform_ctrl_GotFocus);
869     platform_ctrl.LostFocus += new EventHandler(platform_ctrl_LostFocus);
870 william 197 platform_ctrl.Font = GetFormFont();
871     platform_ctrl.Dock = DockStyle.Top;
872     platform_ctrl.Width = this.Width - 10;
873     platform_ctrl.PlatformName = string.Format("You don't have any roms in your rompath: '{0}'",Config.RomPath);
874     platform_flow.Controls.Add(platform_ctrl);
875 william 274 }
876     if (SavedPlatformIndex != -1) {
877     var platform_control = platform_flow.Controls [SavedPlatformIndex] as PlatformControl;
878     platform_flow.SuspendLayout ();
879     CurrentPlatformControl = platform_control;
880     platform_flow.ScrollControlIntoView (CurrentPlatformControl);
881     CurrentPlatformControl.Select ();
882     platform_flow.ResumeLayout ();
883     SavedPlatformIndex = -1;
884     } else {
885     platform_flow.Controls[0].Select();
886     }
887 william 286 //gLog.Profiler.WriteLine("PlatformParser took: {0}s to parse platforms", (int)t.Elapsed.TotalSeconds);
888 william 60 }
889     #endregion
890    
891     private void Form1_FormClosed(object sender, FormClosedEventArgs e)
892     {
893     Cursor.Show();
894     }
895     #endregion
896 william 4 }
897     }

  ViewVC Help
Powered by ViewVC 1.1.22