Application.cs 8.5 KB

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