| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- // Copyright (c) 2004 Novell, Inc.
- //
- // Authors:
- // Peter Bartok [email protected]
- //
- // COMPLETE
- using Microsoft.Win32;
- using System;
- using System.Drawing;
- using System.ComponentModel;
- using System.Collections;
- using System.Diagnostics;
- using System.Globalization;
- using System.IO;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace System.Windows.Forms {
- public sealed class Application {
- private static bool browser_embedded;
- private static bool exiting;
- private static InputLanguage input_language;
- private static bool messageloop_started;
- private static string safe_caption_format;
- private static ArrayList message_filters;
- private static ApplicationContext app_context;
- private Application () {
- input_language = InputLanguage.CurrentInputLanguage;
- message_filters = new ArrayList();
- app_context = null;
- browser_embedded= false;
- exiting = false;
- messageloop_started = false;
- safe_caption_format = "{1} - {0} - {2}";
- }
- #region Private and Internal Methods
- internal static void ModalRun(Form form) {
- MSG msg = new MSG();
- Queue toplevels = new Queue();
- IEnumerator control = Control.controls.GetEnumerator();
- if (form == null) {
- return;
- }
- // Both calls are needed, one is for the WM, the other for our focus logic
- XplatUI.Activate(form.window.Handle);
- form.Activate();
- while (control.MoveNext()) {
- if ((((Control)control.Current).parent == null) && (((Control)control.Current).is_visible) && (((Control)control.Current).is_enabled)) {
- if ((control.Current is Form) && (((Form)control.Current)!=form)) {
- XplatUI.EnableWindow(((Control)control.Current).window.Handle, false);
- toplevels.Enqueue((Control)control.Current);
- }
- }
- }
- form.CreateControl();
- while (!exiting && !form.end_modal && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
- if ((message_filters != null) && (message_filters.Count > 0)) {
- Message m;
- bool drop;
- drop = false;
- m = new Message();
- m.Msg = (int)msg.message;
- m.HWnd = msg.hwnd;
- m.LParam = msg.lParam;
- m.WParam = msg.wParam;
- for (int i = 0; i < message_filters.Count; i++) {
- if (((IMessageFilter)message_filters[i]).PreFilterMessage(ref m)) {
- // we're dropping the message
- drop = true;
- break;
- }
- }
- if (drop) {
- continue;
- }
- }
- XplatUI.TranslateMessage(ref msg);
- XplatUI.DispatchMessage(ref msg);
- // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
- if (form.closing) {
- form.end_modal = true;
- }
- }
- while (toplevels.Count>0) {
- XplatUI.EnableWindow(((Control)toplevels.Dequeue()).window.Handle, true);
- }
- }
- #endregion // Private and Internal Methods
- #region Public Static Properties
- public static bool AllowQuit {
- get {
- return browser_embedded;
- }
- }
- public static string CommonAppDataPath {
- get {
- return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
- }
- }
- public static RegistryKey CommonAppDataRegistry {
- get {
- RegistryKey key;
- key = Registry.LocalMachine.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
- return key;
- }
- }
- public static string CompanyName {
- get {
- AssemblyCompanyAttribute[] attrs = (AssemblyCompanyAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), true);
-
- if ((attrs != null) && attrs.Length>0) {
- return attrs[0].Company;
- }
- return Assembly.GetEntryAssembly().GetName().Name;
- }
- }
- public static CultureInfo CurrentCulture {
- get {
- return Thread.CurrentThread.CurrentUICulture;
- }
- set {
-
- Thread.CurrentThread.CurrentUICulture=value;
- }
- }
- public static InputLanguage CurrentInputLanguage {
- get {
- return input_language;
- }
- set {
- input_language=value;
- }
- }
- public static string ExecutablePath {
- get {
- return Assembly.GetEntryAssembly().Location;
- }
- }
- public static string LocalUserAppDataPath {
- get {
- return Path.Combine(Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), CompanyName), ProductName), ProductVersion);
- }
- }
- public static bool MessageLoop {
- get {
- return messageloop_started;
- }
- }
- public static string ProductName {
- get {
- AssemblyProductAttribute[] attrs = (AssemblyProductAttribute[]) Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), true);
-
- if ((attrs != null) && attrs.Length>0) {
- return attrs[0].Product;
- }
- return Assembly.GetEntryAssembly().GetName().Name;
- }
- }
- public static string ProductVersion {
- get {
- String version;
- version = Assembly.GetEntryAssembly().GetName().Version.ToString();
- if (version.StartsWith("0.")) {
- version="1." + version.Substring(2);
- }
- return version;
- }
- }
- public static string SafeTopLevelCaptionFormat {
- get {
- return safe_caption_format;
- }
- set {
- safe_caption_format=value;
- }
- }
- public static string StartupPath {
- get {
- return Path.GetDirectoryName(Application.ExecutablePath);
- }
- }
- public static string UserAppDataPath {
- get {
- return Path.Combine(Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), CompanyName), ProductName), ProductVersion);
- }
- }
- public static RegistryKey UserAppDataRegistry {
- get {
- RegistryKey key;
- key = Registry.CurrentUser.OpenSubKey("Software\\" + Application.CompanyName + "\\" + Application.ProductName + "\\" + Application.ProductVersion, true);
- return key;
- }
- }
- #endregion
- #region Public Static Methods
- public static void AddMessageFilter(IMessageFilter value) {
- message_filters.Add(value);
- }
- public static void DoEvents() {
- XplatUI.DoEvents();
- }
- public static void EnableVisualStyles() {
- XplatUI.EnableThemes();
- }
- #if NET_2_0
- public static void EnableRTLMirroring ()
- {
- }
- #endif
- public static void Exit() {
- XplatUI.Exit();
- }
- public static void ExitThread() {
- exiting=true;
- }
- private static void InternalExit(object sender, EventArgs e) {
- Application.Exit();
- }
- public static ApartmentState OleRequired() {
- //throw new NotImplementedException("OLE Not supported by this System.Windows.Forms implementation");
- return ApartmentState.Unknown;
- }
- public static void OnThreadException(Exception t) {
- if (Application.ThreadException != null) {
- Application.ThreadException(null, new ThreadExceptionEventArgs(t));
- return;
- }
- #if !later
- else {
- XplatUI.HandleException(t);
- }
- #else
- // TODO: Missing implementation
- //if (SystemInformation.UserInteractive)
- {
- Form form = new ThreadExceptionDialog (t);
- form.ShowDialog ();
- }
- //else
- Console.WriteLine (t.ToString ());
- #endif
- }
- public static void RemoveMessageFilter(IMessageFilter filter) {
- message_filters.Remove(filter);
- }
- public static void Run() {
- MSG msg = new MSG();
- Form form = null;
- if (app_context != null) {
- form = app_context.MainForm;
- }
- if (form != null) {
- // Both calls are needed, one is for the WM, the other for our focus logic
- XplatUI.Activate(form.window.Handle);
- form.Activate();
- }
- messageloop_started = true;
- while (!exiting && XplatUI.GetMessage(ref msg, IntPtr.Zero, 0, 0)) {
- if ((message_filters != null) && (message_filters.Count > 0)) {
- Message m;
- bool drop;
- drop = false;
- m = new Message();
- m.Msg = (int)msg.message;
- m.HWnd = msg.hwnd;
- m.LParam = msg.lParam;
- m.WParam = msg.wParam;
- for (int i = 0; i < message_filters.Count; i++) {
- if (((IMessageFilter)message_filters[i]).PreFilterMessage(ref m)) {
- // we're dropping the message
- drop = true;
- break;
- }
- }
- if (drop) {
- continue;
- }
- }
- XplatUI.TranslateMessage(ref msg);
- XplatUI.DispatchMessage(ref msg);
- // Handle exit, Form might have received WM_CLOSE and set 'closing' in response
- if ((form != null) && form.closing) {
- exiting = true;
- }
- }
- messageloop_started = false;
- if (ThreadExit != null) {
- ThreadExit(null, EventArgs.Empty);
- }
- if (ApplicationExit != null) {
- ApplicationExit(null, EventArgs.Empty);
- }
- }
- public static void Run(Form mainForm) {
- mainForm.CreateControl();
- Run(new ApplicationContext(mainForm));
- }
- public static void Run(ApplicationContext context) {
- app_context=context;
- if (app_context.MainForm!=null) {
- app_context.MainForm.Show();
- app_context.MainForm.PerformLayout();
- app_context.ThreadExit += new EventHandler(InternalExit);
- }
- Run();
- }
- #endregion // Public Static Methods
- #region Events
- public static event EventHandler ApplicationExit;
- public static event EventHandler Idle {
- add {
- XplatUI.Idle += value;
- }
- remove {
- XplatUI.Idle -= value;
- }
- }
- public static event EventHandler ThreadExit;
- public static event ThreadExceptionEventHandler ThreadException;
- #endregion // Events
- }
- }
|