ErrorDialog.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // ErrorDialog.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using Gtk;
  30. using Glade;
  31. public sealed class ErrorDialog : IDisposable
  32. {
  33. [Glade.Widget("ErrorDialog")]
  34. Dialog dialog = null;
  35. [Glade.Widget]
  36. Button okButton = null;
  37. [Glade.Widget]
  38. Label descriptionLabel = null;
  39. [Glade.Widget]
  40. Gtk.TextView detailsTextView = null;
  41. [Glade.Widget]
  42. Gtk.Expander expander = null;
  43. TextTag tagNoWrap;
  44. TextTag tagWrap;
  45. public static void Exception(Exception e)
  46. {
  47. Exception("Unexpected Exception", e);
  48. }
  49. public static void Exception(string Mess, Exception e)
  50. {
  51. ErrorDialog dialog = new ErrorDialog(null);
  52. dialog.Message = Mess + ": " + e.Message;
  53. do {
  54. dialog.AddDetails("\"" + e.Message + "\"" + Environment.NewLine, false);
  55. dialog.AddDetails(e.StackTrace, false);
  56. if(e.InnerException != null) {
  57. dialog.AddDetails(Environment.NewLine + Environment.NewLine + "--Caused by--" + Environment.NewLine + Environment.NewLine, false);
  58. }
  59. e = e.InnerException;
  60. } while(e != null);
  61. dialog.Show();
  62. }
  63. /// <summary>
  64. /// Show error dialog for non-exceptions.
  65. /// </summary>
  66. /// <param name="message">Error message to show</param>
  67. public static void ShowError(string message) {
  68. ErrorDialog dialog = new ErrorDialog(null);
  69. dialog.dialog.Title = "Supertux-Editor Error";
  70. dialog.Message = message;
  71. dialog.Show();
  72. }
  73. /// <summary>
  74. /// Show error dialog for non-exceptions.
  75. /// </summary>
  76. /// <param name="message">Error message to show</param>
  77. /// <param name="details">Message to put under "details".</param>
  78. public static void ShowError(string message, string details) {
  79. ErrorDialog dialog = new ErrorDialog(null);
  80. dialog.dialog.Title = "Supertux-Editor Error";
  81. dialog.Message = message;
  82. dialog.AddDetails(details, true);
  83. dialog.Show();
  84. }
  85. public ErrorDialog(Window parent)
  86. {
  87. new Glade.XML(null, "errordialog.glade", "ErrorDialog", null).Autoconnect(this);
  88. dialog.TransientFor = parent;
  89. okButton.Clicked += new EventHandler(OnClose);
  90. expander.Activated += new EventHandler(OnExpanded);
  91. descriptionLabel.ModifyBg(StateType.Normal, new Gdk.Color(255, 0, 0));
  92. tagNoWrap = new TextTag("nowrap");
  93. tagNoWrap.WrapMode = WrapMode.None;
  94. detailsTextView.Buffer.TagTable.Add(tagNoWrap);
  95. tagWrap = new TextTag("wrap");
  96. tagWrap.WrapMode = WrapMode.Word;
  97. detailsTextView.Buffer.TagTable.Add(tagWrap);
  98. expander.Visible = false;
  99. }
  100. public string Message
  101. {
  102. get { return descriptionLabel.Text; }
  103. set
  104. {
  105. string message = value;
  106. while(message.EndsWith("\r") || message.EndsWith("\n"))
  107. message = message.Substring(0, message.Length - 1);
  108. if(!message.EndsWith("."))
  109. message += ".";
  110. descriptionLabel.Text = message;
  111. }
  112. }
  113. public void AddDetails(string text, bool wrapped)
  114. {
  115. TextIter it = detailsTextView.Buffer.EndIter;
  116. if(wrapped)
  117. detailsTextView.Buffer.InsertWithTags(ref it, text, tagWrap);
  118. else
  119. detailsTextView.Buffer.InsertWithTags(ref it, text, tagNoWrap);
  120. expander.Visible = true;
  121. }
  122. public void Show()
  123. {
  124. dialog.Show();
  125. }
  126. public void Run()
  127. {
  128. dialog.Show();
  129. dialog.Run();
  130. }
  131. public void Dispose()
  132. {
  133. dialog.Destroy();
  134. dialog.Dispose();
  135. }
  136. void OnClose(object sender, EventArgs args)
  137. {
  138. dialog.Destroy();
  139. }
  140. void OnExpanded(object sender, EventArgs args)
  141. {
  142. GLib.Timeout.Add(100, new GLib.TimeoutHandler(UpdateSize));
  143. }
  144. bool UpdateSize()
  145. {
  146. int w, h;
  147. dialog.GetSize(out w, out h);
  148. dialog.Resize(w, 1);
  149. return false;
  150. }
  151. }