forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGX11.cxx
3683 lines (3200 loc) · 118 KB
/
TGX11.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @(#)root/x11:$Id$
// Author: Rene Brun, Olivier Couet, Fons Rademakers 28/11/94
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/// \defgroup x11 X11 backend
/// \brief Interface to X11 graphics.
/// \ingroup GraphicsBackends
/** \class TGX11
\ingroup x11
This class is the basic interface to the X11 (Xlib) graphics system.
It is an implementation of the abstract TVirtualX class.
This class gives access to basic X11 graphics, pixmap, text and font handling
routines.
The companion class for Win32 is TGWin32.
The file G11Gui.cxx contains the implementation of the GUI methods of the
TGX11 class. Most of the methods are used by the machine independent
GUI classes (libGUI.so).
This code was initially developed in the context of HIGZ and PAW
by Olivier Couet (package X11INT).
*/
#include "TROOT.h"
#include "TColor.h"
#include "TGX11.h"
#include "TPoint.h"
#include "TMath.h"
#include "TStorage.h"
#include "TStyle.h"
#include "TExMap.h"
#include "TEnv.h"
#include "TString.h"
#include "TObjString.h"
#include "TObjArray.h"
#include "RStipples.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/keysym.h>
#include <X11/xpm.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#ifdef R__AIX
# include <sys/socket.h>
#endif
extern float XRotVersion(char*, int);
extern void XRotSetMagnification(float);
extern void XRotSetBoundingBoxPad(int);
extern int XRotDrawString(Display*, XFontStruct*, float,
Drawable, GC, int, int, char*);
extern int XRotDrawImageString(Display*, XFontStruct*, float,
Drawable, GC, int, int, char*);
extern int XRotDrawAlignedString(Display*, XFontStruct*, float,
Drawable, GC, int, int, char*, int);
extern int XRotDrawAlignedImageString(Display*, XFontStruct*, float,
Drawable, GC, int, int, char*, int);
extern XPoint *XRotTextExtents(Display*, XFontStruct*, float,
int, int, char*, int);
//---- globals
static XWindow_t *gCws; // gCws: pointer to the current window
static XWindow_t *gTws; // gTws: temporary pointer
const Int_t kBIGGEST_RGB_VALUE = 65535;
//
// Primitives Graphic Contexts global for all windows
//
const int kMAXGC = 7;
static GC gGClist[kMAXGC];
static GC *gGCline = &gGClist[0]; // PolyLines
static GC *gGCmark = &gGClist[1]; // PolyMarker
static GC *gGCfill = &gGClist[2]; // Fill areas
static GC *gGCtext = &gGClist[3]; // Text
static GC *gGCinvt = &gGClist[4]; // Inverse text
static GC *gGCdash = &gGClist[5]; // Dashed lines
static GC *gGCpxmp = &gGClist[6]; // Pixmap management
static GC gGCecho; // Input echo
static Int_t gFillHollow; // Flag if fill style is hollow
static Pixmap gFillPattern = 0; // Fill pattern
//
// Text management
//
const Int_t kMAXFONT = 4;
static struct {
XFontStruct *id;
char name[80]; // Font name
} gFont[kMAXFONT]; // List of fonts loaded
static XFontStruct *gTextFont; // Current font
static Int_t gCurrentFontNumber = 0; // Current font number in gFont[]
//
// Markers
//
const Int_t kMAXMK = 100;
static struct {
int type;
int n;
XPoint xy[kMAXMK];
} gMarker; // Point list to draw marker
//
// Keep style values for line GC
//
static int gLineWidth = 0;
static int gLineStyle = LineSolid;
static int gCapStyle = CapButt;
static int gJoinStyle = JoinMiter;
static char gDashList[10];
static int gDashLength = 0;
static int gDashOffset = 0;
static int gDashSize = 0;
//
// Event masks
//
static ULong_t gMouseMask = ButtonPressMask | ButtonReleaseMask |
EnterWindowMask | LeaveWindowMask |
PointerMotionMask | KeyPressMask |
KeyReleaseMask;
static ULong_t gKeybdMask = ButtonPressMask | KeyPressMask |
EnterWindowMask | LeaveWindowMask;
//
// Data to create an invisible cursor
//
const char null_cursor_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
static Cursor gNullCursor = 0;
struct RXGCValues:XGCValues{};
struct RXColor:XColor{};
struct RXImage:XImage{};
struct RXPoint:XPoint{};
struct RXVisualInfo:XVisualInfo{};
struct RVisual:Visual{};
ClassImp(TGX11);
////////////////////////////////////////////////////////////////////////////////
/// Default constructor.
TGX11::TGX11()
{
int i;
fDisplay = 0;
fScreenNumber = 0;
fVisual = 0;
fRootWin = 0;
fVisRootWin = 0;
fColormap = 0;
fBlackPixel = 0;
fWhitePixel = 0;
fWindows = 0;
fColors = 0;
fXEvent = new XEvent;
fRedDiv = -1;
fGreenDiv = -1;
fBlueDiv = -1;
fRedShift = -1;
fGreenShift = -1;
fBlueShift = -1;
fCharacterUpX = 1;
fCharacterUpY = 1;
fDepth = 0;
fHasTTFonts = kFALSE;
fHasXft = kFALSE;
fMaxNumberOfWindows = 10;
fTextAlignH = 1;
fTextAlignV = 1;
fTextAlign = 7;
fTextMagnitude = 1;
for (i = 0; i < kNumCursors; i++) fCursors[i] = 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Normal Constructor.
TGX11::TGX11(const char *name, const char *title) : TVirtualX(name, title)
{
int i;
fDisplay = 0;
fScreenNumber = 0;
fVisual = 0;
fRootWin = 0;
fVisRootWin = 0;
fColormap = 0;
fBlackPixel = 0;
fWhitePixel = 0;
fDrawMode = kCopy;
fXEvent = new XEvent;
fRedDiv = -1;
fGreenDiv = -1;
fBlueDiv = -1;
fRedShift = -1;
fGreenShift = -1;
fBlueShift = -1;
fCharacterUpX = 1;
fCharacterUpY = 1;
fDepth = 0;
fHasTTFonts = kFALSE;
fHasXft = kFALSE;
fMaxNumberOfWindows = 10;
fTextAlignH = 1;
fTextAlignV = 1;
fTextAlign = 7;
fTextMagnitude = 1;
for (i = 0; i < kNumCursors; i++) fCursors[i] = 0;
//fWindows = new XWindow_t[fMaxNumberOfWindows];
fWindows = (XWindow_t*) TStorage::Alloc(fMaxNumberOfWindows*sizeof(XWindow_t));
for (i = 0; i < fMaxNumberOfWindows; i++)
fWindows[i].fOpen = 0;
fColors = new TExMap;
}
////////////////////////////////////////////////////////////////////////////////
/// Copy constructor. Currently only used by TGX11TTF.
TGX11::TGX11(const TGX11 &org) : TVirtualX(org)
{
int i;
fDisplay = org.fDisplay;
fScreenNumber = org.fScreenNumber;
fVisual = org.fVisual;
fRootWin = org.fRootWin;
fVisRootWin = org.fVisRootWin;
fColormap = org.fColormap;
fBlackPixel = org.fBlackPixel;
fWhitePixel = org.fWhitePixel;
fHasTTFonts = org.fHasTTFonts;
fHasXft = org.fHasXft;
fTextAlignH = org.fTextAlignH;
fTextAlignV = org.fTextAlignV;
fTextAlign = org.fTextAlign;
fTextMagnitude = org.fTextMagnitude;
fCharacterUpX = org.fCharacterUpX;
fCharacterUpY = org.fCharacterUpY;
fDepth = org.fDepth;
fRedDiv = org.fRedDiv;
fGreenDiv = org.fGreenDiv;
fBlueDiv = org.fBlueDiv;
fRedShift = org.fRedShift;
fGreenShift = org.fGreenShift;
fBlueShift = org.fBlueShift;
fDrawMode = org.fDrawMode;
fXEvent = new XEvent;
fMaxNumberOfWindows = org.fMaxNumberOfWindows;
//fWindows = new XWindow_t[fMaxNumberOfWindows];
fWindows = (XWindow_t*) TStorage::Alloc(fMaxNumberOfWindows*sizeof(XWindow_t));
for (i = 0; i < fMaxNumberOfWindows; i++) {
fWindows[i].fOpen = org.fWindows[i].fOpen;
fWindows[i].fDoubleBuffer = org.fWindows[i].fDoubleBuffer;
fWindows[i].fIsPixmap = org.fWindows[i].fIsPixmap;
fWindows[i].fDrawing = org.fWindows[i].fDrawing;
fWindows[i].fWindow = org.fWindows[i].fWindow;
fWindows[i].fBuffer = org.fWindows[i].fBuffer;
fWindows[i].fWidth = org.fWindows[i].fWidth;
fWindows[i].fHeight = org.fWindows[i].fHeight;
fWindows[i].fClip = org.fWindows[i].fClip;
fWindows[i].fXclip = org.fWindows[i].fXclip;
fWindows[i].fYclip = org.fWindows[i].fYclip;
fWindows[i].fWclip = org.fWindows[i].fWclip;
fWindows[i].fHclip = org.fWindows[i].fHclip;
fWindows[i].fNewColors = org.fWindows[i].fNewColors;
fWindows[i].fNcolors = org.fWindows[i].fNcolors;
fWindows[i].fShared = org.fWindows[i].fShared;
}
for (i = 0; i < kNumCursors; i++)
fCursors[i] = org.fCursors[i];
fColors = new TExMap;
Long64_t key, value;
TExMapIter it(org.fColors);
while (it.Next(key, value)) {
XColor_t *colo = (XColor_t *) (Long_t)value;
XColor_t *col = new XColor_t;
col->fPixel = colo->fPixel;
col->fRed = colo->fRed;
col->fGreen = colo->fGreen;
col->fBlue = colo->fBlue;
col->fDefined = colo->fDefined;
fColors->Add(key, (Long_t) col);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Destructor.
TGX11::~TGX11()
{
delete (XEvent*)fXEvent;
if (fWindows) TStorage::Dealloc(fWindows);
if (!fColors) return;
Long64_t key, value;
TExMapIter it(fColors);
while (it.Next(key, value)) {
XColor_t *col = (XColor_t *) (Long_t)value;
delete col;
}
delete fColors;
}
////////////////////////////////////////////////////////////////////////////////
/// Initialize X11 system. Returns kFALSE in case of failure.
Bool_t TGX11::Init(void *display)
{
if (OpenDisplay((Display *) display) == -1) return kFALSE;
return kTRUE;
}
////////////////////////////////////////////////////////////////////////////////
/// Allocate color in colormap. If we are on an <= 8 plane machine
/// we will use XAllocColor. If we are on a >= 15 (15, 16 or 24) plane
/// true color machine we will calculate the pixel value using:
/// for 15 and 16 bit true colors have 6 bits precision per color however
/// only the 5 most significant bits are used in the color index.
/// Except for 16 bits where green uses all 6 bits. I.e.:
/// ~~~ {.cpp}
/// 15 bits = rrrrrgggggbbbbb
/// 16 bits = rrrrrggggggbbbbb
/// ~~~
/// for 24 bits each r, g and b are represented by 8 bits.
///
/// Since all colors are set with a max of 65535 (16 bits) per r, g, b
/// we just right shift them by 10, 11 and 10 bits for 16 planes, and
/// (10, 10, 10 for 15 planes) and by 8 bits for 24 planes.
/// Returns kFALSE in case color allocation failed.
Bool_t TGX11::AllocColor(Colormap cmap, RXColor *color)
{
if (fRedDiv == -1) {
if (XAllocColor((Display*)fDisplay, cmap, color))
return kTRUE;
} else {
color->pixel = (color->red >> fRedDiv) << fRedShift |
(color->green >> fGreenDiv) << fGreenShift |
(color->blue >> fBlueDiv) << fBlueShift;
return kTRUE;
}
return kFALSE;
}
////////////////////////////////////////////////////////////////////////////////
/// Returns the current RGB value for the pixel in the XColor structure.
void TGX11::QueryColors(Colormap cmap, RXColor *color, Int_t ncolors)
{
if (fRedDiv == -1) {
XQueryColors((Display*)fDisplay, cmap, color, ncolors);
} else {
ULong_t r, g, b;
for (Int_t i = 0; i < ncolors; i++) {
r = (color[i].pixel & fVisual->red_mask) >> fRedShift;
color[i].red = UShort_t(r*kBIGGEST_RGB_VALUE/(fVisual->red_mask >> fRedShift));
g = (color[i].pixel & fVisual->green_mask) >> fGreenShift;
color[i].green = UShort_t(g*kBIGGEST_RGB_VALUE/(fVisual->green_mask >> fGreenShift));
b = (color[i].pixel & fVisual->blue_mask) >> fBlueShift;
color[i].blue = UShort_t(b*kBIGGEST_RGB_VALUE/(fVisual->blue_mask >> fBlueShift));
color[i].flags = DoRed | DoGreen | DoBlue;
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Clear the pixmap pix.
void TGX11::ClearPixmap(Drawable *pix)
{
Window root;
int xx, yy;
unsigned int ww, hh, border, depth;
XGetGeometry((Display*)fDisplay, *pix, &root, &xx, &yy, &ww, &hh, &border, &depth);
SetColor(gGCpxmp, 0);
XFillRectangle((Display*)fDisplay, *pix, *gGCpxmp, 0 ,0 ,ww ,hh);
SetColor(gGCpxmp, 1);
XFlush((Display*)fDisplay);
}
////////////////////////////////////////////////////////////////////////////////
/// Clear current window.
void TGX11::ClearWindow()
{
if (!gCws->fIsPixmap && !gCws->fDoubleBuffer) {
XSetWindowBackground((Display*)fDisplay, gCws->fDrawing, GetColor(0).fPixel);
XClearWindow((Display*)fDisplay, gCws->fDrawing);
XFlush((Display*)fDisplay);
} else {
SetColor(gGCpxmp, 0);
XFillRectangle((Display*)fDisplay, gCws->fDrawing, *gGCpxmp,
0, 0, gCws->fWidth, gCws->fHeight);
SetColor(gGCpxmp, 1);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Delete current pixmap.
void TGX11::ClosePixmap()
{
CloseWindow1();
}
////////////////////////////////////////////////////////////////////////////////
/// Delete current window.
void TGX11::CloseWindow()
{
if (gCws->fShared)
gCws->fOpen = 0;
else
CloseWindow1();
// Never close connection. TApplication takes care of that
// if (!gCws) Close(); // close X when no open window left
}
////////////////////////////////////////////////////////////////////////////////
/// Delete current window.
void TGX11::CloseWindow1()
{
int wid;
if (gCws->fIsPixmap)
XFreePixmap((Display*)fDisplay, gCws->fWindow);
else
XDestroyWindow((Display*)fDisplay, gCws->fWindow);
if (gCws->fBuffer) XFreePixmap((Display*)fDisplay, gCws->fBuffer);
if (gCws->fNewColors) {
if (fRedDiv == -1)
XFreeColors((Display*)fDisplay, fColormap, gCws->fNewColors, gCws->fNcolors, 0);
delete [] gCws->fNewColors;
gCws->fNewColors = 0;
}
XFlush((Display*)fDisplay);
gCws->fOpen = 0;
// make first window in list the current window
for (wid = 0; wid < fMaxNumberOfWindows; wid++)
if (fWindows[wid].fOpen) {
gCws = &fWindows[wid];
return;
}
gCws = 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Copy the pixmap wid at the position xpos, ypos in the current window.
void TGX11::CopyPixmap(int wid, int xpos, int ypos)
{
gTws = &fWindows[wid];
XCopyArea((Display*)fDisplay, gTws->fDrawing, gCws->fDrawing, *gGCpxmp, 0, 0, gTws->fWidth,
gTws->fHeight, xpos, ypos);
XFlush((Display*)fDisplay);
}
////////////////////////////////////////////////////////////////////////////////
/// Copy area of current window in the pixmap pix.
void TGX11::CopyWindowtoPixmap(Drawable *pix, int xpos, int ypos )
{
Window root;
int xx, yy;
unsigned int ww, hh, border, depth;
XGetGeometry((Display*)fDisplay, *pix, &root, &xx, &yy, &ww, &hh, &border, &depth);
XCopyArea((Display*)fDisplay, gCws->fDrawing, *pix, *gGCpxmp, xpos, ypos, ww, hh, 0, 0);
XFlush((Display*)fDisplay);
}
////////////////////////////////////////////////////////////////////////////////
/// Draw a box.
///
/// - mode=0 hollow (kHollow)
/// - mode=1 solid (kSolid)
void TGX11::DrawBox(int x1, int y1, int x2, int y2, EBoxMode mode)
{
Int_t x = TMath::Min(x1, x2);
Int_t y = TMath::Min(y1, y2);
Int_t w = TMath::Abs(x2 - x1);
Int_t h = TMath::Abs(y2 - y1);
switch (mode) {
case kHollow:
XDrawRectangle((Display*)fDisplay, gCws->fDrawing, *gGCline, x, y, w, h);
break;
case kFilled:
XFillRectangle((Display*)fDisplay, gCws->fDrawing, *gGCfill, x, y, w, h);
break;
default:
break;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Draw a cell array.
//
/// \param [in] x1,y1 : left down corner
/// \param [in] x2,y2 : right up corner
/// \param [in] nx,ny : array size
/// \param [in] ic : array
///
/// Draw a cell array. The drawing is done with the pixel precision
/// if (X2-X1)/NX (or Y) is not a exact pixel number the position of
/// the top right corner may be wrong.
void TGX11::DrawCellArray(int x1, int y1, int x2, int y2, int nx, int ny, int *ic)
{
int i, j, icol, ix, iy, w, h, current_icol;
current_icol = -1;
w = TMath::Max((x2-x1)/(nx),1);
h = TMath::Max((y1-y2)/(ny),1);
ix = x1;
for (i = 0; i < nx; i++) {
iy = y1-h;
for (j = 0; j < ny; j++) {
icol = ic[i+(nx*j)];
if (icol != current_icol) {
XSetForeground((Display*)fDisplay, *gGCfill, GetColor(icol).fPixel);
current_icol = icol;
}
XFillRectangle((Display*)fDisplay, gCws->fDrawing, *gGCfill, ix, iy, w, h);
iy = iy-h;
}
ix = ix+w;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Fill area described by polygon.
///
/// \param [in] n number of points
/// \param [in] xyt list of points
void TGX11::DrawFillArea(int n, TPoint *xyt)
{
XPoint *xy = (XPoint*)xyt;
if (gFillHollow)
XDrawLines((Display*)fDisplay, gCws->fDrawing, *gGCfill, xy, n, CoordModeOrigin);
else {
XFillPolygon((Display*)fDisplay, gCws->fDrawing, *gGCfill,
xy, n, Nonconvex, CoordModeOrigin);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Draw a line.
///
/// \param [in] x1,y1 : begin of line
/// \param [in] x2,y2 : end of line
void TGX11::DrawLine(int x1, int y1, int x2, int y2)
{
if (gLineStyle == LineSolid)
XDrawLine((Display*)fDisplay, gCws->fDrawing, *gGCline, x1, y1, x2, y2);
else {
XSetDashes((Display*)fDisplay, *gGCdash, gDashOffset, gDashList, gDashSize);
XDrawLine((Display*)fDisplay, gCws->fDrawing, *gGCdash, x1, y1, x2, y2);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Draw a line through all points.
///
/// \param [in] n number of points
/// \param [in] xyt list of points
void TGX11::DrawPolyLine(int n, TPoint *xyt)
{
XPoint *xy = (XPoint*)xyt;
const Int_t kMaxPoints = 1000001;
if (n > kMaxPoints) {
int ibeg = 0;
int iend = kMaxPoints - 1;
while (iend < n) {
DrawPolyLine( kMaxPoints, &xyt[ibeg] );
ibeg = iend;
iend += kMaxPoints - 1;
}
if (ibeg < n) {
int npt = n - ibeg;
DrawPolyLine( npt, &xyt[ibeg] );
}
} else if (n > 1) {
if (gLineStyle == LineSolid)
XDrawLines((Display*)fDisplay, gCws->fDrawing, *gGCline, xy, n, CoordModeOrigin);
else {
int i;
XSetDashes((Display*)fDisplay, *gGCdash,
gDashOffset, gDashList, gDashSize);
XDrawLines((Display*)fDisplay, gCws->fDrawing, *gGCdash, xy, n, CoordModeOrigin);
// calculate length of line to update dash offset
for (i = 1; i < n; i++) {
int dx = xy[i].x - xy[i-1].x;
int dy = xy[i].y - xy[i-1].y;
if (dx < 0) dx = - dx;
if (dy < 0) dy = - dy;
gDashOffset += dx > dy ? dx : dy;
}
gDashOffset %= gDashLength;
}
} else {
int px,py;
px=xy[0].x;
py=xy[0].y;
XDrawPoint((Display*)fDisplay, gCws->fDrawing,
gLineStyle == LineSolid ? *gGCline : *gGCdash, px, py);
}
}
////////////////////////////////////////////////////////////////////////////////
/// Draw n markers with the current attributes at position x, y.
///
/// \param [in] n number of markers to draw
/// \param [in] xyt x,y coordinates of markers
void TGX11::DrawPolyMarker(int n, TPoint *xyt)
{
XPoint *xy = (XPoint*)xyt;
if (gMarker.n <= 0) {
const int kNMAX = 1000000;
int nt = n/kNMAX;
for (int it=0;it<=nt;it++) {
if (it < nt) {
XDrawPoints((Display*)fDisplay, gCws->fDrawing, *gGCmark, &xy[it*kNMAX], kNMAX, CoordModeOrigin);
} else {
XDrawPoints((Display*)fDisplay, gCws->fDrawing, *gGCmark, &xy[it*kNMAX], n-it*kNMAX, CoordModeOrigin);
}
}
} else {
int r = gMarker.n / 2;
int m;
for (m = 0; m < n; m++) {
int hollow = 0;
switch (gMarker.type) {
int i;
case 0: // hollow circle
XDrawArc((Display*)fDisplay, gCws->fDrawing, *gGCmark,
xy[m].x - r, xy[m].y - r, gMarker.n, gMarker.n, 0, 360*64);
break;
case 1: // filled circle
XFillArc((Display*)fDisplay, gCws->fDrawing, *gGCmark,
xy[m].x - r, xy[m].y - r, gMarker.n, gMarker.n, 0, 360*64);
break;
case 2: // hollow polygon
hollow = 1;
case 3: // filled polygon
for (i = 0; i < gMarker.n; i++) {
gMarker.xy[i].x += xy[m].x;
gMarker.xy[i].y += xy[m].y;
}
if (hollow)
XDrawLines((Display*)fDisplay, gCws->fDrawing, *gGCmark,
gMarker.xy, gMarker.n, CoordModeOrigin);
else
XFillPolygon((Display*)fDisplay, gCws->fDrawing, *gGCmark,
gMarker.xy, gMarker.n, Nonconvex, CoordModeOrigin);
for (i = 0; i < gMarker.n; i++) {
gMarker.xy[i].x -= xy[m].x;
gMarker.xy[i].y -= xy[m].y;
}
break;
case 4: // segmented line
for (i = 0; i < gMarker.n; i += 2)
XDrawLine((Display*)fDisplay, gCws->fDrawing, *gGCmark,
xy[m].x + gMarker.xy[i].x, xy[m].y + gMarker.xy[i].y,
xy[m].x + gMarker.xy[i+1].x, xy[m].y + gMarker.xy[i+1].y);
break;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
/// Draw a text string using current font.
///
/// \param [in] mode : drawing mode
/// - mode=0 : the background is not drawn (kClear)
/// - mode=1 : the background is drawn (kOpaque)
/// \param [in] x,y : text position
/// \param [in] angle : text angle
/// \param [in] mgn : magnification factor
/// \param [in] text : text string
void TGX11::DrawText(int x, int y, float angle, float mgn,
const char *text, ETextMode mode)
{
XRotSetMagnification(mgn);
if (!text) return;
switch (mode) {
case kClear:
XRotDrawAlignedString((Display*)fDisplay, gTextFont, angle,
gCws->fDrawing, *gGCtext, x, y, (char*)text, fTextAlign);
break;
case kOpaque:
XRotDrawAlignedImageString((Display*)fDisplay, gTextFont, angle,
gCws->fDrawing, *gGCtext, x, y, (char*)text, fTextAlign);
break;
default:
break;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Find best visual, i.e. the one with the most planes and TrueColor or
/// DirectColor. Sets fVisual, fDepth, fRootWin, fColormap, fBlackPixel
/// and fWhitePixel.
void TGX11::FindBestVisual()
{
Int_t findvis = gEnv->GetValue("X11.FindBestVisual", 1);
Visual *vis = DefaultVisual((Display*)fDisplay, fScreenNumber);
if (((vis->c_class != TrueColor && vis->c_class != DirectColor) ||
DefaultDepth((Display*)fDisplay, fScreenNumber) < 15) && findvis) {
// try to find better visual
static XVisualInfo templates[] = {
// Visual, visualid, screen, depth, class , red_mask, green_mask, blue_mask, colormap_size, bits_per_rgb
{ 0 , 0 , 0 , 24 , TrueColor , 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 32 , TrueColor , 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 16 , TrueColor , 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 15 , TrueColor , 0 , 0 , 0 , 0 , 0 },
// no suitable TrueColorMode found - now do the same thing to DirectColor
{ 0 , 0 , 0 , 24 , DirectColor, 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 32 , DirectColor, 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 16 , DirectColor, 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 15 , DirectColor, 0 , 0 , 0 , 0 , 0 },
{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },
};
Int_t nitems = 0;
XVisualInfo *vlist = 0;
for (Int_t i = 0; templates[i].depth != 0; i++) {
Int_t mask = VisualScreenMask|VisualDepthMask|VisualClassMask;
templates[i].screen = fScreenNumber;
if ((vlist = XGetVisualInfo((Display*)fDisplay, mask, &(templates[i]), &nitems))) {
FindUsableVisual((RXVisualInfo*)vlist, nitems);
XFree(vlist);
vlist = 0;
if (fVisual)
break;
}
}
}
fRootWin = RootWindow((Display*)fDisplay, fScreenNumber);
if (!fVisual) {
fDepth = DefaultDepth((Display*)fDisplay, fScreenNumber);
fVisual = (RVisual*)DefaultVisual((Display*)fDisplay, fScreenNumber);
fVisRootWin = fRootWin;
if (fDepth > 1)
fColormap = DefaultColormap((Display*)fDisplay, fScreenNumber);
fBlackPixel = BlackPixel((Display*)fDisplay, fScreenNumber);
fWhitePixel = WhitePixel((Display*)fDisplay, fScreenNumber);
}
if (gDebug > 1)
Printf("Selected visual 0x%lx: depth %d, class %d, colormap: %s",
fVisual->visualid, fDepth, fVisual->c_class,
fColormap == DefaultColormap((Display*)fDisplay, fScreenNumber) ? "default" :
"custom");
}
////////////////////////////////////////////////////////////////////////////////
/// Dummy error handler for X11. Used by FindUsableVisual().
static Int_t DummyX11ErrorHandler(Display *, XErrorEvent *)
{
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/// Check if visual is usable, if so set fVisual, fDepth, fColormap,
/// fBlackPixel and fWhitePixel.
void TGX11::FindUsableVisual(RXVisualInfo *vlist, Int_t nitems)
{
Int_t (*oldErrorHandler)(Display *, XErrorEvent *) =
XSetErrorHandler(DummyX11ErrorHandler);
XSetWindowAttributes attr;
memset(&attr, 0, sizeof(attr));
Window root = RootWindow((Display*)fDisplay, fScreenNumber);
for (Int_t i = 0; i < nitems; i++) {
Window w = None, wjunk;
UInt_t width, height, ujunk;
Int_t junk;
// try and use default colormap when possible
if (vlist[i].visual == DefaultVisual((Display*)fDisplay, fScreenNumber)) {
attr.colormap = DefaultColormap((Display*)fDisplay, fScreenNumber);
} else {
attr.colormap = XCreateColormap((Display*)fDisplay, root, vlist[i].visual, AllocNone);
}
static XColor black_xcol = { 0, 0x0000, 0x0000, 0x0000, DoRed|DoGreen|DoBlue, 0 };
static XColor white_xcol = { 0, 0xFFFF, 0xFFFF, 0xFFFF, DoRed|DoGreen|DoBlue, 0 };
XAllocColor((Display*)fDisplay, attr.colormap, &black_xcol);
XAllocColor((Display*)fDisplay, attr.colormap, &white_xcol);
attr.border_pixel = black_xcol.pixel;
attr.override_redirect = True;
w = XCreateWindow((Display*)fDisplay, root, -20, -20, 10, 10, 0, vlist[i].depth,
CopyFromParent, vlist[i].visual,
CWColormap|CWBorderPixel|CWOverrideRedirect, &attr);
if (w != None && XGetGeometry((Display*)fDisplay, w, &wjunk, &junk, &junk,
&width, &height, &ujunk, &ujunk)) {
fVisual = (RVisual*)vlist[i].visual;
fDepth = vlist[i].depth;
fColormap = attr.colormap;
fBlackPixel = black_xcol.pixel;
fWhitePixel = white_xcol.pixel;
fVisRootWin = w;
break;
}
if (attr.colormap != DefaultColormap((Display*)fDisplay, fScreenNumber))
XFreeColormap((Display*)fDisplay, attr.colormap);
}
XSetErrorHandler(oldErrorHandler);
}
////////////////////////////////////////////////////////////////////////////////
/// Return character up vector.
void TGX11::GetCharacterUp(Float_t &chupx, Float_t &chupy)
{
chupx = fCharacterUpX;
chupy = fCharacterUpY;
}
////////////////////////////////////////////////////////////////////////////////
/// Return reference to internal color structure associated
/// to color index cid.
XColor_t &TGX11::GetColor(Int_t cid)
{
XColor_t *col = (XColor_t*) (Long_t)fColors->GetValue(cid);
if (!col) {
col = new XColor_t;
fColors->Add(cid, (Long_t) col);
}
return *col;
}
////////////////////////////////////////////////////////////////////////////////
/// Return current window pointer. Protected method used by TGX11TTF.
Window_t TGX11::GetCurrentWindow() const
{
return (Window_t)(gCws ? gCws->fDrawing : 0);
}
////////////////////////////////////////////////////////////////////////////////
/// Return desired Graphics Context ("which" maps directly on gGCList[]).
/// Protected method used by TGX11TTF.
void *TGX11::GetGC(Int_t which) const
{
if (which >= kMAXGC || which < 0) {
Error("GetGC", "trying to get illegal GC (which = %d)", which);
return 0;
}
return &gGClist[which];
}
////////////////////////////////////////////////////////////////////////////////
/// Query the double buffer value for the window wid.
Int_t TGX11::GetDoubleBuffer(int wid)
{
gTws = &fWindows[wid];
if (!gTws->fOpen)
return -1;
else
return gTws->fDoubleBuffer;
}
////////////////////////////////////////////////////////////////////////////////
/// Return position and size of window wid.
///
/// \param [in] wid : window identifier
/// \param [in] x,y : window position (output)
/// \param [in] w,h : window size (output)
///
/// if wid < 0 the size of the display is returned
void TGX11::GetGeometry(int wid, int &x, int &y, unsigned int &w, unsigned int &h)
{
Window junkwin=0;
if (wid < 0) {
x = 0;
y = 0;
w = DisplayWidth((Display*)fDisplay,fScreenNumber);
h = DisplayHeight((Display*)fDisplay,fScreenNumber);
} else {
Window root;
unsigned int border, depth;
unsigned int width, height;
gTws = &fWindows[wid];
XGetGeometry((Display*)fDisplay, gTws->fWindow, &root, &x, &y,
&width, &height, &border, &depth);
XTranslateCoordinates((Display*)fDisplay, gTws->fWindow, fRootWin,
0, 0, &x, &y, &junkwin);
if (width >= 65535)
width = 1;
if (height >= 65535)
height = 1;
if (width > 0 && height > 0) {
gTws->fWidth = width;
gTws->fHeight = height;
}
w = gTws->fWidth;
h = gTws->fHeight;
}
}
////////////////////////////////////////////////////////////////////////////////
/// Return hostname on which the display is opened.
const char *TGX11::DisplayName(const char *dpyName)
{
return XDisplayName(dpyName);
}
////////////////////////////////////////////////////////////////////////////////
/// Return pixel value associated to specified ROOT color number.
ULong_t TGX11::GetPixel(Color_t ci)
{