Application.cs 8.9 KB

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