ErrorDialog.cs 4.5 KB

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