File: TestColorSelection.cs

package info (click to toggle)
gtk-sharp3 2.99.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 25,488 kB
  • sloc: xml: 308,885; cs: 38,796; sh: 11,336; perl: 1,295; makefile: 1,099; ansic: 903
file content (132 lines) | stat: -rw-r--r-- 3,481 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// TestColorSelection.cs
//
// Author: Duncan Mak  (duncan@ximian.com)
//
// Copyright (C) 2002, Duncan Mak, Ximian Inc.
//

using System;
using System.Text;

using Gtk;

namespace WidgetViewer {
	public class TestColorSelection
	{
		static ColorSelectionDialog window = null;
		static Dialog dialog = null;

		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.ContentArea.PackStart (options, false, false, 0);
			window.ContentArea.BorderWidth = 10;

			check_button = new CheckButton("Show Opacity");
			check_button.Active = true;
			options.PackStart (check_button, false, false, 0);
			check_button.Toggled += new EventHandler (Opacity_Callback);

			check_button = new CheckButton("Show Palette");
			check_button.Active = true;
			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);
			window.CancelButton.Clicked += new EventHandler (Color_Selection_Cancel); 

			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;
		}

		static string HexFormat (Gdk.RGBA color)
		{
			StringBuilder s = new StringBuilder ();
			double[] 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 (double val in vals) {
				/* Convert to a range of 0-255, then lookup the
				 * digit for each half-byte */
				byte rounded = (byte) (val * 255);
				s.Append (hexchars[(rounded & 0xf0) >> 4]);
				s.Append (hexchars[rounded & 0x0f]);
			}

			return s.ToString ();
		}
		
		static void Color_Changed (object o, EventArgs args)
		{
			Gdk.RGBA color = window.ColorSelection.CurrentRgba;
			Console.WriteLine (HexFormat (color));
		}

		static void Color_Selection_OK (object o, EventArgs args)
		{
			Gdk.RGBA selected = window.ColorSelection.CurrentRgba;
			window.Hide ();
			Display_Result (selected);
		}

		static void Color_Selection_Cancel (object o, EventArgs args)
		{
			window.Destroy ();
		}

		static void Dialog_Ok (object o, EventArgs args)
		{
			dialog.Destroy ();
			window.ShowAll ();
		}

		static void Display_Result (Gdk.RGBA color)
		{
			dialog = new Dialog ();
			dialog.Title = "Selected Color: " + HexFormat (color);
			
			DrawingArea da = new DrawingArea ();

			da.OverrideBackgroundColor (StateFlags.Normal, color);

			dialog.ContentArea.BorderWidth = 10;
			dialog.ContentArea.PackStart (da, true, true, 10);
			dialog.SetDefaultSize (200, 200);

			Button button = new Button (Stock.Ok);
			button.Clicked += new EventHandler (Dialog_Ok);
			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);
		}
	}
}