Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 "Riostream.h"
33 #include "TApplication.h"
34 
35 ClassImp(TGWindow);
36 ClassImp(TGUnknownWindowHandler);
37 
38 Int_t TGWindow::fgCounter = 0;
39 
40 ////////////////////////////////////////////////////////////////////////////////
41 /// Create a new window. Parent p must exist otherwise the root window
42 /// is taken as parent. No arguments specified results in values from
43 /// parent to be taken (or defaults).
44 
45 TGWindow::TGWindow(const TGWindow *p, Int_t x, Int_t y, UInt_t w, UInt_t h,
46  UInt_t border, Int_t depth, UInt_t clss, void *visual,
47  SetWindowAttributes_t *attr, UInt_t wtype)
48 {
49  UInt_t type = wtype;
50  fId = 0;
51  fParent = 0;
52  fNeedRedraw = kFALSE;
53 
54  if (!p && gClient) {
55  p = gClient->GetRoot();
56  }
57 
58  if (p) {
59  fClient = p->fClient;
60  if (fClient->IsEditable()) type = wtype & ~1;
61 
62  fParent = p;
63  if (fParent && fParent->IsMapSubwindows()) {
64  fId = gVirtualX->CreateWindow(fParent->fId, x, y,
65  TMath::Max(w, (UInt_t) 1),
66  TMath::Max(h, (UInt_t) 1), border,
67  depth, clss, visual, attr, type);
68  fClient->RegisterWindow(this);
69  }
70 
71  // name will be used in SavePrimitive methods
72  fgCounter++;
73  fName = "frame";
74  fName += fgCounter;
75  }
76  fEditDisabled = (fId != gVirtualX->GetDefaultRootWindow()) && fParent ?
77  (fParent->fEditDisabled == kEditDisable) : 0;
78 
79  // add protection for the root window on Cocoa (MacOS X)
80  if (fClient && fClient->GetDefaultRoot())
81  SetWindowName();
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Create a copy of a window.
86 
87 TGWindow::TGWindow(TGClient *c, Window_t id, const TGWindow *parent)
88 {
89  fClient = c;
90  fId = id;
91  fParent = parent;
92  fClient->RegisterWindow(this);
93  fNeedRedraw = kFALSE;
94  fEditDisabled = (fId != gVirtualX->GetDefaultRootWindow()) && fParent ?
95  fParent->fEditDisabled : kFALSE;
96 
97  // name used in SavePrimitive methods
98  fgCounter++;
99  fName = "frame";
100  fName += fgCounter;
101 }
102 
103 ////////////////////////////////////////////////////////////////////////////////
104 /// Window destructor. Unregisters the window.
105 
106 TGWindow::~TGWindow()
107 {
108  if (fClient) {
109  if (fParent == fClient->GetDefaultRoot())
110  DestroyWindow();
111  fClient->UnregisterWindow(this);
112  }
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// Set window name.
117 
118 void TGWindow::SetWindowName(const char *name)
119 {
120  if (!name && gDebug > 0) {
121  // set default frame names only when in debug mode
122  TString wname = ClassName();
123  wname += "::" + fName;
124  gVirtualX->SetWindowName(fId, (char *)wname.Data());
125  } else {
126  gVirtualX->SetWindowName(fId, (char *)name);
127  }
128 }
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Returns top level main frame.
132 
133 const TGWindow *TGWindow::GetMainFrame() const
134 {
135  return ((fParent == 0) || (fParent == fClient->GetDefaultRoot())) ? this : fParent->GetMainFrame();
136 }
137 
138 ////////////////////////////////////////////////////////////////////////////////
139 /// Reparent window, make p the new parent and position the window at
140 /// position (x,y) in new parent.
141 
142 void TGWindow::ReparentWindow(const TGWindow *p, Int_t x, Int_t y)
143 {
144  if (p == fParent) return;
145 
146  if (p) {
147  gVirtualX->ReparentWindow(fId, p->GetId(), x, y);
148  gVirtualX->Update(1);
149  }
150  fParent = p;
151 }
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Move the window.
155 
156 void TGWindow::Move(Int_t x, Int_t y)
157 {
158  gVirtualX->MoveWindow(fId, x, y);
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /// Resize the window.
163 
164 void TGWindow::Resize(UInt_t w, UInt_t h)
165 {
166  gVirtualX->ResizeWindow(fId, TMath::Max(w, (UInt_t)1), TMath::Max(h, (UInt_t)1));
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Move and resize the window.
171 
172 void TGWindow::MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h)
173 {
174  gVirtualX->MoveResizeWindow(fId, x, y, TMath::Max(w, (UInt_t)1), TMath::Max(h, (UInt_t)1));
175 }
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Returns kTRUE if window is mapped on screen, kFALSE otherwise.
179 
180 Bool_t TGWindow::IsMapped()
181 {
182  WindowAttributes_t attr;
183 
184  gVirtualX->GetWindowAttributes(fId, attr);
185  return (attr.fMapState != kIsUnmapped);
186 }
187 
188 ////////////////////////////////////////////////////////////////////////////////
189 /// Print window id.
190 /// If option is "tree" - print all parent windows tree
191 
192 void TGWindow::Print(Option_t *option) const
193 {
194  TString opt = option;
195 
196  if (opt.Contains("tree")) {
197 
198  const TGWindow *parent = fParent;
199  std::cout << ClassName() << ":\t" << fId << std::endl;
200 
201  while (parent && (parent != fClient->GetDefaultRoot())) {
202  std::cout << "\t" << parent->ClassName() << ":\t" << parent->GetId() << std::endl;
203  parent = parent->GetParent();
204  }
205  } else {
206  std::cout << ClassName() << ":\t" << fId << std::endl;
207  }
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Return global window counter (total number of created windows).
212 
213 Int_t TGWindow::GetCounter()
214 {
215  return fgCounter;
216 }
217 
218 ////////////////////////////////////////////////////////////////////////////////
219 /// Return unique name, used in SavePrimitive methods.
220 
221 const char *TGWindow::GetName()const
222 {
223  TGWindow *w = (TGWindow*)this;
224 
225  if (fName.BeginsWith("frame")) {
226  TString cname = ClassName();
227  if (cname.BeginsWith("TGed"))
228  cname.Replace(0, 1, 'f');
229  else if (cname.BeginsWith("TG"))
230  cname.Replace(0,2,'f');
231  else
232  cname.Replace(0, 1, 'f');
233  w->fName.Remove(0,5);
234  w->fName = cname + w->fName;
235  }
236 
237  if (w->fName.Contains(" "))
238  w->fName.ReplaceAll(" ", "");
239  if (w->fName.Contains(":"))
240  w->fName.ReplaceAll(":", "");
241 
242  return fName.Data();
243 }