Application.cs 7.4 KB

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