Application.cs 9.2 KB

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