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;
|
|
|
|
using GtkSharp;
|
|
|
|
|
|
|
|
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);
|
|
|
|
window.VBox.PackStart (options, false, false, 0);
|
|
|
|
window.VBox.BorderWidth = 10;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-07-30 23:02:12 +00:00
|
|
|
static string HexFormat (Gdk.Color color)
|
|
|
|
{
|
|
|
|
StringBuilder s = new StringBuilder ();
|
|
|
|
ushort[] vals = { color.red, color.green, color.blue };
|
|
|
|
char[] hexchars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
|
|
'A', 'B', 'C', 'D', 'E', 'F'};
|
|
|
|
|
|
|
|
s.Append ('#');
|
|
|
|
foreach (ushort val in vals) {
|
|
|
|
/* Convert to a range of 0-255, then lookup the
|
|
|
|
* digit for each half-byte */
|
|
|
|
byte rounded = (byte) (val >> 8);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
Gdk.Color color = window.ColorSelection.CurrentColor;
|
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)
|
|
|
|
{
|
2002-07-16 10:11:49 +00:00
|
|
|
Gdk.Color selected = window.ColorSelection.CurrentColor;
|
2002-11-12 21:51:45 +00:00
|
|
|
if (selected == Gdk.Color.Zero)
|
2002-07-16 10:11:49 +00:00
|
|
|
Console.WriteLine ("Color selection failed.");
|
2002-07-31 07:07:29 +00:00
|
|
|
else {
|
|
|
|
window.Hide ();
|
|
|
|
Display_Result (selected);
|
2002-07-16 10:11:49 +00:00
|
|
|
}
|
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 ();
|
|
|
|
}
|
|
|
|
|
2002-07-16 10:11:49 +00:00
|
|
|
static void Display_Result (Gdk.Color color)
|
|
|
|
{
|
2002-11-12 21:51:45 +00:00
|
|
|
if (color == Gdk.Color.Zero)
|
2002-07-16 10:11:49 +00:00
|
|
|
Console.WriteLine ("Null color");
|
|
|
|
|
2002-07-31 07:07:29 +00:00
|
|
|
dialog = new Dialog ();
|
|
|
|
dialog.Title = "Selected Color: " + HexFormat (color);
|
|
|
|
dialog.HasSeparator = true;
|
2002-07-30 23:02:12 +00:00
|
|
|
|
2002-07-16 10:11:49 +00:00
|
|
|
DrawingArea da = new DrawingArea ();
|
|
|
|
|
|
|
|
da.ModifyBg (StateType.Normal, color);
|
|
|
|
|
2002-07-31 07:07:29 +00:00
|
|
|
dialog.VBox.BorderWidth = 10;
|
|
|
|
dialog.VBox.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
|
|
|
|
2002-07-31 07:07:29 +00:00
|
|
|
Button button = Button.NewFromStock (Stock.Ok);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|