Application.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // System.Windows.Forms.Application
  3. //
  4. // Author:
  5. // stubbed out by Jaak Simm ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Dennis hayes ([email protected])
  8. // WINELib implementation started by John Sohn ([email protected])
  9. //
  10. // (C) Ximian, Inc 2002
  11. //
  12. using System;
  13. using System.Drawing;
  14. using Microsoft.Win32;
  15. using System.ComponentModel;
  16. using System.Threading;
  17. using System.Globalization;
  18. using System.Reflection;
  19. using System.Collections;
  20. using System.Runtime.CompilerServices;
  21. namespace System.Windows.Forms {
  22. /// <summary>
  23. /// Provides static methods and properties to manage an application,
  24. /// such as methods to start and stop an application, to process
  25. /// Windows messages, and properties to get information about an
  26. /// application. This class cannot be inherited.
  27. /// </summary>
  28. [MonoTODO]
  29. public sealed class Application {
  30. static private ApplicationContext applicationContext = null;
  31. static private bool messageLoopStarted = false;
  32. static private bool messageLoopStopRequest = false;
  33. private static ArrayList messageFilters = new ArrayList ();
  34. // --- (public) Properties ---
  35. public static bool AllowQuit {
  36. // according to docs return false if embbedded in a
  37. // browser, not (yet?) embedded in a browser
  38. get { return true; }
  39. }
  40. [MonoTODO]
  41. public static string CommonAppDataPath {
  42. get { throw new NotImplementedException (); }
  43. }
  44. [MonoTODO]
  45. // Registry key not yet defined (this should be interesting)
  46. //public static RegistryKey CommonAppDataRegistry {
  47. // get { throw new NotImplementedException (); }
  48. //}
  49. [MonoTODO]
  50. public static string CompanyName {
  51. get { throw new NotImplementedException (); }
  52. }
  53. [MonoTODO]
  54. public static CultureInfo CurrentCulture {
  55. get { throw new NotImplementedException (); }
  56. set { throw new NotImplementedException (); }
  57. }
  58. [MonoTODO]
  59. public static InputLanguage CurrentInputLanguage {
  60. get { throw new NotImplementedException (); }
  61. }
  62. [MonoTODO]
  63. public static string ExecutablePath {
  64. get { throw new NotImplementedException (); }
  65. }
  66. [MonoTODO]
  67. public static string LocalUserAppDataPath {
  68. get { throw new NotImplementedException (); }
  69. }
  70. public static bool MessageLoop {
  71. get { return messageLoopStarted; }
  72. }
  73. [MonoTODO]
  74. public static string ProductName {
  75. get { throw new NotImplementedException (); }
  76. }
  77. [MonoTODO]
  78. public static string ProductVersion {
  79. get { throw new NotImplementedException (); }
  80. }
  81. [MonoTODO]
  82. public static string SafeTopLevelCaptionFormat {
  83. get { throw new NotImplementedException (); }
  84. set { throw new NotImplementedException (); }
  85. }
  86. [MonoTODO]
  87. public static string StartupPath {
  88. get { throw new NotImplementedException (); }
  89. }
  90. [MonoTODO]
  91. public static string UserAppDataPath {
  92. get { throw new NotImplementedException (); }
  93. }
  94. [MonoTODO]
  95. // Registry key not yet defined
  96. //public static RegistryKey UserAppDataRegistry {
  97. // get { throw new NotImplementedException (); }
  98. //}
  99. // --- Methods ---
  100. public static void AddMessageFilter (IMessageFilter value)
  101. {
  102. messageFilters.Add (value);
  103. }
  104. //Compact Framework
  105. public static void DoEvents ()
  106. {
  107. Win32.MSG msg = new Win32.MSG();
  108. while (Win32.PeekMessageA (ref msg, (IntPtr) 0, 0, 0,
  109. Win32.PM_REMOVE) != 0);
  110. }
  111. //Compact Framework
  112. public static void Exit ()
  113. {
  114. Win32.PostQuitMessage (0);
  115. }
  116. public static void ExitThread ()
  117. {
  118. messageLoopStopRequest = true;
  119. }
  120. [MonoTODO]
  121. public static ApartmentState OleRequired ()
  122. {
  123. throw new NotImplementedException ();
  124. }
  125. [MonoTODO]
  126. public static void OnThreadException (Exception t)
  127. {
  128. throw new NotImplementedException ();
  129. }
  130. public static void RemoveMessageFilter (IMessageFilter value)
  131. {
  132. messageFilters.Remove (value);
  133. }
  134. static private void ApplicationFormClosed (object o, EventArgs args)
  135. {
  136. Win32.PostQuitMessage (0);
  137. }
  138. //Compact Framework
  139. static public void Run ()
  140. {
  141. Win32.MSG msg = new Win32.MSG();
  142. messageLoopStarted = true;
  143. while (!messageLoopStopRequest &&
  144. Win32.GetMessageA (ref msg, 0, 0, 0) != 0) {
  145. bool dispatchMessage = true;
  146. Message message = new Message ();
  147. message.HWnd = msg.hwnd;
  148. message.Msg = (int) msg.message;
  149. message.WParam = msg.wParam;
  150. message.LParam = msg.lParam;
  151. IEnumerator e = messageFilters.GetEnumerator();
  152. while (e.MoveNext()) {
  153. IMessageFilter filter =
  154. (IMessageFilter) e.Current;
  155. // if PreFilterMessage returns true
  156. // the message should not be dispatched
  157. if (filter.PreFilterMessage (ref message))
  158. dispatchMessage = false;
  159. }
  160. if (dispatchMessage) {
  161. Win32.TranslateMessage (ref msg);
  162. Win32.DispatchMessageA (ref msg);
  163. }
  164. //if (Idle != null)
  165. //Idle (null, new EventArgs());
  166. }
  167. //if (ApplicationExit != null)
  168. //ApplicationExit (null, new EventArgs());
  169. }
  170. public static void Run (ApplicationContext context)
  171. {
  172. applicationContext = context;
  173. applicationContext.MainForm.Show ();
  174. applicationContext.MainForm.Closed +=
  175. new EventHandler (ApplicationFormClosed);
  176. Run();
  177. }
  178. //[TypeAttributes.BeforeFieldInit]
  179. public static void Run (Form form)
  180. // Documents say this parameter name should be mainform,
  181. // but the verifier says context.
  182. {
  183. form.CreateControl ();
  184. ApplicationContext context = new ApplicationContext (
  185. form);
  186. Run (context);
  187. }
  188. // --- Events ---
  189. public static event EventHandler ApplicationExit;
  190. public static event EventHandler Idle;
  191. public static event ThreadExceptionEventHandler ThreadException;
  192. public static event EventHandler ThreadExit;
  193. }
  194. }