CMS 3D CMS Logo

FWColorSelect.cc
Go to the documentation of this file.
1 /*
2  All access to this colour picker goes through FWColorSelect.
3  Widget creation: new FWColorSelect(p, n, col, cols, id)
4  p: parent window
5  n: the name of the popup
6  col: initially selected colour (in Pixel_t)
7  cols: a vector<Pixel_t> of colour values
8  id: window ID
9 
10  After creation, connect to signal method ColorSelected(Pixel_t) in
11  FWColorSelect to receive colour changes.
12  */
13 #include <boost/bind.hpp>
14 
15 #include "TGLayout.h"
16 #include "TGClient.h"
17 #include "TGGC.h"
18 #include "TGColorDialog.h"
19 #include "TColor.h"
20 
23 
24 //------------------------------Constants------------------------------//
25 
27 {
30 
33 
36 
37  kColorPopupGray = 0xcccccc
38 };
39 
40 
41 //------------------------------FWColorFrame------------------------------//
42 
43 FWColorFrame::FWColorFrame(const TGWindow *p, Color_t ci) :
44  TGFrame(p, 20, 20, kOwnBackground)
45 {
46  SetColor(ci);
47  Resize(kCFWidth, kCFHeight);
48 }
49 
51 {
52  if (event->fType == kButtonRelease)
53  {
55  }
56  return kTRUE;
57 }
58 
59 void
61 {
62  fColor = ci;
63  SetBackgroundColor(TColor::Number2Pixel(fColor));
64 }
65 
67 {
68  Emit("ColorSelected(Color_t)", ci);
69 }
70 
71 
72 //------------------------------FWColorRow------------------------------//
73 
74 FWColorRow::FWColorRow(const TGWindow *p) :
75  TGHorizontalFrame(p, 10, 10, kOwnBackground, TGFrame::GetBlackPixel()),
76  fBackgroundIsBlack(kTRUE)
77 {
78  fSelectedIndex = -1;
79 }
80 
82 {
83  Cleanup();
84 }
85 
87 {
88  fBackgroundIsBlack= toBlack;
89  if (fBackgroundIsBlack) {
90  SetBackgroundColor(TGFrame::GetBlackPixel());
91  } else {
92  SetBackgroundColor(TGFrame::GetWhitePixel());
93  }
94 }
95 
97 {
98  TGCompositeFrame::DoRedraw();
99  DrawHighlight();
100 }
101 
103 {
104  if (fSelectedIndex >= 0)
105  {
106  Int_t x = fSelectedIndex * (fCc.at(fSelectedIndex)->GetWidth() + kCFPadLeft + kCFPadRight) + kCFPadLeft;
107  Int_t y = kCFPadBelow;
108  Int_t w = fCc.at(fSelectedIndex)->GetWidth();
109  Int_t h = fCc.at(fSelectedIndex)->GetHeight();
110  gVirtualX->DrawRectangle(fId, GetShadowGC()(), x - kHLOffsetX, y - kHLOffsetY, w + kHLExtraWidth, h + kHLExtraHeight);
111  }
112 }
113 
115 {
116  fCc.push_back(new FWColorFrame(this, color));
117  AddFrame(fCc.back(), new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, kCFPadLeft, kCFPadRight, kCFPadAbove, kCFPadBelow));
118  fCc.back()->Connect("ColorSelected(Color_t)", "FWColorRow", this, "ColorChanged(Color_t)");
119 }
120 
121 void FWColorRow::ResetColor(Int_t iIndex, Color_t iColor)
122 {
123  fCc[iIndex]->SetColor(iColor);
124 }
125 
126 Int_t
127 FWColorRow::FindColorIndex(Color_t iColor) const
128 {
129  Int_t returnValue = -1;
130  Int_t index = 0;
131  for(std::vector<FWColorFrame*>::const_iterator it = fCc.begin(), itEnd = fCc.end();
132  it != itEnd; ++it, ++index)
133  {
134  if ((*it)->GetColor() == iColor)
135  return index;
136  }
137  return returnValue;
138 }
139 
140 void FWColorRow::ColorChanged(Color_t ci)
141 {
142  Emit("ColorChanged(Color_t)", ci);
143 }
144 
145 
146 //------------------------------FWColorPopup------------------------------//
147 
148 Bool_t FWColorPopup::fgFreePalette = kFALSE;
149 
150 Bool_t FWColorPopup::HasFreePalette() { return fgFreePalette; }
151 void FWColorPopup::EnableFreePalette() { fgFreePalette = kTRUE; }
152 
153 FWColorPopup::FWColorPopup(const TGWindow *p, Color_t color) :
154  TGVerticalFrame(p, 10, 10, kDoubleBorder | kRaisedFrame | kOwnBackground, kColorPopupGray),
155  fShowWheel(kFALSE)
156 {
157  SetWindowAttributes_t wattr;
158  wattr.fMask = kWAOverrideRedirect;
159  wattr.fOverrideRedirect = kTRUE;
160  gVirtualX->ChangeWindowAttributes(fId, &wattr);
161  AddInput(kStructureNotifyMask); // to notify the client for structure (i.e. unmap) changes
162 
164  fLabel = nullptr;
165  fNumColors = 0;
166 
167  fFirstRow = new FWColorRow(this);
168  fSecondRow = new FWColorRow(this);
169  fFirstRow ->Connect("ColorChanged(Color_t)", "FWColorPopup", this, "ColorSelected(Color_t)");
170  fSecondRow->Connect("ColorChanged(Color_t)", "FWColorPopup", this, "ColorSelected(Color_t)");
171 }
172 
174 {
175  Cleanup();
176 }
177 
179 {
180  if (event->fX < 0 || event->fX >= (Int_t) fWidth ||
181  event->fY < 0 || event->fY >= (Int_t) fHeight)
182  {
183  if (event->fType == kButtonRelease)
184  UnmapWindow();
185  }
186  else
187  {
188  TGFrame *f = GetFrameFromPoint(event->fX, event->fY);
189  if (f && f != this)
190  {
191  TranslateCoordinates(f, event->fX, event->fY, event->fX, event->fY);
192  f->HandleButton(event);
193  }
194  }
195  return kTRUE;
196 }
197 
198 void FWColorPopup::InitContent(const char *name, const std::vector<Color_t>& colors, bool backgroundIsBlack)
199 {
200  fLabel = new TGLabel(this, name);
201  fLabel->SetBackgroundColor(GetBackground());
202  SetColors(colors, backgroundIsBlack);
203 
204  AddFrame(fLabel, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, kCROffsetX, kCROffsetY, kCRPadAbove + 1, kCRPadBelow - 1));
205  AddFrame(fFirstRow, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, kCROffsetX, kCROffsetY, kCRPadAbove, kCRPadBelow));
206  AddFrame(fSecondRow, new TGLayoutHints(kLHintsCenterX | kLHintsCenterY, kCROffsetX, kCROffsetY, kCRPadAbove, kCRPadBelow));
207 
208  fFirstRow->MapSubwindows();
209  fFirstRow->Resize(fFirstRow->GetDefaultSize());
210 
211  fSecondRow->MapSubwindows();
212  fSecondRow->Resize(fSecondRow->GetDefaultSize());
213 
214  if (fgFreePalette)
215  {
216  TGTextButton *b = new TGTextButton(this, "Color wheel");
217  AddFrame(b, new TGLayoutHints(kLHintsTop | kLHintsCenterX | kLHintsExpandX, kCROffsetX, kCROffsetY, kCRPadAbove, 2 * kCRPadBelow));
218  b->Connect("Clicked()", "FWColorPopup", this, "PopupColorWheel()");
219  }
220 }
221 
222 // ----
223 
225 {
226  fShowWheel = kTRUE;
227  UnmapWindow();
228 }
229 
231 {
232  ColorSelected(TColor::GetColor(pix));
233 }
234 
235 // ----
236 
237 void FWColorPopup::SetColors(const std::vector<Color_t>& colors, bool backgroundIsBlack)
238 {
239  fNumColors = colors.size();
240  for (UInt_t i = 0; i < colors.size() / 2; i++)
241  {
242  fFirstRow->AddColor(colors.at(i));
243  }
244  for (UInt_t i = colors.size() / 2; i < colors.size(); i++)
245  {
246  fSecondRow->AddColor(colors.at(i));
247  }
248  fFirstRow->SetBackgroundToBlack(backgroundIsBlack);
249  fSecondRow->SetBackgroundToBlack(backgroundIsBlack);
250 }
251 
252 void FWColorPopup::ResetColors(const std::vector<Color_t>& colors, bool backgroundIsBlack)
253 {
254  fNumColors=colors.size();
255  for (UInt_t i = 0; i < colors.size() / 2; i++)
256  {
257  fFirstRow->ResetColor(i,colors.at(i));
258  }
259  fFirstRow->SetBackgroundToBlack(backgroundIsBlack);
260  UInt_t index = 0;
261  for (UInt_t i = colors.size() / 2; i < colors.size(); i++, ++index)
262  {
263  fSecondRow->ResetColor(index,colors.at(i));
264  }
265  fSecondRow->SetBackgroundToBlack(backgroundIsBlack);
266 }
267 
268 void FWColorPopup::PlacePopup(Int_t x, Int_t y, UInt_t w, UInt_t h)
269 {
270  // Popup TGColorPopup at x,y position
271 
272  Int_t rx, ry;
273  UInt_t rw, rh;
274 
275  // Parent is root window for the popup:
276  gVirtualX->GetWindowSize(fParent->GetId(), rx, ry, rw, rh);
277 
278  if (x < 0) x = 0;
279  if (x + fWidth > rw) x = rw - fWidth;
280  if (y < 0) y = 0;
281  if (y + fHeight > rh) y = rh - fHeight;
282 
283  MoveResize(x, y, w, h);
284  MapSubwindows();
285  Layout();
286  MapRaised();
287 
288  // find out if this is necessary
289  gVirtualX->GrabPointer(fId, kButtonPressMask | kButtonReleaseMask | kPointerMotionMask, kNone, kNone);
290 
291  gClient->WaitForUnmap(this);
292  gVirtualX->GrabPointer(0, 0, 0, 0, kFALSE);
293 
294  if (fShowWheel)
295  {
296  Int_t retc;
297  Pixel_t pixel = TColor::Number2Pixel(fSelectedColor);
298 
299  TGColorDialog *cd = new TGColorDialog(gClient->GetDefaultRoot(), this, &retc, &pixel, kFALSE);
300 
301  cd->Connect("ColorSelected(Pixel_t)", "FWColorPopup", this, "ColorWheelSelected(Pixel_t");
302 
303  cd->MapWindow();
304  fClient->WaitForUnmap(cd);
305  cd->DeleteWindow();
306 
307  fShowWheel = kFALSE;
308  }
309 }
310 
311 void FWColorPopup::SetName(const char* iName)
312 {
313  fLabel->SetText(iName);
314 }
315 
316 void FWColorPopup::SetSelection(Color_t iColor)
317 {
320 }
321 
323 {
324  UnmapWindow();
325  fSelectedColor = ci;
326  Emit("ColorSelected(Color_t)", ci);
327 }
328 
329 
330 //------------------------------FWColorSelect------------------------------//
331 
332 FWColorSelect::FWColorSelect(const TGWindow *p, const char* label, Color_t index,
333  const FWColorManager* iManager, Int_t id) :
334  TGColorSelect(p, TColor::Number2Pixel(index), id),
335  fLabel(label),
336  fSelectedColor(index),
337  fFireworksPopup(nullptr),
338  fColorManager(iManager)
339 {
340  std::vector<Color_t> colors;
342 
343  fFireworksPopup = new FWColorPopup(gClient->GetDefaultRoot(), fColor);
345  fFireworksPopup->Connect("ColorSelected(Color_t)","FWColorSelect", this, "SetColorByIndex(Color_t)");
346 
347  fColorManager->colorsHaveChanged_.connect(boost::bind(&FWColorSelect::UpdateColors,this));
348 }
349 
351 {
352  delete fFireworksPopup;
353 }
354 
356 {
357  TGFrame::HandleButton(event);
358  if (!IsEnabled()) return kTRUE;
359 
360  if (event->fCode != kButton1) return kFALSE;
361 
362  if ((event->fType == kButtonPress) && HasFocus()) WantFocus();
363 
364  if (event->fType == kButtonPress)
365  {
366  fPressPos.fX = fX;
367  fPressPos.fY = fY;
368 
369  if (fState != kButtonDown) {
370  fPrevState = fState;
371  SetState(kButtonDown);
372  }
373  }
374  else
375  {
376  if (fState != kButtonUp)
377  {
378  SetState(kButtonUp);
379 
380  // case when it was dragged during guibuilding
381  if ((fPressPos.fX != fX) || (fPressPos.fY != fY))
382  {
383  return kFALSE;
384  }
385 
386  Window_t wdummy;
387  Int_t ax, ay;
388 
389  std::vector<Color_t> colors;
391 
394 
395  gVirtualX->TranslateCoordinates(fId, gClient->GetDefaultRoot()->GetId(), 0, fHeight, ax, ay, wdummy);
396  fFireworksPopup->PlacePopup(ax, ay, fFireworksPopup->GetDefaultWidth(), fFireworksPopup->GetDefaultHeight());
397  }
398  }
399  return kTRUE;
400 }
401 
402 void
404 {
405  SetColorByIndex(iColor, kTRUE);
406 }
407 
408 void
409 FWColorSelect::SetColorByIndex(Color_t iColor, Bool_t iSendSignal)
410 {
411  fSelectedColor = iColor;
412  SetColor(TColor::Number2Pixel(iColor), kFALSE);
413  if (iSendSignal)
414  {
416  }
417 }
418 
420 {
421  SetColor(fSelectedColor, kFALSE);
422 }
423 
424 void
426 {
427  Emit("ColorChosen(Color_t)", iColor);
428 }
Int_t fSelectedIndex
Definition: FWColorSelect.h:46
void InitContent(const char *name, const std::vector< Color_t > &colors, bool backgroundIsBlack=true)
Bool_t fShowWheel
Definition: FWColorSelect.h:85
const double w
Definition: UKUtility.cc:23
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
void SetColorByIndex(Color_t iColor)
FWColorPopup * fFireworksPopup
static Bool_t HasFreePalette()
#define nullptr
void SetBackgroundToBlack(Bool_t)
void fillLimitedColors(std::vector< Color_t > &cv) const
FWColorRow * fFirstRow
Definition: FWColorSelect.h:82
void SetName(const char *iName) override
void DrawHighlight()
void ColorChosen(Color_t)
TGLabel * fLabel
Definition: FWColorSelect.h:83
Bool_t HandleButton(Event_t *event) override
char const * label
FWColorRow * fSecondRow
Definition: FWColorSelect.h:82
void PlacePopup(Int_t x, Int_t y, UInt_t w, UInt_t h)
vector< Color_t > colors
static Bool_t fgFreePalette
Definition: FWColorSelect.h:87
FWColorPopup(const TGWindow *p=nullptr, Color_t color=0)
void ColorSelected(Color_t)
Int_t fNumColors
Definition: FWColorSelect.h:84
BackgroundColorIndex backgroundColorIndex() const
void SetColor(Color_t)
FWColorSelect(const TGWindow *p, const char *label, Color_t colorIndex, const FWColorManager *, Int_t id)
FWCSelConstants
double f[11][100]
void ResetColors(const std::vector< Color_t > &colors, bool backgroundIsBlack=true)
void SetSelection(Color_t)
void ColorChanged(Color_t)
void ColorSelected(Color_t)
Color_t fSelectedColor
Color_t fSelectedColor
Definition: FWColorSelect.h:81
~FWColorRow() override
Bool_t HandleButton(Event_t *event) override
FWColorFrame(const TGWindow *p=nullptr, Color_t ci=0)
void SetColors(const std::vector< Pixel_t > &colors, bool backgroundIsBlack)
const FWColorManager * fColorManager
~FWColorSelect() override
Bool_t fBackgroundIsBlack
Definition: FWColorSelect.h:45
FWColorRow(const TGWindow *p=nullptr)
double b
Definition: hdecay.h:120
std::string fLabel
virtual void AddColor(Color_t color)
Definition: colors.py:1
void SetSelectedIndex(Int_t i)
Definition: FWColorSelect.h:64
void ResetColor(Int_t, Color_t)
void DoRedraw() override
Color_t fColor
Definition: FWColorSelect.h:19
Bool_t HandleButton(Event_t *event) override
void ColorWheelSelected(Pixel_t)
static void EnableFreePalette()
sigc::signal< void > colorsHaveChanged_
std::vector< FWColorFrame * > fCc
Definition: FWColorSelect.h:47
Int_t FindColorIndex(Color_t) const
void PopupColorWheel()
Definition: event.py:1
~FWColorPopup() override