1 /*
2 Copyright (C) 2011-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: Dk4WxColourView.cpt
12 */
13 
14 /**	@file Dk4WxColourView.cpp The Dk4WxColourView module.
15 */
16 
17 
18 #include "dk4conf.h"
19 #include <libdk4wx/Dk4WxColourView.h>
20 
21 #include <wx/dcbuffer.h>
22 #include <wx/colordlg.h>
23 
24 #include <libdk4wx/Dk4WxColourPalette.h>
25 
26 
27 
28 
29 
30 
31 
32 /**	Handlers for events.
33 */
34 #if	wxCHECK_VERSION(3,0,0)
35 wxBEGIN_EVENT_TABLE(Dk4WxColourView,wxControl)
36 #else
37 BEGIN_EVENT_TABLE(Dk4WxColourView,wxControl)
38 #endif
39   EVT_ERASE_BACKGROUND(Dk4WxColourView::OnErase)
40   EVT_PAINT(Dk4WxColourView::OnPaint)
41   EVT_LEFT_DOWN(Dk4WxColourView::OnLeftMouseButtonDown)
42   EVT_RIGHT_DOWN(Dk4WxColourView::OnRightMouseButtonDown)
43 #if	wxCHECK_VERSION(3,0,0)
44 wxEND_EVENT_TABLE()
45 #else
46 END_EVENT_TABLE()
47 #endif
48 
49 
50 
51 /**	Implementation of dynamic class.
52 */
53 #if	wxCHECK_VERSION(3,0,0)
54 wxIMPLEMENT_DYNAMIC_CLASS(Dk4WxColourView, wxControl);
55 #else
56 IMPLEMENT_DYNAMIC_CLASS(Dk4WxColourView, wxControl)
57 #endif
58 
59 
60 #if	wxCHECK_VERSION(3,0,0)
61 /**	Event generated if the colour was changed.
62 */
63 wxDEFINE_EVENT(wxEVT_DK4_COLOUR_VIEW_EVENT, wxCommandEvent);
64 #else
65 /**	Event generated if the colour was changed.
66 */
67 DEFINE_EVENT_TYPE(wxEVT_DK4_COLOUR_VIEW_EVENT)
68 #endif
69 
70 
71 
72 /**	Name of the control.
73 */
74 wxChar const Dk4WxColourView::Dk4WxColourViewName[] =
75 { wxT("Dk4WxColourView") } ;
76 
77 
78 static int const default_palette_components[] = {
79 	  0,   0,   0,
80 	  0,   0, 255,
81 	  0, 255,   0,
82 	  0, 255, 255,
83 	255,   0,   0,
84 	255,   0, 255,
85 	255, 255,   0,
86 	255, 255, 255,
87 
88 	192, 192, 192,
89 	 54,  78, 109,
90 	  0, 143,   0,
91 	  0, 209, 209,
92 	209,   0,   0,
93 	255, 224, 224,
94 	255, 214,   0,
95 	127,  48,   0,
96 };
97 
98 
Dk4WxColourView()99 Dk4WxColourView::Dk4WxColourView()
100 {
101   pParentWindow = NULL;
102   pHelper = NULL;
103   w = -1;
104   h = -1;
105   r = 255;
106   g = 255;
107   b = 255;
108   ccdx = -1;
109   ccdy = -1;
110   cpdx = -1;
111   cpdy = -1;
112   bActive = true;
113   bPalettePreferred = false;
114   bPlaceColourChooser = false;
115   bMarkActiveInactive = true;
116   bWithBorder		  = true;
117   m_wxid = wxID_ANY;
118 }
119 
120 
121 
Dk4WxColourView(wxWindow * pParent,wxWindowID wxid,Dk4WxApplicationHelper * appHelper,int red,int green,int blue,wxPoint const & pos,wxSize const & size,long style,wxChar const * name)122 Dk4WxColourView::Dk4WxColourView(
123       wxWindow					*pParent,
124       wxWindowID				 wxid,
125       Dk4WxApplicationHelper	*appHelper,
126       int						 red,
127       int						 green,
128       int						 blue,
129       wxPoint const &			 pos,
130       wxSize  const &			 size,
131       long						 style,
132       wxChar const				*name
133 )
134 : wxControl(
135     pParent, wxid, pos, size, style, wxDefaultValidator,
136     wxString(name)
137 )
138 {
139   pParentWindow = pParent;
140   pHelper = appHelper;
141   w = size.x;
142   h = size.y;
143   r = red;
144   g = green;
145   b = blue;
146   ccdx = -1;
147   ccdy = -1;
148   cpdx = -1;
149   cpdy = -1;
150   bActive = true;
151   bPalettePreferred = false;
152   bPlaceColourChooser = false;
153   bMarkActiveInactive = true;
154   bWithBorder		  = true;
155   m_wxid = wxid;
156 }
157 
158 
159 
160 bool
Create(wxWindow * pParent,wxWindowID wxid,Dk4WxApplicationHelper * appHelper,int red,int green,int blue,wxPoint const & pos,wxSize const & size,long style,wxChar const * name)161 Dk4WxColourView::Create(
162       wxWindow					*pParent,
163       wxWindowID				 wxid,
164       Dk4WxApplicationHelper	*appHelper,
165       int						 red,
166       int						 green,
167       int						 blue,
168       wxPoint const &			 pos,
169       wxSize  const &			 size,
170       long						 style,
171       wxChar const				*name
172 )
173 {
174   bool back;
175   back = wxControl::Create(
176     pParent, wxid, pos, size, style, wxDefaultValidator,
177     wxString(name)
178   );
179   pParentWindow = pParent;
180   pHelper = appHelper;
181   w = size.x;
182   h = size.y;
183   r = red;
184   g = green;
185   b = blue;
186   ccdx = -1;
187   ccdy = -1;
188   cpdx = -1;
189   cpdy = -1;
190   bActive = true;
191   bPalettePreferred = false;
192   bPlaceColourChooser = false;
193   bMarkActiveInactive = true;
194   bWithBorder		  = true;
195   m_wxid = wxid;
196   return back;
197 }
198 
199 
200 
201 wxSize
DoGetBestSize() const202 Dk4WxColourView::DoGetBestSize() const
203 {
204   wxSize back(((-1 < w) ? w : 20), ((-1 < h) ? h : 20));
205   return back;
206 }
207 
208 
209 
210 void
OnErase(wxEraseEvent & WXUNUSED (event))211 Dk4WxColourView::OnErase(wxEraseEvent & WXUNUSED(event))
212 {
213 }
214 
215 
216 
217 void
OnPaint(wxPaintEvent & WXUNUSED (event))218 Dk4WxColourView::OnPaint(wxPaintEvent & WXUNUSED(event))
219 {
220   wxBufferedPaintDC		useDC(this);
221   wxSize				sz		= GetClientSize();
222   wxColour				mycolor(
223   	(unsigned char)r,
224 	(unsigned char)g,
225 	(unsigned char)b
226   );
227 #if	wxCHECK_VERSION(3,0,0)
228   wxBrush				myBrush(mycolor, wxBRUSHSTYLE_SOLID);
229 #else
230   wxBrush				myBrush(mycolor, wxSOLID);
231 #endif
232   wxPen					myPen(mycolor);
233   int					b		= 2;
234 
235   if (bActive || (!(bMarkActiveInactive))) {
236   	useDC.SetBrush(myBrush);
237 	useDC.SetPen(myPen);
238   	useDC.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight());
239   }
240   else {
241 	wxColour	iac = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWFRAME);
242   	int w	= sz.GetWidth();
243 	int	h	= sz.GetHeight();
244 	if (w < h) { b = w / 4; } else { b = h / 4; }
245 	wxPen	blackPen(wxColour(127, 0, 0));
246 #if	wxCHECK_VERSION(3,0,0)
247 	wxBrush	grayBrush(iac, wxBRUSHSTYLE_SOLID);
248 #else
249 	wxBrush	grayBrush(iac, wxSOLID);
250 #endif
251 	useDC.SetBrush(grayBrush);
252 	useDC.SetPen(blackPen);
253 	useDC.DrawRectangle(0, 0, sz.GetWidth(), sz.GetHeight());
254 	useDC.SetBrush(myBrush);
255 	useDC.SetPen(myPen);
256 	useDC.DrawRectangle(b, b, (sz.GetWidth() - 2*b), (sz.GetHeight() - 2*b));
257 	useDC.SetPen(blackPen);
258 	useDC.DrawLine(0, sz.GetHeight(), sz.GetWidth(), 0);
259   }
260   useDC.SetBrush(wxNullBrush);
261   useDC.SetPen(wxNullPen);
262 
263 }
264 
265 
266 
267 void
PlaceColourChooser(wxWindow & dlg,int x,int y)268 Dk4WxColourView::PlaceColourChooser(wxWindow & dlg, int x, int y)
269 {
270   if (bPlaceColourChooser) {
271   	Dk4WxApplicationHelper::ChooseChildPosition(*this, dlg);
272   }
273   else {
274 	if ((-1 < x) && (-1 < y)) {
275 	  dlg.SetPosition( wxPoint(x,y) );
276 	}
277 	else {
278 	  if (NULL != pParentWindow) {
279 	  	Dk4WxApplicationHelper::ChooseChildPosition(*pParentWindow, dlg);
280 	  }
281 	}
282   }
283 }
284 
285 
286 
287 void
RunSystemColourChooser(void)288 Dk4WxColourView::RunSystemColourChooser(void)
289 {
290   wxColour			curcol(
291   	(unsigned char)r,
292 	(unsigned char)g,
293 	(unsigned char)b
294   );
295   wxColourData		wxcd;
296   for (int i = 0; i < 16; i++) {
297     wxColour		col(
298 		(unsigned char)(default_palette_components[3*i]),
299 		(unsigned char)(default_palette_components[3*i+1]),
300 		(unsigned char)(default_palette_components[3*i+2])
301 	);
302   	wxcd.SetCustomColour(i, col);
303   }
304   wxcd.SetColour(curcol);
305   wxColourDialog	dlg(this, &wxcd);
306   PlaceColourChooser(dlg, ccdx, ccdy);
307   if(dlg.ShowModal() == wxID_OK) {
308     wxColour col = dlg.GetColourData().GetColour();
309     r = col.Red();
310     g = col.Green();
311     b = col.Blue();
312     dlg.GetPosition(&ccdx, &ccdy);
313     Refresh();
314     Update();
315 	SendColourViewEvent();
316   } else {
317     dlg.GetPosition(&ccdx, &ccdy);
318   }
319 }
320 
321 
322 
323 void
RunPaletteColourChooser(void)324 Dk4WxColourView::RunPaletteColourChooser(void)
325 {
326   Dk4WxColourPalette dlg(
327 	  ((pParentWindow) ? (pParentWindow) : (this)),
328 	  pHelper, r, g, b, bWithBorder
329   );
330   PlaceColourChooser(dlg, cpdx, cpdy);
331   if(dlg.ShowModal() == wxID_OK) {
332     r = dlg.Red();
333     g = dlg.Green();
334     b = dlg.Blue();
335 	dlg.GetPosition(&cpdx, &cpdy);
336     Refresh();
337     Update();
338 	SendColourViewEvent();
339   }
340   else {
341   	dlg.GetPosition(&cpdx, &cpdy);
342   }
343 }
344 
345 
346 
347 void
OnLeftMouseButtonDown(wxMouseEvent & WXUNUSED (event))348 Dk4WxColourView::OnLeftMouseButtonDown(wxMouseEvent & WXUNUSED(event))
349 {
350 
351   if (bActive) {
352     if (bPalettePreferred) {
353 		RunPaletteColourChooser();
354 	}
355 	else {
356 		RunSystemColourChooser();
357     }
358   }
359 
360 }
361 
362 
363 
364 void
OnRightMouseButtonDown(wxMouseEvent & WXUNUSED (event))365 Dk4WxColourView::OnRightMouseButtonDown(wxMouseEvent & WXUNUSED(event))
366 {
367 
368   if (bActive) {
369     if (bPalettePreferred) {
370 	  RunSystemColourChooser();
371 	}
372 	else {
373 	  RunPaletteColourChooser();
374 	}
375   }
376 
377 }
378 
379 
380 
381 int
Red() const382 Dk4WxColourView::Red() const
383 {
384   return r;
385 }
386 
387 
388 
389 int
Green() const390 Dk4WxColourView::Green() const
391 {
392   return g;
393 }
394 
395 
396 
397 int
Blue() const398 Dk4WxColourView::Blue() const
399 {
400   return b;
401 }
402 
403 
404 
405 void
SetRGB(int red,int green,int blue)406 Dk4WxColourView::SetRGB(int red, int green, int blue)
407 {
408   r = red;
409   g = green;
410   b = blue;
411   if(r < 0) r = 0;
412   if(g < 0) g = 0;
413   if(b < 0) b = 0;
414   if(r > 255) r = 255;
415   if(g > 255) g = 255;
416   if(b > 255) b = 255;
417 }
418 
419 
420 
421 void
SetActive(bool b)422 Dk4WxColourView::SetActive(bool b)
423 {
424 	bool	bMustUpdate;
425 
426 	bMustUpdate = (bActive == b);
427 	bActive = b;
428 	if (bMustUpdate) {
429 		Refresh();
430 	}
431 #if	0
432 	wxControl::Enable(b);
433 #endif
434 }
435 
436 
437 
438 void
SetPalettePreferred(bool b)439 Dk4WxColourView::SetPalettePreferred(bool b)
440 {
441   bPalettePreferred = b;
442 }
443 
444 
445 
446 void
SetPlaceColourChooser(bool b)447 Dk4WxColourView::SetPlaceColourChooser(bool b)
448 {
449 	bPlaceColourChooser = b;
450 }
451 
452 
453 
454 void
SetMarkActiveInactive(bool b)455 Dk4WxColourView::SetMarkActiveInactive(bool b)
456 {
457 	bMarkActiveInactive = b;
458 }
459 
460 
461 
462 void
SetColourChooserWithBorder(bool b)463 Dk4WxColourView::SetColourChooserWithBorder(bool b)
464 {
465 	bWithBorder = b;
466 }
467 
468 
469 
470 void
SendColourViewEvent(void)471 Dk4WxColourView::SendColourViewEvent(void)
472 {
473 	if ((NULL != pParentWindow) && (wxID_ANY != m_wxid)) {
474 		wxCommandEvent	ev(wxEVT_DK4_COLOUR_VIEW_EVENT, m_wxid);
475 		wxPostEvent(pParentWindow, ev);
476 	}
477 }
478 
479 
480 
481 /* vim: set ai sw=4 ts=4 : */
482