Logo ROOT  
Reference Guide
TGWindow.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 28/12/97
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, 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  This source is based on Xclass95, a Win95-looking GUI toolkit.
14  Copyright (C) 1996, 1997 David Barth, Ricky Ralston, Hector Peraza.
15 
16  Xclass95 is free software; you can redistribute it and/or
17  modify it under the terms of the GNU Library General Public
18  License as published by the Free Software Foundation; either
19  version 2 of the License, or (at your option) any later version.
20 
21 **************************************************************************/
22 
23 //////////////////////////////////////////////////////////////////////////
24 // //
25 // TGWindow //
26 // //
27 // ROOT GUI Window base class. //
28 // //
29 //////////////////////////////////////////////////////////////////////////
30 
31 #include "TGWindow.h"
32 #include <iostream>
33 #include "TVirtualX.h"
34 #include "TApplication.h"
35 #include "TError.h"
36 #include "TSystem.h"
37 
40 
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Create a new window. Parent p must exist otherwise the root window
45 /// is taken as parent. No arguments specified results in values from
46 /// parent to be taken (or defaults).
47 
49  UInt_t border, Int_t depth, UInt_t clss, void *visual,
50  SetWindowAttributes_t *attr, UInt_t wtype)
51 {
52  UInt_t type = wtype;
53  fId = 0;
54  fParent = 0;
56 
57  if (!p && !gClient && !gApplication) {
58  ::Error("TGWindow::TGWindow",
59  "gClient and gApplication are nullptr!\n"
60  "Please add a TApplication instance in the main() function of your application\n");
61  gSystem->Exit(1);
62  }
63 
64  if (!p && gClient) {
65  p = gClient->GetRoot();
66  }
67 
68  if (p) {
69  fClient = p->fClient;
70  if (fClient->IsEditable()) type = wtype & ~1;
71 
72  fParent = p;
73  if (fParent && fParent->IsMapSubwindows()) {
74  fId = gVirtualX->CreateWindow(fParent->fId, x, y,
75  TMath::Max(w, (UInt_t) 1),
76  TMath::Max(h, (UInt_t) 1), border,
77  depth, clss, visual, attr, type);
78  fClient->RegisterWindow(this);
79  }
80 
81  // name will be used in SavePrimitive methods
82  fgCounter++;
83  fName = "frame";
84  fName += fgCounter;
85  }
86  fEditDisabled = (fId != gVirtualX->GetDefaultRootWindow()) && fParent ?
88 
89  // add protection for the root window on Cocoa (MacOS X)
90  if (fClient && fClient->GetDefaultRoot())
91  SetWindowName();
92 }
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Create a copy of a window.
96 
98 {
99  fClient = c;
100  fId = id;
101  fParent = parent;
102  fClient->RegisterWindow(this);
104  fEditDisabled = (fId != gVirtualX->GetDefaultRootWindow()) && fParent ?
106 
107  // name used in SavePrimitive methods
108  fgCounter++;
109  fName = "frame";
110  fName += fgCounter;
111 }
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Window destructor. Unregisters the window.
115 
117 {
118  if (fClient) {
119  if (fParent == fClient->GetDefaultRoot())
120  DestroyWindow();
121  fClient->UnregisterWindow(this);
122  }
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Set window name.
127 
128 void TGWindow::SetWindowName(const char *name)
129 {
130 #ifdef R__MACOSX
131  // MacOS fails to find the drawable of GetDefaultRootWindow(), only subsequent
132  // windows ids can be found. See discussion here:
133  // https://github.com/root-project/root/pull/6757#discussion_r518776154
134  if (fId == gVirtualX->GetDefaultRootWindow())
135  return;
136 #endif
137 
138  if (!name && gDebug > 0) {
139  // set default frame names only when in debug mode
140  TString wname = ClassName();
141  wname += "::" + fName;
142  gVirtualX->SetWindowName(fId, (char *)wname.Data());
143  } else {
144  gVirtualX->SetWindowName(fId, (char *)name);
145  }
146 }
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Returns top level main frame.
150 
152 {
153  return ((fParent == 0) || (fParent == fClient->GetDefaultRoot())) ? this : fParent->GetMainFrame();
154 }
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// map window
158 
160 {
161  gVirtualX->MapWindow(fId);
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// map sub windows
166 
168 {
169  gVirtualX->MapSubwindows(fId);
170 }
171 
172 ////////////////////////////////////////////////////////////////////////////////
173 /// map raised
174 
176 {
177  gVirtualX->MapRaised(fId);
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// unmap window
182 
184 {
185  gVirtualX->UnmapWindow(fId);
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// destroy window
190 
192 {
193  gVirtualX->DestroyWindow(fId);
194 }
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// destroy sub windows
198 
200 {
201  gVirtualX->DestroySubwindows(fId);
202 }
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// raise window
206 
208 {
209  gVirtualX->RaiseWindow(fId);
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// lower window
214 
216 {
217  gVirtualX->LowerWindow(fId);
218 }
219 
220 ////////////////////////////////////////////////////////////////////////////////
221 /// iconify window
222 
224 {
225  gVirtualX->IconifyWindow(fId);
226 }
227 
228 ////////////////////////////////////////////////////////////////////////////////
229 /// request focus
230 
232 {
233  gVirtualX->SetInputFocus(fId);
234 }
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 /// set background color
238 
240 {
241  gVirtualX->SetWindowBackground(fId, color);
242 }
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /// set background pixmap
246 
248 {
249  gVirtualX->SetWindowBackgroundPixmap(fId, pixmap);
250 }
251 
252 ////////////////////////////////////////////////////////////////////////////////
253 /// Reparent window, make p the new parent and position the window at
254 /// position (x,y) in new parent.
255 
257 {
258  if (p == fParent) return;
259 
260  if (p) {
261  gVirtualX->ReparentWindow(fId, p->GetId(), x, y);
262  gVirtualX->Update(1);
263  }
264  fParent = p;
265 }
266 
267 ////////////////////////////////////////////////////////////////////////////////
268 /// Move the window.
269 
271 {
272  gVirtualX->MoveWindow(fId, x, y);
273 }
274 
275 ////////////////////////////////////////////////////////////////////////////////
276 /// Resize the window.
277 
279 {
280  gVirtualX->ResizeWindow(fId, TMath::Max(w, (UInt_t)1), TMath::Max(h, (UInt_t)1));
281 }
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 /// Move and resize the window.
285 
287 {
288  gVirtualX->MoveResizeWindow(fId, x, y, TMath::Max(w, (UInt_t)1), TMath::Max(h, (UInt_t)1));
289 }
290 
291 ////////////////////////////////////////////////////////////////////////////////
292 /// Returns kTRUE if window is mapped on screen, kFALSE otherwise.
293 
295 {
296  WindowAttributes_t attr;
297 
298  gVirtualX->GetWindowAttributes(fId, attr);
299  return (attr.fMapState != kIsUnmapped);
300 }
301 
302 ////////////////////////////////////////////////////////////////////////////////
303 /// Print window id.
304 /// If option is "tree" - print all parent windows tree
305 
306 void TGWindow::Print(Option_t *option) const
307 {
308  TString opt = option;
309 
310  if (opt.Contains("tree")) {
311 
312  const TGWindow *parent = fParent;
313  std::cout << ClassName() << ":\t" << fId << std::endl;
314 
315  while (parent && (parent != fClient->GetDefaultRoot())) {
316  std::cout << "\t" << parent->ClassName() << ":\t" << parent->GetId() << std::endl;
317  parent = parent->GetParent();
318  }
319  } else {
320  std::cout << ClassName() << ":\t" << fId << std::endl;
321  }
322 }
323 
324 ////////////////////////////////////////////////////////////////////////////////
325 /// Return global window counter (total number of created windows).
326 
328 {
329  return fgCounter;
330 }
331 
332 ////////////////////////////////////////////////////////////////////////////////
333 /// Return unique name, used in SavePrimitive methods.
334 
335 const char *TGWindow::GetName()const
336 {
337  TGWindow *w = (TGWindow*)this;
338 
339  if (fName.BeginsWith("frame")) {
340  TString cname = ClassName();
341  if (cname.BeginsWith("TGed"))
342  cname.Replace(0, 1, 'f');
343  else if (cname.BeginsWith("TG"))
344  cname.Replace(0,2,'f');
345  else
346  cname.Replace(0, 1, 'f');
347  w->fName.Remove(0,5);
348  w->fName = cname + w->fName;
349  }
350 
351  if (w->fName.Contains(" "))
352  w->fName.ReplaceAll(" ", "");
353  if (w->fName.Contains(":"))
354  w->fName.ReplaceAll(":", "");
355 
356  return fName.Data();
357 }
TGWindow::SetWindowName
virtual void SetWindowName(const char *name=0)
Set window name.
Definition: TGWindow.cxx:128
TGWindow::fNeedRedraw
Bool_t fNeedRedraw
Definition: TGWindow.h:37
c
#define c(i)
Definition: RSha256.hxx:101
TGClient::GetDefaultRoot
const TGWindow * GetDefaultRoot() const
Returns the root (i.e.
Definition: TGClient.cxx:235
TGWindow
Definition: TGWindow.h:31
TGClient::RegisterWindow
void RegisterWindow(TGWindow *w)
Add a TGWindow to the clients list of windows.
Definition: TGClient.cxx:513
Pixmap_t
Handle_t Pixmap_t
Pixmap handle.
Definition: GuiTypes.h:30
TGWindow::fgCounter
static Int_t fgCounter
Definition: TGWindow.h:39
Option_t
const char Option_t
Definition: RtypesCore.h:66
TMath::Max
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
TGWindow::RequestFocus
virtual void RequestFocus()
request focus
Definition: TGWindow.cxx:231
kIsUnmapped
@ kIsUnmapped
Definition: GuiTypes.h:46
TGWindow::RaiseWindow
virtual void RaiseWindow()
raise window
Definition: TGWindow.cxx:207
gVirtualX
#define gVirtualX
Definition: TVirtualX.h:338
TString::Data
const char * Data() const
Definition: TString.h:369
Pixel_t
ULong_t Pixel_t
Pixel value.
Definition: GuiTypes.h:40
TGWindow::~TGWindow
virtual ~TGWindow()
Window destructor. Unregisters the window.
Definition: TGWindow.cxx:116
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TString::Replace
TString & Replace(Ssiz_t pos, Ssiz_t n, const char *s)
Definition: TString.h:682
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TGWindow::kEditDisable
@ kEditDisable
Definition: TGWindow.h:58
TGWindow::TGWindow
TGWindow(Window_t id)
Definition: TGWindow.h:42
WindowAttributes_t
Window attributes that can be inquired.
Definition: GuiTypes.h:114
TGWindow::MapRaised
virtual void MapRaised()
map raised
Definition: TGWindow.cxx:175
TGClient::IsEditable
Bool_t IsEditable() const
Definition: TGClient.h:98
Int_t
int Int_t
Definition: RtypesCore.h:45
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
x
Double_t x[n]
Definition: legend1.C:17
TVirtualX.h
TGWindow::SetBackgroundPixmap
virtual void SetBackgroundPixmap(Pixmap_t pixmap)
set background pixmap
Definition: TGWindow.cxx:247
TGWindow::IsMapSubwindows
virtual Bool_t IsMapSubwindows() const
Definition: TGWindow.h:125
TString
Basic string class.
Definition: TString.h:136
TGObject::GetId
Handle_t GetId() const
Definition: TGObject.h:47
TGWindow.h
bool
TString::ReplaceAll
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
id
XFontStruct * id
Definition: TGX11.cxx:109
TGWindow::DestroyWindow
virtual void DestroyWindow()
destroy window
Definition: TGWindow.cxx:191
gClient
#define gClient
Definition: TGClient.h:166
TApplication.h
TGWindow::ReparentWindow
virtual void ReparentWindow(const TGWindow *p, Int_t x=0, Int_t y=0)
Reparent window, make p the new parent and position the window at position (x,y) in new parent.
Definition: TGWindow.cxx:256
TSystem.h
h
#define h(i)
Definition: RSha256.hxx:106
TGWindow::GetName
virtual const char * GetName() const
Return unique name, used in SavePrimitive methods.
Definition: TGWindow.cxx:335
TGObject::fId
Handle_t fId
Definition: TGObject.h:36
TString::Remove
TString & Remove(Ssiz_t pos)
Definition: TString.h:673
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TGWindow::Print
virtual void Print(Option_t *option="") const
Print window id.
Definition: TGWindow.cxx:306
gApplication
R__EXTERN TApplication * gApplication
Definition: TApplication.h:166
gDebug
Int_t gDebug
Definition: TROOT.cxx:590
Window_t
Handle_t Window_t
Window handle.
Definition: GuiTypes.h:29
TString::BeginsWith
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
TGWindow::MapSubwindows
virtual void MapSubwindows()
map sub windows
Definition: TGWindow.cxx:167
y
Double_t y[n]
Definition: legend1.C:17
TGWindow::SetBackgroundColor
virtual void SetBackgroundColor(Pixel_t color)
set background color
Definition: TGWindow.cxx:239
unsigned int
TGWindow::MapWindow
virtual void MapWindow()
map window
Definition: TGWindow.cxx:159
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TGWindow::GetParent
const TGWindow * GetParent() const
Definition: TGWindow.h:84
TGWindow::LowerWindow
virtual void LowerWindow()
lower window
Definition: TGWindow.cxx:215
TSystem::Exit
virtual void Exit(int code, Bool_t mode=kTRUE)
Exit the application.
Definition: TSystem.cxx:717
TGObject::fClient
TGClient * fClient
Definition: TGObject.h:37
TGWindow::GetMainFrame
virtual const TGWindow * GetMainFrame() const
Returns top level main frame.
Definition: TGWindow.cxx:151
TGWindow::Resize
virtual void Resize(UInt_t w, UInt_t h)
Resize the window.
Definition: TGWindow.cxx:278
TGWindow::IconifyWindow
virtual void IconifyWindow()
iconify window
Definition: TGWindow.cxx:223
TGWindow::Move
virtual void Move(Int_t x, Int_t y)
Move the window.
Definition: TGWindow.cxx:270
name
char name[80]
Definition: TGX11.cxx:110
TGWindow::GetCounter
static Int_t GetCounter()
Return global window counter (total number of created windows).
Definition: TGWindow.cxx:327
TGUnknownWindowHandler
Definition: TGWindow.h:142
type
int type
Definition: TGX11.cxx:121
TGWindow::fEditDisabled
UInt_t fEditDisabled
Definition: TGWindow.h:40
WindowAttributes_t::fMapState
Int_t fMapState
kIsUnmapped, kIsUnviewable, kIsViewable
Definition: GuiTypes.h:130
TGWindow::UnmapWindow
virtual void UnmapWindow()
unmap window
Definition: TGWindow.cxx:183
TGWindow::DestroySubwindows
virtual void DestroySubwindows()
destroy sub windows
Definition: TGWindow.cxx:199
TObject::ClassName
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
TGClient
Definition: TGClient.h:46
TGWindow::MoveResize
virtual void MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h)
Move and resize the window.
Definition: TGWindow.cxx:286
TGWindow::fParent
const TGWindow * fParent
Definition: TGWindow.h:36
TGWindow::fName
TString fName
Definition: TGWindow.h:38
int
SetWindowAttributes_t
Attributes that can be used when creating or changing a window.
Definition: GuiTypes.h:93
TError.h
TGClient::UnregisterWindow
void UnregisterWindow(TGWindow *w)
Remove a TGWindow from the list of windows.
Definition: TGClient.cxx:524
TGWindow::IsMapped
virtual Bool_t IsMapped()
Returns kTRUE if window is mapped on screen, kFALSE otherwise.
Definition: TGWindow.cxx:294