Application.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. // Both calls are needed, one is for the WM, the other for our focus logic
  64. XplatUI.Activate(form.window.Handle);
  65. form.Activate();
  66. while (control.MoveNext()) {
  67. if ((((Control)control.Current).parent == null) && (((Control)control.Current).is_visible) && (((Control)control.Current).is_enabled)) {
  68. if ((control.Current is Form) && (((Form)control.Current)!=form)) {
  69. XplatUI.EnableWindow(((Control)control.Current).window.Handle, false);
  70. toplevels.Enqueue((Control)control.Current);
  71. }
  72. }
  73. }
  74. form.CreateControl();
  75. while (!exiting && !form.end_modal && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
  76. if ((message_filters != null) && (message_filters.Count > 0)) {
  77. Message m;
  78. bool drop;
  79. drop = false;
  80. m = new Message();
  81. m.Msg = (int)msg.message;
  82. m.HWnd = msg.hwnd;
  83. m.LParam = msg.lParam;
  84. m.WParam = msg.wParam;
  85. for (int i = 0; i < message_filters.Count; i++) {
  86. if (((IMessageFilter)message_filters[i]).PreFilterMessage(ref m)) {
  87. // we're dropping the message
  88. drop = true;
  89. break;
  90. }
  91. }
  92. if (drop) {
  93. continue;
  94. }
  95. }
  96. XplatUI.TranslateMessage(ref msg);
  97. XplatUI.DispatchMessage(ref msg);
  98. // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
  99. if (form.closing) {
  100. form.end_modal = true;
  101. }
  102. }
  103. while (toplevels.Count>0) {
  104. XplatUI.EnableWindow(((Control)toplevels.Dequeue()).window.Handle, true);
  105. }
  106. }
  107. #endregion // Private and Internal Methods
  108. #region Public Static Properties
  109. public static bool AllowQuit {
  110. get {
  111. return browser_embedded;
  112. }
  113. }
  114. public static string CommonAppDataPath {
  115. get {
  116. return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
  117. }
  118. }
  119. public static RegistryKey CommonAppDataRegistry {
  120. get {
  121. RegistryKey key;
  122. key = Registry.LocalMachine.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
  123. return key;
  124. }
  125. }
  126. public static string CompanyName {
  127. get {
  128. AssemblyCompanyAttribute[] attrs = (AssemblyCompanyAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
  129. if ((attrs != null) && attrs.Length>0) {
  130. return attrs[0].Company;
  131. }
  132. return Assembly.GetEntryAssembly().GetName().Name;
  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 Path.Combine(Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), CompanyName), ProductName), ProductVersion);
  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 Path.Combine(Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), CompanyName), ProductName), ProductVersion);
  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. #if NET_2_0
  222. public static void EnableRTLMirroring ()
  223. {
  224. }
  225. #endif
  226. public static void Exit() {
  227. XplatUI.Exit();
  228. }
  229. public static void ExitThread() {
  230. exiting=true;
  231. }
  232. private static void InternalExit(object sender, EventArgs e) {
  233. Application.Exit();
  234. }
  235. public static ApartmentState OleRequired() {
  236. //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
  237. return ApartmentState.Unknown;
  238. }
  239. public static void OnThreadException(Exception t) {
  240. if (Application.ThreadException != null) {
  241. Application.ThreadException(null, new ThreadExceptionEventArgs(t));
  242. return;
  243. }
  244. #if !later
  245. else {
  246. XplatUI.HandleException(t);
  247. }
  248. #else
  249. // TODO: Missing implementation
  250. //if (SystemInformation.UserInteractive)
  251. {
  252. Form form = new ThreadExceptionDialog (t);
  253. form.ShowDialog ();
  254. }
  255. //else
  256. Console.WriteLine (t.ToString ());
  257. #endif
  258. }
  259. public static void RemoveMessageFilter(IMessageFilter filter) {
  260. message_filters.Remove(filter);
  261. }
  262. public static void Run() {
  263. MSG msg = new MSG();
  264. Form form = null;
  265. if (app_context != null) {
  266. form = app_context.MainForm;
  267. }
  268. if (form != null) {
  269. // Both calls are needed, one is for the WM, the other for our focus logic
  270. XplatUI.Activate(form.window.Handle);
  271. form.Activate();
  272. }
  273. messageloop_started = true;
  274. while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
  275. if ((message_filters != null) && (message_filters.Count > 0)) {
  276. Message m;
  277. bool drop;
  278. drop = false;
  279. m = new Message();
  280. m.Msg = (int)msg.message;
  281. m.HWnd = msg.hwnd;
  282. m.LParam = msg.lParam;
  283. m.WParam = msg.wParam;
  284. for (int i = 0; i < message_filters.Count; i++) {
  285. if (((IMessageFilter)message_filters[i]).PreFilterMessage(ref m)) {
  286. // we're dropping the message
  287. drop = true;
  288. break;
  289. }
  290. }
  291. if (drop) {
  292. continue;
  293. }
  294. }
  295. XplatUI.TranslateMessage(ref msg);
  296. XplatUI.DispatchMessage(ref msg);
  297. // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
  298. if ((form != null) && form.closing) {
  299. exiting = true;
  300. }
  301. }
  302. messageloop_started = false;
  303. if (ThreadExit != null) {
  304. ThreadExit(null, EventArgs.Empty);
  305. }
  306. if (ApplicationExit != null) {
  307. ApplicationExit(null, EventArgs.Empty);
  308. }
  309. }
  310. public static void Run(Form mainForm) {
  311. mainForm.CreateControl();
  312. Run(new ApplicationContext(mainForm));
  313. }
  314. public static void Run(ApplicationContext context) {
  315. app_context=context;
  316. if (app_context.MainForm!=null) {
  317. app_context.MainForm.Show();
  318. app_context.MainForm.PerformLayout();
  319. app_context.ThreadExit += new EventHandler(InternalExit);
  320. }
  321. Run();
  322. }
  323. #endregion // Public Static Methods
  324. #region Events
  325. public static event EventHandler ApplicationExit;
  326. public static event EventHandler Idle {
  327. add {
  328. XplatUI.Idle += value;
  329. }
  330. remove {
  331. XplatUI.Idle -= value;
  332. }
  333. }
  334. public static event EventHandler ThreadExit;
  335. public static event ThreadExceptionEventHandler ThreadException;
  336. #endregion // Events
  337. }
  338. }