Application.cs 8.7 KB

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