49 const TGFont *TGTextView::fgDefaultFont = 0;
50 TGGC *TGTextView::fgDefaultGC = 0;
51 TGGC *TGTextView::fgDefaultSelectedGC = 0;
52 const TGGC *TGTextView::fgDefaultSelectedBackgroundGC = 0;
58 Bool_t TViewTimer::Notify()
60 fView->HandleTimer(
this);
71 void TGTextView::Init(ULong_t back)
74 fFont = GetDefaultFontStruct();
75 fNormGC = GetDefaultGC();
76 fSelGC = GetDefaultSelectedGC();
77 fSelbackGC = GetDefaultSelectedBackgroundGC();
79 fWhiteGC = *fClient->GetResourcePool()->GetDocumentBckgndGC();
80 fWhiteGC.SetGraphicsExposures(kTRUE);
81 fWhiteGC.SetBackground(back);
82 fWhiteGC.SetForeground(back);
92 fClipText =
new TGText();
94 gVirtualX->GetFontProperties(fFont, fMaxAscent, fMaxDescent);
95 fScrollVal.fY = fMaxAscent + fMaxDescent;
96 fScrollVal.fX = fMaxWidth = gVirtualX->TextWidth(fFont,
"@", 1);
98 fScrollTimer =
new TViewTimer(
this, 75);
99 gSystem->AddTimer(fScrollTimer);
102 fDNDTypeList =
new Atom_t[3];
103 fDNDTypeList[0] = gVirtualX->InternAtom(
"application/root", kFALSE);
104 fDNDTypeList[1] = gVirtualX->InternAtom(
"text/uri-list", kFALSE);
106 gVirtualX->SetDNDAware(fId, fDNDTypeList);
109 gVirtualX->ClearWindow(fCanvas->GetId());
116 TGTextView::TGTextView(
const TGWindow *parent, UInt_t w, UInt_t h, Int_t
id,
117 UInt_t sboptions, ULong_t back) :
118 TGView(parent, w, h, id, 3, 3, kSunkenFrame | kDoubleBorder, sboptions, back)
126 TGTextView::TGTextView(
const TGWindow *parent, UInt_t w, UInt_t h, TGText *text,
127 Int_t
id, UInt_t sboptions, ULong_t back) :
128 TGView(parent, w, h, id, 3, 3, kSunkenFrame | kDoubleBorder, sboptions, back)
131 TGLongPosition pos, srcStart, srcEnd;
133 srcStart.fX = srcStart.fY = 0;
134 srcEnd.fY = text->RowCount()-1;
135 srcEnd.fX = text->GetLineLength(srcEnd.fY)-1;
136 fText->InsText(pos, text, srcStart, srcEnd);
142 TGTextView::TGTextView(
const TGWindow *parent, UInt_t w, UInt_t h,
143 const char *
string, Int_t
id, UInt_t sboptions,
145 TGView(parent, w, h, id, 3, 3, kSunkenFrame | kDoubleBorder, sboptions, back)
150 fText->InsText(pos,
string);
156 TGTextView::~TGTextView()
161 delete [] fDNDTypeList;
167 void TGTextView::SetBackground(Pixel_t p)
169 fCanvas->SetBackgroundColor(p);
170 fWhiteGC.SetBackground(p);
171 fWhiteGC.SetForeground(p);
177 void TGTextView::SetSelectBack(Pixel_t p)
179 fSelbackGC.SetBackground(p);
180 fSelbackGC.SetForeground(p);
186 void TGTextView::SetSelectFore(Pixel_t p)
188 fSelGC.SetBackground(p);
189 fSelGC.SetForeground(p);
195 void TGTextView::SetText(TGText *text)
206 void TGTextView::AddText(TGText *text)
208 UInt_t h1 = (UInt_t)ToScrYCoord(fText->RowCount());
210 fText->AddText(text);
213 UInt_t h2 = (UInt_t)ToScrYCoord(fText->RowCount());
219 if (h2 < fCanvas->GetHeight()) {
220 UpdateRegion(0, h1, fCanvas->GetWidth(), h2 - h1);
227 void TGTextView::AddLine(
const char *
string)
229 UInt_t h1 = (UInt_t)ToScrYCoord(fText->RowCount());
234 UInt_t h2 = (UInt_t)ToScrYCoord(fText->RowCount());
239 if (h2 < fCanvas->GetHeight()) {
240 UpdateRegion(0, h1, fCanvas->GetWidth(), h2 - h1);
249 void TGTextView::AddLineFast(
const char *
string)
253 pos.fY = fText->RowCount();
254 fText->InsText(pos,
string);
260 void TGTextView::Update()
263 fExposedRegion.Empty();
264 UpdateRegion(0, 0, fCanvas->GetWidth(), fCanvas->GetHeight());
270 Long_t TGTextView::ReturnLongestLineWidth()
272 Long_t count = 0, longest = 0, width;
273 Long_t rows = fText->RowCount();
274 while (count < rows) {
275 width = ToScrXCoord(fText->GetLineLength(count), count) + fVisible.fX;
276 if (width > longest) {
288 Bool_t TGTextView::Search(
const char *
string, Bool_t direction, Bool_t caseSensitive)
290 TGLongPosition pos, pos2;
291 pos2.fX = pos2.fY = 0;
294 pos2.fX = fMarkedStart.fX;
295 pos2.fY = fMarkedStart.fY;
297 pos2.fX = fMarkedEnd.fX + 1;
298 pos2.fY = fMarkedEnd.fY;
301 if (!fText->Search(&pos, pos2,
string, direction, caseSensitive)) {
306 fMarkedStart.fY = fMarkedEnd.fY = pos.fY;
307 fMarkedStart.fX = pos.fX;
308 fMarkedEnd.fX = fMarkedStart.fX + strlen(
string) - 1;
309 pos.fY = ToObjYCoord(fVisible.fY);
310 if ((fMarkedStart.fY < pos.fY) ||
311 (ToScrYCoord(fMarkedStart.fY) >= (Int_t)fCanvas->GetHeight())) {
312 pos.fY = fMarkedStart.fY;
314 pos.fX = ToObjXCoord(fVisible.fX, pos.fY);
315 if ((fMarkedStart.fX < pos.fX) ||
316 (ToScrXCoord(fMarkedStart.fX, pos.fY) >= (Int_t)fCanvas->GetWidth())) {
317 pos.fX = fMarkedStart.fX;
320 SetVsbPosition((ToScrYCoord(pos.fY) + fVisible.fY)/fScrollVal.fY);
321 SetHsbPosition((ToScrXCoord(pos.fX, pos.fY) + fVisible.fX)/fScrollVal.fX);
322 UpdateRegion(0, (Int_t)ToScrYCoord(fMarkedStart.fY), fCanvas->GetWidth(),
323 UInt_t(ToScrYCoord(fMarkedEnd.fY+1) - ToScrYCoord(fMarkedEnd.fY)));
331 void TGTextView::SetFont(FontStruct_t font)
335 fNormGC.SetFont(gVirtualX->GetFontHandle(fFont));
336 fSelGC.SetFont(gVirtualX->GetFontHandle(fFont));
337 fClient->NeedRedraw(
this);
344 Long_t TGTextView::ToScrYCoord(Long_t yCoord)
346 if (yCoord * (fMaxAscent + fMaxDescent) <= 0) {
349 if (yCoord > fText->RowCount()) {
350 return fText->RowCount() * (fMaxAscent + fMaxDescent);
352 return yCoord * (fMaxAscent + fMaxDescent) - fVisible.fY;
358 Long_t TGTextView::ToScrXCoord(Long_t xCoord, Long_t line)
365 Long_t width = fText->GetLineLength(line);
366 if (xCoord <= 0 || pos.fY < 0 || width <= 0) {
369 if (xCoord > width) {
372 buffer = fText->GetLine(pos, xCoord);
373 width = gVirtualX->TextWidth(fFont, buffer, (Int_t)xCoord) - fVisible.fX;
382 Long_t TGTextView::ToObjYCoord(Long_t yCoord)
384 return yCoord / (fMaxAscent + fMaxDescent);
390 Long_t TGTextView::ToObjXCoord(Long_t xCoord, Long_t line)
393 char *buffer, *travelBuffer;
396 if (line < 0 || line >= fText->RowCount()) {
400 Long_t len = fText->GetLineLength(line);
403 if (len <= 0 || xCoord < 0) {
407 Long_t viscoord = xCoord;
408 buffer = fText->GetLine(pos, len);
409 if (!buffer)
return 0;
410 travelBuffer = buffer;
411 charBuffer = *travelBuffer++;
412 int cw = gVirtualX->TextWidth(fFont, &charBuffer, 1);
414 while (viscoord - cw >= 0 && pos.fX < len) {
417 charBuffer = *travelBuffer++;
418 cw = gVirtualX->TextWidth(fFont, &charBuffer, 1);
428 void TGTextView::Clear(Option_t *)
433 fMarkedStart.fX = fMarkedStart.fY = 0;
434 fMarkedEnd.fX = fMarkedEnd.fY = 0;
438 fText =
new TGText();
440 SendMessage(fMsgWindow, MK_MSG(kC_TEXTVIEW, kTXT_ISMARKED), fWidgetId, kFALSE);
442 gVirtualX->ClearWindow(fCanvas->GetId());
443 SendMessage(fMsgWindow, MK_MSG(kC_TEXTVIEW, kTXT_DATACHANGE), fWidgetId, 0);
452 Bool_t TGTextView::LoadFile(
const char *filename, Long_t startpos, Long_t length)
455 if (!(fp = fopen(filename,
"r")))
461 fText->Load(filename, startpos, length);
469 Bool_t TGTextView::LoadBuffer(
const char *txtbuf)
471 if (!txtbuf || !txtbuf[0]) {
476 fText->LoadBuffer(txtbuf);
484 Bool_t TGTextView::Copy()
486 TGLongPosition insPos, startPos, endPos;
492 fClipText =
new TGText();
493 insPos.fY = insPos.fX = 0;
494 startPos.fX = fMarkedStart.fX;
495 startPos.fY = fMarkedStart.fY;
496 endPos.fX = fMarkedEnd.fX-1;
497 endPos.fY = fMarkedEnd.fY;
498 if (endPos.fX == -1) {
502 endPos.fX = fText->GetLineLength(endPos.fY);
507 fClipText->InsText(insPos, fText, startPos, endPos);
508 gVirtualX->SetPrimarySelectionOwner(fId);
515 Bool_t TGTextView::SelectAll()
517 if (fText->RowCount() == 1 && fText->GetLineLength(0) == 0) {
523 fMarkedEnd.fY = fText->RowCount()-1;
524 fMarkedEnd.fX = fText->GetLineLength(fMarkedEnd.fY);
525 if (fMarkedEnd.fX < 0) {
528 UpdateRegion(0, 0, fCanvas->GetWidth(), fCanvas->GetHeight());
537 void TGTextView::DrawRegion(Int_t x, Int_t y, UInt_t w, UInt_t h)
542 Long_t xoffset, len, len1, len2;
543 Long_t line_count = fText->RowCount();
547 pos.fY = ToObjYCoord(fVisible.fY + h);
548 rect.fHeight = UShort_t(h + ToScrYCoord(pos.fY + 1) - ToScrYCoord(pos.fY));
549 pos.fX = ToObjXCoord(fVisible.fX + w, pos.fY);
550 rect.fWidth = UShort_t(w + ToScrXCoord(pos.fX + 1, pos.fY) - ToScrXCoord(pos.fX, pos.fY));
551 Int_t yloc = rect.fY + (Int_t)fScrollVal.fY;
552 pos.fY = ToObjYCoord(fVisible.fY + rect.fY);
554 while (pos.fY <= line_count &&
555 yloc - fScrollVal.fY <= (Int_t)fCanvas->GetHeight() &&
556 yloc <= rect.fY + rect.fHeight) {
558 pos.fX = ToObjXCoord(fVisible.fX + rect.fX, pos.fY);
559 xoffset = ToScrXCoord(pos.fX, pos.fY);
560 len = fText->GetLineLength(pos.fY) - pos.fX;
562 gVirtualX->ClearArea(fCanvas->GetId(), x, Int_t(ToScrYCoord(pos.fY)),
563 rect.fWidth, UInt_t(ToScrYCoord(pos.fY+1)-ToScrYCoord(pos.fY)));
567 if (len > ToObjXCoord(fVisible.fX + rect.fX + rect.fWidth, pos.fY) - pos.fX) {
568 len = ToObjXCoord(fVisible.fX + rect.fX + rect.fWidth, pos.fY) - pos.fX + 1;
571 xoffset = -fVisible.fX;
573 if (pos.fY >= ToObjYCoord(fVisible.fY)) {
574 buffer = fText->GetLine(pos, len);
578 while (buffer[i] !=
'\0') {
579 if (buffer[i] ==
'\t') {
582 while (buffer[j] == 16 && buffer[j] !=
'\0') {
590 pos.fY < fMarkedStart.fY || pos.fY > fMarkedEnd.fY ||
591 (pos.fY == fMarkedStart.fY &&
592 fMarkedStart.fX >= pos.fX+len &&
593 fMarkedStart.fY != fMarkedEnd.fY) ||
594 (pos.fY == fMarkedEnd.fY &&
595 fMarkedEnd.fX < pos.fX &&
596 fMarkedStart.fY != fMarkedEnd.fY) ||
597 (fMarkedStart.fY == fMarkedEnd.fY &&
598 (fMarkedEnd.fX < pos.fX ||
599 fMarkedStart.fX > pos.fX+len))) {
601 gVirtualX->DrawString(fCanvas->GetId(), fNormGC(), Int_t(xoffset),
602 Int_t(ToScrYCoord(pos.fY+1) - fMaxDescent),
605 if (pos.fY > fMarkedStart.fY && pos.fY < fMarkedEnd.fY) {
609 if (fMarkedStart.fY == fMarkedEnd.fY) {
610 if (fMarkedStart.fX >= pos.fX &&
611 fMarkedStart.fX <= pos.fX + len) {
612 len1 = fMarkedStart.fX - pos.fX;
616 if (fMarkedEnd.fX >= pos.fX &&
617 fMarkedEnd.fX <= pos.fX + len) {
618 len2 = fMarkedEnd.fX - pos.fX - len1;
623 if (pos.fY == fMarkedStart.fY) {
624 if (fMarkedStart.fX < pos.fX) {
628 len1 = fMarkedStart.fX - pos.fX;
632 if (fMarkedEnd.fX > pos.fX+len) {
637 len2 = fMarkedEnd.fX - pos.fX;
642 gVirtualX->DrawString(fCanvas->GetId(), fNormGC(),
643 Int_t(ToScrXCoord(pos.fX, pos.fY)),
644 Int_t(ToScrYCoord(pos.fY+1) - fMaxDescent),
645 buffer, Int_t(len1));
646 gVirtualX->FillRectangle(fCanvas->GetId(), fSelbackGC(),
647 Int_t(ToScrXCoord(pos.fX+len1, pos.fY)),
648 Int_t(ToScrYCoord(pos.fY)),
649 UInt_t(ToScrXCoord(pos.fX+len1+len2, pos.fY) -
650 ToScrXCoord(pos.fX+len1, pos.fY)),
651 UInt_t(ToScrYCoord(pos.fY+1)-ToScrYCoord(pos.fY)));
652 gVirtualX->DrawString(fCanvas->GetId(), fSelGC(),
653 Int_t(ToScrXCoord(pos.fX+len1, pos.fY)),
654 Int_t(ToScrYCoord(pos.fY+1) - fMaxDescent),
655 buffer+len1, Int_t(len2));
656 gVirtualX->DrawString(fCanvas->GetId(), fNormGC(),
657 Int_t(ToScrXCoord(pos.fX+len1+len2, pos.fY)),
658 Int_t(ToScrYCoord(pos.fY+1) - fMaxDescent),
659 buffer+len1+len2, Int_t(len-(len1+len2)));
665 yloc += Int_t(ToScrYCoord(pos.fY) - ToScrYCoord(pos.fY-1));
672 Bool_t TGTextView::HandleCrossing(Event_t *event)
674 if (event->fWindow != fCanvas->GetId())
677 fMousePos.fY = ToObjYCoord(fVisible.fY + event->fY);
678 if (ToScrYCoord(fMousePos.fY+1) >= (Int_t)fCanvas->GetHeight()) {
681 fMousePos.fX = ToObjXCoord(fVisible.fX + event->fX, fMousePos.fY);
682 if (fMousePos.fX >= ReturnLineLength(fMousePos.fY)) {
685 if ((event->fState & kButton1Mask) && fIsMarked && fIsMarking) {
686 if (event->fType == kLeaveNotify) {
691 if (event->fX >= (Int_t)fCanvas->GetWidth()) {
699 if (event->fY >= (Int_t)fCanvas->GetHeight()) {
705 Mark(fMousePos.fX, fMousePos.fY);
717 Bool_t TGTextView::HandleTimer(TTimer *)
719 static const Int_t kAutoScrollFudge = 10;
720 static const Int_t kAcceleration[kAutoScrollFudge + 1] = {1, 1, 1, 1, 2, 3, 4, 6, 8, 12, 16};
725 ev.fType = kButtonPress;
729 if (fMarkedStart.fY == fMarkedEnd.fY) {
732 if (fIsMarked && (fScrolling != -1)) {
734 gVirtualX->QueryPointer(fId, dum1, dum2, ev.fXRoot, ev.fYRoot, x, y, ev.fState);
736 fMousePos.fY = ToObjYCoord(fVisible.fY + y);
738 if (fMousePos.fY >= ReturnLineCount()) {
739 fMousePos.fY = ReturnLineCount() - 1;
741 if (fMousePos.fY < 0) {
744 if (ev.fState & kButton1Mask) {
747 if (y < kAutoScrollFudge) {
748 dy = kAutoScrollFudge - y;
749 }
else if ((Int_t)fCanvas->GetHeight() - kAutoScrollFudge <= y) {
750 dy = fCanvas->GetHeight() - kAutoScrollFudge - y;
752 Int_t ady = TMath::Abs(dy) >> 3;
755 if (ady > kAutoScrollFudge) ady = kAutoScrollFudge;
756 dy = kAcceleration[ady];
761 if (y > (Int_t)fCanvas->GetHeight()) {
771 size.fY = ToObjYCoord(fVisible.fY + fCanvas->GetHeight()) - 1;
772 size.fX = ToObjXCoord(fVisible.fX + fCanvas->GetWidth(), fMousePos.fY) - 1;
773 switch (fScrolling) {
777 if (fVisible.fX == 0) {
781 SetHsbPosition(fVisible.fX / fScrollVal.fX - 1);
782 Mark(ToObjXCoord(fVisible.fX, fMousePos.fY) - 1, fMousePos.fY);
786 if ((Int_t)fCanvas->GetWidth() >= ToScrXCoord(ReturnLineLength(fMousePos.fY), fMousePos.fY)) {
790 SetHsbPosition(fVisible.fX / fScrollVal.fX + 1);
791 Mark(size.fX+1, fMousePos.fY);
795 if (fVisible.fY == 0) {
799 SetVsbPosition(fVisible.fY/fScrollVal.fY - dy);
800 Mark(fMousePos.fX, fMarkedStart.fY - 1);
804 if ((Int_t)fCanvas->GetHeight() >= ToScrYCoord(ReturnLineCount())) {
808 SetVsbPosition(fVisible.fY/fScrollVal.fY + dy);
809 Mark(fMousePos.fX, size.fY + 1);
822 Bool_t TGTextView::HandleButton(Event_t *event)
824 if (event->fWindow != fCanvas->GetId()) {
828 if (event->fCode == kButton1) {
829 if (event->fType == kButtonPress) {
831 if (event->fState & kKeyShiftMask) {
841 fMousePos.fY = ToObjYCoord(fVisible.fY + event->fY);
842 fMousePos.fX = ToObjXCoord(fVisible.fX + event->fX, fMousePos.fY);
843 fMarkedStart.fX = fMarkedEnd.fX = fMousePos.fX;
844 fMarkedStart.fY = fMarkedEnd.fY = fMousePos.fY;
847 if ((fMarkedStart.fX == fMarkedEnd.fX) &&
848 (fMarkedStart.fY == fMarkedEnd.fY)) {
850 SendMessage(fMsgWindow, MK_MSG(kC_TEXTVIEW, kTXT_ISMARKED),
854 SendMessage(fMsgWindow, MK_MSG(kC_TEXTVIEW, kTXT_ISMARKED),
860 }
else if (event->fCode == kButton4) {
862 if (fVisible.fY > 0) {
863 Long_t amount = fVisible.fY / fScrollVal.fY - 3;
864 SetVsbPosition((amount >= 0) ? amount : 0);
867 }
else if (event->fCode == kButton5) {
869 if ((Int_t)fCanvas->GetHeight() < ToScrYCoord(ReturnLineCount())) {
871 size.fY = ToObjYCoord(fVisible.fY + fCanvas->GetHeight()) - 1;
872 SetVsbPosition(fVisible.fY / fScrollVal.fY + 3);
875 }
else if (event->fType == kButtonPress) {
876 if (event->fCode == kButton2) {
877 SendMessage(fMsgWindow, MK_MSG(kC_TEXTVIEW, kTXT_CLICK2),
878 fWidgetId, (event->fYRoot << 16) | event->fXRoot);
880 }
else if (event->fCode == kButton3) {
881 SendMessage(fMsgWindow, MK_MSG(kC_TEXTVIEW, kTXT_CLICK3),
882 fWidgetId, (event->fYRoot << 16) | event->fXRoot);
886 if (event->fType == kButtonRelease) {
887 if (event->fCode == kButton1) {
900 Bool_t TGTextView::HandleDoubleClick(Event_t *)
908 Bool_t TGTextView::HandleMotion(Event_t *event)
910 if ((ToObjYCoord(fVisible.fY+event->fY) == fMousePos.fY) &&
911 (ToObjXCoord(fVisible.fX+event->fX, ToObjYCoord(fVisible.fY + event->fY)) == fMousePos.fX)) {
915 if (fScrolling != -1) {
919 fMousePos.fY = ToObjYCoord(fVisible.fY + event->fY);
920 if (fMousePos.fY >= ReturnLineCount()) {
921 fMousePos.fY = ReturnLineCount()-1;
923 fMousePos.fX = ToObjXCoord(fVisible.fX + event->fX, fMousePos.fY);
925 if (fMousePos.fX > ReturnLineLength(fMousePos.fY)) {
926 fMousePos.fX = ReturnLineLength(fMousePos.fY);
928 if (event->fWindow != fCanvas->GetId()) {
938 if (event->fX >= (Int_t)fCanvas->GetWidth()) {
944 if (event->fY >= (Int_t)fCanvas->GetHeight()) {
947 Mark(fMousePos.fX, fMousePos.fY);
954 Bool_t TGTextView::HandleSelectionClear(Event_t * )
965 Bool_t TGTextView::HandleSelectionRequest(Event_t *event)
968 char *buffer, *temp_buffer;
969 Long_t len, prev_len, temp_len, count;
974 reply.fType = kSelectionNotify;
975 reply.fTime =
event->fTime;
976 reply.fUser[0] =
event->fUser[0];
977 reply.fUser[1] =
event->fUser[1];
978 reply.fUser[2] =
event->fUser[2];
979 reply.fUser[3] =
event->fUser[3];
981 targets[0] = gVirtualX->InternAtom(
"TARGETS", kFALSE);
982 targets[1] = gVirtualX->InternAtom(
"XA_STRING", kFALSE);
984 if ((Atom_t)event->fUser[2] == targets[0]) {
985 type = gVirtualX->InternAtom(
"XA_ATOM", kFALSE);
986 gVirtualX->ChangeProperty((Window_t) event->fUser[0], (Atom_t) event->fUser[3],
987 type, (UChar_t*) targets, (Int_t) 2);
989 gVirtualX->SendEvent((Window_t)event->fUser[0], &reply);
994 for (count = 0; count < fClipText->RowCount(); count++) {
995 len += fClipText->GetLineLength(count)+1;
1000 buffer =
new char[len+1];
1001 prev_len = temp_len = 0;
1002 for (pos.fY = 0; pos.fY < fClipText->RowCount(); pos.fY++) {
1003 temp_len = fClipText->GetLineLength(pos.fY);
1004 if (temp_len < 0)
break;
1005 temp_buffer = fClipText->GetLine(pos, temp_len);
1006 strncpy(buffer+prev_len, temp_buffer, (UInt_t)temp_len);
1007 if (pos.fY < fClipText->RowCount()-1) {
1008 buffer[prev_len+temp_len] = 10;
1009 prev_len += temp_len+1;
1011 prev_len += temp_len;
1012 delete [] temp_buffer;
1019 if (buffer[i] ==
'\t') {
1021 while (buffer[j] == 16 && buffer[j]) {
1025 strcpy(buffer+i+1, buffer+j);
1031 gVirtualX->ChangeProperty((Window_t) event->fUser[0], (Atom_t) event->fUser[3],
1032 (Atom_t) event->fUser[2], (UChar_t*) buffer,
1037 gVirtualX->SendEvent((Window_t)event->fUser[0], &reply);
1048 static Bool_t IsTextFile(
const char *candidate)
1052 Int_t weirdcount = 0;
1057 if (gSystem->GetPathInfo(candidate, buf) || !(buf.fMode & kS_IFREG))
1060 infile = fopen(candidate,
"r");
1063 nchars = fread(buffer, 1, 512, infile);
1066 for (i = 0; i < nchars; i++) {
1067 if (buffer[i] & 128)
1069 if (buffer[i] ==
'\0')
1073 if ((nchars > 0) && ((weirdcount * 100 / nchars) > 30))
1085 Bool_t TGTextView::HandleDNDDrop(TDNDData *data)
1087 static Atom_t rootObj = gVirtualX->InternAtom(
"application/root", kFALSE);
1088 static Atom_t uriObj = gVirtualX->InternAtom(
"text/uri-list", kFALSE);
1090 if (fText->RowCount() > 1) {
1092 new TGMsgBox(fClient->GetRoot(), GetMainFrame(),
1093 "Overvrite",
"Do you want to replace existing text?",
1094 kMBIconExclamation, kMBYes | kMBNo, &ret);
1098 if (data->fDataType == rootObj) {
1099 TBufferFile buf(TBuffer::kRead, data->fDataLength, (
void *)data->fData);
1101 TObject *obj = (TObject *)buf.ReadObjectAny(TObject::Class());
1102 if (obj && obj->InheritsFrom(
"TMacro")) {
1103 TMacro *macro = (TMacro *)obj;
1104 TIter next(macro->GetListOfLines());
1106 while ((objs = (TObjString*) next())) {
1107 AddLine(objs->GetName());
1110 else if (obj && obj->InheritsFrom(
"TSystemFile")) {
1111 TSystemFile *sfile = (TSystemFile *)obj;
1112 LoadFile(sfile->GetName());
1113 DataDropped(sfile->GetName());
1117 else if (data->fDataType == uriObj) {
1118 TString sfname((
char *)data->fData);
1119 if (sfname.Length() > 7) {
1120 sfname.ReplaceAll(
"\r\n",
"");
1121 TUrl uri(sfname.Data());
1122 if (IsTextFile(uri.GetFile())) {
1123 LoadFile(uri.GetFile());
1124 DataDropped(uri.GetFile());
1134 Atom_t TGTextView::HandleDNDPosition(Int_t , Int_t , Atom_t action,
1143 Atom_t TGTextView::HandleDNDEnter(Atom_t *typelist)
1145 static Atom_t rootObj = gVirtualX->InternAtom(
"application/root", kFALSE);
1146 static Atom_t uriObj = gVirtualX->InternAtom(
"text/uri-list", kFALSE);
1148 for (
int i = 0; typelist[i] != kNone; ++i) {
1149 if (typelist[i] == rootObj)
1151 if (typelist[i] == uriObj)
1160 Bool_t TGTextView::HandleDNDLeave()
1168 void TGTextView::Mark(Long_t xPos, Long_t yPos)
1170 TGLongPosition posStart, posEnd, pos;
1174 if (pos.fY > fText->RowCount()-1) {
1175 pos.fY = fText->RowCount()-1;
1177 if (pos.fX > fText->GetLineLength(pos.fY)) {
1178 pos.fX = fText->GetLineLength(pos.fY);
1180 if (pos.fY < fMarkedStart.fY) {
1181 posEnd.fY = fMarkedStart.fY;
1182 if (fMarkedFromY == 1 || fMarkedFromX == 1) {
1183 posEnd.fY = fMarkedEnd.fY;
1184 fMarkedEnd.fX = fMarkedStart.fX;
1185 fMarkedEnd.fY = fMarkedStart.fY;
1187 posStart.fY = pos.fY;
1188 fMarkedStart.fY = pos.fY;
1189 fMarkedStart.fX = pos.fX;
1192 }
else if (pos.fY > fMarkedEnd.fY) {
1193 posStart.fY = fMarkedEnd.fY;
1194 if (fMarkedFromY == 0 || fMarkedFromX == 0) {
1195 if (fMarkedStart.fY != fMarkedEnd.fY) {
1196 posStart.fY = fMarkedStart.fY;
1197 fMarkedStart.fX = fMarkedEnd.fX;
1198 fMarkedStart.fY = fMarkedEnd.fY;
1201 fMarkedEnd.fY = pos.fY;
1202 fMarkedEnd.fX = pos.fX;
1206 posEnd.fY = fMarkedEnd.fY;
1208 if (pos.fX <= fMarkedStart.fX && pos.fY == fMarkedStart.fY) {
1209 posEnd.fY = fMarkedStart.fY;
1210 if (fMarkedFromY == 1 || fMarkedFromX == 1) {
1211 posEnd.fY = fMarkedEnd.fY;
1212 fMarkedEnd.fX = fMarkedStart.fX;
1213 fMarkedEnd.fY = fMarkedStart.fY;
1215 fMarkedStart.fX = pos.fX;
1218 posStart.fY = fMarkedStart.fY;
1220 if (pos.fX > fMarkedEnd.fX && pos.fY == fMarkedEnd.fY) {
1221 posStart.fY = fMarkedEnd.fY;
1222 if (fMarkedFromY == 0 || fMarkedFromX == 0) {
1223 posStart.fY = fMarkedStart.fY;
1224 fMarkedStart.fX = fMarkedEnd.fX;
1225 fMarkedStart.fY = fMarkedEnd.fY;
1227 fMarkedEnd.fX = pos.fX;
1230 posEnd.fY = fMarkedEnd.fY;
1232 if (fMarkedFromY == 0 || fMarkedFromX == 0) {
1233 posStart.fY = fMarkedStart.fY;
1234 fMarkedStart.fY = pos.fY;
1235 fMarkedStart.fX = pos.fX;
1236 posEnd.fY = fMarkedStart.fY;
1238 if (fMarkedStart.fY == fMarkedEnd.fY &&
1239 fMarkedStart.fX > fMarkedEnd.fX) {
1240 fMarkedStart.fX = fMarkedEnd.fX;
1241 fMarkedEnd.fX = pos.fX;
1244 }
else if (fMarkedFromX == 1 || fMarkedFromY == 1) {
1245 posStart.fY = pos.fY;
1246 posEnd.fY = fMarkedEnd.fY;
1247 fMarkedEnd.fY = pos.fY;
1248 fMarkedEnd.fX = pos.fX;
1251 if (fMarkedEnd.fX == -1) {
1252 fMarkedEnd.fY = pos.fY-1;
1253 fMarkedEnd.fX = fText->GetLineLength(fMarkedEnd.fY);
1254 if (fMarkedEnd.fX < 0) {
1259 if (fMarkedStart.fY == fMarkedEnd.fY &&
1260 fMarkedStart.fX > fMarkedEnd.fX) {
1261 fMarkedEnd.fX = fMarkedStart.fX;
1262 fMarkedStart.fX = pos.fX;
1270 if (fMarkedEnd.fX == -1) {
1271 if (fMarkedEnd.fY > 0) {
1274 fMarkedEnd.fX = fText->GetLineLength(fMarkedEnd.fY);
1275 if (fMarkedEnd.fX < 0) {
1281 Int_t yy = (Int_t)ToScrYCoord(posStart.fY);
1282 UInt_t hh = UInt_t(ToScrYCoord(posEnd.fY + 1) - ToScrYCoord(posStart.fY));
1284 DrawRegion(0, yy, fCanvas->GetWidth(), hh);
1291 void TGTextView::UnMark()
1294 ((fMarkedEnd.fY == fMarkedStart.fY) &&
1295 (fMarkedEnd.fX == fMarkedStart.fX))) {
1300 Int_t y = (Int_t)ToScrYCoord(fMarkedStart.fY);
1301 UInt_t h = UInt_t(ToScrYCoord(fMarkedEnd.fY + 1) - y);
1304 UpdateRegion(0, y, fCanvas->GetWidth(), h);
1310 void TGTextView::AdjustWidth()
1312 Long_t line = fText->GetLongestLine();
1316 Long_t size = ToScrXCoord(fText->GetLineLength(line), line) + fVisible.fX;
1317 if (fVsb->IsMapped()) {
1318 size += fVsb->GetDefaultWidth();
1320 size += (fBorderWidth << 1) + fXMargin+1;
1321 Resize((UInt_t)size, fHeight);
1327 void TGTextView::Layout()
1336 void TGTextView::HLayout()
1342 tch = fHeight - (fBorderWidth << 1) - fYMargin-1;
1343 tcw = fWidth - (fBorderWidth << 1) - fXMargin-1;
1345 if (fVsb && fVsb->IsMapped()) {
1346 tcw -= fVsb->GetDefaultWidth();
1347 if (tcw < 0) tcw = 0;
1349 fCanvas->SetHeight(tch);
1350 fCanvas->SetWidth(tcw);
1351 cols = ReturnLongestLineWidth();
1353 if (fHsb && fHsb->IsMapped()) {
1354 SetVisibleStart(0, kHorizontal);
1355 fHsb->UnmapWindow();
1358 fCanvas->MoveResize(fBorderWidth + fXMargin, fBorderWidth + fYMargin, tcw, tch);
1361 tch -= fHsb->GetDefaultHeight();
1362 if (tch < 0) tch = 0;
1363 fHsb->MoveResize(fBorderWidth, fHeight - fHsb->GetDefaultHeight()-fBorderWidth,
1364 tcw+1+fBorderWidth, fHsb->GetDefaultHeight());
1366 fHsb->SetRange(Int_t(cols/fScrollVal.fX), Int_t(tcw/fScrollVal.fX));
1368 fCanvas->MoveResize(fBorderWidth + fXMargin, fBorderWidth + fYMargin, tcw, tch);
1375 void TGTextView::VLayout()
1380 tch = fHeight - (fBorderWidth << 1) - fYMargin-1;
1381 tcw = fWidth - (fBorderWidth << 1) - fXMargin-1;
1382 if (fHsb && fHsb->IsMapped()) {
1383 tch -= fHsb->GetDefaultHeight();
1384 if (tch < 0) tch = 0;
1386 fCanvas->SetHeight(tch);
1387 fCanvas->SetWidth(tcw);
1388 lines = ReturnHeighestColHeight();
1390 if (fVsb && fVsb->IsMapped()) {
1391 SetVisibleStart(0, kVertical);
1392 fVsb->UnmapWindow();
1395 fCanvas->MoveResize(fBorderWidth + fXMargin, fBorderWidth + fYMargin, tcw, tch);
1398 tcw -= fVsb->GetDefaultWidth();
1399 if (tcw < 0) tcw = 0;
1400 fVsb->MoveResize(fWidth - fVsb->GetDefaultWidth() - fBorderWidth, fBorderWidth,
1401 fVsb->GetDefaultWidth(), tch+1+fBorderWidth);
1403 fVsb->SetRange(Int_t(lines/fScrollVal.fY), Int_t(tch/fScrollVal.fY));
1405 fCanvas->MoveResize(fBorderWidth + fXMargin, fBorderWidth + fYMargin, tcw, tch);
1412 void TGTextView::SetSBRange(Int_t direction)
1414 if (direction == kVertical) {
1418 if (ReturnHeighestColHeight() <= (Int_t)fCanvas->GetHeight()) {
1419 if (fVsb->IsMapped()) {
1425 if (!fVsb->IsMapped()) {
1428 fVsb->SetRange(Int_t(ReturnHeighestColHeight()/fScrollVal.fY),
1429 Int_t(fCanvas->GetHeight()/fScrollVal.fY));
1435 if (ReturnLongestLineWidth() <= (Int_t)fCanvas->GetWidth()) {
1436 if (fHsb->IsMapped()) {
1442 if (!fHsb->IsMapped()) {
1445 fHsb->SetRange(Int_t(ReturnLongestLineWidth()/fScrollVal.fX),
1446 Int_t(fCanvas->GetWidth()/fScrollVal.fX));
1454 void TGTextView::SetHsbPosition(Long_t newPos)
1456 if (fHsb && fHsb->IsMapped()) {
1457 fHsb->SetPosition(Int_t(newPos));
1459 SetVisibleStart(Int_t(newPos * fScrollVal.fX), kHorizontal);
1466 void TGTextView::SetVsbPosition(Long_t newPos)
1468 if (fVsb && fVsb->IsMapped()) {
1469 fVsb->SetPosition(Int_t(newPos));
1471 SetVisibleStart(Int_t(newPos * fScrollVal.fY), kVertical);
1478 FontStruct_t TGTextView::GetDefaultFontStruct()
1480 if (!fgDefaultFont) {
1481 fgDefaultFont = gClient->GetResourcePool()->GetDocumentFixedFont();
1483 return fgDefaultFont->GetFontStruct();
1489 void TGTextView::ShowBottom()
1492 Long_t lines, newPos;
1494 tch = fCanvas->GetHeight();
1495 lines = ReturnHeighestColHeight();
1497 newPos = lines / fScrollVal.fY;
1498 SetVsbPosition(newPos);
1506 void TGTextView::ShowTop()
1515 void TGTextView::SetForegroundColor(Pixel_t col)
1517 fNormGC.SetBackground(col);
1518 fNormGC.SetForeground(col);
1524 const TGGC &TGTextView::GetDefaultGC()
1527 fgDefaultGC =
new TGGC(*gClient->GetResourcePool()->GetFrameGC());
1528 fgDefaultGC->SetFont(fgDefaultFont->GetFontHandle());
1530 return *fgDefaultGC;
1536 const TGGC &TGTextView::GetDefaultSelectedGC()
1538 if (!fgDefaultSelectedGC) {
1539 fgDefaultSelectedGC =
new TGGC(*gClient->GetResourcePool()->GetSelectedGC());
1540 fgDefaultSelectedGC->SetFont(fgDefaultFont->GetFontHandle());
1542 return *fgDefaultSelectedGC;
1548 const TGGC &TGTextView::GetDefaultSelectedBackgroundGC()
1550 if (!fgDefaultSelectedBackgroundGC) {
1551 fgDefaultSelectedBackgroundGC = gClient->GetResourcePool()->GetSelectedBckgndGC();
1553 return *fgDefaultSelectedBackgroundGC;
1559 void TGTextView::SavePrimitive(std::ostream &out, Option_t *option )
1562 out <<
" TGTextView *";
1563 out << GetName() <<
" = new TGTextView(" << fParent->GetName()
1564 <<
"," << GetWidth() <<
"," << GetHeight()
1565 <<
");"<< std::endl;
1567 if (option && strstr(option,
"keep_names"))
1568 out <<
" " << GetName() <<
"->SetName(\"" << GetName() <<
"\");" << std::endl;
1570 if (fCanvas->GetBackground() != TGFrame::fgWhitePixel) {
1571 out <<
" " << GetName() <<
"->ChangeBackground(" << fCanvas->GetBackground() <<
");" << std::endl;
1574 TGText *txt = GetText();
1575 Bool_t fromfile = strlen(txt->GetFileName()) ? kTRUE : kFALSE;
1579 const char *filename = txt->GetFileName();
1580 fn = gSystem->ExpandPathName(gSystem->UnixPathName(filename));
1582 fn = TString::Format(
"Txt%s", GetName()+5);
1583 txt->Save(fn.Data());
1585 out <<
" " << GetName() <<
"->LoadFile(" << quote << fn.Data() << quote <<
");" << std::endl;