Application.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // Peter Bartok [email protected]
  24. //
  25. //
  26. // $Revision: 1.3 $
  27. // $Modtime: $
  28. // $Log: Application.cs,v $
  29. // Revision 1.3 2004/08/23 22:09:29 pbartok
  30. // - Added handling of Idle event
  31. // - Added handling of form closing
  32. // - Fixed reporting of MessageLoop property
  33. // - Removed some unneeded code, should provide a bit of a speedup
  34. //
  35. // Revision 1.2 2004/08/11 22:16:50 pbartok
  36. // - Fixed Signature
  37. // - Added .Net 1.1 method
  38. //
  39. // Revision 1.1 2004/07/09 05:21:25 pbartok
  40. // - Initial check-in
  41. //
  42. //
  43. // COMPLETE
  44. using Microsoft.Win32;
  45. using System;
  46. using System.Drawing;
  47. using System.ComponentModel;
  48. using System.Collections;
  49. using System.Diagnostics;
  50. using System.Globalization;
  51. using System.IO;
  52. using System.Reflection;
  53. using System.Runtime.InteropServices;
  54. using System.Threading;
  55. namespace System.Windows.Forms {
  56. public sealed class Application {
  57. private static bool browser_embedded;
  58. private static bool exiting;
  59. private static InputLanguage input_language;
  60. private static bool messageloop_started;
  61. private static string safe_caption_format;
  62. private static ArrayList message_filters;
  63. private static ApplicationContext app_context;
  64. private static Form main_form;
  65. private Application () {
  66. input_language = InputLanguage.CurrentInputLanguage;
  67. message_filters = new ArrayList();
  68. app_context = null;
  69. browser_embedded= false;
  70. exiting = false;
  71. messageloop_started = false;
  72. safe_caption_format = "{1} - {0} - {2}";
  73. }
  74. #region Public Static Properties
  75. public static bool AllowQuit {
  76. get {
  77. return browser_embedded;
  78. }
  79. }
  80. public static string CommonAppDataPath {
  81. get {
  82. return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  83. }
  84. }
  85. public static RegistryKey CommonAppDataRegistry {
  86. get {
  87. RegistryKey key;
  88. key = Registry.LocalMachine.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
  89. return key;
  90. }
  91. }
  92. public static string CompanyName {
  93. get {
  94. StackTrace st;
  95. if (Environment.OSVersion.Platform != (PlatformID)128) {
  96. RegistryKey key;
  97. String ret;
  98. key=Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", false);
  99. ret=(String)key.GetValue("RegisteredOrganization");
  100. return ret;
  101. }
  102. st=new StackTrace();
  103. return st.GetFrame(st.FrameCount-1).GetMethod().DeclaringType.Namespace;
  104. }
  105. }
  106. public static CultureInfo CurrentCulture {
  107. get {
  108. return Thread.CurrentThread.CurrentUICulture;
  109. }
  110. set {
  111. Thread.CurrentThread.CurrentUICulture=value;
  112. }
  113. }
  114. public static InputLanguage CurrentInputLanguage {
  115. get {
  116. return input_language;
  117. }
  118. set {
  119. input_language=value;
  120. }
  121. }
  122. public static string ExecutablePath {
  123. get {
  124. return Assembly.GetEntryAssembly().Location;
  125. }
  126. }
  127. public static string LocalUserAppDataPath {
  128. get {
  129. return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  130. }
  131. }
  132. public static bool MessageLoop {
  133. get {
  134. return messageloop_started;
  135. }
  136. }
  137. public static string ProductName {
  138. get {
  139. AssemblyProductAttribute[] attrs = (AssemblyProductAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true);
  140. if ((attrs != null) && attrs.Length>0) {
  141. return attrs[0].Product;
  142. }
  143. return Assembly.GetEntryAssembly().GetName().Name;
  144. }
  145. }
  146. public static string ProductVersion {
  147. get {
  148. String version;
  149. version = Assembly.GetEntryAssembly().GetName().Version.ToString();
  150. if (version.StartsWith("0.")) {
  151. version="1." + version.Substring(2);
  152. }
  153. return version;
  154. }
  155. }
  156. public static string SafeTopLevelCaptionFormat {
  157. get {
  158. return safe_caption_format;
  159. }
  160. set {
  161. safe_caption_format=value;
  162. }
  163. }
  164. public static string StartupPath {
  165. get {
  166. return Path.GetDirectoryName(Application.ExecutablePath);
  167. }
  168. }
  169. public static string UserAppDataPath {
  170. get {
  171. return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  172. }
  173. }
  174. public static RegistryKey UserAppDataRegistry {
  175. get {
  176. RegistryKey key;
  177. key = Registry.CurrentUser.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
  178. return key;
  179. }
  180. }
  181. #endregion
  182. #region Public Static Methods
  183. public static void AddMessageFilter(IMessageFilter value) {
  184. message_filters.Add(value);
  185. }
  186. public static void DoEvents() {
  187. XplatUI.DoEvents();
  188. }
  189. public static void EnableVisualStyles() {
  190. XplatUI.EnableThemes();
  191. }
  192. public static void Exit() {
  193. XplatUI.Exit();
  194. }
  195. public static void ExitThread() {
  196. exiting=true;
  197. }
  198. private static void InternalExit(object sender, EventArgs e) {
  199. Application.Exit();
  200. }
  201. public static ApartmentState OleRequired() {
  202. //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
  203. return ApartmentState.Unknown;
  204. }
  205. public static void OnThreadException(Exception t) {
  206. if (Application.ThreadException != null) {
  207. Application.ThreadException(null, new ThreadExceptionEventArgs(t));
  208. } else {
  209. XplatUI.HandleException(t);
  210. }
  211. }
  212. public static void RemoveMessageFilter(IMessageFilter filter) {
  213. message_filters.Remove(filter);
  214. }
  215. public static void Run() {
  216. MSG msg = new MSG();
  217. messageloop_started = true;
  218. while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
  219. if (msg.message != Msg.WM_ENTERIDLE) {
  220. XplatUI.TranslateMessage(ref msg);
  221. XplatUI.DispatchMessage(ref msg);
  222. // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
  223. if ((app_context != null) && app_context.MainForm.closing) {
  224. exiting = true;
  225. }
  226. continue;
  227. } else if (Idle != null) {
  228. Idle(null, EventArgs.Empty);
  229. continue;
  230. }
  231. }
  232. messageloop_started = false;
  233. if (ApplicationExit != null) {
  234. ApplicationExit(null, EventArgs.Empty);
  235. }
  236. Console.WriteLine("Application.Run(): returning");
  237. }
  238. public static void Run(Form mainForm) {
  239. mainForm.CreateControl();
  240. Run(new ApplicationContext(mainForm));
  241. Console.WriteLine("Application.Run(Form): returning");
  242. }
  243. public static void Run(ApplicationContext context) {
  244. app_context=context;
  245. if (app_context.MainForm!=null) {
  246. app_context.MainForm.Show();
  247. app_context.ThreadExit += new EventHandler(InternalExit);
  248. }
  249. Run();
  250. Console.WriteLine("Application.Run(Context): returning");
  251. }
  252. #endregion // Public Static Methods
  253. #region Events
  254. public static event EventHandler ApplicationExit;
  255. public static event EventHandler Idle;
  256. public static event EventHandler ThreadExit;
  257. public static event ThreadExceptionEventHandler ThreadException;
  258. #endregion // Events
  259. }
  260. }