Application.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.4 $
  27. // $Modtime: $
  28. // $Log: Application.cs,v $
  29. // Revision 1.4 2004/08/23 22:45:19 pbartok
  30. // - Removed debug output
  31. // - Simplifications
  32. //
  33. // Revision 1.3 2004/08/23 22:09:29 pbartok
  34. // - Added handling of Idle event
  35. // - Added handling of form closing
  36. // - Fixed reporting of MessageLoop property
  37. // - Removed some unneeded code, should provide a bit of a speedup
  38. //
  39. // Revision 1.2 2004/08/11 22:16:50 pbartok
  40. // - Fixed Signature
  41. // - Added .Net 1.1 method
  42. //
  43. // Revision 1.1 2004/07/09 05:21:25 pbartok
  44. // - Initial check-in
  45. //
  46. //
  47. // COMPLETE
  48. using Microsoft.Win32;
  49. using System;
  50. using System.Drawing;
  51. using System.ComponentModel;
  52. using System.Collections;
  53. using System.Diagnostics;
  54. using System.Globalization;
  55. using System.IO;
  56. using System.Reflection;
  57. using System.Runtime.InteropServices;
  58. using System.Threading;
  59. namespace System.Windows.Forms {
  60. public sealed class Application {
  61. private static bool browser_embedded;
  62. private static bool exiting;
  63. private static InputLanguage input_language;
  64. private static bool messageloop_started;
  65. private static string safe_caption_format;
  66. private static ArrayList message_filters;
  67. private static ApplicationContext app_context;
  68. private static Form main_form;
  69. private Application () {
  70. input_language = InputLanguage.CurrentInputLanguage;
  71. message_filters = new ArrayList();
  72. app_context = null;
  73. browser_embedded= false;
  74. exiting = false;
  75. messageloop_started = false;
  76. safe_caption_format = "{1} - {0} - {2}";
  77. }
  78. #region Public Static Properties
  79. public static bool AllowQuit {
  80. get {
  81. return browser_embedded;
  82. }
  83. }
  84. public static string CommonAppDataPath {
  85. get {
  86. return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  87. }
  88. }
  89. public static RegistryKey CommonAppDataRegistry {
  90. get {
  91. RegistryKey key;
  92. key = Registry.LocalMachine.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
  93. return key;
  94. }
  95. }
  96. public static string CompanyName {
  97. get {
  98. StackTrace st;
  99. if (Environment.OSVersion.Platform != (PlatformID)128) {
  100. RegistryKey key;
  101. String ret;
  102. key=Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", false);
  103. ret=(String)key.GetValue("RegisteredOrganization");
  104. return ret;
  105. }
  106. st=new StackTrace();
  107. return st.GetFrame(st.FrameCount-1).GetMethod().DeclaringType.Namespace;
  108. }
  109. }
  110. public static CultureInfo CurrentCulture {
  111. get {
  112. return Thread.CurrentThread.CurrentUICulture;
  113. }
  114. set {
  115. Thread.CurrentThread.CurrentUICulture=value;
  116. }
  117. }
  118. public static InputLanguage CurrentInputLanguage {
  119. get {
  120. return input_language;
  121. }
  122. set {
  123. input_language=value;
  124. }
  125. }
  126. public static string ExecutablePath {
  127. get {
  128. return Assembly.GetEntryAssembly().Location;
  129. }
  130. }
  131. public static string LocalUserAppDataPath {
  132. get {
  133. return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  134. }
  135. }
  136. public static bool MessageLoop {
  137. get {
  138. return messageloop_started;
  139. }
  140. }
  141. public static string ProductName {
  142. get {
  143. AssemblyProductAttribute[] attrs = (AssemblyProductAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true);
  144. if ((attrs != null) && attrs.Length>0) {
  145. return attrs[0].Product;
  146. }
  147. return Assembly.GetEntryAssembly().GetName().Name;
  148. }
  149. }
  150. public static string ProductVersion {
  151. get {
  152. String version;
  153. version = Assembly.GetEntryAssembly().GetName().Version.ToString();
  154. if (version.StartsWith("0.")) {
  155. version="1." + version.Substring(2);
  156. }
  157. return version;
  158. }
  159. }
  160. public static string SafeTopLevelCaptionFormat {
  161. get {
  162. return safe_caption_format;
  163. }
  164. set {
  165. safe_caption_format=value;
  166. }
  167. }
  168. public static string StartupPath {
  169. get {
  170. return Path.GetDirectoryName(Application.ExecutablePath);
  171. }
  172. }
  173. public static string UserAppDataPath {
  174. get {
  175. return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  176. }
  177. }
  178. public static RegistryKey UserAppDataRegistry {
  179. get {
  180. RegistryKey key;
  181. key = Registry.CurrentUser.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
  182. return key;
  183. }
  184. }
  185. #endregion
  186. #region Public Static Methods
  187. public static void AddMessageFilter(IMessageFilter value) {
  188. message_filters.Add(value);
  189. }
  190. public static void DoEvents() {
  191. XplatUI.DoEvents();
  192. }
  193. public static void EnableVisualStyles() {
  194. XplatUI.EnableThemes();
  195. }
  196. public static void Exit() {
  197. XplatUI.Exit();
  198. }
  199. public static void ExitThread() {
  200. exiting=true;
  201. }
  202. private static void InternalExit(object sender, EventArgs e) {
  203. Application.Exit();
  204. }
  205. public static ApartmentState OleRequired() {
  206. //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
  207. return ApartmentState.Unknown;
  208. }
  209. public static void OnThreadException(Exception t) {
  210. if (Application.ThreadException != null) {
  211. Application.ThreadException(null, new ThreadExceptionEventArgs(t));
  212. } else {
  213. XplatUI.HandleException(t);
  214. }
  215. }
  216. public static void RemoveMessageFilter(IMessageFilter filter) {
  217. message_filters.Remove(filter);
  218. }
  219. public static void Run() {
  220. MSG msg = new MSG();
  221. Form form = null;
  222. if (app_context != null) {
  223. form = app_context.MainForm;
  224. }
  225. messageloop_started = true;
  226. while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
  227. if (msg.message != Msg.WM_ENTERIDLE) {
  228. XplatUI.TranslateMessage(ref msg);
  229. XplatUI.DispatchMessage(ref msg);
  230. // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
  231. if ((form != null) && form.closing) {
  232. exiting = true;
  233. }
  234. continue;
  235. } else if (Idle != null) {
  236. Idle(form, EventArgs.Empty);
  237. continue;
  238. }
  239. }
  240. messageloop_started = false;
  241. if (ApplicationExit != null) {
  242. ApplicationExit(null, EventArgs.Empty);
  243. }
  244. }
  245. public static void Run(Form mainForm) {
  246. mainForm.CreateControl();
  247. Run(new ApplicationContext(mainForm));
  248. }
  249. public static void Run(ApplicationContext context) {
  250. app_context=context;
  251. if (app_context.MainForm!=null) {
  252. app_context.MainForm.Show();
  253. app_context.ThreadExit += new EventHandler(InternalExit);
  254. }
  255. Run();
  256. }
  257. #endregion // Public Static Methods
  258. #region Events
  259. public static event EventHandler ApplicationExit;
  260. public static event EventHandler Idle;
  261. public static event EventHandler ThreadExit;
  262. public static event ThreadExceptionEventHandler ThreadException;
  263. #endregion // Events
  264. }
  265. }