1 // RGBChoose.cpp
2 // this file is part of Context Free
3 // ---------------------
4 // Copyright (C) 2008 John Horigan - john@glyphic.com
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 //
20 // John Horigan can be contacted at john@glyphic.com or at
21 // John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
22 //
23 //
24 
25 #include "StdAfx.h"
26 #include "RGBChoose.h"
27 
28 using namespace CFControls;
29 using namespace System;
30 using namespace System::ComponentModel;
31 using namespace System::Collections;
32 using namespace System::Windows::Forms;
33 using namespace System::Data;
34 using namespace System::Drawing;
35 
MoreInitialization()36 void RGBChoose::MoreInitialization()
37 {
38     validNumber = gcnew System::Text::RegularExpressions::Regex("\\d*");
39 
40     EventHandler^ texter = gcnew EventHandler(this, &RGBChoose::textHandler);
41     redBox->TextChanged += texter;
42     greenBox->TextChanged += texter;
43     blueBox->TextChanged += texter;
44 
45     ColorChangeEventHandler^ colorer = gcnew ColorChangeEventHandler(this, &RGBChoose::colorHandler);
46     redBar->ColorChange += colorer;
47     greenBar->ColorChange += colorer;
48     blueBar->ColorChange += colorer;
49 
50     heightToBoxPos = this->Size.Height - redBox->Location.Y;
51     heightToBarHeight = this->Size.Height - redBar->Size.Height;
52     lockedWidth = this->Size.Width;
53 
54     SizeChanged += gcnew EventHandler(this, &RGBChoose::resizeHandler);
55 }
56 
57 void RGBChoose::OnColorChange(ColorChangeEventArgs^ e)
58 {
59     ColorChange(this, e);
60 }
61 
updateMe(int which)62 void RGBChoose::updateMe(int which)
63 {
64     if (which & 1) {
65         redBar->Bright = (double)myColor.R / 255.0;
66         redBox->Text = myColor.R.ToString();
67     }
68 
69     if (which & 2) {
70         greenBar->Bright = (double)myColor.G / 255.0;
71         greenBox->Text = myColor.G.ToString();
72     }
73 
74     if (which & 4) {
75         blueBar->Bright = (double)myColor.B / 255.0;
76         blueBox->Text = myColor.B.ToString();
77     }
78 }
79 
80 System::Void RGBChoose::resizeHandler(System::Object^ sender, System::EventArgs^ e)
81 {
82     Control^ control = dynamic_cast<Control^>(sender);
83 
84     System::Drawing::Size newSize = control->Size;
85     bool accept = true;
86 
87     if (newSize.Width != lockedWidth) {
88         accept = false;
89         newSize.Width = lockedWidth;
90     }
91 
92     int barHeight = newSize.Height - heightToBarHeight;
93     int boxPos = newSize.Height - heightToBoxPos;
94     if (barHeight < 70) {
95         accept = false;
96         newSize.Height = 70 + heightToBarHeight;
97     }
98 
99     if (accept) {
100         SuspendLayout();
101         redBox->Location = Point(redBox->Location.X, boxPos);
102         redBar->Size = System::Drawing::Size(25, barHeight);
103         greenBox->Location = Point(greenBox->Location.X, boxPos);
104         greenBar->Size = System::Drawing::Size(25, barHeight);
105         blueBox->Location = Point(blueBox->Location.X, boxPos);
106         blueBar->Size = System::Drawing::Size(25, barHeight);
107         ResumeLayout();
108     } else {
109         control->Size = newSize;    // generates another SizeChanged event
110     }
111 }
112 
113 System::Void RGBChoose::colorHandler(Object^ sender, ColorChangeEventArgs^ e)
114 {
115     int number = (int)(e->newHSBColor.Bright * 255.0 + 0.5);
116     String^ numberT = number.ToString();
117 
118     if (Object::ReferenceEquals(sender, redBar)) {
119         myColor = System::Drawing::Color::FromArgb(myColor.A,
120             number, myColor.G, myColor.B);
121         redBox->Text = numberT;
122     }
123 
124     if (Object::ReferenceEquals(sender, greenBar)) {
125         myColor = System::Drawing::Color::FromArgb(myColor.A,
126             myColor.R, number, myColor.B);
127         greenBox->Text = numberT;
128     }
129 
130     if (Object::ReferenceEquals(sender, blueBar)) {
131         myColor = System::Drawing::Color::FromArgb(myColor.A,
132             myColor.R, myColor.G, number);
133         blueBox->Text = numberT;
134     }
135 
136     OnColorChange(gcnew ColorChangeEventArgs(myColor));
137 }
138 
139 System::Void RGBChoose::textHandler(System::Object^ sender, System::EventArgs^ e)
140 {
141     TextBox^ box = cli::safe_cast<TextBox^>(sender);
142 
143     if (box == nullptr || !box->Focused) return;
144 
145     String^ numberT = box->Text;
146     int number = 0;
147     try {
148         number = System::Int32::Parse(numberT, Globalization::NumberStyles::AllowLeadingWhite | Globalization::NumberStyles::AllowTrailingWhite);
149     } catch (SystemException^) {
150         number = 0;
151         box->Text = L"0";
152     }
153     double numberD = (double)number / 255.0;
154     bool accept = validNumber->IsMatch(numberT) && number >= 0 && number < 256;
155 
156     if (Object::ReferenceEquals(box, redBox)) {
157         if (accept) {
158             redBar->Bright = numberD;
159             myColor = System::Drawing::Color::FromArgb(myColor.A,
160                 number, myColor.G, myColor.B);
161         } else {
162             redBox->Text = myColor.R.ToString();
163         }
164     }
165 
166     if (Object::ReferenceEquals(box, greenBox)) {
167         if (accept) {
168             greenBar->Bright = numberD;
169             myColor = System::Drawing::Color::FromArgb(myColor.A,
170                 myColor.R, number, myColor.B);
171         } else {
172             greenBox->Text = myColor.G.ToString();
173         }
174     }
175 
176     if (Object::ReferenceEquals(box, blueBox)) {
177         if (accept) {
178             blueBar->Bright = numberD;
179             myColor = System::Drawing::Color::FromArgb(myColor.A,
180                 myColor.R, myColor.G, number);
181         } else {
182             blueBox->Text = myColor.B.ToString();
183         }
184     }
185 
186     if (accept) {
187         OnColorChange(gcnew ColorChangeEventArgs(myColor));
188     } else {
189         System::Media::SystemSounds::Beep->Play();
190     }
191 }
192 
193