summaryrefslogtreecommitdiffstats
path: root/chalk/ui/kis_canvas.cc
blob: d38c7ea10ac25f71b832ee1f2603a27ddc1aa6c4 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
/*
 *  Copyright (c) 1999 Matthias Elter  <[email protected]>
 *  Copyright (c) 2004-2006 Adrian Page <[email protected]>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.g
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *

 Some of the X11-specific event handling code is based upon code from
 src/kernel/qapplication_x11.cpp from the TQt GUI Toolkit and is subject
 to the following license and copyright:

 ****************************************************************************
**
**
** Implementation of X11 startup routines and event handling
**
** Created : 931029
**
** Copyright (C) 1992-2003 Trolltech AS.  All rights reserved.
**
** This file is part of the kernel module of the TQt GUI Toolkit.
**
** This file may be distributed under the terms of the Q Public License
** as defined by Trolltech AS of Norway and appearing in the file
** LICENSE.TQPL included in the packaging of this file.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** Licensees holding valid TQt Enterprise Edition or TQt Professional Edition
** licenses for Unix/X11 may use this file in accordance with the TQt Commercial
** License Agreement provided with the Software.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/pricing.html or email [email protected] for
**   information about TQt Commercial License Agreements.
** See http://www.trolltech.com/qpl/ for TQPL licensing information.
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact [email protected] if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include <tqcursor.h>

#include "kis_canvas.h"
#include "kis_cursor.h"
#include "kis_move_event.h"
#include "kis_button_press_event.h"
#include "kis_button_release_event.h"
#include "kis_double_click_event.h"
#include "kis_config.h"
#include "kis_qpaintdevice_canvas.h"
#include "kis_opengl_canvas.h"
#include "kis_config.h"
#include "kis_input_device.h"
#include "fixx11h.h"

#ifdef Q_WS_X11

#include <tqdesktopwidget.h>
#include <tqapplication.h>

#include <X11/keysym.h>

bool KisCanvasWidget::X11SupportInitialised = false;
long KisCanvasWidget::X11AltMask = 0;
long KisCanvasWidget::X11MetaMask = 0;

#if defined(EXTENDED_X11_TABLET_SUPPORT)

int KisCanvasWidget::X11DeviceMotionNotifyEvent = -1;
int KisCanvasWidget::X11DeviceButtonPressEvent = -1;
int KisCanvasWidget::X11DeviceButtonReleaseEvent = -1;
int KisCanvasWidget::X11ProximityInEvent = -1;
int KisCanvasWidget::X11ProximityOutEvent = -1;

//X11XIDTabletDeviceMap KisCanvasWidget::X11TabletDeviceMap;
std::map<XID, KisCanvasWidget::X11TabletDevice> KisCanvasWidget::X11TabletDeviceMap;

#endif // EXTENDED_X11_TABLET_SUPPORT

#endif // Q_WS_X11

KisCanvasWidget::KisCanvasWidget()
{
    m_enableMoveEventCompressionHint = false;
    m_lastPressure = 0;

#ifdef Q_WS_X11
    if (!X11SupportInitialised) {
        initX11Support();
    }

    m_lastRootX = -1;
    m_lastRootY = -1;
#endif
}

KisCanvasWidget::~KisCanvasWidget()
{
}

void KisCanvasWidget::widgetGotPaintEvent(TQPaintEvent *e)
{
    emit sigGotPaintEvent(e);
}

void KisCanvasWidget::widgetGotMousePressEvent(TQMouseEvent *e)
{
    KisButtonPressEvent ke(KisInputDevice::mouse(), KisPoint(e->pos()), KisPoint(e->globalPos()), PRESSURE_DEFAULT, 0, 0, e->button(), e->state());
    buttonPressEvent(&ke);
}

void KisCanvasWidget::widgetGotMouseReleaseEvent(TQMouseEvent *e)
{
    KisButtonReleaseEvent ke(KisInputDevice::mouse(), KisPoint(e->pos()), KisPoint(e->globalPos()), PRESSURE_DEFAULT, 0, 0, e->button(), e->state());
    buttonReleaseEvent(&ke);
}

void KisCanvasWidget::widgetGotMouseDoubleClickEvent(TQMouseEvent *e)
{
    KisDoubleClickEvent ke(KisInputDevice::mouse(), KisPoint(e->pos()), KisPoint(e->globalPos()), PRESSURE_DEFAULT, 0, 0, e->button(), e->state());
    doubleClickEvent(&ke);
}

void KisCanvasWidget::widgetGotMouseMoveEvent(TQMouseEvent *e)
{
    KisMoveEvent ke(KisInputDevice::mouse(), KisPoint(e->pos()), KisPoint(e->globalPos()), PRESSURE_DEFAULT, 0, 0, e->state());
    moveEvent(&ke);
}

void KisCanvasWidget::widgetGotTabletEvent(TQTabletEvent *e)
{
    KisInputDevice device;

    switch (e->device()) {
    default:
    case TQTabletEvent::NoDevice:
    case TQTabletEvent::Stylus:
        device = KisInputDevice::stylus();
        break;
    case TQTabletEvent::Puck:
        device = KisInputDevice::puck();
        break;
    case TQTabletEvent::Eraser:
        device = KisInputDevice::eraser();
        break;
    }

    double pressure = e->pressure() / 255.0;

    if (e->type() == TQEvent::TabletPress) {
        KisButtonPressEvent ke(device, KisPoint(e->pos()), KisPoint(e->globalPos()), pressure, e->xTilt(), e->yTilt(), Qt::LeftButton, Qt::NoButton);
        translateTabletEvent(&ke);
    }
    else
    if (e->type() == TQEvent::TabletRelease) {
        KisButtonReleaseEvent ke(device, KisPoint(e->pos()), KisPoint(e->globalPos()), pressure, e->xTilt(), e->yTilt(), Qt::LeftButton, Qt::NoButton);
        translateTabletEvent(&ke);
    }
    else {
        KisMoveEvent ke(device, KisPoint(e->pos()), KisPoint(e->globalPos()), pressure, e->xTilt(), e->yTilt(), Qt::NoButton);
        translateTabletEvent(&ke);
#ifdef Q_WS_X11
        // Fix the problem that when you change from using a tablet device to the mouse,
        // the first mouse button event is not recognised. This is because we handle
        // X11 core mouse move events directly so TQt does not get to see them. This breaks
        // the tablet event accept/ignore mechanism, causing TQt to consume the first
        // mouse button event it sees, instead of a mouse move. 'Ignoring' tablet move events
        // stops TQt from stealing the next mouse button event. This does not affect the
        // tablet aware tools as they do not care about mouse moves while the tablet device is
        // drawing.
        e->ignore();
#endif
    }
}

void KisCanvasWidget::widgetGotEnterEvent(TQEvent *e)
{
    emit sigGotEnterEvent(e);
}

void KisCanvasWidget::widgetGotLeaveEvent(TQEvent *e)
{
    emit sigGotLeaveEvent(e);
}

void KisCanvasWidget::widgetGotWheelEvent(TQWheelEvent *e)
{
    emit sigGotMouseWheelEvent(e);
}

void KisCanvasWidget::widgetGotKeyPressEvent(TQKeyEvent *e)
{
    emit sigGotKeyPressEvent(e);
}

void KisCanvasWidget::widgetGotKeyReleaseEvent(TQKeyEvent *e)
{
    emit sigGotKeyReleaseEvent(e);
}

void KisCanvasWidget::widgetGotDragEnterEvent(TQDragEnterEvent *e)
{
    emit sigGotDragEnterEvent(e);
}

void KisCanvasWidget::widgetGotDropEvent(TQDropEvent *e)
{
    emit sigGotDropEvent(e);
}

void KisCanvasWidget::moveEvent(KisMoveEvent *e)
{
    emit sigGotMoveEvent(e);
}

void KisCanvasWidget::buttonPressEvent(KisButtonPressEvent *e)
{
    TQWidget *widget = dynamic_cast<TQWidget *>(this);
    Q_ASSERT(widget != 0);

    if (widget) {
        widget->setFocus();
    }

    emit sigGotButtonPressEvent(e);
}

void KisCanvasWidget::buttonReleaseEvent(KisButtonReleaseEvent *e)
{
    emit sigGotButtonReleaseEvent(e);
}

void KisCanvasWidget::doubleClickEvent(KisDoubleClickEvent *e)
{
    emit sigGotDoubleClickEvent(e);
}

void KisCanvasWidget::translateTabletEvent(KisEvent *e)
{
    bool checkThresholdOnly = false;

    if (e->type() == KisEvent::ButtonPressEvent || e->type() == KisEvent::ButtonReleaseEvent) {
        KisButtonEvent *b = static_cast<KisButtonEvent *>(e);

        if (b->button() == Qt::MidButton || b->button() == Qt::RightButton) {

            if (e->type() == KisEvent::ButtonPressEvent) {
                buttonPressEvent(static_cast<KisButtonPressEvent *>(e));
            } else {
                buttonReleaseEvent(static_cast<KisButtonReleaseEvent *>(e));
            }

            checkThresholdOnly = true;
        }
    }

    // Use pressure threshold to detect 'left button' press/release
    if (e->pressure() >= PRESSURE_THRESHOLD && m_lastPressure < PRESSURE_THRESHOLD) {
        KisButtonPressEvent ke(e->device(), e->pos(), e->globalPos(), e->pressure(), e->xTilt(), e->yTilt(), Qt::LeftButton, e->state());
        buttonPressEvent(&ke);
    } else if (e->pressure() < PRESSURE_THRESHOLD && m_lastPressure >= PRESSURE_THRESHOLD) {
        KisButtonReleaseEvent ke(e->device(), e->pos(), e->globalPos(), e->pressure(), e->xTilt(), e->yTilt(), Qt::LeftButton, e->state());
        buttonReleaseEvent(&ke);
    } else {
        if (!checkThresholdOnly) {
            KisMoveEvent ke(e->device(), e->pos(), e->globalPos(), e->pressure(), e->xTilt(), e->yTilt(), e->state());
            moveEvent(&ke);
        }
    }

    m_lastPressure = e->pressure();
}

#ifdef Q_WS_X11

void KisCanvasWidget::initX11Support()
{
    if (X11SupportInitialised)
    {
        return;
    }

    X11SupportInitialised = true;

    Display *x11Display = TQApplication::desktop()->x11Display();

    // Look at the modifier mapping and get the correct masks for alt/meta
    XModifierKeymap *map = XGetModifierMapping(x11Display);

    if (map) {
        int mapIndex = 0;

        for (int maskIndex = 0; maskIndex < 8; maskIndex++) {
            for (int i = 0; i < map->max_keypermod; i++) {
                if (map->modifiermap[mapIndex]) {

                    KeySym sym = XKeycodeToKeysym(x11Display, map->modifiermap[mapIndex], 0);

                    if (X11AltMask == 0 && (sym == XK_Alt_L || sym == XK_Alt_R)) {
                        X11AltMask = 1 << maskIndex;
                    }
                    if (X11MetaMask == 0 && (sym == XK_Meta_L || sym == XK_Meta_R)) {
                        X11MetaMask = 1 << maskIndex;
                    }
                }

                mapIndex++;
            }
        }

        XFreeModifiermap(map);
    }
    else {
        // Assume defaults
        X11AltMask = Mod1Mask;
        X11MetaMask = Mod4Mask;
    }

#if defined(EXTENDED_X11_TABLET_SUPPORT)

    int numDevices = 0;
    const XDeviceInfo *devices = XListInputDevices(x11Display, &numDevices);

    if (devices != NULL) {
        XID lastStylusSeen = 0;
        XID lastEraserSeen = 0;
        bool foundStylus = false;
        bool foundEraser = false;

        for (int i = 0; i < numDevices; i++) {

            const XDeviceInfo *device = devices + i;
            X11TabletDevice tabletDevice(device);

            if (tabletDevice.mightBeTabletDevice()) {

                tabletDevice.readSettingsFromConfig();

                TQString lowerCaseName = tabletDevice.name().lower();

                // Find the devices that TQt will use as its stylus and eraser devices.
                if (!foundStylus || !foundEraser) {
                    if (lowerCaseName.startsWith("stylus") || lowerCaseName.startsWith("pen")) {
                        lastStylusSeen = device->id;
                        foundStylus = true;
                    }
                    else if (lowerCaseName.startsWith("eraser")) {
                        lastEraserSeen = device->id;
                        foundEraser = true;
                    }
                }

                X11TabletDeviceMap[device->id] = tabletDevice;

                // Event types are device-independent. Store any
                // the device supports.
                if (tabletDevice.buttonPressEvent() >= 0) {
                    X11DeviceButtonPressEvent = tabletDevice.buttonPressEvent();
                }
                if (tabletDevice.buttonReleaseEvent() >= 0) {
                    X11DeviceButtonReleaseEvent = tabletDevice.buttonReleaseEvent();
                }
                if (tabletDevice.motionNotifyEvent() >= 0) {
                    X11DeviceMotionNotifyEvent = tabletDevice.motionNotifyEvent();
                }
                if (tabletDevice.proximityInEvent() >= 0) {
                    X11ProximityInEvent = tabletDevice.proximityInEvent();
                }
                if (tabletDevice.proximityOutEvent() >= 0) {
                    X11ProximityOutEvent = tabletDevice.proximityOutEvent();
                }
            }
        }

        // Allocate input devices.
        for (X11XIDTabletDeviceMap::iterator it = X11TabletDeviceMap.begin(); it != X11TabletDeviceMap.end(); ++it) {

            X11TabletDevice& tabletDevice = (*it).second;

            if (foundStylus && tabletDevice.id() == lastStylusSeen) {
                tabletDevice.setInputDevice(KisInputDevice::stylus());
            } else if (foundEraser && tabletDevice.id() == lastEraserSeen) {
                tabletDevice.setInputDevice(KisInputDevice::eraser());
            } else {
                tabletDevice.setInputDevice(KisInputDevice::allocateInputDevice());
            }
        }

        XFreeDeviceList(const_cast<XDeviceInfo *>(devices));
    }
#endif // EXTENDED_X11_TABLET_SUPPORT
}

TQt::ButtonState KisCanvasWidget::translateX11ButtonState(int state)
{
    int buttonState = 0;

    if (state & Button1Mask)
        buttonState |= Qt::LeftButton;
    if (state & Button2Mask)
        buttonState |= Qt::MidButton;
    if (state & Button3Mask)
        buttonState |= Qt::RightButton;
    if (state & ShiftMask)
        buttonState |= TQt::ShiftButton;
    if (state & ControlMask)
        buttonState |= TQt::ControlButton;
    if (state & X11AltMask)
        buttonState |= TQt::AltButton;
    if (state & X11MetaMask)
        buttonState |= TQt::MetaButton;

    return static_cast<TQt::ButtonState>(buttonState);
}

TQt::ButtonState KisCanvasWidget::translateX11Button(unsigned int X11Button)
{
    TQt::ButtonState qtButton;

    switch (X11Button) {
    case Button1:
        qtButton = Qt::LeftButton;
        break;
    case Button2:
        qtButton = Qt::MidButton;
        break;
    case Button3:
        qtButton = Qt::RightButton;
        break;
    default:
        qtButton = Qt::NoButton;
    }

    return qtButton;
}

#if defined(EXTENDED_X11_TABLET_SUPPORT)

KisCanvasWidget::X11TabletDevice::X11TabletDevice()
{
    m_mightBeTabletDevice = false;
    m_inputDevice = KisInputDevice::unknown();
    m_enabled = false;
    m_xAxis = NoAxis;
    m_yAxis = NoAxis;
    m_pressureAxis = NoAxis;
    m_xTiltAxis = NoAxis;
    m_yTiltAxis = NoAxis;
    m_wheelAxis = NoAxis;
    m_toolIDAxis = NoAxis;
    m_serialNumberAxis = NoAxis;
    m_buttonPressEvent = -1;
    m_buttonReleaseEvent = -1;
    m_motionNotifyEvent = -1;
    m_proximityInEvent = -1;
    m_proximityOutEvent = -1;
}

KisCanvasWidget::X11TabletDevice::X11TabletDevice(const XDeviceInfo *deviceInfo)
{
    m_mightBeTabletDevice = false;
    m_inputDevice = KisInputDevice::unknown();
    m_enabled = false;
    m_xAxis = NoAxis;
    m_yAxis = NoAxis;
    m_pressureAxis = NoAxis;
    m_xTiltAxis = NoAxis;
    m_yTiltAxis = NoAxis;
    m_wheelAxis = NoAxis;
    m_toolIDAxis = NoAxis;
    m_serialNumberAxis = NoAxis;

    m_deviceId = deviceInfo->id;
    m_name = deviceInfo->name;

    // Get the ranges of the valuators
    XAnyClassPtr classInfo = const_cast<XAnyClassPtr>(deviceInfo->inputclassinfo);

    for (int i = 0; i < deviceInfo->num_classes; i++) {

        if (classInfo->c_class == ValuatorClass) {

            const XValuatorInfo *valuatorInfo = reinterpret_cast<const XValuatorInfo *>(classInfo);

            // Need at least x, y, and pressure.

            if (valuatorInfo->num_axes >= 3) {

                for (unsigned int axis = 0; axis < valuatorInfo->num_axes; axis++) {
                    m_axisInfo.append(valuatorInfo->axes[axis]);
                }

                m_mightBeTabletDevice = true;
            }
        }

        classInfo = reinterpret_cast<XAnyClassPtr>(reinterpret_cast<char *>(classInfo) + classInfo->length);
    }

    // Determine the event types it supports. We're only interested in
    // buttons and motion at the moment.
    m_buttonPressEvent = -1;
    m_buttonReleaseEvent = -1;
    m_motionNotifyEvent = -1;
    m_proximityInEvent = -1;
    m_proximityOutEvent = -1;

    m_XDevice = XOpenDevice(TQApplication::desktop()->x11Display(), m_deviceId);

    if (m_XDevice != NULL) {
        for (int i = 0; i < m_XDevice->num_classes; i++) {

            XEventClass eventClass;

            if (m_XDevice->classes[i].input_class == ButtonClass) {
                DeviceButtonPress(m_XDevice, m_buttonPressEvent, eventClass);
                m_eventClassList.append(eventClass);

                DeviceButtonRelease(m_XDevice, m_buttonReleaseEvent, eventClass);
                m_eventClassList.append(eventClass);
            }
            else
            if (m_XDevice->classes[i].input_class == ValuatorClass) {
                DeviceMotionNotify(m_XDevice, m_motionNotifyEvent, eventClass);
                m_eventClassList.append(eventClass);
            }
            else
            if (m_XDevice->classes[i].input_class == ProximityClass) {
                ProximityIn(m_XDevice, m_proximityInEvent, eventClass);
                m_eventClassList.append(eventClass);

                ProximityOut(m_XDevice, m_proximityOutEvent, eventClass);
                m_eventClassList.append(eventClass);
            }
        }

        // Note: We don't XCloseXDevice() since TQt will have already opened
        // it, and only one XCloseDevice() call closes it for all opens.
    }

    if (m_buttonPressEvent == -1 || m_buttonReleaseEvent == -1 || m_motionNotifyEvent == -1) {
        m_mightBeTabletDevice = false;
    }
}

void KisCanvasWidget::X11TabletDevice::setEnabled(bool enabled)
{
    m_enabled = enabled;
}

bool KisCanvasWidget::X11TabletDevice::enabled() const
{
    return m_enabled;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::numAxes() const
{
    return m_axisInfo.count();
}

void KisCanvasWidget::X11TabletDevice::setXAxis(TQ_INT32 axis)
{
    m_xAxis = axis;
}

void KisCanvasWidget::X11TabletDevice::setYAxis(TQ_INT32 axis)
{
    m_yAxis = axis;
}

void KisCanvasWidget::X11TabletDevice::setPressureAxis(TQ_INT32 axis)
{
    m_pressureAxis = axis;
}

void KisCanvasWidget::X11TabletDevice::setXTiltAxis(TQ_INT32 axis)
{
    m_xTiltAxis = axis;
}

void KisCanvasWidget::X11TabletDevice::setYTiltAxis(TQ_INT32 axis)
{
    m_yTiltAxis = axis;
}

void KisCanvasWidget::X11TabletDevice::setWheelAxis(TQ_INT32 axis)
{
    m_wheelAxis = axis;
}

void KisCanvasWidget::X11TabletDevice::setToolIDAxis(TQ_INT32 axis)
{
    m_toolIDAxis = axis;
}

void KisCanvasWidget::X11TabletDevice::setSerialNumberAxis(TQ_INT32 axis)
{
    m_serialNumberAxis = axis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::xAxis() const
{
    return m_xAxis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::yAxis() const
{
    return m_yAxis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::pressureAxis() const
{
    return m_pressureAxis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::xTiltAxis() const
{
    return m_xTiltAxis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::yTiltAxis() const
{
    return m_yTiltAxis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::wheelAxis() const
{
    return m_wheelAxis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::toolIDAxis() const
{
    return m_toolIDAxis;
}

TQ_INT32 KisCanvasWidget::X11TabletDevice::serialNumberAxis() const
{
    return m_serialNumberAxis;
}

void KisCanvasWidget::X11TabletDevice::readSettingsFromConfig()
{
    KisConfig cfg;

    m_enabled = cfg.tabletDeviceEnabled(m_name);

    m_xAxis = cfg.tabletDeviceAxis(m_name, "XAxis", DefaultAxis);
    m_yAxis = cfg.tabletDeviceAxis(m_name, "YAxis", DefaultAxis);
    m_pressureAxis = cfg.tabletDeviceAxis(m_name, "PressureAxis", DefaultAxis);
    m_xTiltAxis = cfg.tabletDeviceAxis(m_name, "XTiltAxis", DefaultAxis);
    m_yTiltAxis = cfg.tabletDeviceAxis(m_name, "YTiltAxis", DefaultAxis);
    m_wheelAxis = cfg.tabletDeviceAxis(m_name, "WheelAxis", DefaultAxis);
    m_toolIDAxis = cfg.tabletDeviceAxis(m_name, "ToolIDAxis", DefaultAxis);
    m_serialNumberAxis = cfg.tabletDeviceAxis(m_name, "SerialNumberAxis", DefaultAxis);

    if (!m_enabled && m_xAxis == DefaultAxis && m_yAxis == DefaultAxis && m_pressureAxis == DefaultAxis &&
         m_xTiltAxis == DefaultAxis && m_yTiltAxis == DefaultAxis && m_wheelAxis == DefaultAxis &&
         m_toolIDAxis == DefaultAxis && m_serialNumberAxis == DefaultAxis) {
        // This is the first time this device has been seen. Set up default values, assuming
        // it's a Wacom pad.
        m_xAxis = 0;
        m_yAxis = 1;
        m_pressureAxis = 2;

        if (m_axisInfo.count() >= 4) {
            m_xTiltAxis = 3;
        } else {
            m_xTiltAxis = NoAxis;
        }

        if (m_axisInfo.count() >= 5) {
            m_yTiltAxis = 4;
        } else {
            m_yTiltAxis = NoAxis;
        }

        if (m_axisInfo.count() >= 6) {
            m_wheelAxis = 5;
        } else {
            m_wheelAxis = NoAxis;
        }

        // Available since driver version 0.7.2.
        if (m_axisInfo.count() >= 7) {
            m_toolIDAxis = 6;
        } else {
            m_toolIDAxis = NoAxis;
        }

        if (m_axisInfo.count() >= 8) {
            m_serialNumberAxis = 7;
        } else {
            m_serialNumberAxis = NoAxis;
        }
    }
}

void KisCanvasWidget::X11TabletDevice::writeSettingsToConfig()
{
    KisConfig cfg;

    cfg.setTabletDeviceEnabled(m_name, m_enabled);

    cfg.setTabletDeviceAxis(m_name, "XAxis", m_xAxis);
    cfg.setTabletDeviceAxis(m_name, "YAxis", m_yAxis);
    cfg.setTabletDeviceAxis(m_name, "PressureAxis", m_pressureAxis);
    cfg.setTabletDeviceAxis(m_name, "XTiltAxis", m_xTiltAxis);
    cfg.setTabletDeviceAxis(m_name, "YTiltAxis", m_yTiltAxis);
    cfg.setTabletDeviceAxis(m_name, "WheelAxis", m_wheelAxis);
    cfg.setTabletDeviceAxis(m_name, "ToolIDAxis", m_toolIDAxis);
    cfg.setTabletDeviceAxis(m_name, "SerialNumberAxis", m_serialNumberAxis);
}

void KisCanvasWidget::X11TabletDevice::enableEvents(TQWidget *widget) const
{
    if (!m_eventClassList.isEmpty()) {
        int result = XSelectExtensionEvent(TQT_TQPAINTDEVICE(widget)->x11AppDisplay(), widget->handle(), 
                                           const_cast<XEventClass*>(&m_eventClassList[0]), 
                                           m_eventClassList.count());
    
        if (result != Success) {
            kdDebug(41001) << "Failed to select extension events for " << m_name << endl;
        }
    }
}

double KisCanvasWidget::X11TabletDevice::translateAxisValue(int value, const XAxisInfo& axisInfo) const
{
    int axisRange = axisInfo.max_value - axisInfo.min_value;
    double translatedValue = 0;

    if (axisRange != 0) {
        translatedValue = (static_cast<double>(value) - axisInfo.min_value) / axisRange;
        if (axisInfo.min_value < 0) {
            translatedValue -= 0.5;
        }
    }

    return translatedValue;
}

KisCanvasWidget::X11TabletDevice::State::State(const KisPoint& pos, double pressure, const KisVector2D& tilt, double wheel,
                                               TQ_UINT32 toolID, TQ_UINT32 serialNumber)
    : m_pos(pos), 
      m_pressure(pressure), 
      m_tilt(tilt),
      m_wheel(wheel),
      m_toolID(toolID),
      m_serialNumber(serialNumber)
{
}

KisCanvasWidget::X11TabletDevice::State KisCanvasWidget::X11TabletDevice::translateAxisData(const int *axisData) const
{
    KisPoint pos(0, 0);

    if (m_xAxis != NoAxis && m_yAxis != NoAxis) {
        pos = KisPoint(translateAxisValue(axisData[m_xAxis], m_axisInfo[m_xAxis]), 
                       translateAxisValue(axisData[m_yAxis], m_axisInfo[m_yAxis]));
    }

    double pressure = PRESSURE_DEFAULT;

    if (m_pressureAxis != NoAxis) {
        pressure = translateAxisValue(axisData[m_pressureAxis], m_axisInfo[m_pressureAxis]);
    }

    KisVector2D tilt = KisVector2D(0, 0);
    TQ_UINT32 toolID = 0;
    TQ_UINT32 serialNumber = 0;

    if (m_xTiltAxis != NoAxis) {
        // Latest wacom driver returns the tool id and serial number in 
        // the upper 16 bits of the x and y tilts and wheel.
        int xTiltAxisValue = (TQ_INT16)(axisData[m_xTiltAxis] & 0xffff);
        toolID = ((TQ_UINT32)axisData[m_xTiltAxis] >> 16) & 0xffff;

        tilt.setX(translateAxisValue(xTiltAxisValue, m_axisInfo[m_xTiltAxis]));
    }

    if (m_yTiltAxis != NoAxis) {
        int yTiltAxisValue = (TQ_INT16)(axisData[m_yTiltAxis] & 0xffff);
        serialNumber = (TQ_UINT32)axisData[m_yTiltAxis] & 0xffff0000;

        tilt.setY(translateAxisValue(yTiltAxisValue, m_axisInfo[m_yTiltAxis]));
    }

    double wheel = 0;

    if (m_wheelAxis != NoAxis) {
        int wheelAxisValue = (TQ_INT16)(axisData[m_wheelAxis] & 0xffff);
        serialNumber |= ((TQ_UINT32)axisData[m_wheelAxis] >> 16) & 0xffff;

        wheel = translateAxisValue(wheelAxisValue, m_axisInfo[m_wheelAxis]);
    }

    //TQString ids;
    //ids.sprintf("Tool ID: %8x Serial Number: %8x", toolID, serialNumber);

    return State(pos, pressure, tilt, wheel, toolID, serialNumber);
}

KisCanvasWidget::X11XIDTabletDeviceMap& KisCanvasWidget::tabletDeviceMap()
{
    return X11TabletDeviceMap;
}

void KisCanvasWidget::selectTabletDeviceEvents(TQWidget *widget)
{
    for (X11XIDTabletDeviceMap::const_iterator it = X11TabletDeviceMap.begin(); it != X11TabletDeviceMap.end(); ++it) {

        const X11TabletDevice& device = (*it).second;

        if (device.enabled()) {
            device.enableEvents(widget);
        }
    }
}

#endif // EXTENDED_X11_TABLET_SUPPORT

bool KisCanvasWidget::x11Event(XEvent *event, Display *x11Display, WId winId, TQPoint widgetOriginPos)
{
    if (event->type == MotionNotify) {
        // Mouse move
        if (!m_enableMoveEventCompressionHint) {

            XMotionEvent motion = event->xmotion;
            TQPoint globalPos(motion.x_root, motion.y_root);

            if (globalPos.x() != m_lastRootX || globalPos.y() != m_lastRootY) {

                int state = translateX11ButtonState(motion.state);
                TQPoint pos(motion.x, motion.y);
                TQMouseEvent e(TQEvent::MouseMove, pos, globalPos, Qt::NoButton, state);

                widgetGotMouseMoveEvent(&e);
            }

            m_lastRootX = globalPos.x();
            m_lastRootY = globalPos.y();

            return true;
        }
        else {
            return false;
        }
    }
    else
#if defined(EXTENDED_X11_TABLET_SUPPORT)
    if (event->type == X11DeviceMotionNotifyEvent || event->type == X11DeviceButtonPressEvent || event->type == X11DeviceButtonReleaseEvent) {
        // Tablet event.
        int deviceId;
        const int *axisData;
        TQt::ButtonState button;
        TQt::ButtonState buttonState;

        if (event->type == X11DeviceMotionNotifyEvent) {
            // Tablet move
            const XDeviceMotionEvent *motion = reinterpret_cast<const XDeviceMotionEvent *>(event);
            XEvent mouseEvent;

            // Look for an accompanying core event.
            if (XCheckTypedWindowEvent(x11Display, winId, MotionNotify, &mouseEvent)) {
                if (motion->time == mouseEvent.xmotion.time) {
                    // Do nothing
                } else {
                    XPutBackEvent(x11Display, &mouseEvent);
                }
            }

            if (m_enableMoveEventCompressionHint) {
                while (true) {
                    // Look for another motion notify in the queue and skip
                    // to that if found.
                    if (!XCheckTypedWindowEvent(x11Display, winId, X11DeviceMotionNotifyEvent, &mouseEvent)) {
                        break;
                    }

                    motion = reinterpret_cast<const XDeviceMotionEvent *>(&mouseEvent);

                    XEvent coreMotionEvent;

                    // Look for an accompanying core event.
                    if (!XCheckTypedWindowEvent(x11Display, winId, MotionNotify, &coreMotionEvent)) {
                        // Do nothing
                    }
                }
            }

            deviceId = motion->deviceid;
            axisData = motion->axis_data;
            button = Qt::NoButton;
            buttonState = translateX11ButtonState(motion->state);
        }
        else
        if (event->type == X11DeviceButtonPressEvent) {
            // Tablet button press
            const XDeviceButtonPressedEvent *buttonPressed = reinterpret_cast<const XDeviceButtonPressedEvent *>(event);
            deviceId = buttonPressed->deviceid;
            axisData = buttonPressed->axis_data;
            button = translateX11Button(buttonPressed->button);
            buttonState = translateX11ButtonState(buttonPressed->state);

            if (TQApplication::activePopupWidget() == 0) {
                XEvent mouseEvent;

                // Look for and swallow an accompanying core event, but only if there's
                // no active popup, as that needs to see it.
                if (XCheckTypedWindowEvent(x11Display, winId, ButtonPress, &mouseEvent)) {
                    if (buttonPressed->time == mouseEvent.xbutton.time) {
                        // Do nothing
                    }
                    else {
                        XPutBackEvent(x11Display, &mouseEvent);
                    }
                }
            }
        }
        else {
            // Tablet button release
            const XDeviceButtonReleasedEvent *buttonReleased = reinterpret_cast<const XDeviceButtonReleasedEvent *>(event);
            deviceId = buttonReleased->deviceid;
            axisData = buttonReleased->axis_data;
            button = translateX11Button(buttonReleased->button);
            buttonState = translateX11ButtonState(buttonReleased->state);

            if (TQApplication::activePopupWidget() == 0) {
                XEvent mouseEvent;

                // Look for and swallow an accompanying core event, but only if there's
                // no active popup, as that needs to see it.
                if (XCheckTypedWindowEvent(x11Display, winId, ButtonRelease, &mouseEvent)) {
                    if (buttonReleased->time == mouseEvent.xbutton.time) {
                        // Do nothing
                    }
                    else {
                        XPutBackEvent(x11Display, &mouseEvent);
                    }
                }
            }
        }

        X11XIDTabletDeviceMap::const_iterator it = X11TabletDeviceMap.find(deviceId);

        if (it != X11TabletDeviceMap.end()) {

            const X11TabletDevice& tabletDevice = (*it).second;

            if (tabletDevice.enabled()) {
                X11TabletDevice::State deviceState = tabletDevice.translateAxisData(axisData);

                // Map normalised position coordinates to screen coordinates
                TQDesktopWidget *desktop = TQApplication::desktop();
                KisPoint globalPos(deviceState.pos().x() * desktop->width(), deviceState.pos().y() * desktop->height());
                // Convert screen coordinates to widget coordinates
                KisPoint pos = globalPos - KoPoint( widgetOriginPos );

                // Map tilt to -60 - +60 degrees
                KisVector2D tilt(deviceState.tilt().x() * 60, deviceState.tilt().y() * 60);

                if (event->type == X11DeviceMotionNotifyEvent) {
                    KisMoveEvent e(tabletDevice.inputDevice(), pos, globalPos, deviceState.pressure(), tilt.x(), tilt.y(), buttonState);
                    translateTabletEvent(&e);
                }
                else
                if (event->type == X11DeviceButtonPressEvent) {
                    KisButtonPressEvent e(tabletDevice.inputDevice(), pos, globalPos, deviceState.pressure(), tilt.x(), tilt.y(), button, buttonState);
                    translateTabletEvent(&e);
                }
                else {
                    KisButtonReleaseEvent e(tabletDevice.inputDevice(), pos, globalPos, deviceState.pressure(), tilt.x(), tilt.y(), button, buttonState);
                    translateTabletEvent(&e);
                }
            }

            // Consume the event even if the device is disabled otherwise TQt will
            // process it and send a TQTabletEvent.
            return true;
        }
        else {
            return false;
        }
    }
    else
#endif // EXTENDED_X11_TABLET_SUPPORT
    {
        return false;
    }
}

#if defined(EXTENDED_X11_TABLET_SUPPORT)

KisInputDevice KisCanvasWidget::findActiveInputDevice()
{
    X11XIDTabletDeviceMap::const_iterator it;

    for (it = X11TabletDeviceMap.begin(); it != X11TabletDeviceMap.end(); ++it) {
        const X11TabletDevice& tabletDevice = (*it).second;

        XDeviceState *deviceState = XQueryDeviceState(TQApplication::desktop()->x11Display(),
                                                      tabletDevice.xDevice());

        // If your the laptop sleeps, and you remove the mouse from the usb
        // port, then on wake-up Chalk can crash because the above call will
        // return 0. 
        if (!deviceState) continue;
        
        const XInputClass *inputClass = deviceState->data;
        bool deviceIsInProximity = false;

        for (int i = 0; i < deviceState->num_classes; i++) {

            if (inputClass->c_class == ValuatorClass) {

                const XValuatorState *valuatorState = reinterpret_cast<const XValuatorState *>(inputClass);

                if ((valuatorState->mode & ProximityState) == InProximity) {
                    deviceIsInProximity = true;
                    break;
                }
            }

            inputClass = reinterpret_cast<const XInputClass *>(reinterpret_cast<const char *>(inputClass) + inputClass->length);
        }

        XFreeDeviceState(deviceState);

        if (deviceIsInProximity && tabletDevice.enabled()) {
            return tabletDevice.inputDevice();
        }
    }

    return KisInputDevice::mouse();
}

#endif // EXTENDED_X11_TABLET_SUPPORT


#endif // Q_WS_X11

/*************************************************************************/

#define TQPAINTDEVICE_CANVAS_WIDGET false
#define OPENGL_CANVAS_WIDGET true

KisCanvas::KisCanvas(TQWidget *parent, const char *name)
{
    m_parent = parent;
    m_name = name;
    m_enableMoveEventCompressionHint = false;
    m_canvasWidget = 0;
    m_useOpenGL = false;
    createCanvasWidget(TQPAINTDEVICE_CANVAS_WIDGET);
}

KisCanvas::~KisCanvas()
{
    delete m_canvasWidget;
}

#ifdef HAVE_GL
void KisCanvas::createCanvasWidget(bool useOpenGL, TQGLWidget *sharedContextWidget)
#else
void KisCanvas::createCanvasWidget(bool useOpenGL)
#endif
{
    delete m_canvasWidget;

#ifndef HAVE_GL
    useOpenGL = false;
#else
    if (useOpenGL && !TQGLFormat::hasOpenGL()) {
        kdDebug(41001) << "Tried to create OpenGL widget when system doesn't have OpenGL\n";
        useOpenGL = false;
    }

    if (useOpenGL) {
        m_canvasWidget = new KisOpenGLCanvasWidget(m_parent, m_name.latin1(), sharedContextWidget);
    } else
#endif
    {
        m_canvasWidget = new KisTQPaintDeviceCanvasWidget(m_parent, m_name.latin1());
    }

    m_useOpenGL = useOpenGL;

    Q_CHECK_PTR(m_canvasWidget);
    TQWidget *widget = dynamic_cast<TQWidget *>(m_canvasWidget);

    widget->setBackgroundMode(TQWidget::NoBackground);
    widget->setMouseTracking(true);
    widget->setAcceptDrops(true);
    m_canvasWidget->enableMoveEventCompressionHint(m_enableMoveEventCompressionHint);

#if defined(EXTENDED_X11_TABLET_SUPPORT)
    selectTabletDeviceEvents();
#endif

    connect(m_canvasWidget, TQT_SIGNAL(sigGotPaintEvent(TQPaintEvent *)), TQT_SIGNAL(sigGotPaintEvent(TQPaintEvent *)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotEnterEvent(TQEvent*)), TQT_SIGNAL(sigGotEnterEvent(TQEvent*)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotLeaveEvent(TQEvent*)), TQT_SIGNAL(sigGotLeaveEvent(TQEvent*)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotMouseWheelEvent(TQWheelEvent*)), TQT_SIGNAL(sigGotMouseWheelEvent(TQWheelEvent*)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotKeyPressEvent(TQKeyEvent*)), TQT_SIGNAL(sigGotKeyPressEvent(TQKeyEvent*)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotKeyReleaseEvent(TQKeyEvent*)), TQT_SIGNAL(sigGotKeyReleaseEvent(TQKeyEvent*)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotDragEnterEvent(TQDragEnterEvent*)), TQT_SIGNAL(sigGotDragEnterEvent(TQDragEnterEvent*)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotDropEvent(TQDropEvent*)), TQT_SIGNAL(sigGotDropEvent(TQDropEvent*)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotMoveEvent(KisMoveEvent *)), TQT_SIGNAL(sigGotMoveEvent(KisMoveEvent *)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotButtonPressEvent(KisButtonPressEvent *)), TQT_SIGNAL(sigGotButtonPressEvent(KisButtonPressEvent *)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotButtonReleaseEvent(KisButtonReleaseEvent *)), TQT_SIGNAL(sigGotButtonReleaseEvent(KisButtonReleaseEvent *)));
    connect(m_canvasWidget, TQT_SIGNAL(sigGotDoubleClickEvent(KisDoubleClickEvent *)), TQT_SIGNAL(sigGotDoubleClickEvent(KisDoubleClickEvent *)));
}

void KisCanvas::createTQPaintDeviceCanvas()
{
	createCanvasWidget(TQPAINTDEVICE_CANVAS_WIDGET);
}

#ifdef HAVE_GL
void KisCanvas::createOpenGLCanvas(TQGLWidget *sharedContextWidget)
{
    createCanvasWidget(OPENGL_CANVAS_WIDGET, sharedContextWidget);
}
#endif

bool KisCanvas::isOpenGLCanvas() const
{
    return m_useOpenGL;
}

void KisCanvas::enableMoveEventCompressionHint(bool enableMoveCompression)
{
    m_enableMoveEventCompressionHint = enableMoveCompression;
    if (m_canvasWidget != 0) {
        m_canvasWidget->enableMoveEventCompressionHint(enableMoveCompression);
    }
}

TQWidget *KisCanvas::TQPaintDeviceWidget() const
{
    if (m_useOpenGL) {
        return 0;
    } else {
        return dynamic_cast<TQWidget *>(m_canvasWidget);
    }
}

#ifdef HAVE_GL
TQGLWidget *KisCanvas::OpenGLWidget() const
{
    if (m_useOpenGL) {
        return dynamic_cast<TQGLWidget *>(m_canvasWidget);
    } else {
        return 0;
    }
}
#endif

KisCanvasWidgetPainter *KisCanvas::createPainter()
{
    Q_ASSERT(m_canvasWidget != 0);
    return m_canvasWidget->createPainter();
}

KisCanvasWidget *KisCanvas::canvasWidget() const
{
    return m_canvasWidget;
}

void KisCanvas::setGeometry(int x, int y, int width, int height)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->setGeometry(x, y, width, height);
}

void KisCanvas::show()
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->show();
}

void KisCanvas::hide()
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->hide();
}

int KisCanvas::width() const
{
    Q_ASSERT(m_canvasWidget);
    return dynamic_cast<TQWidget *>(m_canvasWidget)->width();
}

int KisCanvas::height() const
{
    Q_ASSERT(m_canvasWidget);
    return dynamic_cast<TQWidget *>(m_canvasWidget)->height();
}

void KisCanvas::update()
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->update();
}

void KisCanvas::update(const TQRect& r)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->update(r);
}

void KisCanvas::update(int x, int y, int width, int height)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->update(x, y, width, height);
}

void KisCanvas::repaint()
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->repaint();
}

void KisCanvas::repaint(bool erase)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->repaint(erase);
}

void KisCanvas::repaint(int x, int y, int width, int height, bool erase)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->repaint(x, y, width, height, erase);
}

void KisCanvas::repaint(const TQRect& r, bool erase)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->repaint(r, erase);
}

void KisCanvas::repaint(const TQRegion& r, bool erase)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->repaint(r, erase);
}

bool KisCanvas::isUpdatesEnabled() const
{
    Q_ASSERT(m_canvasWidget);
    return dynamic_cast<TQWidget *>(m_canvasWidget)->isUpdatesEnabled();
}

void KisCanvas::setUpdatesEnabled(bool updatesEnabled)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->setUpdatesEnabled(updatesEnabled);
}

void KisCanvas::updateGeometry()
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->updateGeometry();
}

void KisCanvas::setFocusPolicy(TQ_FocusPolicy focusPolicy)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->setFocusPolicy(focusPolicy);
}

const TQCursor& KisCanvas::cursor() const
{
    Q_ASSERT(m_canvasWidget);
    return dynamic_cast<TQWidget *>(m_canvasWidget)->cursor();
}

void KisCanvas::setCursor(const TQCursor& cursor)
{
    Q_ASSERT(m_canvasWidget);
    dynamic_cast<TQWidget *>(m_canvasWidget)->setCursor(cursor);
}

#if defined(EXTENDED_X11_TABLET_SUPPORT)
void KisCanvas::selectTabletDeviceEvents()
{
    Q_ASSERT(m_canvasWidget);
    m_canvasWidget->selectTabletDeviceEvents();
}
#endif

bool KisCanvas::cursorIsOverCanvas() const
{
    if (TQApplication::activePopupWidget() != 0) {
        return false;
    }
    if (TQApplication::activeModalWidget() != 0) {
        return false;
    }

    TQWidget *canvasWidget = dynamic_cast<TQWidget *>(m_canvasWidget);
    Q_ASSERT(canvasWidget != 0);

    if (canvasWidget) {
        if (TQApplication::widgetAt(TQCursor::pos(), true) == canvasWidget) {
            return true;
        }
    }
    return false;
}

void KisCanvas::handleKeyEvent(TQEvent *e)
{
    TQKeyEvent *ke = dynamic_cast<TQKeyEvent *>(e);

    Q_ASSERT(ke != 0);

    if (ke) {
        TQWidget *canvasWidget = dynamic_cast<TQWidget *>(m_canvasWidget);
        Q_ASSERT(canvasWidget != 0);

        if (canvasWidget) {
            canvasWidget->setFocus();

            if (e->type() == TQEvent::KeyPress) {
                emit sigGotKeyPressEvent(ke);
            } else {
                emit sigGotKeyReleaseEvent(ke);
            }
        }
    }
}

#include "kis_canvas.moc"