Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TGView.cxx
Go to the documentation of this file.
1 // @(#)root/gui:$Id$
2 // Author: Fons Rademakers 30/6/2000
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 // TGView //
26 // //
27 // A TGView provides the infrastructure for text viewer and editor //
28 // widgets. It provides a canvas (TGViewFrame) and (optionally) a //
29 // vertical and horizontal scrollbar and methods for marking and //
30 // scrolling. //
31 // //
32 // The TGView (and derivatives) will generate the following //
33 // event messages: //
34 // kC_TEXTVIEW, kTXT_ISMARKED, widget id, [true|false] //
35 // kC_TEXTVIEW, kTXT_DATACHANGE, widget id, 0 //
36 // kC_TEXTVIEW, kTXT_CLICK2, widget id, position (y << 16) | x) //
37 // kC_TEXTVIEW, kTXT_CLICK3, widget id, position (y << 16) | x) //
38 // kC_TEXTVIEW, kTXT_F3, widget id, true //
39 // kC_TEXTVIEW, kTXT_OPEN, widget id, 0 //
40 // kC_TEXTVIEW, kTXT_CLOSE, widget id, 0 //
41 // kC_TEXTVIEW, kTXT_SAVE, widget id, 0 //
42 // //
43 //////////////////////////////////////////////////////////////////////////
44 
45 #include "TGView.h"
46 #include "TGScrollBar.h"
47 #include "TGResourcePool.h"
48 #include "TMath.h"
49 #include "KeySymbols.h"
50 #include "RConfigure.h"
51 
52 
53 
54 ClassImp(TGViewFrame);
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Create a editor frame.
58 
59 TGViewFrame::TGViewFrame(TGView *v, UInt_t w, UInt_t h, UInt_t options,
60  ULong_t back) :
61  TGCompositeFrame(v, w, h, options | kOwnBackground, back)
62 {
63  fView = v;
64 
65  SetBackgroundColor(back);
66 
67  gVirtualX->GrabButton(fId, kAnyButton, kAnyModifier,
68  kButtonPressMask | kButtonReleaseMask |
69  kButtonMotionMask, kNone, kNone);
70 
71  AddInput(kKeyPressMask | kEnterWindowMask | kLeaveWindowMask |
72  kFocusChangeMask);
73 
74  SetWindowAttributes_t wattr;
75  wattr.fMask = kWAWinGravity | kWABitGravity;
76  wattr.fBitGravity = 1; // NorthWestGravity
77  wattr.fWinGravity = 1;
78  gVirtualX->ChangeWindowAttributes(fId, &wattr);
79 
80  // guibuiding settings
81  fEditDisabled = kEditDisableGrab | kEditDisableKeyEnable | kEditDisableBtnEnable;
82 }
83 
84 
85 ClassImp(TGView);
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Create an editor view, containing an TGEditorFrame and (optionally)
89 /// a horizontal and vertical scrollbar.
90 
91 TGView::TGView(const TGWindow *p, UInt_t w, UInt_t h, Int_t id,
92  UInt_t xMargin, UInt_t yMargin, UInt_t options,
93  UInt_t sboptions, ULong_t back)
94  : TGCompositeFrame(p, w, h, options, GetDefaultFrameBackground())
95 {
96  fWidgetId = id;
97  fMsgWindow = p;
98  fWidgetFlags = kWidgetWantFocus;
99 
100  fXMargin = xMargin;
101  fYMargin = yMargin;
102  fScrollVal.fX = 1;
103  fScrollVal.fY = 1;
104  fExposedRegion.Empty();
105 
106  fClipboard = fClient->GetResourcePool()->GetClipboard();
107 
108  fCanvas = new TGViewFrame(this, 10, 10, kChildFrame | kOwnBackground, back);
109  AddFrame(fCanvas);
110 
111  if (!(sboptions & kNoHSB)) {
112  fHsb = new TGHScrollBar(this, 10, 10, kChildFrame);
113  AddFrame(fHsb);
114  fHsb->Associate(this);
115  } else {
116  fHsb = 0;
117  }
118 
119  if (!(sboptions & kNoVSB)) {
120  fVsb = new TGVScrollBar(this, 10, 10, kChildFrame);
121  AddFrame(fVsb);
122  fVsb->Associate(this);
123  } else {
124  fVsb = 0;
125  }
126 
127  fWhiteGC.SetGraphicsExposures(kTRUE);
128  fWhiteGC.SetBackground(back);
129 
130  // sets for guibuilding
131  if (fVsb) {
132  fVsb->SetEditDisabled(kEditDisableGrab | kEditDisableBtnEnable);
133  }
134  if (fHsb) {
135  fHsb->SetEditDisabled(kEditDisableGrab | kEditDisableBtnEnable);
136  }
137 
138  fEditDisabled = kEditDisableLayout;
139 
140  // layout manager is not used
141  delete fLayoutManager;
142  fLayoutManager = 0;
143 }
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// Delete view.
147 
148 TGView::~TGView()
149 {
150  if (!MustCleanup()) {
151  delete fCanvas;
152  delete fHsb;
153  delete fVsb;
154  }
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// Clear view.
159 
160 void TGView::Clear(Option_t *)
161 {
162  fScrolling = -1;
163 
164  fMousePos.fX = fMousePos.fY = -1;
165  fVisible.fX = fVisible.fY = 0;
166  UpdateBackgroundStart();
167  fVirtualSize = TGDimension(0, 0);
168 
169  gVirtualX->ClearArea(fCanvas->GetId(), 0, 0,
170  fCanvas->GetWidth(), fCanvas->GetHeight());
171  Layout();
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// Scroll view in specified direction to make newTop the visible location.
176 
177 void TGView::SetVisibleStart(Int_t newTop, Int_t direction)
178 {
179  if (direction == kHorizontal) {
180  if (newTop / fScrollVal.fX == fVisible.fX / fScrollVal.fX) {
181  return;
182  }
183  ScrollCanvas(newTop, kHorizontal);
184  } else {
185  if (newTop / fScrollVal.fY == fVisible.fY / fScrollVal.fY) {
186  return;
187  }
188  ScrollCanvas(newTop, kVertical);
189  }
190 }
191 
192 ////////////////////////////////////////////////////////////////////////////////
193 /// Draw region.
194 
195 void TGView::DrawRegion(Int_t, Int_t, UInt_t, UInt_t)
196 {
197  return;
198 }
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// update a part of view
202 
203 void TGView::UpdateRegion(Int_t x, Int_t y, UInt_t w, UInt_t h)
204 {
205  x = x < 0 ? 0 : x;
206  y = y < 0 ? 0 : y;
207 
208  w = x + w > fCanvas->GetWidth() ? fCanvas->GetWidth() - x : w;
209  h = y + h > fCanvas->GetHeight() ? fCanvas->GetHeight() - y : h;
210 
211  if (fExposedRegion.IsEmpty()) {
212  fExposedRegion.fX = x;
213  fExposedRegion.fY = y;
214  fExposedRegion.fW = w;
215  fExposedRegion.fH = h;
216  } else {
217  TGRectangle r(x, y, w, h);
218  fExposedRegion.Merge(r);
219  }
220 
221  fClient->NeedRedraw(this);
222 }
223 
224 ////////////////////////////////////////////////////////////////////////////////
225 /// set some gc values
226 
227 void TGView::UpdateBackgroundStart()
228 {
229  fWhiteGC.SetTileStipXOrigin(-fVisible.fX);
230  fWhiteGC.SetTileStipYOrigin(-fVisible.fY);
231 }
232 
233 ////////////////////////////////////////////////////////////////////////////////
234 /// handle button
235 
236 Bool_t TGView::HandleButton(Event_t *event)
237 {
238  if (event->fType == kButtonPress) {
239  int amount, ch;
240 
241  ch = fCanvas->GetHeight();
242 
243  if (fScrollVal.fY == 1) {
244  amount = fScrollVal.fY * TMath::Max(ch/6, 1);
245  } else {
246  amount = fScrollVal.fY * 5;
247  }
248 
249  if (event->fState & kKeyShiftMask) {
250  amount = fScrollVal.fY;
251  } else if (event->fState & kKeyControlMask) {
252  amount = ch - TMath::Max(ch / 20, 1);
253  }
254 
255  if (event->fCode == kButton4) {
256  ScrollDown(amount);
257  return kTRUE;
258  } else if (event->fCode == kButton5) {
259  ScrollUp(amount);
260  return kTRUE;
261  }
262  }
263  return kFALSE;
264 }
265 
266 ////////////////////////////////////////////////////////////////////////////////
267 /// redraw
268 
269 void TGView::DoRedraw()
270 {
271  DrawBorder();
272 
273  if (!fExposedRegion.IsEmpty()) {
274  DrawRegion(fExposedRegion.fX, fExposedRegion.fY,
275  fExposedRegion.fW, fExposedRegion.fH);
276  fExposedRegion.Empty();
277  }
278 }
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 /// Handle expose events.
282 
283 Bool_t TGView::HandleExpose(Event_t *event)
284 {
285  if (event->fWindow == fCanvas->GetId()) {
286 
287  TGPosition pos(event->fX, event->fY);
288  TGDimension dim(event->fWidth, event->fHeight);
289  TGRectangle rect(pos, dim);
290 
291  if (fExposedRegion.IsEmpty()) {
292  fExposedRegion = rect;
293  } else {
294  if (((!rect.fX && !fExposedRegion.fY) ||
295  (!rect.fY && !fExposedRegion.fX)) &&
296  ((rect.fX >= (int)fExposedRegion.fW) ||
297  (rect.fY >= (int)fExposedRegion.fH))) {
298  DrawRegion(rect.fX, rect.fY, rect.fW, rect.fY);
299  } else {
300  fExposedRegion.Merge(rect);
301  }
302  }
303 
304  fClient->NeedRedraw(this);
305  } else {
306  return TGCompositeFrame::HandleExpose(event);
307  }
308 
309  return kTRUE;
310 }
311 
312 ////////////////////////////////////////////////////////////////////////////////
313 /// Process scrollbar messages.
314 
315 Bool_t TGView::ProcessMessage(Long_t msg, Long_t parm1, Long_t)
316 {
317  switch(GET_MSG(msg)) {
318  case kC_HSCROLL:
319  switch(GET_SUBMSG(msg)) {
320  case kSB_SLIDERTRACK:
321  case kSB_SLIDERPOS:
322  SetVisibleStart(Int_t(parm1 * fScrollVal.fX), kHorizontal);
323  break;
324  }
325  break;
326 
327  case kC_VSCROLL:
328  switch(GET_SUBMSG(msg)) {
329  case kSB_SLIDERTRACK:
330  case kSB_SLIDERPOS:
331  SetVisibleStart(Int_t(parm1 * fScrollVal.fY), kVertical);
332  break;
333  }
334  break;
335 
336  default:
337  break;
338  }
339  return kTRUE;
340 }
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// layout view
344 
345 void TGView::Layout()
346 {
347  Bool_t need_vsb, need_hsb;
348  Int_t cw, ch;
349 
350  need_vsb = need_hsb = kFALSE;
351 
352  // test whether we need scrollbars
353  cw = fWidth - (fBorderWidth << 1) - fXMargin - 1;
354  ch = fHeight - (fBorderWidth << 1) - fYMargin - 1;
355 
356  fCanvas->SetWidth(cw);
357  fCanvas->SetHeight(ch);
358  ItemLayout();
359 
360  if ((Int_t)fVirtualSize.fWidth > cw) {
361  if (fHsb) {
362  need_hsb = kTRUE;
363  if (fVsb) ch -= fVsb->GetDefaultWidth();
364  if (ch < 0) ch = 0;
365  fCanvas->SetHeight(ch);
366  ItemLayout();
367  }
368  }
369 
370  if ((Int_t)fVirtualSize.fHeight > ch) {
371  if (fVsb) {
372  need_vsb = kTRUE;
373  if (fHsb) cw -= fHsb->GetDefaultHeight();
374  if (cw < 0) cw = 0;
375  fCanvas->SetWidth(cw);
376  ItemLayout();
377  }
378  }
379 
380  // re-check again (putting the scrollbar could have changed things)
381 
382  if ((Int_t)fVirtualSize.fWidth > cw) {
383  if (!need_hsb) {
384  need_hsb = kTRUE;
385  if (fVsb) ch -= fVsb->GetDefaultWidth();
386  if (ch < 0) ch = 0;
387  fCanvas->SetHeight(ch);
388  ItemLayout();
389  }
390  }
391 
392  if (fHsb) {
393  if (need_hsb) {
394  fHsb->MoveResize(fBorderWidth + fXMargin, ch + fBorderWidth + fYMargin,
395  cw, fHsb->GetDefaultHeight());
396  fHsb->MapRaised();
397  } else {
398  fHsb->UnmapWindow();
399  fHsb->SetPosition(0);
400  }
401  }
402 
403  if (fVsb) {
404  if (need_vsb) {
405  fVsb->MoveResize(cw + fBorderWidth + fXMargin, fBorderWidth + fYMargin,
406  fVsb->GetDefaultWidth(), ch);
407  fVsb->MapWindow();
408  } else {
409  fVsb->UnmapWindow();
410  fVsb->SetPosition(0);
411  }
412  }
413  fCanvas->MoveResize(fBorderWidth + fXMargin, fBorderWidth + fYMargin, cw, ch);
414 
415  if (fHsb) {
416  fHsb->SetRange(fVirtualSize.fWidth / fScrollVal.fX, fCanvas->GetWidth() / fScrollVal.fX);
417  }
418 
419  if (fVsb) {
420  fVsb->SetRange(fVirtualSize.fHeight / fScrollVal.fY, fCanvas->GetHeight() / fScrollVal.fY);
421  }
422 }
423 
424 ////////////////////////////////////////////////////////////////////////////////
425 /// Draw the border of the text edit widget.
426 
427 void TGView::DrawBorder()
428 {
429  switch (fOptions & (kSunkenFrame | kRaisedFrame | kDoubleBorder)) {
430  case kSunkenFrame | kDoubleBorder:
431  if (gClient->GetStyle() < 2) {
432  gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, fWidth-2, 0);
433  gVirtualX->DrawLine(fId, GetShadowGC()(), 0, 0, 0, fHeight-2);
434  gVirtualX->DrawLine(fId, GetBlackGC()(), 1, 1, fWidth-3, 1);
435  gVirtualX->DrawLine(fId, GetBlackGC()(), 1, 1, 1, fHeight-3);
436 
437  gVirtualX->DrawLine(fId, GetHilightGC()(), 0, fHeight-1, fWidth-1, fHeight-1);
438  gVirtualX->DrawLine(fId, GetHilightGC()(), fWidth-1, fHeight-1, fWidth-1, 0);
439  gVirtualX->DrawLine(fId, GetBckgndGC()(), 1, fHeight-2, fWidth-2, fHeight-2);
440  gVirtualX->DrawLine(fId, GetBckgndGC()(), fWidth-2, 1, fWidth-2, fHeight-2);
441  break;
442  }
443  default:
444  TGFrame::DrawBorder();
445  break;
446  }
447 }
448 
449 ////////////////////////////////////////////////////////////////////////////////
450 /// Scroll the canvas to pos.
451 
452 void TGView::ScrollToPosition(TGLongPosition pos)
453 {
454  if (pos.fX < 0) pos.fX = 0;
455  if (pos.fY < 0) pos.fY = 0;
456  if (pos.fX != fHsb->GetPosition()) fHsb->SetPosition(pos.fX / fScrollVal.fX);
457  if (pos.fY != fVsb->GetPosition()) fVsb->SetPosition(pos.fY / fScrollVal.fY);
458 }
459 
460 ////////////////////////////////////////////////////////////////////////////////
461 /// Scroll the canvas to new_top in the kVertical or kHorizontal direction.
462 
463 void TGView::ScrollCanvas(Int_t new_top, Int_t direction)
464 {
465  Point_t points[4];
466  Int_t xsrc, ysrc, xdest, ydest, cpyheight, cpywidth;
467 
468  if (new_top < 0) {
469  return;
470  }
471 
472  if (direction == kVertical) {
473  if (new_top == fVisible.fY) {
474  return;
475  }
476 
477  points[0].fX = points[3].fX = 0;
478  points[1].fX = points[2].fX = fCanvas->GetWidth();
479  xsrc = xdest = 0;
480  cpywidth = 0;
481  if (new_top < fVisible.fY) {
482  ysrc = 0;
483  ydest = Int_t(fVisible.fY - new_top);
484  cpyheight = ydest;
485  if (ydest > (Int_t)fCanvas->GetHeight()) {
486  ydest = fCanvas->GetHeight();
487  }
488 
489  points[1].fY = points[0].fY = 0;
490  points[3].fY = points[2].fY = ydest; // -1;
491  } else {
492  ydest = 0;
493  ysrc = Int_t(new_top - fVisible.fY);
494  cpyheight= ysrc;
495  if (ysrc > (Int_t)fCanvas->GetHeight()) {
496  ysrc = fCanvas->GetHeight();
497  }
498  points[1].fY = points[0].fY = fCanvas->GetHeight()-ysrc; // +1;
499  points[3].fY = points[2].fY = fCanvas->GetHeight();
500  }
501  fVisible.fY = new_top;
502 
503  if (fVisible.fY < 0) {
504  fVisible.fY = 0;
505  }
506  } else {
507  if (new_top == fVisible.fX) {
508  return;
509  }
510 
511  points[0].fY = points[1].fY = 0;
512  points[2].fY = points[3].fY = fCanvas->GetHeight();
513  ysrc = ydest = 0;
514  cpyheight = 0;
515 
516  if (new_top < fVisible.fX) {
517  xsrc = 0;
518  xdest = Int_t(fVisible.fX - new_top);
519  cpywidth = xdest;
520  if (xdest < 0) {
521  xdest = fCanvas->GetWidth();
522  }
523  points[0].fX = points[3].fX = 0;
524  points[1].fX = points[2].fX = xdest ; // -1;
525  } else {
526  xdest = 0;
527  xsrc = Int_t(new_top - fVisible.fX);
528  cpywidth = xsrc;
529  if (xsrc > (Int_t)fCanvas->GetWidth()) {
530  xsrc = fCanvas->GetWidth();
531  }
532  points[0].fX = points[3].fX = fCanvas->GetWidth()-xsrc; // +1;
533  points[1].fX = points[2].fX = fCanvas->GetWidth();
534  }
535  fVisible.fX = new_top;
536  if (fVisible.fX < 0) {
537  fVisible.fX = 0;
538  }
539  }
540 
541  UpdateBackgroundStart();
542 
543 #ifdef R__HAS_COCOA
544  //With QuartzView it's quite tough to copy window's pixels to window.
545  //TODO: non-optimal solution.
546  DrawRegion(0, 0, GetWidth(), GetHeight());
547 #else
548  // Copy the scrolled region to its new position
549  gVirtualX->CopyArea(fCanvas->GetId(), fCanvas->GetId(), fWhiteGC(),
550  xsrc, ysrc, fCanvas->GetWidth()-cpywidth,
551  fCanvas->GetHeight()-cpyheight, xdest, ydest);
552 
553  UInt_t xdiff = points[2].fX - points[0].fX;
554  UInt_t ydiff = points[2].fY - points[0].fY;
555 
556  // under windows we need to redraw larger area (why?)
557 #ifdef WIN32
558  xdiff = xdiff << 1;
559  ydiff = ydiff << 1;
560 #endif
561 
562  DrawRegion(points[0].fX, points[0].fY, xdiff, ydiff);
563 #endif
564 }
565 
566 ////////////////////////////////////////////////////////////////////////////////
567 /// Change background color of the canvas frame.
568 
569 void TGView::ChangeBackground(Pixel_t col)
570 {
571  fCanvas->SetBackgroundColor(col);
572  fWhiteGC.SetBackground(col);
573  fWhiteGC.SetForeground(col);
574  DrawRegion(0, 0, fCanvas->GetWidth(), fCanvas->GetHeight());
575 }
576 
577 ////////////////////////////////////////////////////////////////////////////////
578 /// Set background color of the canvas frame.
579 
580 void TGView::SetBackgroundColor(Pixel_t col)
581 {
582  fCanvas->SetBackgroundColor(col);
583  fWhiteGC.SetBackground(col);
584  fWhiteGC.SetForeground(col);
585 }
586 
587 ////////////////////////////////////////////////////////////////////////////////
588 /// Set backgound pixmap
589 
590 void TGView::SetBackgroundPixmap(Pixmap_t p)
591 {
592  fCanvas->SetBackgroundPixmap(p);
593 }