/*************************************************************************** * MessageArea.cs * * Copyright (C) 2005 Novell * Written by Aaron Bockover (aaron@aaronbock.net) * Modified by Kyle Ambroff * * This file is based on the Banshee HighlightMessageArea class. ****************************************************************************/ /* THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW: * * 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. */ using System; using Gtk; namespace Search { public class MessageArea : Alignment { private HBox box; private VBox button_box; private Image image; private Label label; private Label details_label; private Button button; private Button close_button; private bool is_locked; public bool Locked { get { return is_locked; } set { is_locked = value; } } public event EventHandler ButtonClicked; public MessageArea () : base(0.0f, 0.5f, 1.0f, 0.0f) { this.BorderWidth = 5; HBox shell_box = new HBox (); shell_box.Spacing = 10; box = new HBox (); box.Spacing = 10; image = new Image (); image.Yalign = 0.0f; label = new Label (); label.Xalign = 0.0f; label.Yalign = 0.0f; label.Show(); details_label = new Label (); details_label.Xalign = 0.0f; details_label.Yalign = 1.0f; details_label.Show (); VBox label_box = new VBox (); label_box.PackStart (label, true, true, 0); label_box.PackStart (details_label, true, true, 0); box.PackStart (image, false, false, 0); box.PackStart (label_box, true, true, 0); box.Show (); button_box = new VBox (); button_box.Spacing = 3; button = new Button (); button.Relief = ReliefStyle.Half; button.Xalign = 0.0f; button.UseUnderline = true; button.Clicked += delegate (object o, EventArgs args) { EventHandler handler = ButtonClicked; if (handler != null) handler (this, new EventArgs ()); Hide (); Reset (); }; button_box.Add (button); close_button = new Button ("_Ignore this message"); close_button.Relief = ReliefStyle.Half; close_button.Xalign = 0.0f; close_button.Clicked += delegate (object o, EventArgs args) { Hide(); Reset (); }; close_button.ShowAll (); close_button.Hide (); button_box.Add (close_button); shell_box.PackStart (box, true, true, 0); shell_box.PackStart (button_box, false, false, 0); shell_box.Show (); this.Add (shell_box); this.EnsureStyle (); } public void Reset () { this.is_locked = false; this.Message = String.Empty; this.Details = String.Empty; this.Pixbuf = null; this.ButtonLabel = String.Empty; this.ShowCloseButton = true; this.ButtonClicked = null; } public void Display (Gdk.Pixbuf pixbuf, string message, string details, string button_label, EventHandler button_cb) { // Do this in the idle loop, so that multiple events // can try to GLib.Timeout.Add (1000, delegate () { // don't do anything if the widget is currently // in use, and try again in one second. if (this.Locked) return true; // lock it for exclusive use. It will be unlocked // when a button is clicked. this.Locked = true; this.Pixbuf = pixbuf; this.Message = message; this.Details = details; this.ButtonLabel = button_label; this.ButtonClicked += button_cb; this.Show (); return false; }); } protected override bool OnExposeEvent (Gdk.EventExpose evnt) { // Draw border around widget GdkWindow.DrawRectangle (Style.BackgroundGC (StateType.Selected), true, Allocation.X, Allocation.Y, Allocation.Width, Allocation.Height); // Draw background color GdkWindow.DrawRectangle (Style.BackgroundGC (StateType.Normal), true, Allocation.X + 1, Allocation.Y + 1, Allocation.Width - 2, Allocation.Height - 2); base.OnExposeEvent (evnt); return true; } private bool changing_style = false; protected override void OnStyleSet (Gtk.Style previousStyle) { if (changing_style) { return; } changing_style = true; Window win = new Window (WindowType.Popup); win.Name = "gtk-tooltips"; win.EnsureStyle (); Style = win.Style; changing_style = false; } public string ButtonLabel { set { bool should_remove = false; foreach (Widget child in box.Children) { if (child == button) { should_remove = true; break; } } if (should_remove && value == null) box.Remove (button); if (value != null) { button.Label = value; button.Show (); box.PackStart (button, false, false, 0); } QueueDraw (); } } public bool ShowCloseButton { set { close_button.Visible = value; QueueDraw (); } } public bool ButtonUseStock { set { button.UseStock = value; QueueDraw (); } } public string Message { set { label.Markup = String.Format ("{0}", value);; QueueDraw (); } } public string Details { set { details_label.Markup = value; QueueDraw (); } } public Gdk.Pixbuf Pixbuf { set { image.Pixbuf = value; image.Visible = value != null; QueueDraw (); } } } }