Application.cs 7.3 KB

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