-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgwindow.cpp
More file actions
1529 lines (1338 loc) · 38.8 KB
/
gwindow.cpp
File metadata and controls
1529 lines (1338 loc) · 38.8 KB
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
// File "gwindow.cpp"
// Simple graphic window based on XLib
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include "gwindow.h"
Display* GWindow::m_Display = 0;
int GWindow::m_Screen = 0;
Atom GWindow::m_WMProtocolsAtom = 0;
Atom GWindow::m_WMDeleteWindowAtom = 0;
int GWindow::m_NumWindows = 0;
int GWindow::m_NumCreatedWindows = 0;
ListHeader GWindow::m_WindowList(
&GWindow::m_WindowList, &GWindow::m_WindowList
);
ListHeader GWindow::m_FontList(
&GWindow::m_FontList, &GWindow::m_FontList
);
bool GWindow::getNextEvent(XEvent& e) {
long eventMask =
ExposureMask | ButtonPressMask | ButtonReleaseMask
| KeyPressMask | PointerMotionMask
| StructureNotifyMask // For resize event
| SubstructureNotifyMask
| FocusChangeMask
| ButtonMotionMask;
return (
XCheckMaskEvent(m_Display, eventMask, &e) != 0 ||
XCheckTypedEvent(m_Display, ClientMessage, &e) != 0
);
}
void GWindow::messageLoop(GWindow* dialogWnd /* = 0 */) {
XEvent event;
//+++
// printf("Message loop...\n");
//+++
while (
m_NumCreatedWindows > 0 &&
(dialogWnd == 0 || dialogWnd->m_Window != 0)
) {
//... XNextEvent(m_Display, &event);
if (!getNextEvent(event)) {
// Sleep a bit
timeval dt;
dt.tv_sec = 0;
dt.tv_usec = 10000; // 0.01 sec
select(1, 0, 0, 0, &dt); // sleep...
continue;
}
// printf("got event: type=%d\n", event.type);
dispatchEvent(event);
}
while (getNextEvent(event)) {
dispatchEvent(event);
}
}
void GWindow::dispatchEvent(XEvent& event) {
// printf("got event: type=%d\n", event.type);
GWindow* w = findWindow(event.xany.window);
if (w == 0) {
/*
printf(
"In dispatchEvent: Could not find a window %d, event type=%d\n",
(int) event.xany.window, (int) event.type
);
*/
return;
}
if (event.type == Expose) {
// printf("Expose event.\n");
if (w->m_BeginExposeSeries) {
w->m_ClipRectangle.x = event.xexpose.x;
w->m_ClipRectangle.y = event.xexpose.y;
w->m_ClipRectangle.width = event.xexpose.width;
w->m_ClipRectangle.height = event.xexpose.height;
w->m_BeginExposeSeries = false;
} else {
// Add the current rectangle to the clip rectangle
if (event.xexpose.x < w->m_ClipRectangle.x)
w->m_ClipRectangle.x = event.xexpose.x;
if (event.xexpose.y < w->m_ClipRectangle.y)
w->m_ClipRectangle.y = event.xexpose.y;
if (event.xexpose.x + event.xexpose.width >
w->m_ClipRectangle.x + w->m_ClipRectangle.width) {
w->m_ClipRectangle.width = event.xexpose.x +
event.xexpose.width - w->m_ClipRectangle.x;
}
if (event.xexpose.y + event.xexpose.height >
w->m_ClipRectangle.y + w->m_ClipRectangle.height) {
w->m_ClipRectangle.height = event.xexpose.y +
event.xexpose.height - w->m_ClipRectangle.y;
}
}
if (event.xexpose.count == 0) {
// Restrict a drawing to clip rectangle
XSetClipRectangles(
m_Display, w->m_GC,
0, 0, // Clip origin
&(w->m_ClipRectangle), 1, Unsorted
);
w->onExpose(event);
// Restore the clip region
w->m_ClipRectangle.x = 0;
w->m_ClipRectangle.y = 0;
w->m_ClipRectangle.width = w->m_IWinRect.width();
w->m_ClipRectangle.height = w->m_IWinRect.height();
XSetClipRectangles(
m_Display, w->m_GC,
0, 0, // Clip origin
&(w->m_ClipRectangle), 1, Unsorted
);
w->m_BeginExposeSeries = true;
}
} else if (event.type == KeyPress) {
// printf("KeyPress event.\n");
w->onKeyPress(event);
} else if (event.type == ButtonPress) {
// printf("ButtonPress event.\n");
w->onButtonPress(event);
} else if (event.type == ButtonRelease) {
// printf("ButtonRelease event.\n");
w->onButtonRelease(event);
} else if (event.type == MotionNotify) {
// printf("MotionNotify event.\n");
w->onMotionNotify(event);
} else if (event.type == CreateNotify) {
// printf("CreateNotify event: m_Window=%d\n", (int) w->m_Window);
w->onCreateNotify(event);
} else if (event.type == DestroyNotify) {
/*
printf(
"DestroyNotify event: Event window=%d, window=%d\n",
(int) event.xdestroywindow.event,
(int) event.xdestroywindow.window
);
*/
// printf("Before: m_NumCreatedWindows=%d\n", m_NumCreatedWindows);
w->onDestroyNotify(event);
GWindow* destroyedWindow = findWindow(event.xdestroywindow.window);
if (destroyedWindow != 0) {
if (destroyedWindow->m_WindowCreated) {
destroyedWindow->m_WindowCreated = false;
m_NumCreatedWindows--;
}
// Exclude a window from the window list
if (destroyedWindow->prev != 0) {
destroyedWindow->prev->link(*(destroyedWindow->next));
destroyedWindow->prev = 0;
destroyedWindow->next = 0;
m_NumWindows--;
}
}
// printf("After: m_NumCreatedWindows=%d\n", m_NumCreatedWindows);
} else if (event.type == FocusIn) {
// printf("FocusIn event.\n");
w->onFocusIn(event);
} else if (event.type == FocusOut) {
// printf("FocusOut event.\n");
w->onFocusOut(event);
// } else if (event.type == ResizeRequest) {
} else if (event.type == ConfigureNotify) {
int newWidth = event.xconfigure.width;
int newHeight = event.xconfigure.height;
// printf("ConfigureNotify: x=%d, y=%d, w=%d, h=%d\n",
// event.xconfigure.x, event.xconfigure.y,
// event.xconfigure.width, event.xconfigure.height);
if (
newWidth != w->m_IWinRect.width() ||
newHeight != w->m_IWinRect.height()
) {
// printf("Resize: w=%d, h=%d\n",
// event.xconfigure.width, event.xconfigure.height);
w->m_IWinRect.setWidth(newWidth);
w->m_IWinRect.setHeight(newHeight);
w->recalculateMap();
if (w->m_Pixmap != 0) {
// Offscreen drawing is used
int depth = DefaultDepth(
m_Display, DefaultScreen(m_Display)
);
::XFreePixmap(m_Display, w->m_Pixmap);
w->m_Pixmap = ::XCreatePixmap(
m_Display, w->m_Window,
newWidth, newHeight,
depth
);
}
w->onResize(event);
w->redraw();
}
} else if (event.type == ClientMessage) { // Window closing, etc.
w->onClientMessage(event);
}
}
void GWindow::doModal() {
messageLoop(this);
}
GWindow* GWindow::findWindow(Window w) {
ListHeader* p = m_WindowList.next;
int i = 0;
while (i < m_NumWindows && p != &m_WindowList) {
if (((GWindow*) p)->m_Window == w) {
return ((GWindow *)p);
}
p = p->next;
i++;
}
return 0;
}
GWindow::GWindow():
m_Window(0),
m_Pixmap(0),
m_GC(0),
m_WindowPosition(0, 0),
m_IWinRect(I2Point(0, 0), 300, 200), // Some arbitrary values
m_RWinRect(
R2Point(0., 0.),
300., 200.
),
m_ICurPos(0, 0),
m_RCurPos(0., 0.),
m_xcoeff(1.),
m_ycoeff(1.),
m_WindowCreated(false),
m_bgPixel(0),
m_fgPixel(0),
m_bgColorName(0),
m_fgColorName(0),
m_BorderWidth(DEFAULT_BORDER_WIDTH),
m_BeginExposeSeries(true)
{
strcpy(m_WindowTitle, "Graphic Window");
}
GWindow::GWindow(
const I2Rectangle& frameRect,
const char* title /* = 0 */
):
m_Window(0),
m_Pixmap(0),
m_GC(0),
m_WindowPosition(frameRect.left(), frameRect.top()),
m_IWinRect(I2Point(0, 0), frameRect.width(), frameRect.height()),
m_RWinRect(),
m_ICurPos(0, 0),
m_RCurPos(0., 0.),
m_xcoeff(1.),
m_ycoeff(1.),
m_WindowCreated(false),
m_bgPixel(0),
m_fgPixel(0),
m_bgColorName(0),
m_fgColorName(0),
m_BorderWidth(DEFAULT_BORDER_WIDTH)
{
GWindow( // Call another constructor
frameRect,
R2Rectangle(
R2Point(0., 0.),
(double) frameRect.width(), (double) frameRect.height()
),
title
);
}
GWindow::GWindow(
const I2Rectangle& frameRect,
const R2Rectangle& coordRect,
const char* title /* = 0 */
):
m_Window(0),
m_Pixmap(0),
m_GC(0),
m_WindowPosition(frameRect.left(), frameRect.top()),
m_IWinRect(I2Point(0, 0), frameRect.width(), frameRect.height()),
m_RWinRect(coordRect),
m_ICurPos(0, 0),
m_RCurPos(0., 0.),
m_xcoeff(1.),
m_ycoeff(1.),
m_WindowCreated(false),
m_bgPixel(0),
m_fgPixel(0),
m_bgColorName(0),
m_fgColorName(0),
m_BorderWidth(DEFAULT_BORDER_WIDTH)
{
if (title == 0) {
strcpy(m_WindowTitle, "Graphic Window");
} else {
strncpy(m_WindowTitle, title, 127);
m_WindowTitle[127] = 0;
}
if (m_IWinRect.width() == 0) {
m_IWinRect.setWidth(1);
m_RWinRect.setWidth(1);
}
if (m_IWinRect.height() == 0) {
m_IWinRect.setHeight(1);
m_RWinRect.setHeight(1);
}
m_xcoeff = double(frameRect.width()) / coordRect.width();
m_ycoeff = double(frameRect.height()) / coordRect.height();
m_ICurPos = map(m_RCurPos);
}
void GWindow::createWindow(
GWindow* parentWindow /* = 0 */, // parent window
int borderWidth /* = DEFAULT_BORDER_WIDTH */,
unsigned int wndClass /* = InputOutput */, // InputOnly, CopyFromParent
Visual* visual /* = CopyFromParent */,
unsigned long attributesValueMask /* = 0 */, // which attr. are defined
XSetWindowAttributes* attributes /* = 0 */ // attributes structure
) {
// Include a window in the head of the window list
link(*(m_WindowList.next));
m_WindowList.link(*this);
m_NumWindows++;
m_NumCreatedWindows++;
// Open a display, if necessary
if (m_Display == 0)
initX();
// Create window and map it
//+++
// printf("Creating window: width=%d, height=%d\n",
// m_IWinRect.width(), m_IWinRect.height());
//+++
if (m_IWinRect.left() != 0 || m_IWinRect.top() != 0) {
int x = m_IWinRect.left();
int y = m_IWinRect.top();
int w = m_IWinRect.width();
int h = m_IWinRect.height();
m_WindowPosition.x += x;
m_WindowPosition.y += y;
m_IWinRect = I2Rectangle(0, 0, w, h);
}
if (m_bgColorName != 0) {
XColor bg;
XParseColor(
m_Display,
DefaultColormap(m_Display, m_Screen),
m_bgColorName,
&bg
);
XAllocColor(
m_Display, DefaultColormap(m_Display, m_Screen), &bg
);
m_bgPixel = bg.pixel;
} else {
m_bgPixel = WhitePixel(m_Display, m_Screen);
}
if (m_fgColorName != 0) {
XColor fg;
XParseColor(
m_Display,
DefaultColormap(m_Display, m_Screen),
m_fgColorName,
&fg
);
XAllocColor(
m_Display, DefaultColormap(m_Display, m_Screen), &fg
);
m_fgPixel = fg.pixel;
} else {
m_fgPixel = BlackPixel(m_Display, m_Screen);
}
m_BorderWidth = borderWidth;
/*...
m_Window = XCreateSimpleWindow(
m_Display,
DefaultRootWindow(m_Display),
m_WindowPosition.x,
m_WindowPosition.y,
m_IWinRect.width(),
m_IWinRect.height(),
m_BorderWidth,
m_fgPixel,
m_bgPixel
);
...*/
Window parent;
if (parentWindow != 0 && parentWindow->m_Window != 0)
parent = parentWindow->m_Window;
else
parent = DefaultRootWindow(m_Display);
XSetWindowAttributes windowAttributes;
XSetWindowAttributes* winAttributes = &windowAttributes;
if (attributesValueMask != 0 && attributes != 0)
winAttributes = attributes;
else
memset(&windowAttributes, 0, sizeof(windowAttributes));
m_Window = XCreateWindow(
m_Display,
parent,
m_WindowPosition.x,
m_WindowPosition.y,
m_IWinRect.width(),
m_IWinRect.height(),
m_BorderWidth,
CopyFromParent,
wndClass,
visual,
attributesValueMask,
winAttributes
);
m_WindowCreated = true;
XSetStandardProperties(
m_Display,
m_Window,
m_WindowTitle, // Window name
m_WindowTitle, // Icon name
None, // Icon pixmap
0, // argv
0, // argc
0 // XSizeHints
);
XSelectInput(
m_Display,
m_Window,
ExposureMask | ButtonPressMask | ButtonReleaseMask
| KeyPressMask | PointerMotionMask
| StructureNotifyMask // For resize event
| SubstructureNotifyMask
| FocusChangeMask
);
m_GC = XCreateGC(
m_Display,
m_Window,
0,
0
);
XSetBackground(
m_Display,
m_GC,
m_bgPixel
);
XSetForeground(
m_Display,
m_GC,
m_fgPixel
);
XClearWindow(
m_Display,
m_Window
);
XMapRaised(
m_Display,
m_Window
);
// To prevent application closing on pressing the window close box
XSetWMProtocols(
m_Display, m_Window,
&m_WMDeleteWindowAtom, 1
);
// printf("In createWindow: m_Window = %d\n", (int) m_Window);
}
void GWindow::setWindowTitle(const char* title) {
strncpy(m_WindowTitle, title, 127);
m_WindowTitle[127] = 0;
if (m_WindowCreated)
XStoreName(m_Display, m_Window, m_WindowTitle);
}
void GWindow::setBgColorName(const char* colorName) {
if (m_WindowCreated)
setBackground(colorName);
else
m_bgColorName = colorName;
}
void GWindow::setFgColorName(const char* colorName) {
if (m_WindowCreated)
setForeground(colorName);
else
m_fgColorName = colorName;
}
void GWindow::createWindow(
const I2Rectangle& frameRect,
const char* title /* = 0 */,
GWindow *parentWindow /* = 0 */,
int borderWidth /* = DEFAULT_BORDER_WIDTH */
) {
if (title == 0) {
strcpy(m_WindowTitle, "Graphic Window");
} else {
strncpy(m_WindowTitle, title, 127);
m_WindowTitle[127] = 0;
}
m_WindowPosition.x = frameRect.left();
m_WindowPosition.y = frameRect.top();
m_IWinRect.setLeft(0);
m_IWinRect.setTop(0);
m_IWinRect.setWidth(frameRect.width());
m_IWinRect.setHeight(frameRect.height());
m_RWinRect.setLeft(0.);
m_RWinRect.setBottom(0.);
m_RWinRect.setWidth((double) frameRect.width());
m_RWinRect.setHeight((double) frameRect.height());
if (m_IWinRect.width() == 0) {
m_IWinRect.setWidth(1);
m_RWinRect.setWidth(1);
}
if (m_IWinRect.height() == 0) {
m_IWinRect.setHeight(1);
m_RWinRect.setHeight(1);
}
m_xcoeff = double(m_IWinRect.width()) / m_RWinRect.width();
m_ycoeff = double(m_IWinRect.height()) / m_RWinRect.height();
m_ICurPos = map(m_RCurPos);
createWindow(parentWindow, borderWidth);
}
void GWindow::createWindow(
const I2Rectangle& frameRect,
const R2Rectangle& coordRect,
const char* title /* = 0 */,
GWindow* parentWindow /* = 0 */,
int borderWidth /* = DEFAULT_BORDER_WIDTH */
) {
if (title == 0) {
strcpy(m_WindowTitle, "Graphic Window");
} else {
strncpy(m_WindowTitle, title, 127);
m_WindowTitle[127] = 0;
}
m_WindowPosition.x = frameRect.left();
m_WindowPosition.y = frameRect.top();
m_IWinRect.setLeft(0);
m_IWinRect.setTop(0);
m_IWinRect.setWidth(frameRect.width());
m_IWinRect.setHeight(frameRect.height());
m_RWinRect = coordRect;
if (m_IWinRect.width() == 0) {
m_IWinRect.setWidth(1);
}
if (m_IWinRect.height() == 0) {
m_IWinRect.setHeight(1);
}
//... if (m_RWinRect.width() == 0.) {
if (fabs(m_RWinRect.width()) <= R2GRAPH_EPSILON) {
m_RWinRect.setWidth(1.);
}
//... if (m_RWinRect.height() == 0.) {
if (fabs(m_RWinRect.height()) <= R2GRAPH_EPSILON) {
m_RWinRect.setHeight(1.);
}
m_xcoeff = double(m_IWinRect.width()) / m_RWinRect.width();
m_ycoeff = double(m_IWinRect.height()) / m_RWinRect.height();
m_ICurPos = map(m_RCurPos);
createWindow(parentWindow, borderWidth);
}
GWindow::~GWindow() {
if (m_WindowCreated) {
destroyWindow(); // Destroy window
m_WindowCreated = false;
}
// Exclude a window from the window list
if (prev != 0) {
prev->link(*next);
prev = 0;
next = 0;
m_NumWindows--;
}
}
void GWindow::destroyWindow() {
// printf("In destroyWindow: m_Window = %d\n", (int) m_Window);
if (!m_WindowCreated)
return;
m_WindowCreated = false;
m_NumCreatedWindows--;
if (m_GC != 0) {
XFreeGC(
m_Display,
m_GC
);
m_GC = 0;
}
if (m_Pixmap != 0) {
XFreePixmap(
m_Display,
m_Pixmap
);
m_Pixmap = 0;
}
if (m_Window != 0) {
XDestroyWindow(
m_Display,
m_Window
);
m_Window = 0;
}
}
bool GWindow::initX() {
//+++
// printf("Initializing display...\n");
//+++
m_Display = XOpenDisplay((char *)0);
if (m_Display == 0) {
perror("Cannot open display");
return false;
}
m_Screen = DefaultScreen(m_Display);
// For interconnetion with Window Manager
m_WMProtocolsAtom = XInternAtom(
GWindow::m_Display,
"WM_PROTOCOLS",
False
);
m_WMDeleteWindowAtom = XInternAtom(
GWindow::m_Display,
"WM_DELETE_WINDOW",
False
);
return true;
}
void GWindow::closeX() {
if (m_Display == 0)
return;
releaseFonts();
//+++
// printf("Closing display...\n");
//+++
XCloseDisplay(m_Display);
m_Display = 0;
}
int GWindow::screenMaxX() {
if (m_Display == 0)
initX();
return XDisplayWidth(m_Display, m_Screen);
}
int GWindow::screenMaxY() {
if (m_Display == 0)
initX();
return XDisplayHeight(m_Display, m_Screen);
}
void GWindow::drawFrame() {
}
void GWindow::moveTo(const I2Point& p) {
m_ICurPos = p;
m_RCurPos = invMap(m_ICurPos);
}
void GWindow::moveTo(const R2Point& p) {
m_RCurPos = p;
m_ICurPos = map(m_RCurPos);
}
void GWindow::moveTo(int x, int y) {
moveTo(I2Point(x, y));
}
void GWindow::moveTo(double x, double y) {
moveTo(R2Point(x, y));
}
void GWindow::setCoordinates(
double xmin, double ymin, double width, double height
) {
setCoordinates(R2Rectangle(xmin, ymin, width, height));
}
void GWindow::setCoordinates(
const R2Point& leftBottom, const R2Point& rightTop
) {
setCoordinates(
R2Rectangle(
leftBottom,
rightTop.x - leftBottom.x,
rightTop.y - leftBottom.y
)
);
}
void GWindow::setCoordinates(const R2Rectangle& coordRect) {
m_RWinRect = coordRect;
//... if (m_RWinRect.width() == 0) {
if (fabs(m_RWinRect.width()) <= R2GRAPH_EPSILON) {
m_RWinRect.setWidth(1);
}
//... if (m_RWinRect.height() == 0) {
if (fabs(m_RWinRect.height()) <= R2GRAPH_EPSILON) {
m_RWinRect.setHeight(1);
}
m_xcoeff = double(m_IWinRect.width()) / double(m_RWinRect.width());
m_ycoeff = double(m_IWinRect.height()) / double(m_RWinRect.height());
}
void GWindow::drawAxes(
const char* axesColor /* = 0 */,
bool drawGrid /* = false */,
const char* gridColor /* = 0 */,
bool offscreen /* = false */
) {
// Af first, draw a grid
if (drawGrid) {
if (gridColor != 0)
setForeground(gridColor);
// Vertical grid
int xmin = (int) ceil(m_RWinRect.left());
int xmax = (int) floor(m_RWinRect.right());
int i;
for (i = xmin; i <= xmax; i++) {
if (i == 0)
continue;
drawLine(
R2Point((double) i, m_RWinRect.bottom()),
R2Point((double) i, m_RWinRect.top()),
offscreen
);
}
int ymin = (int) ceil(m_RWinRect.bottom());
int ymax = (int) floor(m_RWinRect.top());
for (i = ymin; i <= ymax; i++) {
if (i == 0)
continue;
drawLine(
R2Point(m_RWinRect.left(), (double) i),
R2Point(m_RWinRect.right(), (double) i),
offscreen
);
}
}
if (axesColor != 0)
setForeground(axesColor);
drawLine(R2Point(getXMin(), 0.), R2Point(getXMax(), 0.), offscreen);
drawLine(R2Point(0., getYMin()), R2Point(0., getYMax()), offscreen);
// Scale
drawLine(R2Point(1., -0.1), R2Point(1., 0.1), offscreen);
drawLine(R2Point(-0.1, 1.), R2Point(0.1, 1.), offscreen);
double w = m_RWinRect.width();
double h = m_RWinRect.height();
drawString(R2Point(getXMin() + w * 0.9, -h*0.06), "x", 1, offscreen);
drawString(R2Point(w * 0.03, getYMin() + h*0.9), "y", 1, offscreen);
}
void GWindow::moveRel(const I2Vector& v) {
moveTo(m_ICurPos + v);
}
void GWindow::moveRel(const R2Vector& v) {
moveTo(m_RCurPos + v);
}
void GWindow::moveRel(int dx, int dy) {
moveRel(I2Vector(dx, dy));
}
void GWindow::moveRel(double dx, double dy) {
moveRel(R2Vector(dx, dy));
}
void GWindow::drawLineTo(const I2Point& p, bool offscreen /* = false */) {
drawLine(m_ICurPos, p, offscreen);
moveTo(p);
}
void GWindow::drawLineTo(int x, int y, bool offscreen /* = false */) {
drawLineTo(I2Point(x, y), offscreen);
}
void GWindow::drawLineTo(const R2Point& p, bool offscreen /* = false */) {
drawLine(m_RCurPos, p, offscreen);
moveTo(p);
}
void GWindow::drawLineTo(double x, double y, bool offscreen /* = false */) {
drawLineTo(R2Point(x, y), offscreen);
}
void GWindow::drawLineRel(const I2Vector& v, bool offscreen /* = false */) {
drawLineTo(m_ICurPos + v, offscreen);
}
void GWindow::drawLineRel(const R2Vector& v, bool offscreen /* = false */) {
drawLineTo(m_RCurPos + v, offscreen);
}
void GWindow::drawLineRel(int dx, int dy, bool offscreen /* = false */) {
drawLineRel(I2Vector(dx, dy), offscreen);
}
void GWindow::drawLineRel(double dx, double dy, bool offscreen /* = false */) {
drawLineRel(R2Vector(dx, dy), offscreen);
}
void GWindow::drawLine(
const I2Point& p1, const I2Point& p2, bool offscreen /* = false */
) {
//... drawLine(invMap(p1), invMap(p2));
Drawable draw = m_Window;
if (offscreen && m_Pixmap != 0)
draw = m_Pixmap;
if (
abs(p1.x) < SHRT_MAX && abs(p1.y) < SHRT_MAX &&
abs(p2.x) < SHRT_MAX && abs(p2.y) < SHRT_MAX
)
{
::XDrawLine(
m_Display,
draw,
m_GC,
p1.x, p1.y,
p2.x, p2.y
);
} else {
R2Point c1, c2;
if (
R2Rectangle(
m_IWinRect.left(), m_IWinRect.top(),
m_IWinRect.width(), m_IWinRect.height()
).clip(
R2Point(p1.x, p1.y), R2Point(p1.x, p1.y),
c1, c2
)
) {
::XDrawLine(
m_Display,
draw,
m_GC,
(int)(c1.x + 0.5), (int)(c1.y + 0.5),
(int)(c2.x + 0.5), (int)(c2.y + 0.5)
);
}
}
moveTo(invMap(p2));
}
void GWindow::drawLine(
const I2Point& p, const I2Vector& v, bool offscreen /* = false */
) {
drawLine(p, p+v, offscreen);
}
void GWindow::drawLine(
int x1, int y1, int x2, int y2, bool offscreen /* = false */
) {
drawLine(I2Point(x1, y1), I2Point(x2, y2), offscreen);
}
void GWindow::drawLine(
const R2Point& p1, const R2Point& p2, bool offscreen /* = false */
) {
Drawable draw = m_Window;
if (offscreen && m_Pixmap != 0)
draw = m_Pixmap;
R2Point c1, c2;
if (m_RWinRect.clip(p1, p2, c1, c2)) {
I2Point ip1 = map(c1), ip2 = map(c2);
// printf("Line from (%d, %d) to (%d, %d)\n",
// ip1.x, ip1.y, ip2.x, ip2.y);
::XDrawLine(
m_Display,
draw,
m_GC,
ip1.x, ip1.y,
ip2.x, ip2.y
);
}
}
void GWindow::drawLine(
const R2Point& p, const R2Vector& v, bool offscreen /* = false */
) {
drawLine(p, p+v, offscreen);
}
void GWindow::drawLine(
double x1, double y1, double x2, double y2, bool offscreen /* = false */
) {
drawLine(R2Point(x1, y1), R2Point(x2, y2), offscreen);
}
void GWindow::fillRectangle(const I2Rectangle& r, bool offscreen /* = false */) {
Drawable draw = m_Window;
if (offscreen && m_Pixmap != 0)
draw = m_Pixmap;
::XFillRectangle(
m_Display,
draw,
m_GC,
r.left(), r.top(), r.width(), r.height()
);
}
void GWindow::fillRectangle(const R2Rectangle& r, bool offscreen /* = false */) {
Drawable draw = m_Window;
if (offscreen && m_Pixmap != 0)
draw = m_Pixmap;
I2Point leftTop = map(R2Point(r.left(), r.top()));
I2Point rightBottom = map(R2Point(r.right(), r.bottom()));
//+++ printf("leftTop = (%d, %d), rightBottom = (%d, %d)\n",
//+++ leftTop.x, leftTop.y, rightBottom.x, rightBottom.y);
::XFillRectangle(
m_Display,
draw,
m_GC,
leftTop.x, leftTop.y,
rightBottom.x - leftTop.x, rightBottom.y - leftTop.y
);
}
void GWindow::fillPolygon(
const R2Point* points, int numPoints, bool offscreen /* = false */
) {
if (numPoints <= 2)
return;
I2Point* pnt = new I2Point[numPoints];
for (int i = 0; i < numPoints; ++i) {
pnt[i] = map(points[i]);
}
fillPolygon(pnt, numPoints, offscreen);
delete[] pnt;
}
void GWindow::fillPolygon(