chalk

kis_canvas.h
1/*
2 * Copyright (c) 1999 Matthias Elter <me@kde.org>
3 * Copyright (c) 2004-2006 Adrian Page <adrian@pagenet.plus.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#ifndef KIS_CANVAS_H_
21#define KIS_CANVAS_H_
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#include <tqwidget.h>
28#ifdef HAVE_GL
29#include <tqgl.h>
30#endif
31#include <tqpainter.h>
32
33#include "kis_global.h"
34#include "kis_point.h"
35#include "kis_vec.h"
36#include "kis_input_device.h"
37
38#ifdef Q_MOC_RUN
39#define TQ_WS_X11
40#endif // Q_MOC_RUN
41
42#ifdef TQ_WS_X11
43
44// Irix has a different (and better) XInput tablet driver to
45// the XFree/xorg driver. TQt needs a separate code path for that
46// and so would we.
47#if defined(HAVE_XINPUTEXT) && !defined(Q_OS_IRIX)
48#define EXTENDED_X11_TABLET_SUPPORT
49#endif
50
51#include <map>
52#include <X11/Xlib.h>
53
54#if defined(EXTENDED_X11_TABLET_SUPPORT)
55#include <X11/extensions/XInput.h>
56#endif
57
58#endif // TQ_WS_X11
59
60class KisEvent;
61class KisMoveEvent;
62class KisButtonPressEvent;
63class KisButtonReleaseEvent;
64class KisDoubleClickEvent;
65class KisCanvasWidgetPainter;
66
67class KisCanvasWidget : public TQObject {
68 TQ_OBJECT
69
70
71public:
72 KisCanvasWidget();
73 virtual ~KisCanvasWidget();
74
75 // When enabled, the canvas may throw away move events if the application
76 // is unable to keep up with them, i.e. intermediate move events in the event
77 // queue are skipped.
78 void enableMoveEventCompressionHint(bool enableMoveCompression) { m_enableMoveEventCompressionHint = enableMoveCompression; }
79
80 virtual KisCanvasWidgetPainter *createPainter() = 0;
81
82#ifdef EXTENDED_X11_TABLET_SUPPORT
83 static KisInputDevice findActiveInputDevice();
84 virtual void selectTabletDeviceEvents() = 0;
85
86 static void selectTabletDeviceEvents(TQWidget *widget);
87#endif
88
89#ifdef TQ_WS_X11
90 static void initX11Support();
91#endif
92
93signals:
94 void sigGotPaintEvent(TQPaintEvent*);
95 void sigGotEnterEvent(TQEvent*);
96 void sigGotLeaveEvent(TQEvent*);
97 void sigGotMouseWheelEvent(TQWheelEvent*);
98 void sigGotKeyPressEvent(TQKeyEvent*);
99 void sigGotKeyReleaseEvent(TQKeyEvent*);
100 void sigGotDragEnterEvent(TQDragEnterEvent*);
101 void sigGotDropEvent(TQDropEvent*);
102 void sigGotMoveEvent(KisMoveEvent *);
103 void sigGotButtonPressEvent(KisButtonPressEvent *);
104 void sigGotButtonReleaseEvent(KisButtonReleaseEvent *);
105 void sigGotDoubleClickEvent(KisDoubleClickEvent *);
106
107protected:
108 void widgetGotPaintEvent(TQPaintEvent *event);
109 void widgetGotMousePressEvent(TQMouseEvent *event);
110 void widgetGotMouseReleaseEvent(TQMouseEvent *event);
111 void widgetGotMouseDoubleClickEvent(TQMouseEvent *event);
112 void widgetGotMouseMoveEvent(TQMouseEvent *event);
113 void widgetGotTabletEvent(TQTabletEvent *event);
114 void widgetGotEnterEvent(TQEvent *event );
115 void widgetGotLeaveEvent(TQEvent *event);
116 void widgetGotWheelEvent(TQWheelEvent *event);
117 void widgetGotKeyPressEvent(TQKeyEvent *event);
118 void widgetGotKeyReleaseEvent(TQKeyEvent *event);
119 void widgetGotDragEnterEvent(TQDragEnterEvent *event);
120 void widgetGotDropEvent(TQDropEvent *event);
121 void moveEvent(KisMoveEvent *event);
122 void buttonPressEvent(KisButtonPressEvent *event);
123 void buttonReleaseEvent(KisButtonReleaseEvent *event);
124 void doubleClickEvent(KisDoubleClickEvent *event);
125 void translateTabletEvent(KisEvent *event);
126
127protected:
128
129 bool m_enableMoveEventCompressionHint;
130 double m_lastPressure;
131
132#ifdef TQ_WS_X11
133 // On X11 systems, TQt throws away mouse move events if the application
134 // is unable to keep up with them. We override this behaviour so that
135 // we receive all move events, so that painting follows the mouse's motion
136 // accurately.
137 bool x11Event(XEvent *event, Display *x11Display, WId winId, TQPoint widgetOriginPos);
138 static TQt::ButtonState translateX11ButtonState(int state);
139 static TQt::ButtonState translateX11Button(unsigned int button);
140
141 static bool X11SupportInitialised;
142
143 // Modifier masks for alt/meta - detected at run-time
144 static long X11AltMask;
145 static long X11MetaMask;
146
147 int m_lastRootX;
148 int m_lastRootY;
149
150#ifdef EXTENDED_X11_TABLET_SUPPORT
151
152public:
153 class X11TabletDevice
154 {
155 public:
156 X11TabletDevice();
157 X11TabletDevice(const XDeviceInfo *deviceInfo);
158
159 bool mightBeTabletDevice() const { return m_mightBeTabletDevice; }
160
161 XID id() const { return m_deviceId; }
162 XDevice *xDevice() const { return m_XDevice; }
163 TQString name() const { return m_name; }
164
165 KisInputDevice inputDevice() const { return m_inputDevice; }
166 void setInputDevice(KisInputDevice inputDevice) { m_inputDevice = inputDevice; }
167
168 void setEnabled(bool enabled);
169 bool enabled() const;
170
171 TQ_INT32 numAxes() const;
172
173 void setXAxis(TQ_INT32 axis);
174 void setYAxis(TQ_INT32 axis);
175 void setPressureAxis(TQ_INT32 axis);
176 void setXTiltAxis(TQ_INT32 axis);
177 void setYTiltAxis(TQ_INT32 axis);
178 void setWheelAxis(TQ_INT32 axis);
179 void setToolIDAxis(TQ_INT32 axis);
180 void setSerialNumberAxis(TQ_INT32 axis);
181
182 static const TQ_INT32 NoAxis = -1;
183 static const TQ_INT32 DefaultAxis = -2;
184
185 TQ_INT32 xAxis() const;
186 TQ_INT32 yAxis() const;
187 TQ_INT32 pressureAxis() const;
188 TQ_INT32 xTiltAxis() const;
189 TQ_INT32 yTiltAxis() const;
190 TQ_INT32 wheelAxis() const;
191 TQ_INT32 toolIDAxis() const;
192 TQ_INT32 serialNumberAxis() const;
193
194 void readSettingsFromConfig();
195 void writeSettingsToConfig();
196
197 // These return -1 if the device does not support the event
198 int buttonPressEvent() const { return m_buttonPressEvent; }
199 int buttonReleaseEvent() const { return m_buttonReleaseEvent; }
200 int motionNotifyEvent() const { return m_motionNotifyEvent; }
201 int proximityInEvent() const { return m_proximityInEvent; }
202 int proximityOutEvent() const { return m_proximityOutEvent; }
203
204 void enableEvents(TQWidget *widget) const;
205
206 class State
207 {
208 public:
209 State() {}
210 State(const KisPoint& pos, double pressure, const KisVector2D& tilt, double wheel,
211 TQ_UINT32 toolID, TQ_UINT32 serialNumber);
212
213 // Position, pressure and wheel are normalised to 0 - 1
214 KisPoint pos() const { return m_pos; }
215 double pressure() const { return m_pressure; }
216 // Tilt is normalised to -1->+1
217 KisVector2D tilt() const { return m_tilt; }
218 double wheel() const { return m_wheel; }
219 // Wacom tool id and serial number of device.
220 TQ_UINT32 toolID() const { return m_toolID; }
221 TQ_UINT32 serialNumber() const { return m_serialNumber; }
222
223 private:
224 KisPoint m_pos;
225 double m_pressure;
226 KisVector2D m_tilt;
227 double m_wheel;
228 TQ_UINT32 m_toolID;
229 TQ_UINT32 m_serialNumber;
230 };
231
232 State translateAxisData(const int *axisData) const;
233
234 private:
235 double translateAxisValue(int value, const XAxisInfo& axisInfo) const;
236
237 XID m_deviceId;
238 XDevice *m_XDevice;
239
240 TQString m_name;
241
242 bool m_mightBeTabletDevice;
243 KisInputDevice m_inputDevice;
244
245 bool m_enabled;
246
247 TQ_INT32 m_xAxis;
248 TQ_INT32 m_yAxis;
249 TQ_INT32 m_pressureAxis;
250 TQ_INT32 m_xTiltAxis;
251 TQ_INT32 m_yTiltAxis;
252 TQ_INT32 m_wheelAxis;
253 TQ_INT32 m_toolIDAxis;
254 TQ_INT32 m_serialNumberAxis;
255
256 TQValueVector<XAxisInfo> m_axisInfo;
257
258 int m_motionNotifyEvent;
259 int m_buttonPressEvent;
260 int m_buttonReleaseEvent;
261 int m_proximityInEvent;
262 int m_proximityOutEvent;
263
264 TQValueVector<XEventClass> m_eventClassList;
265 };
266
267 typedef std::map<XID, X11TabletDevice> X11XIDTabletDeviceMap;
268 static X11XIDTabletDeviceMap& tabletDeviceMap();
269
270protected:
271 static int X11DeviceMotionNotifyEvent;
272 static int X11DeviceButtonPressEvent;
273 static int X11DeviceButtonReleaseEvent;
274 static int X11ProximityInEvent;
275 static int X11ProximityOutEvent;
276
277 static X11XIDTabletDeviceMap X11TabletDeviceMap;
278
279#endif // EXTENDED_X11_TABLET_SUPPORT
280
281#endif // TQ_WS_X11
282};
283
284class KisCanvas : public TQObject {
285 TQ_OBJECT
286
287
288public:
289 KisCanvas(TQWidget *parent, const char *name);
290 virtual ~KisCanvas();
291
292 // When enabled, the canvas may throw away move events if the application
293 // is unable to keep up with them, i.e. intermediate move events in the event
294 // queue are skipped.
295 void enableMoveEventCompressionHint(bool enableMoveCompression);
296
297 bool isOpenGLCanvas() const;
298
302 bool cursorIsOverCanvas() const;
303
308 void handleKeyEvent(TQEvent *e);
309
310 int width() const;
311 int height() const;
312
313 void update();
314 void update(const TQRect& r);
315 void update(int x, int y, int width, int height);
316 void repaint();
317 void repaint(bool erase);
318 void repaint(int x, int y, int width, int height, bool erase = true);
319 void repaint(const TQRect& r, bool erase = true);
320 void repaint(const TQRegion& r, bool erase = true);
321
322 void updateGeometry();
323
324#if defined(EXTENDED_X11_TABLET_SUPPORT)
325 void selectTabletDeviceEvents();
326#endif
327
328signals:
329 void sigGotPaintEvent(TQPaintEvent*);
330 void sigGotEnterEvent(TQEvent*);
331 void sigGotLeaveEvent(TQEvent*);
332 void sigGotMouseWheelEvent(TQWheelEvent*);
333 void sigGotKeyPressEvent(TQKeyEvent*);
334 void sigGotKeyReleaseEvent(TQKeyEvent*);
335 void sigGotDragEnterEvent(TQDragEnterEvent*);
336 void sigGotDropEvent(TQDropEvent*);
337 void sigGotMoveEvent(KisMoveEvent *);
338 void sigGotButtonPressEvent(KisButtonPressEvent *);
339 void sigGotButtonReleaseEvent(KisButtonReleaseEvent *);
340 void sigGotDoubleClickEvent(KisDoubleClickEvent *);
341
342protected:
343 // Allow KisView to render on the widget directly, but everything else
344 // has restricted access.
345 friend class KisView;
346 friend class KisCanvasPainter;
347
348 // One of these will be valid, the other null. In TQt3, using a TQPainter on
349 // a TQGLWidget is not reliable.
350 TQWidget *TQPaintDeviceWidget() const;
351#ifdef HAVE_GL
352 TQGLWidget *OpenGLWidget() const;
353#endif
354 void createTQPaintDeviceCanvas();
355#ifdef HAVE_GL
356 void createOpenGLCanvas(TQGLWidget *sharedContextWidget);
357#endif
358 void show();
359 void hide();
360 void setGeometry(int x, int y, int width, int height);
361
362 void setUpdatesEnabled(bool updatesEnabled);
363 bool isUpdatesEnabled() const;
364
365 void setFocusPolicy(TQWidget::FocusPolicy focusPolicy);
366
367 const TQCursor& cursor() const;
368 void setCursor(const TQCursor& cursor);
369
370 KisCanvasWidgetPainter *createPainter();
371 KisCanvasWidget *canvasWidget() const;
372
373protected:
374#ifdef HAVE_GL
375 void createCanvasWidget(bool useOpenGL, TQGLWidget *sharedContextWidget = 0);
376#else
377 void createCanvasWidget(bool useOpenGL);
378#endif
379 TQWidget *m_parent;
380 TQString m_name;
381 KisCanvasWidget *m_canvasWidget;
382 bool m_enableMoveEventCompressionHint;
383 bool m_useOpenGL;
384};
385
386#endif // KIS_CANVAS_H_
387
A double-based point class that can return it's coordinates approximated to integers.
Definition kis_point.h:28