Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TAttTextEditor.cxx
Go to the documentation of this file.
1 // @(#)root/ged:$Id$
2 // Author: Ilka Antcheva 11/05/04
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2002, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TAttTextEditor //
15 // //
16 // Implements GUI for editing text attributes. //
17 // color, font, font size and alignment //
18 // //
19 //////////////////////////////////////////////////////////////////////////
20 //Begin_Html
21 /*
22 <img src="gif/TAttTextEditor.gif">
23 */
24 //End_Html
25 
26 
27 #include "TAttTextEditor.h"
28 #include "TGedEditor.h"
29 #include "TGColorSelect.h"
30 #include "TGComboBox.h"
31 #include "TColor.h"
32 #include "TPaveLabel.h"
33 #include "TVirtualPad.h"
34 #include "TGLabel.h"
35 #include "TGNumberEntry.h"
36 #include "TPad.h"
37 #include "TCanvas.h"
38 #include "TROOT.h"
39 
40 ClassImp(TAttTextEditor);
41 
42 enum ETextWid {
43  kCOLOR,
44  kFONT_SIZE,
45  kFONT_STYLE,
46  kFONT_ALIGN,
47  kALPHA,
48  kALPHAFIELD
49 };
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Constructor of text attributes GUI.
53 
54 TAttTextEditor::TAttTextEditor(const TGWindow *p, Int_t width,
55  Int_t height, UInt_t options, Pixel_t back)
56  : TGedFrame(p, width, height, options | kVerticalFrame, back)
57 {
58  fPriority = 3;
59 
60  fAttText = 0;
61 
62  MakeTitle("Text");
63 
64  TGCompositeFrame *f2 = new TGCompositeFrame(this, 80, 20, kHorizontalFrame);
65  fColorSelect = new TGColorSelect(f2, 0, kCOLOR);
66  f2->AddFrame(fColorSelect, new TGLayoutHints(kLHintsLeft, 1, 1, 1, 1));
67  fColorSelect->Associate(this);
68  fSizeCombo = BuildFontSizeComboBox(f2, kFONT_SIZE);
69  f2->AddFrame(fSizeCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
70  fSizeCombo->Resize(91, 20);
71  fSizeCombo->Associate(this);
72  AddFrame(f2, new TGLayoutHints(kLHintsTop, 1, 1, 0, 0));
73  fTypeCombo = new TGFontTypeComboBox(this, kFONT_STYLE);
74  fTypeCombo->Resize(137, 20);
75  AddFrame(fTypeCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
76  fAlignCombo = BuildTextAlignComboBox(this, kFONT_ALIGN);
77  fAlignCombo->Resize(137, 20);
78  AddFrame(fAlignCombo, new TGLayoutHints(kLHintsLeft, 3, 1, 1, 1));
79 
80  TGLabel *AlphaLabel = new TGLabel(this,"Opacity");
81  AddFrame(AlphaLabel,
82  new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
83  TGHorizontalFrame *f2a = new TGHorizontalFrame(this);
84  fAlpha = new TGHSlider(f2a,100,kSlider2|kScaleNo,kALPHA);
85  fAlpha->SetRange(0,1000);
86  f2a->AddFrame(fAlpha,new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
87  fAlphaField = new TGNumberEntryField(f2a, kALPHAFIELD, 0,
88  TGNumberFormat::kNESReal,
89  TGNumberFormat::kNEANonNegative);
90  fAlphaField->Resize(40,20);
91  if (!TCanvas::SupportAlpha()) {
92  fAlpha->SetEnabled(kFALSE);
93  AlphaLabel->Disable(kTRUE);
94  fAlphaField->SetEnabled(kFALSE);
95  }
96  f2a->AddFrame(fAlphaField,new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
97  AddFrame(f2a, new TGLayoutHints(kLHintsLeft | kLHintsCenterY));
98 }
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Destructor of text editor.
102 
103 TAttTextEditor::~TAttTextEditor()
104 {
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Connect signals to slots.
109 
110 void TAttTextEditor::ConnectSignals2Slots()
111 {
112  fAlpha->Connect("Released()","TAttTextEditor", this, "DoAlpha()");
113  fAlpha->Connect("PositionChanged(Int_t)","TAttTextEditor", this, "DoLiveAlpha(Int_t)");
114  fAlphaField->Connect("ReturnPressed()","TAttTextEditor", this, "DoAlphaField()");
115  fAlpha->Connect("Pressed()","TAttTextEditor", this, "GetCurAlpha()");
116  fColorSelect->Connect("ColorSelected(Pixel_t)", "TAttTextEditor", this, "DoTextColor(Pixel_t)");
117  fColorSelect->Connect("AlphaColorSelected(ULong_t)", "TAttTextEditor", this, "DoTextAlphaColor(ULong_t)");
118  fInit = kFALSE;
119 }
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Pick up the values of used text attributes.
123 
124 void TAttTextEditor::SetModel(TObject* obj)
125 {
126  TAttText *atttext = dynamic_cast<TAttText *>(obj);
127  if (!atttext) return;
128 
129  fAttText = atttext;
130  fAvoidSignal = kTRUE;
131 
132  fTypeCombo->Select(fAttText->GetTextFont() / 10);
133 
134  Float_t s = fAttText->GetTextSize();
135  Float_t dy;
136 
137  if (obj->InheritsFrom(TPaveLabel::Class())) {
138  TBox *pl = (TBox*)obj;
139  dy = s * (pl->GetY2() - pl->GetY1());
140  }
141  else
142  dy = s * (fGedEditor->GetPad()->GetY2() - fGedEditor->GetPad()->GetY1());
143 
144  Int_t size = fGedEditor->GetPad()->YtoPixel(0.0) - fGedEditor->GetPad()->YtoPixel(dy);
145  if (size > 50) size = 50;
146  if (size < 0) size = 0;
147  fSizeCombo->Select(size, kFALSE);
148 
149  fAlignCombo->Select(fAttText->GetTextAlign(), kFALSE);
150 
151  Color_t c = fAttText->GetTextColor();
152  Pixel_t p = TColor::Number2Pixel(c);
153  fColorSelect->SetColor(p, kFALSE);
154 
155  if (fInit) ConnectSignals2Slots();
156  fAvoidSignal = kFALSE;
157 
158  if (TColor *color = gROOT->GetColor(fAttText->GetTextColor())) {
159  fAlpha->SetPosition((Int_t)color->GetAlpha()*1000);
160  fAlphaField->SetNumber(color->GetAlpha());
161  }
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// Slot connected to the marker color.
166 
167 void TAttTextEditor::DoTextColor(Pixel_t color)
168 {
169  if (fAvoidSignal) return;
170  fAttText->SetTextColor(TColor::GetColor(color));
171 
172  if (TColor *tcolor = gROOT->GetColor(TColor::GetColor(color))) {
173  fAlpha->SetPosition((Int_t)(tcolor->GetAlpha()*1000));
174  fAlphaField->SetNumber(tcolor->GetAlpha());
175  }
176 
177  Update();
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Slot connected to the color with alpha.
182 
183 void TAttTextEditor::DoTextAlphaColor(ULong_t p)
184 {
185  TColor *color = (TColor *)p;
186 
187  if (fAvoidSignal) return;
188  fAttText->SetTextColor(color->GetNumber());
189  fAlpha->SetPosition((Int_t)(color->GetAlpha()*1000));
190  fAlphaField->SetNumber(color->GetAlpha());
191 
192  Update();
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Process message.
197 
198 Bool_t TAttTextEditor::ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
199 {
200  if(!fGedEditor || !fGedEditor->GetModel()) return kTRUE;
201 
202  Bool_t b = kFALSE;
203 
204  if (GET_MSG(msg) == kC_COLORSEL && GET_SUBMSG(msg) == kCOL_SELCHANGED) {
205  if (parm1 != 0) fAttText->SetTextColor(TColor::GetColor(parm2));
206  b = kTRUE;
207  // SendMessage(fMsgWindow, msg, parm1, parm2);
208  }
209 
210  if (GET_MSG(msg) == kC_COMMAND && GET_SUBMSG(msg) == kCM_COMBOBOX) {
211 
212  if (parm1 == kFONT_SIZE) {
213  TVirtualPad* pad = fGedEditor->GetPad();
214  Float_t dy = pad->AbsPixeltoY(0) - pad->AbsPixeltoY(parm2);
215  Float_t textSize;
216 
217  if (fGedEditor->GetModel()->InheritsFrom(TPaveLabel::Class())) {
218  TBox *pl = (TBox*)fGedEditor->GetModel();
219  textSize = dy/(pl->GetY2() - pl->GetY1());
220  }
221  else
222  textSize = dy/(pad->GetY2() - pad->GetY1());
223 
224  fAttText->SetTextSize(textSize);
225  b = kTRUE;
226  } else if (parm1 == kFONT_STYLE) {
227  Int_t fontPrec = fAttText->GetTextFont()%10;
228  fAttText->SetTextFont(parm2 * 10 + fontPrec);
229  b = kTRUE;
230  } else if (parm1 == kFONT_ALIGN) {
231  fAttText->SetTextAlign(parm2);
232  b = kTRUE;
233  }
234  }
235 
236  if (b && !fAvoidSignal) Update();
237 
238  return kTRUE;
239 }
240 
241 ////////////////////////////////////////////////////////////////////////////////
242 /// Create text size combo box.
243 
244 TGComboBox* TAttTextEditor::BuildFontSizeComboBox(TGFrame* parent, Int_t id)
245 {
246  char a[100];
247  TGComboBox *c = new TGComboBox(parent, id);
248 
249  c->AddEntry("Default", 0);
250  for (int i = 1; i <= 50; i++) {
251  snprintf(a, 99, "%d", i);
252  c->AddEntry(a, i);
253  }
254 
255  return c;
256 }
257 
258 ////////////////////////////////////////////////////////////////////////////////
259 /// Create text align combo box.
260 
261 TGComboBox* TAttTextEditor::BuildTextAlignComboBox(TGFrame* parent, Int_t id)
262 {
263  TGComboBox *c = new TGComboBox(parent, id);
264 
265  c->AddEntry("11 Bottom, Left", 11);
266  c->AddEntry("21 Bottom, Middle", 21);
267  c->AddEntry("31 Bottom, Right", 31);
268  c->AddEntry("12 Middle, Left", 12);
269  c->AddEntry("22 Middle, Middle", 22);
270  c->AddEntry("32 Middle, Right", 32);
271  c->AddEntry("13 Top, Left", 13);
272  c->AddEntry("23 Top, Middle", 23);
273  c->AddEntry("33 Top, Right", 33);
274 
275  return c;
276 }
277 
278 ////////////////////////////////////////////////////////////////////////////////
279 /// Slot to set the alpha value from the entry field.
280 
281 void TAttTextEditor::DoAlphaField()
282 {
283  if (fAvoidSignal) return;
284 
285  if (TColor *color = gROOT->GetColor(fAttText->GetTextColor())) {
286  color->SetAlpha((Float_t)fAlphaField->GetNumber());
287  fAlpha->SetPosition((Int_t)(fAlphaField->GetNumber()*1000));
288  }
289  Update();
290 }
291 
292 ////////////////////////////////////////////////////////////////////////////////
293 /// Slot to set the alpha value
294 
295 void TAttTextEditor::DoAlpha()
296 {
297  if (fAvoidSignal) return;
298 
299  if (TColor *color = gROOT->GetColor(fAttText->GetTextColor())) {
300  color->SetAlpha((Float_t)fAlpha->GetPosition()/1000);
301  fAlphaField->SetNumber((Float_t)fAlpha->GetPosition()/1000);
302  }
303  Update();
304 }
305 
306 ////////////////////////////////////////////////////////////////////////////////
307 /// Slot to set alpha value online.
308 
309 void TAttTextEditor::DoLiveAlpha(Int_t a)
310 {
311  if (fAvoidSignal) return;
312  fAlphaField->SetNumber((Float_t)a/1000);
313 
314  if (TColor *color = gROOT->GetColor(fAttText->GetTextColor())) {
315  // In case the color is not transparent a new color is created.
316  if (color->GetAlpha() == 1.) {
317  fAttText->SetTextColor(TColor::GetColorTransparent(color->GetNumber(),0.99));
318  } else {
319  color->SetAlpha((Float_t)a/1000);
320  }
321  }
322  Update();
323 }
324 
325 ////////////////////////////////////////////////////////////////////////////////
326 /// Slot to update alpha value on click on Slider
327 
328 void TAttTextEditor::GetCurAlpha()
329 {
330  if (fAvoidSignal) return;
331 
332  if (TColor *color = gROOT->GetColor(fAttText->GetTextColor())) {
333  fAlpha->SetPosition((Int_t)(color->GetAlpha()*1000));
334  fAlphaField->SetNumber(color->GetAlpha());
335  }
336  Update();
337 }