2002-07-12 17:15:06 +00:00
|
|
|
//
|
|
|
|
// TestColorSelection.cs
|
|
|
|
//
|
|
|
|
// Author: Duncan Mak (duncan@ximian.com)
|
|
|
|
//
|
|
|
|
// Copyright (C) 2002, Duncan Mak, Ximian Inc.
|
|
|
|
//
|
|
|
|
|
2002-07-12 16:52:55 +00:00
|
|
|
using System;
|
2002-07-30 23:02:12 +00:00
|
|
|
using System.Text;
|
2002-07-12 16:52:55 +00:00
|
|
|
|
|
|
|
using Gtk;
|
|
|
|
|
|
|
|
namespace WidgetViewer {
|
|
|
|
public class TestColorSelection
|
|
|
|
{
|
|
|
|
static ColorSelectionDialog window = null;
|
2002-07-16 10:11:49 +00:00
|
|
|
static Dialog dialog = null;
|
2002-07-12 16:52:55 +00:00
|
|
|
|
|
|
|
public static Gtk.Window Create ()
|
|
|
|
{
|
|
|
|
HBox options = new HBox (false, 0);
|
|
|
|
CheckButton check_button = null;
|
|
|
|
|
|
|
|
window = new ColorSelectionDialog ("Color selection dialog");
|
|
|
|
window.ColorSelection.HasOpacityControl = true;
|
|
|
|
window.ColorSelection.HasPalette = true;
|
|
|
|
|
|
|
|
window.SetDefaultSize (250, 200);
|
2011-06-18 16:53:06 +00:00
|
|
|
window.ContentArea.PackStart (options, false, false, 0);
|
|
|
|
window.ContentArea.BorderWidth = 10;
|
2002-07-12 16:52:55 +00:00
|
|
|
|
|
|
|
check_button = new CheckButton("Show Opacity");
|
2002-07-16 10:11:49 +00:00
|
|
|
check_button.Active = true;
|
2002-07-12 16:52:55 +00:00
|
|
|
options.PackStart (check_button, false, false, 0);
|
|
|
|
check_button.Toggled += new EventHandler (Opacity_Callback);
|
|
|
|
|
|
|
|
check_button = new CheckButton("Show Palette");
|
2002-07-16 10:11:49 +00:00
|
|
|
check_button.Active = true;
|
2002-07-12 16:52:55 +00:00
|
|
|
options.PackEnd (check_button, false, false, 0);
|
|
|
|
check_button.Toggled += new EventHandler (Palette_Callback);
|
|
|
|
|
|
|
|
window.ColorSelection.ColorChanged += new EventHandler (Color_Changed);
|
|
|
|
window.OkButton.Clicked += new EventHandler (Color_Selection_OK);
|
2002-07-17 08:59:11 +00:00
|
|
|
window.CancelButton.Clicked += new EventHandler (Color_Selection_Cancel);
|
2002-07-12 16:52:55 +00:00
|
|
|
|
|
|
|
options.ShowAll ();
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Opacity_Callback (object o, EventArgs args)
|
|
|
|
{
|
|
|
|
window.ColorSelection.HasOpacityControl = ((ToggleButton )o).Active;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Palette_Callback (object o, EventArgs args)
|
|
|
|
{
|
|
|
|
window.ColorSelection.HasPalette = ((ToggleButton )o).Active;
|
|
|
|
}
|
|
|
|
|
2011-06-18 16:53:06 +00:00
|
|
|
static string HexFormat (Gdk.RGBA color)
|
2002-07-30 23:02:12 +00:00
|
|
|
{
|
|
|
|
StringBuilder s = new StringBuilder ();
|
2011-06-18 16:53:06 +00:00
|
|
|
double[] vals = { color.Red, color.Green, color.Blue };
|
2002-07-30 23:02:12 +00:00
|
|
|
char[] hexchars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
|
|
|
|
|
s.Append ('#');
|
2011-06-18 16:53:06 +00:00
|
|
|
foreach (double val in vals) {
|
2002-07-30 23:02:12 +00:00
|
|
|
/* Convert to a range of 0-255, then lookup the
|
|
|
|
* digit for each half-byte */
|
2011-06-18 16:53:06 +00:00
|
|
|
byte rounded = (byte) (val * 255);
|
2002-07-30 23:02:12 +00:00
|
|
|
s.Append (hexchars[(rounded & 0xf0) >> 4]);
|
|
|
|
s.Append (hexchars[rounded & 0x0f]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ToString ();
|
|
|
|
}
|
|
|
|
|
2002-07-12 16:52:55 +00:00
|
|
|
static void Color_Changed (object o, EventArgs args)
|
|
|
|
{
|
2011-06-18 16:53:06 +00:00
|
|
|
Gdk.RGBA color = window.ColorSelection.CurrentRgba;
|
2002-07-30 23:02:12 +00:00
|
|
|
Console.WriteLine (HexFormat (color));
|
2002-07-12 16:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Color_Selection_OK (object o, EventArgs args)
|
|
|
|
{
|
2011-06-18 16:53:06 +00:00
|
|
|
Gdk.RGBA selected = window.ColorSelection.CurrentRgba;
|
2004-02-10 16:04:50 +00:00
|
|
|
window.Hide ();
|
|
|
|
Display_Result (selected);
|
2002-07-12 16:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Color_Selection_Cancel (object o, EventArgs args)
|
|
|
|
{
|
|
|
|
window.Destroy ();
|
2002-07-16 10:11:49 +00:00
|
|
|
}
|
|
|
|
|
2002-07-31 07:07:29 +00:00
|
|
|
static void Dialog_Ok (object o, EventArgs args)
|
|
|
|
{
|
|
|
|
dialog.Destroy ();
|
|
|
|
window.ShowAll ();
|
|
|
|
}
|
|
|
|
|
2011-06-18 16:53:06 +00:00
|
|
|
static void Display_Result (Gdk.RGBA color)
|
2002-07-16 10:11:49 +00:00
|
|
|
{
|
2002-07-31 07:07:29 +00:00
|
|
|
dialog = new Dialog ();
|
|
|
|
dialog.Title = "Selected Color: " + HexFormat (color);
|
2011-06-18 16:53:06 +00:00
|
|
|
|
2002-07-16 10:11:49 +00:00
|
|
|
DrawingArea da = new DrawingArea ();
|
|
|
|
|
2011-06-18 16:53:06 +00:00
|
|
|
da.OverrideBackgroundColor (StateFlags.Normal, color);
|
2002-07-16 10:11:49 +00:00
|
|
|
|
2011-06-18 16:53:06 +00:00
|
|
|
dialog.ContentArea.BorderWidth = 10;
|
|
|
|
dialog.ContentArea.PackStart (da, true, true, 10);
|
2002-07-30 23:02:12 +00:00
|
|
|
dialog.SetDefaultSize (200, 200);
|
2002-07-16 10:11:49 +00:00
|
|
|
|
2004-02-10 16:04:50 +00:00
|
|
|
Button button = new Button (Stock.Ok);
|
2002-07-31 07:07:29 +00:00
|
|
|
button.Clicked += new EventHandler (Dialog_Ok);
|
2002-07-16 10:11:49 +00:00
|
|
|
button.CanDefault = true;
|
|
|
|
dialog.ActionArea.PackStart (button, true, true, 0);
|
|
|
|
button.GrabDefault ();
|
|
|
|
|
|
|
|
dialog.ShowAll ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Close_Button (object o, EventArgs args)
|
|
|
|
{
|
|
|
|
Color_Selection_Cancel (o, args);
|
2002-07-12 16:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|